From: Jérôme Benoit Date: Fri, 9 Sep 2022 20:02:05 +0000 (+0200) Subject: UI Server: Always delete reponse handler in error case X-Git-Tag: v1.1.73~20 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=e2c77f1010574abfb90423c9e593c09d6e447c96;p=e-mobility-charging-stations-simulator.git UI Server: Always delete reponse handler in error case Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 6dc6620d..37cd1e64 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -159,7 +159,7 @@ export class Bootstrap { } private initializeWorkerImplementation(): void { - !this.workerImplementation && + this.workerImplementation === null && (this.workerImplementation = WorkerFactory.getWorkerImplementation( this.workerScript, Configuration.getWorker().processType, @@ -218,19 +218,19 @@ export class Bootstrap { } } - private workerEventStarted(data: ChargingStationData) { + private workerEventStarted = (data: ChargingStationData) => { this.uiServer?.chargingStations.set(data.stationInfo.hashId, data); ++this.numberOfStartedChargingStations; - } + }; - private workerEventStopped(data: ChargingStationData) { + private workerEventStopped = (data: ChargingStationData) => { this.uiServer?.chargingStations.set(data.stationInfo.hashId, data); --this.numberOfStartedChargingStations; - } + }; - private workerEventUpdated(data: ChargingStationData) { + private workerEventUpdated = (data: ChargingStationData) => { this.uiServer?.chargingStations.set(data.stationInfo.hashId, data); - } + }; private workerEventPerformanceStatistics = (data: Statistics) => { this.storage.storePerformanceStatistics(data) as void; diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index 822244f9..37c579dc 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -47,7 +47,6 @@ export default class UIHttpServer extends AbstractUIServer { 'Content-Type': 'application/json', }) .end(JSON.stringify(payload)); - this.responseHandlers.delete(uuid); } else { logger.error( `${this.logPrefix(moduleName, 'sendResponse')} Response for unknown request id: ${uuid}` @@ -58,6 +57,8 @@ export default class UIHttpServer extends AbstractUIServer { `${this.logPrefix(moduleName, 'sendResponse')} Error at sending response id '${uuid}':`, error ); + } finally { + this.responseHandlers.delete(uuid); } } diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index 03cd184d..f8f836b1 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -99,8 +99,16 @@ export default class UIWebSocketServer extends AbstractUIServer { const ws = this.responseHandlers.get(responseId) as WebSocket; if (ws?.readyState === WebSocket.OPEN) { ws.send(JSON.stringify(response)); + } else { + logger.error( + `${this.logPrefix( + moduleName, + 'sendResponse' + )} Error at sending response id '${responseId}', WebSocket is not open: ${ + ws?.readyState + }` + ); } - this.responseHandlers.delete(responseId); } else { logger.error( `${this.logPrefix( @@ -117,6 +125,8 @@ export default class UIWebSocketServer extends AbstractUIServer { )} Error at sending response id '${responseId}':`, error ); + } finally { + this.responseHandlers.delete(responseId); } }