1 import { RequestCommand
} from
'../types/ocpp/Requests';
3 StartTransactionRequest
,
4 StartTransactionResponse
,
6 StopTransactionRequest
,
7 StopTransactionResponse
,
8 } from
'../types/ocpp/Transaction';
10 BroadcastChannelProcedureName
,
11 BroadcastChannelRequest
,
12 } from
'../types/WorkerBroadcastChannel';
13 import ChargingStation from
'./ChargingStation';
14 import WorkerBroadcastChannel from
'./WorkerBroadcastChannel';
16 const moduleName
= 'ChargingStationWorkerBroadcastChannel';
18 type MessageEvent
= { data
: unknown
};
20 export default class ChargingStationWorkerBroadcastChannel
extends WorkerBroadcastChannel
{
21 private readonly chargingStation
: ChargingStation
;
23 constructor(chargingStation
: ChargingStation
) {
25 this.chargingStation
= chargingStation
;
26 this.onmessage
= this.requestHandler
.bind(this) as (message
: MessageEvent
) => void;
29 private async requestHandler(messageEvent
: MessageEvent
): Promise
<void> {
30 const [, command
, payload
] = messageEvent
.data
as BroadcastChannelRequest
;
32 if (payload
.hashId
!== this.chargingStation
.hashId
) {
36 // TODO: return a response stating the command success or failure
38 case BroadcastChannelProcedureName
.START_TRANSACTION
:
39 await this.chargingStation
.ocppRequestService
.requestHandler
<
40 StartTransactionRequest
,
41 StartTransactionResponse
42 >(this.chargingStation
, RequestCommand
.START_TRANSACTION
, {
43 connectorId
: payload
.connectorId
,
47 case BroadcastChannelProcedureName
.STOP_TRANSACTION
:
48 await this.chargingStation
.ocppRequestService
.requestHandler
<
49 StopTransactionRequest
,
50 StopTransactionResponse
51 >(this.chargingStation
, RequestCommand
.STOP_TRANSACTION
, {
52 transactionId
: payload
.transactionId
,
53 meterStop
: this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(
56 idTag
: this.chargingStation
.getTransactionIdTag(payload
.transactionId
),
57 reason
: StopTransactionReason
.NONE
,
60 case BroadcastChannelProcedureName
.START_CHARGING_STATION
:
61 this.chargingStation
.start();
63 case BroadcastChannelProcedureName
.STOP_CHARGING_STATION
:
64 await this.chargingStation
.stop();