Commit | Line | Data |
---|---|---|
8114d10e | 1 | import BaseError from '../../../exception/BaseError'; |
9d73266c | 2 | import type OCPPError from '../../../exception/OCPPError'; |
5a010bf0 | 3 | import { Bootstrap } from '../../../internal'; |
675fa8e3 | 4 | import { |
32de5a57 | 5 | ProcedureName, |
976d11ec JB |
6 | type ProtocolRequest, |
7 | type ProtocolRequestHandler, | |
8 | type ProtocolVersion, | |
9 | type RequestPayload, | |
10 | type ResponsePayload, | |
32de5a57 | 11 | ResponseStatus, |
675fa8e3 | 12 | } from '../../../types/UIProtocol'; |
976d11ec | 13 | import { |
0d2cec76 | 14 | BroadcastChannelProcedureName, |
976d11ec | 15 | type BroadcastChannelRequestPayload, |
0d2cec76 | 16 | } from '../../../types/WorkerBroadcastChannel'; |
675fa8e3 | 17 | import logger from '../../../utils/Logger'; |
0d2cec76 | 18 | import Utils from '../../../utils/Utils'; |
6c8f5d90 | 19 | import UIServiceWorkerBroadcastChannel from '../../UIServiceWorkerBroadcastChannel'; |
db2336d9 | 20 | import type { AbstractUIServer } from '../AbstractUIServer'; |
4198ad5c | 21 | |
32de5a57 LM |
22 | const moduleName = 'AbstractUIService'; |
23 | ||
4198ad5c | 24 | export default abstract class AbstractUIService { |
d56ea27c | 25 | protected static readonly ProcedureNameToBroadCastChannelProcedureNameMap: Omit< |
976d11ec | 26 | Record<ProcedureName, BroadcastChannelProcedureName>, |
17e9e8ce JB |
27 | | ProcedureName.START_SIMULATOR |
28 | | ProcedureName.STOP_SIMULATOR | |
29 | | ProcedureName.LIST_CHARGING_STATIONS | |
976d11ec JB |
30 | > = { |
31 | [ProcedureName.START_CHARGING_STATION]: BroadcastChannelProcedureName.START_CHARGING_STATION, | |
32 | [ProcedureName.STOP_CHARGING_STATION]: BroadcastChannelProcedureName.STOP_CHARGING_STATION, | |
33 | [ProcedureName.CLOSE_CONNECTION]: BroadcastChannelProcedureName.CLOSE_CONNECTION, | |
34 | [ProcedureName.OPEN_CONNECTION]: BroadcastChannelProcedureName.OPEN_CONNECTION, | |
35 | [ProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR]: | |
36 | BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR, | |
37 | [ProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR]: | |
38 | BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR, | |
39 | [ProcedureName.START_TRANSACTION]: BroadcastChannelProcedureName.START_TRANSACTION, | |
40 | [ProcedureName.STOP_TRANSACTION]: BroadcastChannelProcedureName.STOP_TRANSACTION, | |
41 | [ProcedureName.AUTHORIZE]: BroadcastChannelProcedureName.AUTHORIZE, | |
8bfbc743 | 42 | [ProcedureName.BOOT_NOTIFICATION]: BroadcastChannelProcedureName.BOOT_NOTIFICATION, |
976d11ec JB |
43 | [ProcedureName.STATUS_NOTIFICATION]: BroadcastChannelProcedureName.STATUS_NOTIFICATION, |
44 | [ProcedureName.HEARTBEAT]: BroadcastChannelProcedureName.HEARTBEAT, | |
45 | [ProcedureName.METER_VALUES]: BroadcastChannelProcedureName.METER_VALUES, | |
91a7d3ea | 46 | [ProcedureName.DATA_TRANSFER]: BroadcastChannelProcedureName.DATA_TRANSFER, |
c9a4f9ea JB |
47 | [ProcedureName.DIAGNOSTICS_STATUS_NOTIFICATION]: |
48 | BroadcastChannelProcedureName.DIAGNOSTICS_STATUS_NOTIFICATION, | |
49 | [ProcedureName.FIRMWARE_STATUS_NOTIFICATION]: | |
50 | BroadcastChannelProcedureName.FIRMWARE_STATUS_NOTIFICATION, | |
976d11ec JB |
51 | }; |
52 | ||
02a6943a | 53 | protected readonly requestHandlers: Map<ProcedureName, ProtocolRequestHandler>; |
53870cff JB |
54 | private readonly version: ProtocolVersion; |
55 | private readonly uiServer: AbstractUIServer; | |
56 | private readonly uiServiceWorkerBroadcastChannel: UIServiceWorkerBroadcastChannel; | |
0d2cec76 | 57 | private readonly broadcastChannelRequests: Map<string, number>; |
4198ad5c | 58 | |
33cea517 | 59 | constructor(uiServer: AbstractUIServer, version: ProtocolVersion) { |
675fa8e3 | 60 | this.uiServer = uiServer; |
976d11ec | 61 | this.version = version; |
02a6943a | 62 | this.requestHandlers = new Map<ProcedureName, ProtocolRequestHandler>([ |
32de5a57 LM |
63 | [ProcedureName.LIST_CHARGING_STATIONS, this.handleListChargingStations.bind(this)], |
64 | [ProcedureName.START_SIMULATOR, this.handleStartSimulator.bind(this)], | |
65 | [ProcedureName.STOP_SIMULATOR, this.handleStopSimulator.bind(this)], | |
4198ad5c | 66 | ]); |
6812b4e1 | 67 | this.uiServiceWorkerBroadcastChannel = new UIServiceWorkerBroadcastChannel(this); |
0d2cec76 | 68 | this.broadcastChannelRequests = new Map<string, number>(); |
4198ad5c JB |
69 | } |
70 | ||
5e3cb728 | 71 | public async requestHandler(request: ProtocolRequest): Promise<void> { |
98a5256a | 72 | let messageId: string; |
32de5a57 | 73 | let command: ProcedureName; |
6c8f5d90 | 74 | let requestPayload: RequestPayload | undefined; |
32de5a57 LM |
75 | let responsePayload: ResponsePayload; |
76 | try { | |
5e3cb728 | 77 | [messageId, command, requestPayload] = request; |
32de5a57 | 78 | |
02a6943a | 79 | if (this.requestHandlers.has(command) === false) { |
32de5a57 LM |
80 | throw new BaseError( |
81 | `${command} is not implemented to handle message payload ${JSON.stringify( | |
82 | requestPayload, | |
83 | null, | |
84 | 2 | |
85 | )}` | |
86 | ); | |
4198ad5c | 87 | } |
89b7a234 | 88 | |
6c8f5d90 | 89 | // Call the request handler to build the response payload |
5e8e29f4 | 90 | responsePayload = await this.requestHandlers.get(command)(messageId, command, requestPayload); |
32de5a57 LM |
91 | } catch (error) { |
92 | // Log | |
1f7fa4de | 93 | logger.error(`${this.logPrefix(moduleName, 'messageHandler')} Handle request error:`, error); |
6c8f5d90 | 94 | responsePayload = { |
551e477c | 95 | hashIds: requestPayload?.hashIds, |
6c8f5d90 JB |
96 | status: ResponseStatus.FAILURE, |
97 | command, | |
98 | requestPayload, | |
99 | responsePayload, | |
100 | errorMessage: (error as Error).message, | |
101 | errorStack: (error as Error).stack, | |
9d73266c | 102 | errorDetails: (error as OCPPError).details, |
6c8f5d90 | 103 | }; |
623b39b5 JB |
104 | } finally { |
105 | // Send response for payload not forwarded to broadcast channel | |
106 | if (responsePayload !== undefined) { | |
d3195f0a | 107 | this.sendResponse(messageId, responsePayload); |
623b39b5 | 108 | } |
4198ad5c | 109 | } |
6c8f5d90 JB |
110 | } |
111 | ||
112 | public sendRequest( | |
113 | messageId: string, | |
114 | procedureName: ProcedureName, | |
115 | requestPayload: RequestPayload | |
116 | ): void { | |
852a4c5f JB |
117 | this.uiServer.sendRequest( |
118 | this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload) | |
119 | ); | |
6c8f5d90 | 120 | } |
32de5a57 | 121 | |
6c8f5d90 | 122 | public sendResponse(messageId: string, responsePayload: ResponsePayload): void { |
852a4c5f | 123 | this.uiServer.sendResponse(this.uiServer.buildProtocolResponse(messageId, responsePayload)); |
4198ad5c JB |
124 | } |
125 | ||
6c8f5d90 | 126 | public logPrefix(modName: string, methodName: string): string { |
0d2cec76 JB |
127 | return this.uiServer.logPrefix(modName, methodName, this.version); |
128 | } | |
129 | ||
130 | public deleteBroadcastChannelRequest(uuid: string): void { | |
131 | this.broadcastChannelRequests.delete(uuid); | |
132 | } | |
133 | ||
134 | public getBroadcastChannelExpectedResponses(uuid: string): number { | |
135 | return this.broadcastChannelRequests.get(uuid) ?? 0; | |
136 | } | |
137 | ||
2a3cf7fc JB |
138 | protected handleProtocolRequest( |
139 | uuid: string, | |
140 | procedureName: ProcedureName, | |
141 | payload: RequestPayload | |
142 | ): void { | |
143 | this.sendBroadcastChannelRequest( | |
144 | uuid, | |
145 | AbstractUIService.ProcedureNameToBroadCastChannelProcedureNameMap[ | |
146 | procedureName | |
147 | ] as BroadcastChannelProcedureName, | |
148 | payload | |
149 | ); | |
150 | } | |
151 | ||
152 | private sendBroadcastChannelRequest( | |
0d2cec76 JB |
153 | uuid: string, |
154 | procedureName: BroadcastChannelProcedureName, | |
155 | payload: BroadcastChannelRequestPayload | |
156 | ): void { | |
157 | if (!Utils.isEmptyArray(payload.hashIds)) { | |
158 | payload.hashIds = payload.hashIds | |
72092cfc | 159 | .map((hashId) => { |
0d2cec76 JB |
160 | if (this.uiServer.chargingStations.has(hashId) === true) { |
161 | return hashId; | |
162 | } | |
163 | logger.warn( | |
164 | `${this.logPrefix( | |
165 | moduleName, | |
166 | 'sendBroadcastChannelRequest' | |
167 | )} Charging station with hashId '${hashId}' not found` | |
168 | ); | |
169 | }) | |
72092cfc | 170 | .filter((hashId) => hashId !== undefined); |
0d2cec76 JB |
171 | } |
172 | const expectedNumberOfResponses = !Utils.isEmptyArray(payload.hashIds) | |
173 | ? payload.hashIds.length | |
174 | : this.uiServer.chargingStations.size; | |
0d2cec76 | 175 | this.uiServiceWorkerBroadcastChannel.sendRequest([uuid, procedureName, payload]); |
853ed24a | 176 | this.broadcastChannelRequests.set(uuid, expectedNumberOfResponses); |
6c8f5d90 JB |
177 | } |
178 | ||
89b7a234 | 179 | private handleListChargingStations(): ResponsePayload { |
32de5a57 LM |
180 | return { |
181 | status: ResponseStatus.SUCCESS, | |
5e3cb728 JB |
182 | chargingStations: [...this.uiServer.chargingStations.values()], |
183 | } as ResponsePayload; | |
32de5a57 LM |
184 | } |
185 | ||
186 | private async handleStartSimulator(): Promise<ResponsePayload> { | |
187 | await Bootstrap.getInstance().start(); | |
188 | return { status: ResponseStatus.SUCCESS }; | |
189 | } | |
190 | ||
191 | private async handleStopSimulator(): Promise<ResponsePayload> { | |
192 | await Bootstrap.getInstance().stop(); | |
193 | return { status: ResponseStatus.SUCCESS }; | |
4198ad5c JB |
194 | } |
195 | } |