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