From 8cc482a9324a0989516b6eb6db85a16258c4b4d1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 19 Jul 2023 20:50:32 +0200 Subject: [PATCH] fix: fix 'Cannot read properties of undefined' with revervation enabled MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../schneider-evses.station-template.json | 2 +- src/charging-station/Bootstrap.ts | 9 ++++++--- src/charging-station/ChargingStation.ts | 8 ++++---- src/charging-station/ChargingStationWorker.ts | 2 +- src/types/ChargingStationWorker.ts | 4 ++-- src/utils/MessageChannelUtils.ts | 8 ++++---- src/worker/WorkerTypes.ts | 2 +- 7 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/assets/station-templates/schneider-evses.station-template.json b/src/assets/station-templates/schneider-evses.station-template.json index f292519e..79ce114c 100644 --- a/src/assets/station-templates/schneider-evses.station-template.json +++ b/src/assets/station-templates/schneider-evses.station-template.json @@ -27,7 +27,7 @@ { "key": "SupportedFeatureProfiles", "readonly": true, - "value": "Core,FirmwareManagement,LocalAuthListManagement,SmartCharging,RemoteTrigger" + "value": "Core,FirmwareManagement,LocalAuthListManagement,SmartCharging,RemoteTrigger,Reservation" }, { "key": "LocalAuthListEnabled", diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 957a2f6f..37fe0e6b 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -260,8 +260,11 @@ export class Bootstrap extends EventEmitter { // 2, // )}`, // ); + if (isNullOrUndefined(msg?.event)) { + return; + } try { - switch (msg.id) { + switch (msg.event) { case ChargingStationWorkerMessageEvents.started: this.workerEventStarted(msg.data as ChargingStationData); this.emit(ChargingStationWorkerMessageEvents.started, msg.data as ChargingStationData); @@ -283,13 +286,13 @@ export class Bootstrap extends EventEmitter { break; default: throw new BaseError( - `Unknown event type: '${msg.id}' for data: ${JSON.stringify(msg.data, null, 2)}`, + `Unknown event type: '${msg.event}' for data: ${JSON.stringify(msg.data, null, 2)}`, ); } } catch (error) { logger.error( `${this.logPrefix()} ${moduleName}.messageHandler: Error occurred while handling '${ - msg.id + msg.event }' event:`, error, ); diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index dd5bdda0..d130392e 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1031,9 +1031,9 @@ export class ChargingStation { if (this.hasEvses) { for (const evseStatus of this.evses.values()) { for (const connectorStatus of evseStatus.connectors.values()) { - if (connectorStatus.reservation!.expiryDate < now) { + if (connectorStatus.reservation && connectorStatus.reservation.expiryDate < now) { await this.removeReservation( - connectorStatus.reservation!, + connectorStatus.reservation, ReservationTerminationReason.EXPIRED, ); } @@ -1041,9 +1041,9 @@ export class ChargingStation { } } else { for (const connectorStatus of this.connectors.values()) { - if (connectorStatus.reservation!.expiryDate < now) { + if (connectorStatus.reservation && connectorStatus.reservation.expiryDate < now) { await this.removeReservation( - connectorStatus.reservation!, + connectorStatus.reservation, ReservationTerminationReason.EXPIRED, ); } diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index d506637d..3a723eca 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -26,7 +26,7 @@ class ChargingStationWorker extends AsyncResource { super(moduleName); // Add message listener to create and start charging station from the main thread parentPort?.on('message', (message: WorkerMessage) => { - if (message.id === WorkerMessageEvents.startWorkerElement) { + if (message.event === WorkerMessageEvents.startWorkerElement) { this.runInAsyncScope( startChargingStation.bind(this) as (data?: ChargingStationWorkerData) => void, this, diff --git a/src/types/ChargingStationWorker.ts b/src/types/ChargingStationWorker.ts index 6fe529f9..2326d765 100644 --- a/src/types/ChargingStationWorker.ts +++ b/src/types/ChargingStationWorker.ts @@ -56,7 +56,7 @@ export type ChargingStationWorkerMessageData = ChargingStationData | Statistics; export type ChargingStationWorkerMessage = Omit< WorkerMessage, - 'id' + 'event' > & { - id: ChargingStationWorkerMessageEvents; + event: ChargingStationWorkerMessageEvents; }; diff --git a/src/utils/MessageChannelUtils.ts b/src/utils/MessageChannelUtils.ts index edaaa49e..f3008b50 100644 --- a/src/utils/MessageChannelUtils.ts +++ b/src/utils/MessageChannelUtils.ts @@ -16,7 +16,7 @@ export const buildStartedMessage = ( chargingStation: ChargingStation, ): ChargingStationWorkerMessage => { return { - id: ChargingStationWorkerMessageEvents.started, + event: ChargingStationWorkerMessageEvents.started, data: buildChargingStationDataPayload(chargingStation), }; }; @@ -25,7 +25,7 @@ export const buildStoppedMessage = ( chargingStation: ChargingStation, ): ChargingStationWorkerMessage => { return { - id: ChargingStationWorkerMessageEvents.stopped, + event: ChargingStationWorkerMessageEvents.stopped, data: buildChargingStationDataPayload(chargingStation), }; }; @@ -34,7 +34,7 @@ export const buildUpdatedMessage = ( chargingStation: ChargingStation, ): ChargingStationWorkerMessage => { return { - id: ChargingStationWorkerMessageEvents.updated, + event: ChargingStationWorkerMessageEvents.updated, data: buildChargingStationDataPayload(chargingStation), }; }; @@ -43,7 +43,7 @@ export const buildPerformanceStatisticsMessage = ( statistics: Statistics, ): ChargingStationWorkerMessage => { return { - id: ChargingStationWorkerMessageEvents.performanceStatistics, + event: ChargingStationWorkerMessageEvents.performanceStatistics, data: statistics, }; }; diff --git a/src/worker/WorkerTypes.ts b/src/worker/WorkerTypes.ts index b7b21ba3..90a4175f 100644 --- a/src/worker/WorkerTypes.ts +++ b/src/worker/WorkerTypes.ts @@ -45,7 +45,7 @@ export interface WorkerSetElement { } export interface WorkerMessage { - id: WorkerMessageEvents; + event: WorkerMessageEvents; data: T; } -- 2.34.1