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