1 import BaseError from
'../../../exception/BaseError';
2 import type OCPPError from
'../../../exception/OCPPError';
3 import { Bootstrap
} from
'../../../internal';
7 type ProtocolRequestHandler
,
12 } from
'../../../types/UIProtocol';
14 BroadcastChannelProcedureName
,
15 type BroadcastChannelRequestPayload
,
16 } from
'../../../types/WorkerBroadcastChannel';
17 import logger from
'../../../utils/Logger';
18 import Utils from
'../../../utils/Utils';
19 import UIServiceWorkerBroadcastChannel from
'../../UIServiceWorkerBroadcastChannel';
20 import type { AbstractUIServer
} from
'../AbstractUIServer';
22 const moduleName
= 'AbstractUIService';
24 export default abstract class AbstractUIService
{
25 protected static readonly ProcedureNameToBroadCastChannelProcedureNameMap
: Omit
<
26 Record
<ProcedureName
, BroadcastChannelProcedureName
>,
27 | ProcedureName
.START_SIMULATOR
28 | ProcedureName
.STOP_SIMULATOR
29 | ProcedureName
.LIST_CHARGING_STATIONS
31 [ProcedureName
.START_CHARGING_STATION
]: BroadcastChannelProcedureName
.START_CHARGING_STATION
,
32 [ProcedureName
.STOP_CHARGING_STATION
]: BroadcastChannelProcedureName
.STOP_CHARGING_STATION
,
33 [ProcedureName
.CLOSE_CONNECTION
]: BroadcastChannelProcedureName
.CLOSE_CONNECTION
,
34 [ProcedureName
.OPEN_CONNECTION
]: BroadcastChannelProcedureName
.OPEN_CONNECTION
,
35 [ProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
]:
36 BroadcastChannelProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
,
37 [ProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
]:
38 BroadcastChannelProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
,
39 [ProcedureName
.START_TRANSACTION
]: BroadcastChannelProcedureName
.START_TRANSACTION
,
40 [ProcedureName
.STOP_TRANSACTION
]: BroadcastChannelProcedureName
.STOP_TRANSACTION
,
41 [ProcedureName
.AUTHORIZE
]: BroadcastChannelProcedureName
.AUTHORIZE
,
42 [ProcedureName
.BOOT_NOTIFICATION
]: BroadcastChannelProcedureName
.BOOT_NOTIFICATION
,
43 [ProcedureName
.STATUS_NOTIFICATION
]: BroadcastChannelProcedureName
.STATUS_NOTIFICATION
,
44 [ProcedureName
.HEARTBEAT
]: BroadcastChannelProcedureName
.HEARTBEAT
,
45 [ProcedureName
.METER_VALUES
]: BroadcastChannelProcedureName
.METER_VALUES
,
48 protected readonly requestHandlers
: Map
<ProcedureName
, ProtocolRequestHandler
>;
49 private readonly version
: ProtocolVersion
;
50 private readonly uiServer
: AbstractUIServer
;
51 private readonly uiServiceWorkerBroadcastChannel
: UIServiceWorkerBroadcastChannel
;
52 private readonly broadcastChannelRequests
: Map
<string, number>;
54 constructor(uiServer
: AbstractUIServer
, version
: ProtocolVersion
) {
55 this.uiServer
= uiServer
;
56 this.version
= version
;
57 this.requestHandlers
= new Map
<ProcedureName
, ProtocolRequestHandler
>([
58 [ProcedureName
.LIST_CHARGING_STATIONS
, this.handleListChargingStations
.bind(this)],
59 [ProcedureName
.START_SIMULATOR
, this.handleStartSimulator
.bind(this)],
60 [ProcedureName
.STOP_SIMULATOR
, this.handleStopSimulator
.bind(this)],
62 this.uiServiceWorkerBroadcastChannel
= new UIServiceWorkerBroadcastChannel(this);
63 this.broadcastChannelRequests
= new Map
<string, number>();
66 public async requestHandler(request
: ProtocolRequest
): Promise
<void> {
67 let messageId
: string;
68 let command
: ProcedureName
;
69 let requestPayload
: RequestPayload
| undefined;
70 let responsePayload
: ResponsePayload
;
72 [messageId
, command
, requestPayload
] = request
;
74 if (this.requestHandlers
.has(command
) === false) {
76 `${command} is not implemented to handle message payload ${JSON.stringify(
84 // Call the request handler to build the response payload
85 responsePayload
= await this.requestHandlers
.get(command
)(messageId
, command
, requestPayload
);
88 logger
.error(`${this.logPrefix(moduleName, 'messageHandler')} Handle request error:`, error
);
90 hashIds
: requestPayload
.hashIds
,
91 status: ResponseStatus
.FAILURE
,
95 errorMessage
: (error
as Error).message
,
96 errorStack
: (error
as Error).stack
,
97 errorDetails
: (error
as OCPPError
).details
,
100 // Send response for payload not forwarded to broadcast channel
101 if (responsePayload
!== undefined) {
102 this.sendResponse(messageId
, responsePayload
);
109 procedureName
: ProcedureName
,
110 requestPayload
: RequestPayload
112 this.uiServer
.sendRequest(
113 this.uiServer
.buildProtocolRequest(messageId
, procedureName
, requestPayload
)
117 public sendResponse(messageId
: string, responsePayload
: ResponsePayload
): void {
118 this.uiServer
.sendResponse(this.uiServer
.buildProtocolResponse(messageId
, responsePayload
));
121 public logPrefix(modName
: string, methodName
: string): string {
122 return this.uiServer
.logPrefix(modName
, methodName
, this.version
);
125 public deleteBroadcastChannelRequest(uuid
: string): void {
126 this.broadcastChannelRequests
.delete(uuid
);
129 public getBroadcastChannelExpectedResponses(uuid
: string): number {
130 return this.broadcastChannelRequests
.get(uuid
) ?? 0;
133 protected handleProtocolRequest(
135 procedureName
: ProcedureName
,
136 payload
: RequestPayload
138 this.sendBroadcastChannelRequest(
140 AbstractUIService
.ProcedureNameToBroadCastChannelProcedureNameMap
[
142 ] as BroadcastChannelProcedureName
,
147 private sendBroadcastChannelRequest(
149 procedureName
: BroadcastChannelProcedureName
,
150 payload
: BroadcastChannelRequestPayload
152 if (!Utils
.isEmptyArray(payload
.hashIds
)) {
153 payload
.hashIds
= payload
.hashIds
155 if (this.uiServer
.chargingStations
.has(hashId
) === true) {
161 'sendBroadcastChannelRequest'
162 )} Charging station with hashId '${hashId}' not found`
165 .filter((hashId
) => hashId
!== undefined);
167 const expectedNumberOfResponses
= !Utils
.isEmptyArray(payload
.hashIds
)
168 ? payload
.hashIds
.length
169 : this.uiServer
.chargingStations
.size
;
170 this.uiServiceWorkerBroadcastChannel
.sendRequest([uuid
, procedureName
, payload
]);
171 this.broadcastChannelRequests
.set(uuid
, expectedNumberOfResponses
);
174 private handleListChargingStations(): ResponsePayload
{
176 status: ResponseStatus
.SUCCESS
,
177 chargingStations
: [...this.uiServer
.chargingStations
.values()],
178 } as ResponsePayload
;
181 private async handleStartSimulator(): Promise
<ResponsePayload
> {
182 await Bootstrap
.getInstance().start();
183 return { status: ResponseStatus
.SUCCESS
};
186 private async handleStopSimulator(): Promise
<ResponsePayload
> {
187 await Bootstrap
.getInstance().stop();
188 return { status: ResponseStatus
.SUCCESS
};