fix: fix 'Cannot read properties of undefined' with revervation enabled
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 19 Jul 2023 18:50:32 +0000 (20:50 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 19 Jul 2023 18:50:32 +0000 (20:50 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/assets/station-templates/schneider-evses.station-template.json
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStation.ts
src/charging-station/ChargingStationWorker.ts
src/types/ChargingStationWorker.ts
src/utils/MessageChannelUtils.ts
src/worker/WorkerTypes.ts

index f292519e1bed4eb5d0f3963b0c363758f91c78f3..79ce114c9c1c8e70d22688db6c4bd2bd76c48df4 100644 (file)
@@ -27,7 +27,7 @@
       {
         "key": "SupportedFeatureProfiles",
         "readonly": true,
-        "value": "Core,FirmwareManagement,LocalAuthListManagement,SmartCharging,RemoteTrigger"
+        "value": "Core,FirmwareManagement,LocalAuthListManagement,SmartCharging,RemoteTrigger,Reservation"
       },
       {
         "key": "LocalAuthListEnabled",
index 957a2f6feefa1882f568a5af9cda192909dc7d10..37fe0e6b5ae7c5506df07f5d17b94af3b37abfcf 100644 (file)
@@ -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,
       );
index dd5bdda05c6ea04370c769c8c2fe78e1890d9457..d130392e6c0257da7a6feea2cb8cb205249bc7bc 100644 (file)
@@ -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,
               );
             }
index d506637dcccfda43ec2d1989484ae58633a0ae7c..3a723eca25dd5dd86902c510354c9069332643d6 100644 (file)
@@ -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<ChargingStationWorkerData>) => {
-      if (message.id === WorkerMessageEvents.startWorkerElement) {
+      if (message.event === WorkerMessageEvents.startWorkerElement) {
         this.runInAsyncScope(
           startChargingStation.bind(this) as (data?: ChargingStationWorkerData) => void,
           this,
index 6fe529f91ac45183dd4ffaf147136f1745cddc54..2326d765c7ddd05f3021cb46d1d001ae2f4da0b9 100644 (file)
@@ -56,7 +56,7 @@ export type ChargingStationWorkerMessageData = ChargingStationData | Statistics;
 
 export type ChargingStationWorkerMessage<T extends ChargingStationWorkerMessageData> = Omit<
   WorkerMessage<T>,
-  'id'
+  'event'
 > & {
-  id: ChargingStationWorkerMessageEvents;
+  event: ChargingStationWorkerMessageEvents;
 };
index edaaa49ee7280cef16f63e91911f61ddba73e293..f3008b50d117b675cfeec772a8b90518669a998d 100644 (file)
@@ -16,7 +16,7 @@ export const buildStartedMessage = (
   chargingStation: ChargingStation,
 ): ChargingStationWorkerMessage<ChargingStationData> => {
   return {
-    id: ChargingStationWorkerMessageEvents.started,
+    event: ChargingStationWorkerMessageEvents.started,
     data: buildChargingStationDataPayload(chargingStation),
   };
 };
@@ -25,7 +25,7 @@ export const buildStoppedMessage = (
   chargingStation: ChargingStation,
 ): ChargingStationWorkerMessage<ChargingStationData> => {
   return {
-    id: ChargingStationWorkerMessageEvents.stopped,
+    event: ChargingStationWorkerMessageEvents.stopped,
     data: buildChargingStationDataPayload(chargingStation),
   };
 };
@@ -34,7 +34,7 @@ export const buildUpdatedMessage = (
   chargingStation: ChargingStation,
 ): ChargingStationWorkerMessage<ChargingStationData> => {
   return {
-    id: ChargingStationWorkerMessageEvents.updated,
+    event: ChargingStationWorkerMessageEvents.updated,
     data: buildChargingStationDataPayload(chargingStation),
   };
 };
@@ -43,7 +43,7 @@ export const buildPerformanceStatisticsMessage = (
   statistics: Statistics,
 ): ChargingStationWorkerMessage<Statistics> => {
   return {
-    id: ChargingStationWorkerMessageEvents.performanceStatistics,
+    event: ChargingStationWorkerMessageEvents.performanceStatistics,
     data: statistics,
   };
 };
index b7b21ba30f580c39ffd9cd925a1b106b77fbc6d8..90a4175fdbb97170db6c1c4aa27d75f2433ac3ca 100644 (file)
@@ -45,7 +45,7 @@ export interface WorkerSetElement {
 }
 
 export interface WorkerMessage<T extends WorkerData> {
-  id: WorkerMessageEvents;
+  event: WorkerMessageEvents;
   data: T;
 }