UI Protocol: Expose ATG status and use array for all list
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationWorkerBroadcastChannel.ts
CommitLineData
6c8f5d90 1import BaseError from '../exception/BaseError';
89b7a234
JB
2import { RequestCommand } from '../types/ocpp/Requests';
3import {
6c8f5d90 4 AuthorizationStatus,
89b7a234
JB
5 StartTransactionRequest,
6 StartTransactionResponse,
7 StopTransactionReason,
8 StopTransactionRequest,
9 StopTransactionResponse,
10} from '../types/ocpp/Transaction';
11import {
12 BroadcastChannelProcedureName,
13 BroadcastChannelRequest,
6c8f5d90
JB
14 BroadcastChannelRequestPayload,
15 BroadcastChannelResponsePayload,
16 MessageEvent,
89b7a234 17} from '../types/WorkerBroadcastChannel';
f27eb751 18import { ResponseStatus } from '../ui/web/src/types/UIProtocol';
6c8f5d90 19import logger from '../utils/Logger';
db2336d9 20import type ChargingStation from './ChargingStation';
1598b27c 21import WorkerBroadcastChannel from './WorkerBroadcastChannel';
89b7a234 22
4e3ff94d
JB
23const moduleName = 'ChargingStationWorkerBroadcastChannel';
24
6c8f5d90 25type CommandResponse = StartTransactionResponse | StopTransactionResponse;
89b7a234 26
1598b27c 27export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel {
89b7a234
JB
28 private readonly chargingStation: ChargingStation;
29
30 constructor(chargingStation: ChargingStation) {
1598b27c 31 super();
89b7a234 32 this.chargingStation = chargingStation;
02a6943a 33 this.onmessage = this.requestHandler.bind(this) as (message: MessageEvent) => void;
6c8f5d90 34 this.onmessageerror = this.messageErrorHandler.bind(this) as (message: MessageEvent) => void;
89b7a234
JB
35 }
36
02a6943a 37 private async requestHandler(messageEvent: MessageEvent): Promise<void> {
6c8f5d90
JB
38 if (this.isResponse(messageEvent.data)) {
39 return;
40 }
5e3cb728
JB
41 const [uuid, command, requestPayload] = this.validateMessageEvent(messageEvent)
42 .data as BroadcastChannelRequest;
6c8f5d90 43
18057587
JB
44 if (requestPayload?.hashIds !== undefined || requestPayload?.hashId !== undefined) {
45 if (
46 requestPayload?.hashId === undefined &&
47 requestPayload?.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false
48 ) {
49 return;
50 }
51 if (
52 requestPayload?.hashIds === undefined &&
53 requestPayload?.hashId !== this.chargingStation.stationInfo.hashId
54 ) {
55 return;
56 }
57 if (requestPayload?.hashId !== undefined) {
58 logger.warn(
59 `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' instead`
60 );
61 }
4eca248c 62 }
89b7a234 63
6c8f5d90
JB
64 let responsePayload: BroadcastChannelResponsePayload;
65 let commandResponse: CommandResponse;
66 try {
67 commandResponse = await this.commandHandler(command, requestPayload);
68 if (commandResponse === undefined) {
10d244c0 69 responsePayload = {
51c83d6f 70 hashId: this.chargingStation.stationInfo.hashId,
10d244c0
JB
71 status: ResponseStatus.SUCCESS,
72 };
6c8f5d90 73 } else {
10d244c0 74 responsePayload = {
51c83d6f 75 hashId: this.chargingStation.stationInfo.hashId,
10d244c0
JB
76 status: this.commandResponseToResponseStatus(commandResponse),
77 };
6c8f5d90
JB
78 }
79 } catch (error) {
80 logger.error(
81 `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`,
82 error
83 );
84 responsePayload = {
51c83d6f 85 hashId: this.chargingStation.stationInfo.hashId,
6c8f5d90
JB
86 status: ResponseStatus.FAILURE,
87 command,
88 requestPayload,
89 commandResponse,
90 errorMessage: (error as Error).message,
91 errorStack: (error as Error).stack,
92 };
93 }
94 this.sendResponse([uuid, responsePayload]);
95 }
96
97 private messageErrorHandler(messageEvent: MessageEvent): void {
98 logger.error(
99 `${this.chargingStation.logPrefix()} ${moduleName}.messageErrorHandler: Error at handling message:`,
5e3cb728 100 { messageEvent }
6c8f5d90
JB
101 );
102 }
103
104 private async commandHandler(
105 command: BroadcastChannelProcedureName,
106 requestPayload: BroadcastChannelRequestPayload
107 ): Promise<CommandResponse | undefined> {
89b7a234 108 switch (command) {
4f69be04
JB
109 case BroadcastChannelProcedureName.START_CHARGING_STATION:
110 this.chargingStation.start();
111 break;
112 case BroadcastChannelProcedureName.STOP_CHARGING_STATION:
113 await this.chargingStation.stop();
114 break;
115 case BroadcastChannelProcedureName.OPEN_CONNECTION:
116 this.chargingStation.openWSConnection();
117 break;
118 case BroadcastChannelProcedureName.CLOSE_CONNECTION:
119 this.chargingStation.closeWSConnection();
120 break;
89b7a234 121 case BroadcastChannelProcedureName.START_TRANSACTION:
6c8f5d90 122 return this.chargingStation.ocppRequestService.requestHandler<
89b7a234
JB
123 StartTransactionRequest,
124 StartTransactionResponse
125 >(this.chargingStation, RequestCommand.START_TRANSACTION, {
6c8f5d90
JB
126 connectorId: requestPayload.connectorId,
127 idTag: requestPayload.idTag,
89b7a234 128 });
89b7a234 129 case BroadcastChannelProcedureName.STOP_TRANSACTION:
6c8f5d90 130 return this.chargingStation.ocppRequestService.requestHandler<
89b7a234
JB
131 StopTransactionRequest,
132 StopTransactionResponse
133 >(this.chargingStation, RequestCommand.STOP_TRANSACTION, {
6c8f5d90 134 transactionId: requestPayload.transactionId,
89b7a234 135 meterStop: this.chargingStation.getEnergyActiveImportRegisterByTransactionId(
07989fad
JB
136 requestPayload.transactionId,
137 true
89b7a234 138 ),
6c8f5d90 139 idTag: this.chargingStation.getTransactionIdTag(requestPayload.transactionId),
5e3cb728 140 reason: requestPayload.reason ?? StopTransactionReason.NONE,
89b7a234 141 });
4f69be04 142 case BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR:
a5e9befc 143 this.chargingStation.startAutomaticTransactionGenerator(requestPayload.connectorIds);
89b7a234 144 break;
4f69be04 145 case BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR:
a5e9befc 146 this.chargingStation.stopAutomaticTransactionGenerator(requestPayload.connectorIds);
db2336d9 147 break;
6c8f5d90
JB
148 default:
149 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
ce7a4fc3 150 throw new BaseError(`Unknown worker broadcast channel command: ${command}`);
6c8f5d90
JB
151 }
152 }
153
154 private commandResponseToResponseStatus(commandResponse: CommandResponse): ResponseStatus {
155 if (commandResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
156 return ResponseStatus.SUCCESS;
89b7a234 157 }
6c8f5d90 158 return ResponseStatus.FAILURE;
89b7a234
JB
159 }
160}