1 import BaseError from
'../exception/BaseError';
2 import { RequestCommand
} from
'../types/ocpp/Requests';
5 StartTransactionRequest
,
6 StartTransactionResponse
,
8 StopTransactionRequest
,
9 StopTransactionResponse
,
10 } from
'../types/ocpp/Transaction';
12 BroadcastChannelProcedureName
,
13 BroadcastChannelRequest
,
14 BroadcastChannelRequestPayload
,
15 BroadcastChannelResponsePayload
,
17 } from
'../types/WorkerBroadcastChannel';
18 import { ResponseStatus
} from
'../ui/web/src/type/UIProtocol';
19 import logger from
'../utils/Logger';
20 import type ChargingStation from
'./ChargingStation';
21 import WorkerBroadcastChannel from
'./WorkerBroadcastChannel';
23 const moduleName
= 'ChargingStationWorkerBroadcastChannel';
25 type CommandResponse
= StartTransactionResponse
| StopTransactionResponse
;
27 export default class ChargingStationWorkerBroadcastChannel
extends WorkerBroadcastChannel
{
28 private readonly chargingStation
: ChargingStation
;
30 constructor(chargingStation
: ChargingStation
) {
32 this.chargingStation
= chargingStation
;
33 this.onmessage
= this.requestHandler
.bind(this) as (message
: MessageEvent
) => void;
34 this.onmessageerror
= this.messageErrorHandler
.bind(this) as (message
: MessageEvent
) => void;
37 private async requestHandler(messageEvent
: MessageEvent
): Promise
<void> {
38 if (this.isResponse(messageEvent
.data
)) {
41 this.validateMessageEvent(messageEvent
);
43 const [uuid
, command
, requestPayload
] = messageEvent
.data
as BroadcastChannelRequest
;
46 requestPayload
?.hashId
=== undefined &&
47 (requestPayload
?.hashIds
as string[])?.includes(this.chargingStation
.hashId
) === false
52 requestPayload
?.hashIds
=== undefined &&
53 requestPayload
?.hashId
!== this.chargingStation
.hashId
57 if (requestPayload
?.hashId
!== undefined) {
59 `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' instead`
63 let responsePayload
: BroadcastChannelResponsePayload
;
64 let commandResponse
: CommandResponse
;
66 commandResponse
= await this.commandHandler(command
, requestPayload
);
67 if (commandResponse
=== undefined) {
68 responsePayload
= { status: ResponseStatus
.SUCCESS
};
70 responsePayload
= { status: this.commandResponseToResponseStatus(commandResponse
) };
74 `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`,
78 status: ResponseStatus
.FAILURE
,
82 errorMessage
: (error
as Error).message
,
83 errorStack
: (error
as Error).stack
,
86 this.sendResponse([uuid
, responsePayload
]);
89 private messageErrorHandler(messageEvent
: MessageEvent
): void {
91 `${this.chargingStation.logPrefix()} ${moduleName}.messageErrorHandler: Error at handling message:`,
92 { messageEvent
, messageEventData
: messageEvent
.data
}
96 private async commandHandler(
97 command
: BroadcastChannelProcedureName
,
98 requestPayload
: BroadcastChannelRequestPayload
99 ): Promise
<CommandResponse
| undefined> {
101 case BroadcastChannelProcedureName
.START_TRANSACTION
:
102 return this.chargingStation
.ocppRequestService
.requestHandler
<
103 StartTransactionRequest
,
104 StartTransactionResponse
105 >(this.chargingStation
, RequestCommand
.START_TRANSACTION
, {
106 connectorId
: requestPayload
.connectorId
,
107 idTag
: requestPayload
.idTag
,
109 case BroadcastChannelProcedureName
.STOP_TRANSACTION
:
110 return this.chargingStation
.ocppRequestService
.requestHandler
<
111 StopTransactionRequest
,
112 StopTransactionResponse
113 >(this.chargingStation
, RequestCommand
.STOP_TRANSACTION
, {
114 transactionId
: requestPayload
.transactionId
,
115 meterStop
: this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(
116 requestPayload
.transactionId
118 idTag
: this.chargingStation
.getTransactionIdTag(requestPayload
.transactionId
),
119 reason
: StopTransactionReason
.NONE
,
121 case BroadcastChannelProcedureName
.START_CHARGING_STATION
:
122 this.chargingStation
.start();
124 case BroadcastChannelProcedureName
.STOP_CHARGING_STATION
:
125 await this.chargingStation
.stop();
127 case BroadcastChannelProcedureName
.OPEN_CONNECTION
:
128 this.chargingStation
.openWSConnection();
130 case BroadcastChannelProcedureName
.CLOSE_CONNECTION
:
131 this.chargingStation
.closeWSConnection();
134 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
135 throw new BaseError(`Unknown worker broadcast channel command: ${command}`);
139 private commandResponseToResponseStatus(commandResponse
: CommandResponse
): ResponseStatus
{
140 if (commandResponse
?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
) {
141 return ResponseStatus
.SUCCESS
;
143 return ResponseStatus
.FAILURE
;