1 import type ChargingStation from
'./ChargingStation';
2 import { ChargingStationConfigurationUtils
} from
'./ChargingStationConfigurationUtils';
3 import { OCPP16ServiceUtils
} from
'./ocpp/1.6/OCPP16ServiceUtils';
4 import WorkerBroadcastChannel from
'./WorkerBroadcastChannel';
5 import BaseError from
'../exception/BaseError';
6 import type OCPPError from
'../exception/OCPPError';
7 import { StandardParametersKey
} from
'../types/ocpp/Configuration';
9 type BootNotificationRequest
,
10 type DataTransferRequest
,
11 type DiagnosticsStatusNotificationRequest
,
12 type FirmwareStatusNotificationRequest
,
13 type HeartbeatRequest
,
14 type MeterValuesRequest
,
17 type StatusNotificationRequest
,
18 } from
'../types/ocpp/Requests';
20 type BootNotificationResponse
,
21 type DataTransferResponse
,
23 type DiagnosticsStatusNotificationResponse
,
24 type FirmwareStatusNotificationResponse
,
25 type HeartbeatResponse
,
26 type MeterValuesResponse
,
27 RegistrationStatusEnumType
,
28 type StatusNotificationResponse
,
29 } from
'../types/ocpp/Responses';
32 type AuthorizeRequest
,
33 type AuthorizeResponse
,
34 type StartTransactionRequest
,
35 type StartTransactionResponse
,
36 type StopTransactionRequest
,
37 type StopTransactionResponse
,
38 } from
'../types/ocpp/Transaction';
39 import { ResponseStatus
} from
'../types/UIProtocol';
41 BroadcastChannelProcedureName
,
42 type BroadcastChannelRequest
,
43 type BroadcastChannelRequestPayload
,
44 type BroadcastChannelResponsePayload
,
46 } from
'../types/WorkerBroadcastChannel';
47 import Constants from
'../utils/Constants';
48 import logger from
'../utils/Logger';
49 import Utils from
'../utils/Utils';
51 const moduleName
= 'ChargingStationWorkerBroadcastChannel';
53 type CommandResponse
=
54 | StartTransactionResponse
55 | StopTransactionResponse
57 | BootNotificationResponse
58 | StatusNotificationResponse
61 | DataTransferResponse
62 | DiagnosticsStatusNotificationResponse
63 | FirmwareStatusNotificationResponse
;
65 type CommandHandler
= (
66 requestPayload
?: BroadcastChannelRequestPayload
67 ) => Promise
<CommandResponse
| void> | void;
69 export default class ChargingStationWorkerBroadcastChannel
extends WorkerBroadcastChannel
{
70 private readonly commandHandlers
: Map
<BroadcastChannelProcedureName
, CommandHandler
>;
71 private readonly chargingStation
: ChargingStation
;
73 constructor(chargingStation
: ChargingStation
) {
75 const requestParams
: RequestParams
= {
78 this.commandHandlers
= new Map
<BroadcastChannelProcedureName
, CommandHandler
>([
79 [BroadcastChannelProcedureName
.START_CHARGING_STATION
, () => this.chargingStation
.start()],
81 BroadcastChannelProcedureName
.STOP_CHARGING_STATION
,
82 async () => this.chargingStation
.stop(),
85 BroadcastChannelProcedureName
.OPEN_CONNECTION
,
86 () => this.chargingStation
.openWSConnection(),
89 BroadcastChannelProcedureName
.CLOSE_CONNECTION
,
90 () => this.chargingStation
.closeWSConnection(),
93 BroadcastChannelProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
,
94 (requestPayload
?: BroadcastChannelRequestPayload
) =>
95 this.chargingStation
.startAutomaticTransactionGenerator(requestPayload
.connectorIds
),
98 BroadcastChannelProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
,
99 (requestPayload
?: BroadcastChannelRequestPayload
) =>
100 this.chargingStation
.stopAutomaticTransactionGenerator(requestPayload
.connectorIds
),
103 BroadcastChannelProcedureName
.START_TRANSACTION
,
104 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
105 this.chargingStation
.ocppRequestService
.requestHandler
<
106 StartTransactionRequest
,
107 StartTransactionResponse
108 >(this.chargingStation
, RequestCommand
.START_TRANSACTION
, requestPayload
, requestParams
),
111 BroadcastChannelProcedureName
.STOP_TRANSACTION
,
112 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
113 this.chargingStation
.ocppRequestService
.requestHandler
<
114 StopTransactionRequest
,
115 StartTransactionResponse
116 >(this.chargingStation
, RequestCommand
.STOP_TRANSACTION
, {
117 meterStop
: this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(
118 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
<
187 >(this.chargingStation
, RequestCommand
.METER_VALUES
, {
189 OCPP16ServiceUtils
.buildMeterValue(
190 this.chargingStation
,
191 requestPayload
.connectorId
,
192 this.chargingStation
.getConnectorStatus(requestPayload
.connectorId
)?.transactionId
,
193 configuredMeterValueSampleInterval
194 ? Utils
.convertToInt(configuredMeterValueSampleInterval
.value
) * 1000
195 : Constants
.DEFAULT_METER_VALUES_INTERVAL
204 BroadcastChannelProcedureName
.DATA_TRANSFER
,
205 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
206 this.chargingStation
.ocppRequestService
.requestHandler
<
209 >(this.chargingStation
, RequestCommand
.DATA_TRANSFER
, requestPayload
, requestParams
),
212 BroadcastChannelProcedureName
.DIAGNOSTICS_STATUS_NOTIFICATION
,
213 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
214 this.chargingStation
.ocppRequestService
.requestHandler
<
215 DiagnosticsStatusNotificationRequest
,
216 DiagnosticsStatusNotificationResponse
218 this.chargingStation
,
219 RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
,
225 BroadcastChannelProcedureName
.FIRMWARE_STATUS_NOTIFICATION
,
226 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
227 this.chargingStation
.ocppRequestService
.requestHandler
<
228 FirmwareStatusNotificationRequest
,
229 FirmwareStatusNotificationResponse
231 this.chargingStation
,
232 RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
,
238 this.chargingStation
= chargingStation
;
239 this.onmessage
= this.requestHandler
.bind(this) as (message
: MessageEvent
) => void;
240 this.onmessageerror
= this.messageErrorHandler
.bind(this) as (message
: MessageEvent
) => void;
243 private async requestHandler(messageEvent
: MessageEvent
): Promise
<void> {
244 const validatedMessageEvent
= this.validateMessageEvent(messageEvent
);
245 if (validatedMessageEvent
=== false) {
248 if (this.isResponse(validatedMessageEvent
.data
) === true) {
251 const [uuid
, command
, requestPayload
] = validatedMessageEvent
.data
as BroadcastChannelRequest
;
253 requestPayload
?.hashIds
!== undefined &&
254 requestPayload
?.hashIds
?.includes(this.chargingStation
.stationInfo
.hashId
) === false
258 if (requestPayload
?.hashId
!== undefined) {
260 `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead`
264 let responsePayload
: BroadcastChannelResponsePayload
;
265 let commandResponse
: CommandResponse
| void;
267 commandResponse
= await this.commandHandler(command
, requestPayload
);
269 commandResponse
=== undefined ||
270 commandResponse
=== null ||
271 Utils
.isEmptyObject(commandResponse
as CommandResponse
)
274 hashId
: this.chargingStation
.stationInfo
.hashId
,
275 status: ResponseStatus
.SUCCESS
,
278 responsePayload
= this.commandResponseToResponsePayload(
281 commandResponse
as CommandResponse
286 `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`,
290 hashId
: this.chargingStation
.stationInfo
.hashId
,
291 status: ResponseStatus
.FAILURE
,
294 commandResponse
: commandResponse
as CommandResponse
,
295 errorMessage
: (error
as Error).message
,
296 errorStack
: (error
as Error).stack
,
297 errorDetails
: (error
as OCPPError
).details
,
300 this.sendResponse([uuid
, responsePayload
]);
304 private messageErrorHandler(messageEvent
: MessageEvent
): void {
306 `${this.chargingStation.logPrefix()} ${moduleName}.messageErrorHandler: Error at handling message:`,
311 private async commandHandler(
312 command
: BroadcastChannelProcedureName
,
313 requestPayload
: BroadcastChannelRequestPayload
314 ): Promise
<CommandResponse
| void> {
315 if (this.commandHandlers
.has(command
) === true) {
316 this.cleanRequestPayload(command
, requestPayload
);
317 return this.commandHandlers
.get(command
)(requestPayload
);
319 throw new BaseError(`Unknown worker broadcast channel command: ${command}`);
322 private cleanRequestPayload(
323 command
: BroadcastChannelProcedureName
,
324 requestPayload
: BroadcastChannelRequestPayload
326 delete requestPayload
.hashId
;
327 delete requestPayload
.hashIds
;
329 BroadcastChannelProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
,
330 BroadcastChannelProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
,
331 ].includes(command
) === false && delete requestPayload
.connectorIds
;
334 private commandResponseToResponsePayload(
335 command
: BroadcastChannelProcedureName
,
336 requestPayload
: BroadcastChannelRequestPayload
,
337 commandResponse
: CommandResponse
338 ): BroadcastChannelResponsePayload
{
339 const responseStatus
= this.commandResponseToResponseStatus(command
, commandResponse
);
340 if (responseStatus
=== ResponseStatus
.SUCCESS
) {
342 hashId
: this.chargingStation
.stationInfo
.hashId
,
343 status: responseStatus
,
347 hashId
: this.chargingStation
.stationInfo
.hashId
,
348 status: responseStatus
,
355 private commandResponseToResponseStatus(
356 command
: BroadcastChannelProcedureName
,
357 commandResponse
: CommandResponse
360 case BroadcastChannelProcedureName
.START_TRANSACTION
:
361 case BroadcastChannelProcedureName
.STOP_TRANSACTION
:
362 case BroadcastChannelProcedureName
.AUTHORIZE
:
366 | StartTransactionResponse
367 | StopTransactionResponse
369 )?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
371 return ResponseStatus
.SUCCESS
;
373 return ResponseStatus
.FAILURE
;
374 case BroadcastChannelProcedureName
.BOOT_NOTIFICATION
:
375 if (commandResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
) {
376 return ResponseStatus
.SUCCESS
;
378 return ResponseStatus
.FAILURE
;
379 case BroadcastChannelProcedureName
.DATA_TRANSFER
:
380 if (commandResponse
?.status === DataTransferStatus
.ACCEPTED
) {
381 return ResponseStatus
.SUCCESS
;
383 return ResponseStatus
.FAILURE
;
384 case BroadcastChannelProcedureName
.STATUS_NOTIFICATION
:
385 case BroadcastChannelProcedureName
.METER_VALUES
:
386 if (Utils
.isEmptyObject(commandResponse
) === true) {
387 return ResponseStatus
.SUCCESS
;
389 return ResponseStatus
.FAILURE
;
390 case BroadcastChannelProcedureName
.HEARTBEAT
:
391 if ('currentTime' in commandResponse
) {
392 return ResponseStatus
.SUCCESS
;
394 return ResponseStatus
.FAILURE
;
396 return ResponseStatus
.FAILURE
;