| 1 | import { BaseError, type OCPPError } from '../../../exception'; |
| 2 | import { |
| 3 | BroadcastChannelProcedureName, |
| 4 | type BroadcastChannelRequestPayload, |
| 5 | ProcedureName, |
| 6 | type ProtocolRequest, |
| 7 | type ProtocolRequestHandler, |
| 8 | type ProtocolVersion, |
| 9 | type RequestPayload, |
| 10 | type ResponsePayload, |
| 11 | ResponseStatus, |
| 12 | } from '../../../types'; |
| 13 | import { Utils, logger } from '../../../utils'; |
| 14 | import { Bootstrap } from '../../Bootstrap'; |
| 15 | import { UIServiceWorkerBroadcastChannel } from '../../broadcast-channel/UIServiceWorkerBroadcastChannel'; |
| 16 | import type { AbstractUIServer } from '../AbstractUIServer'; |
| 17 | |
| 18 | const moduleName = 'AbstractUIService'; |
| 19 | |
| 20 | export abstract class AbstractUIService { |
| 21 | protected static readonly ProcedureNameToBroadCastChannelProcedureNameMap: Omit< |
| 22 | Record<ProcedureName, BroadcastChannelProcedureName>, |
| 23 | | ProcedureName.START_SIMULATOR |
| 24 | | ProcedureName.STOP_SIMULATOR |
| 25 | | ProcedureName.LIST_CHARGING_STATIONS |
| 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, |
| 35 | [ProcedureName.SET_SUPERVISION_URL]: BroadcastChannelProcedureName.SET_SUPERVISION_URL, |
| 36 | [ProcedureName.START_TRANSACTION]: BroadcastChannelProcedureName.START_TRANSACTION, |
| 37 | [ProcedureName.STOP_TRANSACTION]: BroadcastChannelProcedureName.STOP_TRANSACTION, |
| 38 | [ProcedureName.AUTHORIZE]: BroadcastChannelProcedureName.AUTHORIZE, |
| 39 | [ProcedureName.BOOT_NOTIFICATION]: BroadcastChannelProcedureName.BOOT_NOTIFICATION, |
| 40 | [ProcedureName.STATUS_NOTIFICATION]: BroadcastChannelProcedureName.STATUS_NOTIFICATION, |
| 41 | [ProcedureName.HEARTBEAT]: BroadcastChannelProcedureName.HEARTBEAT, |
| 42 | [ProcedureName.METER_VALUES]: BroadcastChannelProcedureName.METER_VALUES, |
| 43 | [ProcedureName.DATA_TRANSFER]: BroadcastChannelProcedureName.DATA_TRANSFER, |
| 44 | [ProcedureName.DIAGNOSTICS_STATUS_NOTIFICATION]: |
| 45 | BroadcastChannelProcedureName.DIAGNOSTICS_STATUS_NOTIFICATION, |
| 46 | [ProcedureName.FIRMWARE_STATUS_NOTIFICATION]: |
| 47 | BroadcastChannelProcedureName.FIRMWARE_STATUS_NOTIFICATION, |
| 48 | }; |
| 49 | |
| 50 | protected readonly requestHandlers: Map<ProcedureName, ProtocolRequestHandler>; |
| 51 | private readonly version: ProtocolVersion; |
| 52 | private readonly uiServer: AbstractUIServer; |
| 53 | private readonly uiServiceWorkerBroadcastChannel: UIServiceWorkerBroadcastChannel; |
| 54 | private readonly broadcastChannelRequests: Map<string, number>; |
| 55 | |
| 56 | constructor(uiServer: AbstractUIServer, version: ProtocolVersion) { |
| 57 | this.uiServer = uiServer; |
| 58 | this.version = version; |
| 59 | this.requestHandlers = new Map<ProcedureName, ProtocolRequestHandler>([ |
| 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)], |
| 63 | ]); |
| 64 | this.uiServiceWorkerBroadcastChannel = new UIServiceWorkerBroadcastChannel(this); |
| 65 | this.broadcastChannelRequests = new Map<string, number>(); |
| 66 | } |
| 67 | |
| 68 | public async requestHandler(request: ProtocolRequest): Promise<void> { |
| 69 | let messageId: string; |
| 70 | let command: ProcedureName; |
| 71 | let requestPayload: RequestPayload | undefined; |
| 72 | let responsePayload: ResponsePayload; |
| 73 | try { |
| 74 | [messageId, command, requestPayload] = request; |
| 75 | |
| 76 | if (this.requestHandlers.has(command) === false) { |
| 77 | throw new BaseError( |
| 78 | `${command} is not implemented to handle message payload ${JSON.stringify( |
| 79 | requestPayload, |
| 80 | null, |
| 81 | 2 |
| 82 | )}` |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | // Call the request handler to build the response payload |
| 87 | responsePayload = await this.requestHandlers.get(command)(messageId, command, requestPayload); |
| 88 | } catch (error) { |
| 89 | // Log |
| 90 | logger.error(`${this.logPrefix(moduleName, 'messageHandler')} Handle request error:`, error); |
| 91 | responsePayload = { |
| 92 | hashIds: requestPayload?.hashIds, |
| 93 | status: ResponseStatus.FAILURE, |
| 94 | command, |
| 95 | requestPayload, |
| 96 | responsePayload, |
| 97 | errorMessage: (error as Error).message, |
| 98 | errorStack: (error as Error).stack, |
| 99 | errorDetails: (error as OCPPError).details, |
| 100 | }; |
| 101 | } finally { |
| 102 | // Send response for payload not forwarded to broadcast channel |
| 103 | if (!Utils.isNullOrUndefined(responsePayload)) { |
| 104 | this.sendResponse(messageId, responsePayload); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public sendRequest( |
| 110 | messageId: string, |
| 111 | procedureName: ProcedureName, |
| 112 | requestPayload: RequestPayload |
| 113 | ): void { |
| 114 | this.uiServer.sendRequest( |
| 115 | this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload) |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | public sendResponse(messageId: string, responsePayload: ResponsePayload): void { |
| 120 | this.uiServer.sendResponse(this.uiServer.buildProtocolResponse(messageId, responsePayload)); |
| 121 | } |
| 122 | |
| 123 | public logPrefix = (modName: string, methodName: string): string => { |
| 124 | return this.uiServer.logPrefix(modName, methodName, this.version); |
| 125 | }; |
| 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 | |
| 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( |
| 150 | uuid: string, |
| 151 | procedureName: BroadcastChannelProcedureName, |
| 152 | payload: BroadcastChannelRequestPayload |
| 153 | ): void { |
| 154 | if (Utils.isNotEmptyArray(payload.hashIds)) { |
| 155 | payload.hashIds = payload.hashIds |
| 156 | .filter((hashId) => !Utils.isNullOrUndefined(hashId)) |
| 157 | .map((hashId) => { |
| 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 | ); |
| 167 | }); |
| 168 | } |
| 169 | const expectedNumberOfResponses = Utils.isNotEmptyArray(payload.hashIds) |
| 170 | ? payload.hashIds.length |
| 171 | : this.uiServer.chargingStations.size; |
| 172 | this.uiServiceWorkerBroadcastChannel.sendRequest([uuid, procedureName, payload]); |
| 173 | this.broadcastChannelRequests.set(uuid, expectedNumberOfResponses); |
| 174 | } |
| 175 | |
| 176 | private handleListChargingStations(): ResponsePayload { |
| 177 | return { |
| 178 | status: ResponseStatus.SUCCESS, |
| 179 | chargingStations: [...this.uiServer.chargingStations.values()], |
| 180 | } as ResponsePayload; |
| 181 | } |
| 182 | |
| 183 | private async handleStartSimulator(): Promise<ResponsePayload> { |
| 184 | try { |
| 185 | await Bootstrap.getInstance().start(); |
| 186 | return { status: ResponseStatus.SUCCESS }; |
| 187 | } catch (error) { |
| 188 | return { status: ResponseStatus.FAILURE }; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | private async handleStopSimulator(): Promise<ResponsePayload> { |
| 193 | try { |
| 194 | await Bootstrap.getInstance().stop(); |
| 195 | return { status: ResponseStatus.SUCCESS }; |
| 196 | } catch (error) { |
| 197 | return { status: ResponseStatus.FAILURE }; |
| 198 | } |
| 199 | } |
| 200 | } |