UI Server: Improve error handling
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / ui-services / AbstractUIService.ts
1 import BaseError from '../../../exception/BaseError';
2 import type OCPPError from '../../../exception/OCPPError';
3 import { Bootstrap } from '../../../internal';
4 import {
5 ProcedureName,
6 type ProtocolRequest,
7 type ProtocolRequestHandler,
8 type ProtocolVersion,
9 type RequestPayload,
10 type ResponsePayload,
11 ResponseStatus,
12 } from '../../../types/UIProtocol';
13 import {
14 BroadcastChannelProcedureName,
15 type BroadcastChannelRequestPayload,
16 } from '../../../types/WorkerBroadcastChannel';
17 import logger from '../../../utils/Logger';
18 import Utils from '../../../utils/Utils';
19 import UIServiceWorkerBroadcastChannel from '../../UIServiceWorkerBroadcastChannel';
20 import type { AbstractUIServer } from '../AbstractUIServer';
21
22 const moduleName = 'AbstractUIService';
23
24 export default abstract class AbstractUIService {
25 protected static readonly ProcedureNameToBroadCastChannelProcedureNameMap: Omit<
26 Record<ProcedureName, BroadcastChannelProcedureName>,
27 'startSimulator' | 'stopSimulator' | 'listChargingStations'
28 > = {
29 [ProcedureName.START_CHARGING_STATION]: BroadcastChannelProcedureName.START_CHARGING_STATION,
30 [ProcedureName.STOP_CHARGING_STATION]: BroadcastChannelProcedureName.STOP_CHARGING_STATION,
31 [ProcedureName.CLOSE_CONNECTION]: BroadcastChannelProcedureName.CLOSE_CONNECTION,
32 [ProcedureName.OPEN_CONNECTION]: BroadcastChannelProcedureName.OPEN_CONNECTION,
33 [ProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR]:
34 BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR,
35 [ProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR]:
36 BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR,
37 [ProcedureName.START_TRANSACTION]: BroadcastChannelProcedureName.START_TRANSACTION,
38 [ProcedureName.STOP_TRANSACTION]: BroadcastChannelProcedureName.STOP_TRANSACTION,
39 [ProcedureName.AUTHORIZE]: BroadcastChannelProcedureName.AUTHORIZE,
40 [ProcedureName.STATUS_NOTIFICATION]: BroadcastChannelProcedureName.STATUS_NOTIFICATION,
41 [ProcedureName.HEARTBEAT]: BroadcastChannelProcedureName.HEARTBEAT,
42 [ProcedureName.METER_VALUES]: BroadcastChannelProcedureName.METER_VALUES,
43 };
44
45 protected readonly requestHandlers: Map<ProcedureName, ProtocolRequestHandler>;
46 private readonly version: ProtocolVersion;
47 private readonly uiServer: AbstractUIServer;
48 private readonly uiServiceWorkerBroadcastChannel: UIServiceWorkerBroadcastChannel;
49 private readonly broadcastChannelRequests: Map<string, number>;
50
51 constructor(uiServer: AbstractUIServer, version: ProtocolVersion) {
52 this.uiServer = uiServer;
53 this.version = version;
54 this.requestHandlers = new Map<ProcedureName, ProtocolRequestHandler>([
55 [ProcedureName.LIST_CHARGING_STATIONS, this.handleListChargingStations.bind(this)],
56 [ProcedureName.START_SIMULATOR, this.handleStartSimulator.bind(this)],
57 [ProcedureName.STOP_SIMULATOR, this.handleStopSimulator.bind(this)],
58 ]);
59 this.uiServiceWorkerBroadcastChannel = new UIServiceWorkerBroadcastChannel(this);
60 this.broadcastChannelRequests = new Map<string, number>();
61 }
62
63 public async requestHandler(request: ProtocolRequest): Promise<void> {
64 let messageId: string;
65 let command: ProcedureName;
66 let requestPayload: RequestPayload | undefined;
67 let responsePayload: ResponsePayload;
68 try {
69 [messageId, command, requestPayload] = request;
70
71 if (this.requestHandlers.has(command) === false) {
72 throw new BaseError(
73 `${command} is not implemented to handle message payload ${JSON.stringify(
74 requestPayload,
75 null,
76 2
77 )}`
78 );
79 }
80
81 // Call the request handler to build the response payload
82 responsePayload = await this.requestHandlers.get(command)(messageId, command, requestPayload);
83 } catch (error) {
84 // Log
85 logger.error(`${this.logPrefix(moduleName, 'messageHandler')} Handle request error:`, error);
86 responsePayload = {
87 hashIds: requestPayload.hashIds,
88 status: ResponseStatus.FAILURE,
89 command,
90 requestPayload,
91 responsePayload,
92 errorMessage: (error as Error).message,
93 errorStack: (error as Error).stack,
94 errorDetails: (error as OCPPError).details,
95 };
96 } finally {
97 // Send response for payload not forwarded to broadcast channel
98 if (responsePayload !== undefined) {
99 this.sendResponse(messageId, responsePayload);
100 }
101 }
102 }
103
104 public sendRequest(
105 messageId: string,
106 procedureName: ProcedureName,
107 requestPayload: RequestPayload
108 ): void {
109 this.uiServer.sendRequest(
110 this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload)
111 );
112 }
113
114 public sendResponse(messageId: string, responsePayload: ResponsePayload): void {
115 this.uiServer.sendResponse(this.uiServer.buildProtocolResponse(messageId, responsePayload));
116 }
117
118 public logPrefix(modName: string, methodName: string): string {
119 return this.uiServer.logPrefix(modName, methodName, this.version);
120 }
121
122 public deleteBroadcastChannelRequest(uuid: string): void {
123 this.broadcastChannelRequests.delete(uuid);
124 }
125
126 public getBroadcastChannelExpectedResponses(uuid: string): number {
127 return this.broadcastChannelRequests.get(uuid) ?? 0;
128 }
129
130 protected sendBroadcastChannelRequest(
131 uuid: string,
132 procedureName: BroadcastChannelProcedureName,
133 payload: BroadcastChannelRequestPayload
134 ): void {
135 if (!Utils.isEmptyArray(payload.hashIds)) {
136 payload.hashIds = payload.hashIds
137 .map((hashId) => {
138 if (this.uiServer.chargingStations.has(hashId) === true) {
139 return hashId;
140 }
141 logger.warn(
142 `${this.logPrefix(
143 moduleName,
144 'sendBroadcastChannelRequest'
145 )} Charging station with hashId '${hashId}' not found`
146 );
147 })
148 .filter((hashId) => hashId !== undefined);
149 }
150 const expectedNumberOfResponses = !Utils.isEmptyArray(payload.hashIds)
151 ? payload.hashIds.length
152 : this.uiServer.chargingStations.size;
153 this.uiServiceWorkerBroadcastChannel.sendRequest([uuid, procedureName, payload]);
154 this.broadcastChannelRequests.set(uuid, expectedNumberOfResponses);
155 }
156
157 protected handleProtocolRequest(
158 uuid: string,
159 procedureName: ProcedureName,
160 payload: RequestPayload
161 ): void {
162 this.sendBroadcastChannelRequest(
163 uuid,
164 AbstractUIService.ProcedureNameToBroadCastChannelProcedureNameMap[
165 procedureName
166 ] as BroadcastChannelProcedureName,
167 payload
168 );
169 }
170
171 private handleListChargingStations(): ResponsePayload {
172 return {
173 status: ResponseStatus.SUCCESS,
174 chargingStations: [...this.uiServer.chargingStations.values()],
175 } as ResponsePayload;
176 }
177
178 private async handleStartSimulator(): Promise<ResponsePayload> {
179 await Bootstrap.getInstance().start();
180 return { status: ResponseStatus.SUCCESS };
181 }
182
183 private async handleStopSimulator(): Promise<ResponsePayload> {
184 await Bootstrap.getInstance().stop();
185 return { status: ResponseStatus.SUCCESS };
186 }
187 }