1 import { secondsToMilliseconds
} from
'date-fns'
3 import { WorkerBroadcastChannel
} from
'./WorkerBroadcastChannel.js'
4 import { BaseError
, type OCPPError
} from
'../../exception/index.js'
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
39 } from
'../../types/index.js'
46 } from
'../../utils/index.js'
47 import type { ChargingStation
} from
'../ChargingStation.js'
48 import { getConfigurationKey
} from
'../ConfigurationKeyUtils.js'
49 import { buildMeterValue
} from
'../ocpp/index.js'
51 const moduleName
= 'ChargingStationWorkerBroadcastChannel'
53 type CommandResponse
=
55 | StartTransactionResponse
56 | StopTransactionResponse
58 | BootNotificationResponse
60 | DataTransferResponse
62 type CommandHandler
= (
63 requestPayload
?: BroadcastChannelRequestPayload
64 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
65 ) => Promise
<CommandResponse
| void> | CommandResponse
| void
67 export class ChargingStationWorkerBroadcastChannel
extends WorkerBroadcastChannel
{
68 private readonly commandHandlers
: Map
<BroadcastChannelProcedureName
, CommandHandler
>
69 private readonly chargingStation
: ChargingStation
71 constructor (chargingStation
: ChargingStation
) {
73 const requestParams
: RequestParams
= {
76 this.commandHandlers
= new Map
<BroadcastChannelProcedureName
, CommandHandler
>([
78 BroadcastChannelProcedureName
.START_CHARGING_STATION
,
80 this.chargingStation
.start()
84 BroadcastChannelProcedureName
.STOP_CHARGING_STATION
,
86 await this.chargingStation
.stop()
90 BroadcastChannelProcedureName
.OPEN_CONNECTION
,
92 this.chargingStation
.openWSConnection()
96 BroadcastChannelProcedureName
.CLOSE_CONNECTION
,
98 this.chargingStation
.closeWSConnection()
102 BroadcastChannelProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
,
103 (requestPayload
?: BroadcastChannelRequestPayload
) => {
104 this.chargingStation
.startAutomaticTransactionGenerator(requestPayload
?.connectorIds
)
108 BroadcastChannelProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
,
109 (requestPayload
?: BroadcastChannelRequestPayload
) => {
110 this.chargingStation
.stopAutomaticTransactionGenerator(requestPayload
?.connectorIds
)
114 BroadcastChannelProcedureName
.SET_SUPERVISION_URL
,
115 (requestPayload
?: BroadcastChannelRequestPayload
) => {
116 this.chargingStation
.setSupervisionUrl(requestPayload
?.url
as string)
120 BroadcastChannelProcedureName
.START_TRANSACTION
,
121 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
122 await this.chargingStation
.ocppRequestService
.requestHandler
<
123 StartTransactionRequest
,
124 StartTransactionResponse
125 >(this.chargingStation
, RequestCommand
.START_TRANSACTION
, requestPayload
, requestParams
)
128 BroadcastChannelProcedureName
.STOP_TRANSACTION
,
129 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
130 await this.chargingStation
.ocppRequestService
.requestHandler
<
131 StopTransactionRequest
,
132 StartTransactionResponse
134 this.chargingStation
,
135 RequestCommand
.STOP_TRANSACTION
,
137 meterStop
: this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(
138 requestPayload
?.transactionId
,
147 BroadcastChannelProcedureName
.AUTHORIZE
,
148 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
149 await this.chargingStation
.ocppRequestService
.requestHandler
<
152 >(this.chargingStation
, RequestCommand
.AUTHORIZE
, requestPayload
, requestParams
)
155 BroadcastChannelProcedureName
.BOOT_NOTIFICATION
,
156 async (requestPayload
?: BroadcastChannelRequestPayload
) => {
157 this.chargingStation
.bootNotificationResponse
=
158 await this.chargingStation
.ocppRequestService
.requestHandler
<
159 BootNotificationRequest
,
160 BootNotificationResponse
162 this.chargingStation
,
163 RequestCommand
.BOOT_NOTIFICATION
,
165 ...this.chargingStation
.bootNotificationRequest
,
169 skipBufferingOnError
: true,
173 return this.chargingStation
.bootNotificationResponse
177 BroadcastChannelProcedureName
.STATUS_NOTIFICATION
,
178 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
179 await this.chargingStation
.ocppRequestService
.requestHandler
<
180 StatusNotificationRequest
,
181 StatusNotificationResponse
182 >(this.chargingStation
, RequestCommand
.STATUS_NOTIFICATION
, requestPayload
, requestParams
)
185 BroadcastChannelProcedureName
.HEARTBEAT
,
186 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
187 await this.chargingStation
.ocppRequestService
.requestHandler
<
190 >(this.chargingStation
, RequestCommand
.HEARTBEAT
, requestPayload
, requestParams
)
193 BroadcastChannelProcedureName
.METER_VALUES
,
194 async (requestPayload
?: BroadcastChannelRequestPayload
) => {
195 const configuredMeterValueSampleInterval
= getConfigurationKey(
197 StandardParametersKey
.MeterValueSampleInterval
199 return await this.chargingStation
.ocppRequestService
.requestHandler
<
203 this.chargingStation
,
204 RequestCommand
.METER_VALUES
,
208 this.chargingStation
,
209 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
210 requestPayload
!.connectorId
!,
211 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
212 this.chargingStation
.getConnectorStatus(requestPayload
!.connectorId
!)!
214 configuredMeterValueSampleInterval
!= null
215 ? secondsToMilliseconds(convertToInt(configuredMeterValueSampleInterval
.value
))
216 : Constants
.DEFAULT_METER_VALUES_INTERVAL
226 BroadcastChannelProcedureName
.DATA_TRANSFER
,
227 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
228 await this.chargingStation
.ocppRequestService
.requestHandler
<
231 >(this.chargingStation
, RequestCommand
.DATA_TRANSFER
, requestPayload
, requestParams
)
234 BroadcastChannelProcedureName
.DIAGNOSTICS_STATUS_NOTIFICATION
,
235 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
236 await this.chargingStation
.ocppRequestService
.requestHandler
<
237 DiagnosticsStatusNotificationRequest
,
238 DiagnosticsStatusNotificationResponse
240 this.chargingStation
,
241 RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
,
247 BroadcastChannelProcedureName
.FIRMWARE_STATUS_NOTIFICATION
,
248 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
249 await this.chargingStation
.ocppRequestService
.requestHandler
<
250 FirmwareStatusNotificationRequest
,
251 FirmwareStatusNotificationResponse
253 this.chargingStation
,
254 RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
,
260 this.chargingStation
= chargingStation
261 this.onmessage
= this.requestHandler
.bind(this) as (message
: unknown
) => void
262 this.onmessageerror
= this.messageErrorHandler
.bind(this) as (message
: unknown
) => void
265 private requestHandler (messageEvent
: MessageEvent
): void {
266 const validatedMessageEvent
= this.validateMessageEvent(messageEvent
)
267 if (validatedMessageEvent
=== false) {
270 if (this.isResponse(validatedMessageEvent
.data
)) {
273 const [uuid
, command
, requestPayload
] = validatedMessageEvent
.data
as BroadcastChannelRequest
275 requestPayload
.hashIds
!= null &&
276 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
277 !requestPayload
.hashIds
.includes(this.chargingStation
.stationInfo
!.hashId
)
281 if (requestPayload
.hashId
!= null) {
283 `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead`
287 let responsePayload
: BroadcastChannelResponsePayload
| undefined
288 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
289 let commandResponse
: CommandResponse
| void
290 this.commandHandler(command
, requestPayload
)
291 .then(commandResponse
=> {
292 if (commandResponse
== null || isEmptyObject(commandResponse
)) {
294 hashId
: this.chargingStation
.stationInfo
?.hashId
,
295 status: ResponseStatus
.SUCCESS
298 responsePayload
= this.commandResponseToResponsePayload(
307 `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`,
311 hashId
: this.chargingStation
.stationInfo
?.hashId
,
312 status: ResponseStatus
.FAILURE
,
315 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
316 commandResponse
: commandResponse
!,
317 errorMessage
: (error
as OCPPError
).message
,
318 errorStack
: (error
as OCPPError
).stack
,
319 errorDetails
: (error
as OCPPError
).details
323 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
324 this.sendResponse([uuid
, responsePayload
!])
328 private messageErrorHandler (messageEvent
: MessageEvent
): void {
330 `${this.chargingStation.logPrefix()} ${moduleName}.messageErrorHandler: Error at handling message:`,
335 private async commandHandler (
336 command
: BroadcastChannelProcedureName
,
337 requestPayload
: BroadcastChannelRequestPayload
338 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
339 ): Promise
<CommandResponse
| void> {
340 if (this.commandHandlers
.has(command
)) {
341 this.cleanRequestPayload(command
, requestPayload
)
342 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
343 const commandHandler
= this.commandHandlers
.get(command
)!
344 if (isAsyncFunction(commandHandler
)) {
345 return await commandHandler(requestPayload
)
349 requestPayload
?: BroadcastChannelRequestPayload
350 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
351 ) => CommandResponse
| void
354 throw new BaseError(`Unknown worker broadcast channel command: '${command}'`)
357 private cleanRequestPayload (
358 command
: BroadcastChannelProcedureName
,
359 requestPayload
: BroadcastChannelRequestPayload
361 delete requestPayload
.hashId
362 delete requestPayload
.hashIds
364 BroadcastChannelProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
,
365 BroadcastChannelProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
366 ].includes(command
) && delete requestPayload
.connectorIds
369 private commandResponseToResponsePayload (
370 command
: BroadcastChannelProcedureName
,
371 requestPayload
: BroadcastChannelRequestPayload
,
372 commandResponse
: CommandResponse
373 ): BroadcastChannelResponsePayload
{
374 const responseStatus
= this.commandResponseToResponseStatus(command
, commandResponse
)
375 if (responseStatus
=== ResponseStatus
.SUCCESS
) {
377 hashId
: this.chargingStation
.stationInfo
?.hashId
,
378 status: responseStatus
382 hashId
: this.chargingStation
.stationInfo
?.hashId
,
383 status: responseStatus
,
390 private commandResponseToResponseStatus (
391 command
: BroadcastChannelProcedureName
,
392 commandResponse
: CommandResponse
395 case BroadcastChannelProcedureName
.START_TRANSACTION
:
396 case BroadcastChannelProcedureName
.STOP_TRANSACTION
:
397 case BroadcastChannelProcedureName
.AUTHORIZE
:
401 | StartTransactionResponse
402 | StopTransactionResponse
404 ).idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
406 return ResponseStatus
.SUCCESS
408 return ResponseStatus
.FAILURE
409 case BroadcastChannelProcedureName
.BOOT_NOTIFICATION
:
410 if (commandResponse
.status === RegistrationStatusEnumType
.ACCEPTED
) {
411 return ResponseStatus
.SUCCESS
413 return ResponseStatus
.FAILURE
414 case BroadcastChannelProcedureName
.DATA_TRANSFER
:
415 if (commandResponse
.status === DataTransferStatus
.ACCEPTED
) {
416 return ResponseStatus
.SUCCESS
418 return ResponseStatus
.FAILURE
419 case BroadcastChannelProcedureName
.STATUS_NOTIFICATION
:
420 case BroadcastChannelProcedureName
.METER_VALUES
:
421 if (isEmptyObject(commandResponse
)) {
422 return ResponseStatus
.SUCCESS
424 return ResponseStatus
.FAILURE
425 case BroadcastChannelProcedureName
.HEARTBEAT
:
426 if ('currentTime' in commandResponse
) {
427 return ResponseStatus
.SUCCESS
429 return ResponseStatus
.FAILURE
431 return ResponseStatus
.FAILURE