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