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