1 import { secondsToMilliseconds
} from
'date-fns';
3 import { WorkerBroadcastChannel
} from
'./WorkerBroadcastChannel';
4 import { BaseError
, type OCPPError
} from
'../../exception';
8 type AuthorizeResponse
,
9 type BootNotificationRequest
,
10 type BootNotificationResponse
,
11 BroadcastChannelProcedureName
,
12 type BroadcastChannelRequest
,
13 type BroadcastChannelRequestPayload
,
14 type BroadcastChannelResponsePayload
,
15 type DataTransferRequest
,
16 type DataTransferResponse
,
18 type DiagnosticsStatusNotificationRequest
,
19 type DiagnosticsStatusNotificationResponse
,
21 type FirmwareStatusNotificationRequest
,
22 type FirmwareStatusNotificationResponse
,
23 type HeartbeatRequest
,
24 type HeartbeatResponse
,
26 type MeterValuesRequest
,
27 type MeterValuesResponse
,
28 RegistrationStatusEnumType
,
32 StandardParametersKey
,
33 type StartTransactionRequest
,
34 type StartTransactionResponse
,
35 type StatusNotificationRequest
,
36 type StatusNotificationResponse
,
37 type StopTransactionRequest
,
38 type StopTransactionResponse
,
40 import { Constants
, convertToInt
, isEmptyObject
, isNullOrUndefined
, logger
} from
'../../utils';
41 import type { ChargingStation
} from
'../ChargingStation';
42 import { getConfigurationKey
} from
'../ConfigurationKeyUtils';
43 import { OCPP16ServiceUtils
} from
'../ocpp';
45 const moduleName
= 'ChargingStationWorkerBroadcastChannel';
47 type CommandResponse
=
49 | StartTransactionResponse
50 | StopTransactionResponse
52 | BootNotificationResponse
54 | DataTransferResponse
;
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
= getConfigurationKey(
181 StandardParametersKey
.MeterValueSampleInterval
,
183 return this.chargingStation
.ocppRequestService
.requestHandler
<
187 this.chargingStation
,
188 RequestCommand
.METER_VALUES
,
191 // FIXME: Implement OCPP version agnostic helpers
192 OCPP16ServiceUtils
.buildMeterValue(
193 this.chargingStation
,
194 requestPayload
!.connectorId
!,
195 this.chargingStation
.getConnectorStatus(requestPayload
!.connectorId
!)!
197 configuredMeterValueSampleInterval
198 ? secondsToMilliseconds(convertToInt(configuredMeterValueSampleInterval
.value
))
199 : Constants
.DEFAULT_METER_VALUES_INTERVAL
,
209 BroadcastChannelProcedureName
.DATA_TRANSFER
,
210 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
211 this.chargingStation
.ocppRequestService
.requestHandler
<
214 >(this.chargingStation
, RequestCommand
.DATA_TRANSFER
, requestPayload
, requestParams
),
217 BroadcastChannelProcedureName
.DIAGNOSTICS_STATUS_NOTIFICATION
,
218 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
219 this.chargingStation
.ocppRequestService
.requestHandler
<
220 DiagnosticsStatusNotificationRequest
,
221 DiagnosticsStatusNotificationResponse
223 this.chargingStation
,
224 RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
,
230 BroadcastChannelProcedureName
.FIRMWARE_STATUS_NOTIFICATION
,
231 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
232 this.chargingStation
.ocppRequestService
.requestHandler
<
233 FirmwareStatusNotificationRequest
,
234 FirmwareStatusNotificationResponse
236 this.chargingStation
,
237 RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
,
243 this.chargingStation
= chargingStation
;
244 this.onmessage
= this.requestHandler
.bind(this) as (message
: unknown
) => void;
245 this.onmessageerror
= this.messageErrorHandler
.bind(this) as (message
: unknown
) => void;
248 private async requestHandler(messageEvent
: MessageEvent
): Promise
<void> {
249 const validatedMessageEvent
= this.validateMessageEvent(messageEvent
);
250 if (validatedMessageEvent
=== false) {
253 if (this.isResponse(validatedMessageEvent
.data
) === true) {
256 const [uuid
, command
, requestPayload
] = validatedMessageEvent
.data
as BroadcastChannelRequest
;
258 !isNullOrUndefined(requestPayload
.hashIds
) &&
259 requestPayload
.hashIds
?.includes(this.chargingStation
.stationInfo
.hashId
) === false
263 if (!isNullOrUndefined(requestPayload
.hashId
)) {
265 `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead`,
269 let responsePayload
: BroadcastChannelResponsePayload
| undefined;
270 let commandResponse
: CommandResponse
| void | undefined;
272 commandResponse
= await this.commandHandler(command
, requestPayload
);
273 if (isNullOrUndefined(commandResponse
) || isEmptyObject(commandResponse
as CommandResponse
)) {
275 hashId
: this.chargingStation
.stationInfo
.hashId
,
276 status: ResponseStatus
.SUCCESS
,
279 responsePayload
= this.commandResponseToResponsePayload(
282 commandResponse
as CommandResponse
,
287 `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`,
291 hashId
: this.chargingStation
.stationInfo
.hashId
,
292 status: ResponseStatus
.FAILURE
,
295 commandResponse
: commandResponse
as CommandResponse
,
296 errorMessage
: (error
as Error).message
,
297 errorStack
: (error
as Error).stack
,
298 errorDetails
: (error
as OCPPError
).details
,
301 this.sendResponse([uuid
, responsePayload
!]);
305 private messageErrorHandler(messageEvent
: MessageEvent
): void {
307 `${this.chargingStation.logPrefix()} ${moduleName}.messageErrorHandler: Error at handling message:`,
312 private async commandHandler(
313 command
: BroadcastChannelProcedureName
,
314 requestPayload
: BroadcastChannelRequestPayload
,
315 ): Promise
<CommandResponse
| void> {
316 if (this.commandHandlers
.has(command
) === true) {
317 this.cleanRequestPayload(command
, requestPayload
);
318 return this.commandHandlers
.get(command
)!(requestPayload
);
320 throw new BaseError(`Unknown worker broadcast channel command: '${command}'`);
323 private cleanRequestPayload(
324 command
: BroadcastChannelProcedureName
,
325 requestPayload
: BroadcastChannelRequestPayload
,
327 delete requestPayload
.hashId
;
328 delete requestPayload
.hashIds
;
330 BroadcastChannelProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
,
331 BroadcastChannelProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
,
332 ].includes(command
) === false && delete requestPayload
.connectorIds
;
335 private commandResponseToResponsePayload(
336 command
: BroadcastChannelProcedureName
,
337 requestPayload
: BroadcastChannelRequestPayload
,
338 commandResponse
: CommandResponse
,
339 ): BroadcastChannelResponsePayload
{
340 const responseStatus
= this.commandResponseToResponseStatus(command
, commandResponse
);
341 if (responseStatus
=== ResponseStatus
.SUCCESS
) {
343 hashId
: this.chargingStation
.stationInfo
.hashId
,
344 status: responseStatus
,
348 hashId
: this.chargingStation
.stationInfo
.hashId
,
349 status: responseStatus
,
356 private commandResponseToResponseStatus(
357 command
: BroadcastChannelProcedureName
,
358 commandResponse
: CommandResponse
,
361 case BroadcastChannelProcedureName
.START_TRANSACTION
:
362 case BroadcastChannelProcedureName
.STOP_TRANSACTION
:
363 case BroadcastChannelProcedureName
.AUTHORIZE
:
367 | StartTransactionResponse
368 | StopTransactionResponse
370 )?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
372 return ResponseStatus
.SUCCESS
;
374 return ResponseStatus
.FAILURE
;
375 case BroadcastChannelProcedureName
.BOOT_NOTIFICATION
:
376 if (commandResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
) {
377 return ResponseStatus
.SUCCESS
;
379 return ResponseStatus
.FAILURE
;
380 case BroadcastChannelProcedureName
.DATA_TRANSFER
:
381 if (commandResponse
?.status === DataTransferStatus
.ACCEPTED
) {
382 return ResponseStatus
.SUCCESS
;
384 return ResponseStatus
.FAILURE
;
385 case BroadcastChannelProcedureName
.STATUS_NOTIFICATION
:
386 case BroadcastChannelProcedureName
.METER_VALUES
:
387 if (isEmptyObject(commandResponse
) === true) {
388 return ResponseStatus
.SUCCESS
;
390 return ResponseStatus
.FAILURE
;
391 case BroadcastChannelProcedureName
.HEARTBEAT
:
392 if ('currentTime' in commandResponse
) {
393 return ResponseStatus
.SUCCESS
;
395 return ResponseStatus
.FAILURE
;
397 return ResponseStatus
.FAILURE
;