Forward UI request UUID to broadcast channel request
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationWorkerBroadcastChannel.ts
1 import { RequestCommand } from '../types/ocpp/Requests';
2 import {
3 StartTransactionRequest,
4 StartTransactionResponse,
5 StopTransactionReason,
6 StopTransactionRequest,
7 StopTransactionResponse,
8 } from '../types/ocpp/Transaction';
9 import {
10 BroadcastChannelProcedureName,
11 BroadcastChannelRequest,
12 } from '../types/WorkerBroadcastChannel';
13 import ChargingStation from './ChargingStation';
14 import WorkerBroadcastChannel from './WorkerBroadcastChannel';
15
16 const moduleName = 'ChargingStationWorkerBroadcastChannel';
17
18 type MessageEvent = { data: unknown };
19
20 export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel {
21 private readonly chargingStation: ChargingStation;
22
23 constructor(chargingStation: ChargingStation) {
24 super();
25 this.chargingStation = chargingStation;
26 this.onmessage = this.handleRequest.bind(this) as (message: MessageEvent) => void;
27 }
28
29 private async handleRequest(messageEvent: MessageEvent): Promise<void> {
30 const [, command, payload] = messageEvent.data as BroadcastChannelRequest;
31
32 if (payload.hashId !== this.chargingStation.hashId) {
33 return;
34 }
35
36 // TODO: return a response stating the command success or failure
37 switch (command) {
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,
44 idTag: payload.idTag,
45 });
46 break;
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(
54 payload.transactionId
55 ),
56 idTag: this.chargingStation.getTransactionIdTag(payload.transactionId),
57 reason: StopTransactionReason.NONE,
58 });
59 break;
60 case BroadcastChannelProcedureName.START_CHARGING_STATION:
61 this.chargingStation.start();
62 break;
63 case BroadcastChannelProcedureName.STOP_CHARGING_STATION:
64 await this.chargingStation.stop();
65 break;
66 }
67 }
68 }