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 type MessageEvent
= { data
: unknown
};
18 export default class ChargingStationWorkerBroadcastChannel
extends WorkerBroadcastChannel
{
19 private readonly chargingStation
: ChargingStation
;
21 constructor(chargingStation
: ChargingStation
) {
23 this.chargingStation
= chargingStation
;
24 this.onmessage
= this.handleRequest
.bind(this) as (message
: MessageEvent
) => void;
27 private async handleRequest(messageEvent
: MessageEvent
): Promise
<void> {
28 const [, command
, payload
] = messageEvent
.data
as BroadcastChannelRequest
;
30 if (payload
.hashId
!== this.chargingStation
.hashId
) {
34 // TODO: return a response stating the command success or failure
36 case BroadcastChannelProcedureName
.START_TRANSACTION
:
37 await this.chargingStation
.ocppRequestService
.requestHandler
<
38 StartTransactionRequest
,
39 StartTransactionResponse
40 >(this.chargingStation
, RequestCommand
.START_TRANSACTION
, {
41 connectorId
: payload
.connectorId
,
45 case BroadcastChannelProcedureName
.STOP_TRANSACTION
:
46 await this.chargingStation
.ocppRequestService
.requestHandler
<
47 StopTransactionRequest
,
48 StopTransactionResponse
49 >(this.chargingStation
, RequestCommand
.STOP_TRANSACTION
, {
50 transactionId
: payload
.transactionId
,
51 meterStop
: this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(
54 idTag
: this.chargingStation
.getTransactionIdTag(payload
.transactionId
),
55 reason
: StopTransactionReason
.NONE
,
58 case BroadcastChannelProcedureName
.START_CHARGING_STATION
:
59 this.chargingStation
.start();
61 case BroadcastChannelProcedureName
.STOP_CHARGING_STATION
:
62 await this.chargingStation
.stop();