1 import { BaseError
, type OCPPError
} from
'../../../exception';
3 BroadcastChannelProcedureName
,
4 type BroadcastChannelRequestPayload
,
7 type ProtocolRequestHandler
,
12 } from
'../../../types';
13 import { logger
} from
'../../../utils/Logger';
14 import { Utils
} from
'../../../utils/Utils';
15 import { type AbstractUIServer
, Bootstrap
} from
'../../internal';
16 import { UIServiceWorkerBroadcastChannel
} from
'../../UIServiceWorkerBroadcastChannel';
18 const moduleName
= 'AbstractUIService';
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
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
.START_TRANSACTION
]: BroadcastChannelProcedureName
.START_TRANSACTION
,
36 [ProcedureName
.STOP_TRANSACTION
]: BroadcastChannelProcedureName
.STOP_TRANSACTION
,
37 [ProcedureName
.AUTHORIZE
]: BroadcastChannelProcedureName
.AUTHORIZE
,
38 [ProcedureName
.BOOT_NOTIFICATION
]: BroadcastChannelProcedureName
.BOOT_NOTIFICATION
,
39 [ProcedureName
.STATUS_NOTIFICATION
]: BroadcastChannelProcedureName
.STATUS_NOTIFICATION
,
40 [ProcedureName
.HEARTBEAT
]: BroadcastChannelProcedureName
.HEARTBEAT
,
41 [ProcedureName
.METER_VALUES
]: BroadcastChannelProcedureName
.METER_VALUES
,
42 [ProcedureName
.DATA_TRANSFER
]: BroadcastChannelProcedureName
.DATA_TRANSFER
,
43 [ProcedureName
.DIAGNOSTICS_STATUS_NOTIFICATION
]:
44 BroadcastChannelProcedureName
.DIAGNOSTICS_STATUS_NOTIFICATION
,
45 [ProcedureName
.FIRMWARE_STATUS_NOTIFICATION
]:
46 BroadcastChannelProcedureName
.FIRMWARE_STATUS_NOTIFICATION
,
49 protected readonly requestHandlers
: Map
<ProcedureName
, ProtocolRequestHandler
>;
50 private readonly version
: ProtocolVersion
;
51 private readonly uiServer
: AbstractUIServer
;
52 private readonly uiServiceWorkerBroadcastChannel
: UIServiceWorkerBroadcastChannel
;
53 private readonly broadcastChannelRequests
: Map
<string, number>;
55 constructor(uiServer
: AbstractUIServer
, version
: ProtocolVersion
) {
56 this.uiServer
= uiServer
;
57 this.version
= version
;
58 this.requestHandlers
= new Map
<ProcedureName
, ProtocolRequestHandler
>([
59 [ProcedureName
.LIST_CHARGING_STATIONS
, this.handleListChargingStations
.bind(this)],
60 [ProcedureName
.START_SIMULATOR
, this.handleStartSimulator
.bind(this)],
61 [ProcedureName
.STOP_SIMULATOR
, this.handleStopSimulator
.bind(this)],
63 this.uiServiceWorkerBroadcastChannel
= new UIServiceWorkerBroadcastChannel(this);
64 this.broadcastChannelRequests
= new Map
<string, number>();
67 public async requestHandler(request
: ProtocolRequest
): Promise
<void> {
68 let messageId
: string;
69 let command
: ProcedureName
;
70 let requestPayload
: RequestPayload
| undefined;
71 let responsePayload
: ResponsePayload
;
73 [messageId
, command
, requestPayload
] = request
;
75 if (this.requestHandlers
.has(command
) === false) {
77 `${command} is not implemented to handle message payload ${JSON.stringify(
85 // Call the request handler to build the response payload
86 responsePayload
= await this.requestHandlers
.get(command
)(messageId
, command
, requestPayload
);
89 logger
.error(`${this.logPrefix(moduleName, 'messageHandler')} Handle request error:`, error
);
91 hashIds
: requestPayload
?.hashIds
,
92 status: ResponseStatus
.FAILURE
,
96 errorMessage
: (error
as Error).message
,
97 errorStack
: (error
as Error).stack
,
98 errorDetails
: (error
as OCPPError
).details
,
101 // Send response for payload not forwarded to broadcast channel
102 if (responsePayload
!== undefined) {
103 this.sendResponse(messageId
, responsePayload
);
110 procedureName
: ProcedureName
,
111 requestPayload
: RequestPayload
113 this.uiServer
.sendRequest(
114 this.uiServer
.buildProtocolRequest(messageId
, procedureName
, requestPayload
)
118 public sendResponse(messageId
: string, responsePayload
: ResponsePayload
): void {
119 this.uiServer
.sendResponse(this.uiServer
.buildProtocolResponse(messageId
, responsePayload
));
122 public logPrefix
= (modName
: string, methodName
: string): string => {
123 return this.uiServer
.logPrefix(modName
, methodName
, this.version
);
126 public deleteBroadcastChannelRequest(uuid
: string): void {
127 this.broadcastChannelRequests
.delete(uuid
);
130 public getBroadcastChannelExpectedResponses(uuid
: string): number {
131 return this.broadcastChannelRequests
.get(uuid
) ?? 0;
134 protected handleProtocolRequest(
136 procedureName
: ProcedureName
,
137 payload
: RequestPayload
139 this.sendBroadcastChannelRequest(
141 AbstractUIService
.ProcedureNameToBroadCastChannelProcedureNameMap
[
143 ] as BroadcastChannelProcedureName
,
148 private sendBroadcastChannelRequest(
150 procedureName
: BroadcastChannelProcedureName
,
151 payload
: BroadcastChannelRequestPayload
153 if (Utils
.isNotEmptyArray(payload
.hashIds
)) {
154 payload
.hashIds
= payload
.hashIds
156 if (this.uiServer
.chargingStations
.has(hashId
) === true) {
162 'sendBroadcastChannelRequest'
163 )} Charging station with hashId '${hashId}' not found`
166 .filter((hashId
) => hashId
!== undefined);
168 const expectedNumberOfResponses
= Utils
.isNotEmptyArray(payload
.hashIds
)
169 ? payload
.hashIds
.length
170 : this.uiServer
.chargingStations
.size
;
171 this.uiServiceWorkerBroadcastChannel
.sendRequest([uuid
, procedureName
, payload
]);
172 this.broadcastChannelRequests
.set(uuid
, expectedNumberOfResponses
);
175 private handleListChargingStations(): ResponsePayload
{
177 status: ResponseStatus
.SUCCESS
,
178 chargingStations
: [...this.uiServer
.chargingStations
.values()],
179 } as ResponsePayload
;
182 private async handleStartSimulator(): Promise
<ResponsePayload
> {
184 await Bootstrap
.getInstance().start();
185 return { status: ResponseStatus
.SUCCESS
};
187 return { status: ResponseStatus
.FAILURE
};
191 private async handleStopSimulator(): Promise
<ResponsePayload
> {
193 await Bootstrap
.getInstance().stop();
194 return { status: ResponseStatus
.SUCCESS
};
196 return { status: ResponseStatus
.FAILURE
};