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 // FIXME: Implement OCPP version agnostic helpers
190 OCPP16ServiceUtils
.buildMeterValue(
191 this.chargingStation
,
192 requestPayload
.connectorId
,
193 this.chargingStation
.getConnectorStatus(requestPayload
.connectorId
)?.transactionId
,
194 configuredMeterValueSampleInterval
195 ? Utils
.convertToInt(configuredMeterValueSampleInterval
.value
) * 1000
196 : Constants
.DEFAULT_METER_VALUES_INTERVAL
205 BroadcastChannelProcedureName
.DATA_TRANSFER
,
206 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
207 this.chargingStation
.ocppRequestService
.requestHandler
<
210 >(this.chargingStation
, RequestCommand
.DATA_TRANSFER
, requestPayload
, requestParams
),
213 BroadcastChannelProcedureName
.DIAGNOSTICS_STATUS_NOTIFICATION
,
214 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
215 this.chargingStation
.ocppRequestService
.requestHandler
<
216 DiagnosticsStatusNotificationRequest
,
217 DiagnosticsStatusNotificationResponse
219 this.chargingStation
,
220 RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
,
226 BroadcastChannelProcedureName
.FIRMWARE_STATUS_NOTIFICATION
,
227 async (requestPayload
?: BroadcastChannelRequestPayload
) =>
228 this.chargingStation
.ocppRequestService
.requestHandler
<
229 FirmwareStatusNotificationRequest
,
230 FirmwareStatusNotificationResponse
232 this.chargingStation
,
233 RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
,
239 this.chargingStation
= chargingStation
;
240 this.onmessage
= this.requestHandler
.bind(this) as (message
: MessageEvent
) => void;
241 this.onmessageerror
= this.messageErrorHandler
.bind(this) as (message
: MessageEvent
) => void;
244 private async requestHandler(messageEvent
: MessageEvent
): Promise
<void> {
245 const validatedMessageEvent
= this.validateMessageEvent(messageEvent
);
246 if (validatedMessageEvent
=== false) {
249 if (this.isResponse(validatedMessageEvent
.data
) === true) {
252 const [uuid
, command
, requestPayload
] = validatedMessageEvent
.data
as BroadcastChannelRequest
;
254 requestPayload
?.hashIds
!== undefined &&
255 requestPayload
?.hashIds
?.includes(this.chargingStation
.stationInfo
.hashId
) === false
259 if (requestPayload
?.hashId
!== undefined) {
261 `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead`
265 let responsePayload
: BroadcastChannelResponsePayload
;
266 let commandResponse
: CommandResponse
| void;
268 commandResponse
= await this.commandHandler(command
, requestPayload
);
270 commandResponse
=== undefined ||
271 commandResponse
=== null ||
272 Utils
.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 (Utils
.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
;