1 import { WorkerBroadcastChannel
} from
'./WorkerBroadcastChannel';
2 import { BaseError
, type OCPPError
} from
'../../exception';
6 type AuthorizeResponse
,
7 type BootNotificationRequest
,
8 type BootNotificationResponse
,
9 BroadcastChannelProcedureName
,
10 type BroadcastChannelRequest
,
11 type BroadcastChannelRequestPayload
,
12 type BroadcastChannelResponsePayload
,
13 type DataTransferRequest
,
14 type DataTransferResponse
,
16 type DiagnosticsStatusNotificationRequest
,
17 type DiagnosticsStatusNotificationResponse
,
18 type FirmwareStatusNotificationRequest
,
19 type FirmwareStatusNotificationResponse
,
20 type HeartbeatRequest
,
21 type HeartbeatResponse
,
23 type MeterValuesRequest
,
24 type MeterValuesResponse
,
25 RegistrationStatusEnumType
,
29 StandardParametersKey
,
30 type StartTransactionRequest
,
31 type StartTransactionResponse
,
32 type StatusNotificationRequest
,
33 type StatusNotificationResponse
,
34 type StopTransactionRequest
,
35 type StopTransactionResponse
,
37 import { Constants
, Utils
, logger
} from
'../../utils';
38 import type { ChargingStation
} from
'../ChargingStation';
39 import { ChargingStationConfigurationUtils
} from
'../ChargingStationConfigurationUtils';
40 import { OCPP16ServiceUtils
} from
'../ocpp';
42 const moduleName
= 'ChargingStationWorkerBroadcastChannel';
44 type CommandResponse
=
45 | StartTransactionResponse
46 | StopTransactionResponse
48 | BootNotificationResponse
49 | StatusNotificationResponse
52 | DataTransferResponse
53 | DiagnosticsStatusNotificationResponse
54 | FirmwareStatusNotificationResponse
;
56 type CommandHandler
= (
57 requestPayload
?: BroadcastChannelRequestPayload
58 ) => Promise
<CommandResponse
| void> | void;
60 export class ChargingStationWorkerBroadcastChannel
extends WorkerBroadcastChannel
{
61 private readonly commandHandlers
: Map
<BroadcastChannelProcedureName
, CommandHandler
>;
62 private readonly chargingStation
: ChargingStation
;
64 constructor(chargingStation
: ChargingStation
) {
66 const requestParams
: RequestParams
= {
69 this.commandHandlers
= new Map
<BroadcastChannelProcedureName
, CommandHandler
>([
70 [BroadcastChannelProcedureName
.START_CHARGING_STATION
, () => this.chargingStation
.start()],
72 BroadcastChannelProcedureName
.STOP_CHARGING_STATION
,
73 async () => this.chargingStation
.stop(),
76 BroadcastChannelProcedureName
.OPEN_CONNECTION
,
77 () => this.chargingStation
.openWSConnection(),
80 BroadcastChannelProcedureName
.CLOSE_CONNECTION
,
81 () => this.chargingStation
.closeWSConnection(),
84 BroadcastChannelProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
,
85 (requestPayload
?: BroadcastChannelRequestPayload
) =>
86 this.chargingStation
.startAutomaticTransactionGenerator(requestPayload
?.connectorIds
),
89 BroadcastChannelProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
,
90 (requestPayload
?: BroadcastChannelRequestPayload
) =>
91 this.chargingStation
.stopAutomaticTransactionGenerator(requestPayload
?.connectorIds
),
94 BroadcastChannelProcedureName
.SET_SUPERVISION_URL
,
95 (requestPayload
?: BroadcastChannelRequestPayload
) =>
96 this.chargingStation
.setSupervisionUrl(requestPayload
?.url
as string),
99 BroadcastChannelProcedureName
.START_TRANSACTION
,
100 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
101 this.chargingStation
.ocppRequestService
.requestHandler
<
102 StartTransactionRequest
,
103 StartTransactionResponse
104 >(this.chargingStation
, RequestCommand
.START_TRANSACTION
, requestPayload
, requestParams
),
107 BroadcastChannelProcedureName
.STOP_TRANSACTION
,
108 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
109 this.chargingStation
.ocppRequestService
.requestHandler
<
110 StopTransactionRequest
,
111 StartTransactionResponse
113 this.chargingStation
,
114 RequestCommand
.STOP_TRANSACTION
,
116 meterStop
: this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(
117 requestPayload
.transactionId
,
126 BroadcastChannelProcedureName
.AUTHORIZE
,
127 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
128 this.chargingStation
.ocppRequestService
.requestHandler
<
131 >(this.chargingStation
, RequestCommand
.AUTHORIZE
, requestPayload
, requestParams
),
134 BroadcastChannelProcedureName
.BOOT_NOTIFICATION
,
135 async (requestPayload
?: BroadcastChannelRequestPayload
) => {
136 this.chargingStation
.bootNotificationResponse
=
137 await this.chargingStation
.ocppRequestService
.requestHandler
<
138 BootNotificationRequest
,
139 BootNotificationResponse
141 this.chargingStation
,
142 RequestCommand
.BOOT_NOTIFICATION
,
144 ...this.chargingStation
.bootNotificationRequest
,
148 skipBufferingOnError
: true,
152 return this.chargingStation
.bootNotificationResponse
;
156 BroadcastChannelProcedureName
.STATUS_NOTIFICATION
,
157 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
158 this.chargingStation
.ocppRequestService
.requestHandler
<
159 StatusNotificationRequest
,
160 StatusNotificationResponse
162 this.chargingStation
,
163 RequestCommand
.STATUS_NOTIFICATION
,
169 BroadcastChannelProcedureName
.HEARTBEAT
,
170 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
171 this.chargingStation
.ocppRequestService
.requestHandler
<
174 >(this.chargingStation
, RequestCommand
.HEARTBEAT
, requestPayload
, requestParams
),
177 BroadcastChannelProcedureName
.METER_VALUES
,
178 async (requestPayload
?: BroadcastChannelRequestPayload
) => {
179 const configuredMeterValueSampleInterval
=
180 ChargingStationConfigurationUtils
.getConfigurationKey(
182 StandardParametersKey
.MeterValueSampleInterval
184 return this.chargingStation
.ocppRequestService
.requestHandler
<
188 this.chargingStation
,
189 RequestCommand
.METER_VALUES
,
192 // FIXME: Implement OCPP version agnostic helpers
193 OCPP16ServiceUtils
.buildMeterValue(
194 this.chargingStation
,
195 requestPayload
.connectorId
,
196 this.chargingStation
.getConnectorStatus(requestPayload
.connectorId
)
198 configuredMeterValueSampleInterval
199 ? Utils
.convertToInt(configuredMeterValueSampleInterval
.value
) * 1000
200 : Constants
.DEFAULT_METER_VALUES_INTERVAL
210 BroadcastChannelProcedureName
.DATA_TRANSFER
,
211 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
212 this.chargingStation
.ocppRequestService
.requestHandler
<
215 >(this.chargingStation
, RequestCommand
.DATA_TRANSFER
, requestPayload
, requestParams
),
218 BroadcastChannelProcedureName
.DIAGNOSTICS_STATUS_NOTIFICATION
,
219 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
220 this.chargingStation
.ocppRequestService
.requestHandler
<
221 DiagnosticsStatusNotificationRequest
,
222 DiagnosticsStatusNotificationResponse
224 this.chargingStation
,
225 RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
,
231 BroadcastChannelProcedureName
.FIRMWARE_STATUS_NOTIFICATION
,
232 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
233 this.chargingStation
.ocppRequestService
.requestHandler
<
234 FirmwareStatusNotificationRequest
,
235 FirmwareStatusNotificationResponse
237 this.chargingStation
,
238 RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
,
244 this.chargingStation
= chargingStation
;
245 this.onmessage
= this.requestHandler
.bind(this) as (message
: MessageEvent
) => void;
246 this.onmessageerror
= this.messageErrorHandler
.bind(this) as (message
: MessageEvent
) => void;
249 private async requestHandler(messageEvent
: MessageEvent
): Promise
<void> {
250 const validatedMessageEvent
= this.validateMessageEvent(messageEvent
);
251 if (validatedMessageEvent
=== false) {
254 if (this.isResponse(validatedMessageEvent
.data
) === true) {
257 const [uuid
, command
, requestPayload
] = validatedMessageEvent
.data
as BroadcastChannelRequest
;
259 !Utils
.isNullOrUndefined(requestPayload
?.hashIds
) &&
260 requestPayload
?.hashIds
?.includes(this.chargingStation
.stationInfo
.hashId
) === false
264 if (!Utils
.isNullOrUndefined(requestPayload
?.hashId
)) {
266 `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead`
270 let responsePayload
: BroadcastChannelResponsePayload
;
271 let commandResponse
: CommandResponse
| void;
273 commandResponse
= await this.commandHandler(command
, requestPayload
);
275 Utils
.isNullOrUndefined(commandResponse
) ||
276 Utils
.isEmptyObject(commandResponse
as CommandResponse
)
279 hashId
: this.chargingStation
.stationInfo
.hashId
,
280 status: ResponseStatus
.SUCCESS
,
283 responsePayload
= this.commandResponseToResponsePayload(
286 commandResponse
as CommandResponse
291 `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`,
295 hashId
: this.chargingStation
.stationInfo
.hashId
,
296 status: ResponseStatus
.FAILURE
,
299 commandResponse
: commandResponse
as CommandResponse
,
300 errorMessage
: (error
as Error).message
,
301 errorStack
: (error
as Error).stack
,
302 errorDetails
: (error
as OCPPError
).details
,
305 this.sendResponse([uuid
, responsePayload
]);
309 private messageErrorHandler(messageEvent
: MessageEvent
): void {
311 `${this.chargingStation.logPrefix()} ${moduleName}.messageErrorHandler: Error at handling message:`,
316 private async commandHandler(
317 command
: BroadcastChannelProcedureName
,
318 requestPayload
: BroadcastChannelRequestPayload
319 ): Promise
<CommandResponse
| void> {
320 if (this.commandHandlers
.has(command
) === true) {
321 this.cleanRequestPayload(command
, requestPayload
);
322 return this.commandHandlers
.get(command
)(requestPayload
);
324 throw new BaseError(`Unknown worker broadcast channel command: ${command}`);
327 private cleanRequestPayload(
328 command
: BroadcastChannelProcedureName
,
329 requestPayload
: BroadcastChannelRequestPayload
331 delete requestPayload
.hashId
;
332 delete requestPayload
.hashIds
;
334 BroadcastChannelProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
,
335 BroadcastChannelProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
,
336 ].includes(command
) === false && delete requestPayload
.connectorIds
;
339 private commandResponseToResponsePayload(
340 command
: BroadcastChannelProcedureName
,
341 requestPayload
: BroadcastChannelRequestPayload
,
342 commandResponse
: CommandResponse
343 ): BroadcastChannelResponsePayload
{
344 const responseStatus
= this.commandResponseToResponseStatus(command
, commandResponse
);
345 if (responseStatus
=== ResponseStatus
.SUCCESS
) {
347 hashId
: this.chargingStation
.stationInfo
.hashId
,
348 status: responseStatus
,
352 hashId
: this.chargingStation
.stationInfo
.hashId
,
353 status: responseStatus
,
360 private commandResponseToResponseStatus(
361 command
: BroadcastChannelProcedureName
,
362 commandResponse
: CommandResponse
365 case BroadcastChannelProcedureName
.START_TRANSACTION
:
366 case BroadcastChannelProcedureName
.STOP_TRANSACTION
:
367 case BroadcastChannelProcedureName
.AUTHORIZE
:
371 | StartTransactionResponse
372 | StopTransactionResponse
374 )?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
376 return ResponseStatus
.SUCCESS
;
378 return ResponseStatus
.FAILURE
;
379 case BroadcastChannelProcedureName
.BOOT_NOTIFICATION
:
380 if (commandResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
) {
381 return ResponseStatus
.SUCCESS
;
383 return ResponseStatus
.FAILURE
;
384 case BroadcastChannelProcedureName
.DATA_TRANSFER
:
385 if (commandResponse
?.status === DataTransferStatus
.ACCEPTED
) {
386 return ResponseStatus
.SUCCESS
;
388 return ResponseStatus
.FAILURE
;
389 case BroadcastChannelProcedureName
.STATUS_NOTIFICATION
:
390 case BroadcastChannelProcedureName
.METER_VALUES
:
391 if (Utils
.isEmptyObject(commandResponse
) === true) {
392 return ResponseStatus
.SUCCESS
;
394 return ResponseStatus
.FAILURE
;
395 case BroadcastChannelProcedureName
.HEARTBEAT
:
396 if ('currentTime' in commandResponse
) {
397 return ResponseStatus
.SUCCESS
;
399 return ResponseStatus
.FAILURE
;
401 return ResponseStatus
.FAILURE
;