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