fix: warn about reserved transaction started not found
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 13 Jan 2024 17:41:32 +0000 (18:41 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 13 Jan 2024 17:41:32 +0000 (18:41 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/OCPPServiceUtils.ts

index 571fc2005398340bca8f8180cc82df9444143652..b8fce37c98d0dab197adfb868e52c3064478eeea 100644 (file)
@@ -615,33 +615,40 @@ export class OCPP16ResponseService extends OCPPResponseService {
           requestPayload.meterStart
         )
       if (requestPayload.reservationId != null) {
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
         const reservation = chargingStation.getReservationBy(
           'reservationId',
           requestPayload.reservationId
-        )!
-        if (reservation.idTag !== requestPayload.idTag) {
-          logger.warn(
-            `${chargingStation.logPrefix()} Reserved transaction ${
-              payload.transactionId
-            } started with a different idTag ${requestPayload.idTag} than the reservation one ${
-              reservation.idTag
-            }`
+        )
+        if (reservation != null) {
+          if (reservation.idTag !== requestPayload.idTag) {
+            logger.warn(
+              `${chargingStation.logPrefix()} Reserved transaction ${
+                payload.transactionId
+              } started with a different idTag ${requestPayload.idTag} than the reservation one ${
+                reservation.idTag
+              }`
+            )
+          }
+          if (hasReservationExpired(reservation)) {
+            logger.warn(
+              `${chargingStation.logPrefix()} Reserved transaction ${
+                payload.transactionId
+              } started with expired reservation ${
+                requestPayload.reservationId
+              } (expiry date: ${reservation.expiryDate.toISOString()}))`
+            )
+          }
+          await chargingStation.removeReservation(
+            reservation,
+            ReservationTerminationReason.TRANSACTION_STARTED
           )
-        }
-        if (hasReservationExpired(reservation)) {
+        } else {
           logger.warn(
             `${chargingStation.logPrefix()} Reserved transaction ${
               payload.transactionId
-            } started with expired reservation ${
-              requestPayload.reservationId
-            } (expiry date: ${reservation.expiryDate.toISOString()}))`
+            } started with unknown reservation ${requestPayload.reservationId}`
           )
         }
-        await chargingStation.removeReservation(
-          reservation,
-          ReservationTerminationReason.TRANSACTION_STARTED
-        )
       }
       chargingStation.stationInfo?.beginEndMeterValues === true &&
         (await chargingStation.ocppRequestService.requestHandler<
index 3dd6ff7938a2be849dfc90374efa92e5cef663df..535480659fcb56398d9234ff9c0f1a8d689032fb 100644 (file)
@@ -443,7 +443,6 @@ export const buildMeterValue = (
         }
       }
       if (powerSampledValueTemplate != null) {
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
         checkMeasurandPowerDivider(chargingStation, powerSampledValueTemplate.measurand)
         const errMsg = `MeterValues measurand ${
           powerSampledValueTemplate.measurand ?? MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER
@@ -931,7 +930,6 @@ export const buildMeterValue = (
       // Energy.Active.Import.Register measurand (default)
       energySampledValueTemplate = getSampledValueTemplate(chargingStation, connectorId)
       if (energySampledValueTemplate != null) {
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
         checkMeasurandPowerDivider(chargingStation, energySampledValueTemplate.measurand)
         const unitDivider =
           energySampledValueTemplate.unit === MeterValueUnit.KILO_WATT_HOUR ? 1000 : 1