1 import { BaseError
, type OCPPError
} from
'../../../exception';
3 BroadcastChannelProcedureName
,
4 type BroadcastChannelRequestPayload
,
7 type ProtocolRequestHandler
,
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';
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
.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
,
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>;
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)],
64 this.uiServiceWorkerBroadcastChannel
= new UIServiceWorkerBroadcastChannel(this);
65 this.broadcastChannelRequests
= new Map
<string, number>();
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
;
74 [messageId
, command
, requestPayload
] = request
;
76 if (this.requestHandlers
.has(command
) === false) {
78 `${command} is not implemented to handle message payload ${JSON.stringify(
86 // Call the request handler to build the response payload
87 responsePayload
= await this.requestHandlers
.get(command
)(messageId
, command
, requestPayload
);
90 logger
.error(`${this.logPrefix(moduleName, 'messageHandler')} Handle request error:`, error
);
92 hashIds
: requestPayload
?.hashIds
,
93 status: ResponseStatus
.FAILURE
,
97 errorMessage
: (error
as Error).message
,
98 errorStack
: (error
as Error).stack
,
99 errorDetails
: (error
as OCPPError
).details
,
102 // Send response for payload not forwarded to broadcast channel
103 if (!Utils
.isNullOrUndefined(responsePayload
)) {
104 this.sendResponse(messageId
, responsePayload
);
111 procedureName
: ProcedureName
,
112 requestPayload
: RequestPayload
114 this.uiServer
.sendRequest(
115 this.uiServer
.buildProtocolRequest(messageId
, procedureName
, requestPayload
)
119 public sendResponse(messageId
: string, responsePayload
: ResponsePayload
): void {
120 this.uiServer
.sendResponse(this.uiServer
.buildProtocolResponse(messageId
, responsePayload
));
123 public logPrefix
= (modName
: string, methodName
: string): string => {
124 return this.uiServer
.logPrefix(modName
, methodName
, this.version
);
127 public deleteBroadcastChannelRequest(uuid
: string): void {
128 this.broadcastChannelRequests
.delete(uuid
);
131 public getBroadcastChannelExpectedResponses(uuid
: string): number {
132 return this.broadcastChannelRequests
.get(uuid
) ?? 0;
135 protected handleProtocolRequest(
137 procedureName
: ProcedureName
,
138 payload
: RequestPayload
140 this.sendBroadcastChannelRequest(
142 AbstractUIService
.ProcedureNameToBroadCastChannelProcedureNameMap
[
144 ] as BroadcastChannelProcedureName
,
149 private sendBroadcastChannelRequest(
151 procedureName
: BroadcastChannelProcedureName
,
152 payload
: BroadcastChannelRequestPayload
154 if (Utils
.isNotEmptyArray(payload
.hashIds
)) {
155 payload
.hashIds
= payload
.hashIds
156 .filter((hashId
) => !Utils
.isNullOrUndefined(hashId
))
158 if (this.uiServer
.chargingStations
.has(hashId
) === true) {
164 'sendBroadcastChannelRequest'
165 )} Charging station with hashId '${hashId}' not found`
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
);
176 private handleListChargingStations(): ResponsePayload
{
178 status: ResponseStatus
.SUCCESS
,
179 chargingStations
: [...this.uiServer
.chargingStations
.values()],
180 } as ResponsePayload
;
183 private async handleStartSimulator(): Promise
<ResponsePayload
> {
185 await Bootstrap
.getInstance().start();
186 return { status: ResponseStatus
.SUCCESS
};
188 return { status: ResponseStatus
.FAILURE
};
192 private async handleStopSimulator(): Promise
<ResponsePayload
> {
194 await Bootstrap
.getInstance().stop();
195 return { status: ResponseStatus
.SUCCESS
};
197 return { status: ResponseStatus
.FAILURE
};