Fix serial number propagation at charging station template change
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 23 May 2022 21:15:16 +0000 (23:15 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 23 May 2022 21:15:16 +0000 (23:15 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ChargingStationUtils.ts
src/types/ChargingStationInfo.ts
src/types/ocpp/1.6/Requests.ts

index 96d889ee48b7f2a9168ccc5737d554885e546bc9..064e33ad805a19ac5063d8175e5747c9bda88689 100644 (file)
@@ -826,7 +826,12 @@ export default class ChargingStation {
       }
       return stationInfoFromFile;
     }
-    ChargingStationUtils.createSerialNumber(this.getTemplateFromFile(), stationInfoFromFile);
+    stationInfoFromFile &&
+      ChargingStationUtils.propagateSerialNumber(
+        this.getTemplateFromFile(),
+        stationInfoFromFile,
+        stationInfoFromTemplate
+      );
     return stationInfoFromTemplate;
   }
 
index b57e98e6741222f309d04d8ac84ed3ccbf4bec37..582912b0d6a937097bef372150e0d7b212639051 100644 (file)
@@ -7,6 +7,7 @@ import ChargingStationTemplate, {
 } from '../types/ChargingStationTemplate';
 import { MeterValueMeasurand, MeterValuePhase } from '../types/ocpp/MeterValues';
 
+import BaseError from '../exception/BaseError';
 import { BootNotificationRequest } from '../types/ocpp/Requests';
 import ChargingStation from './ChargingStation';
 import { ChargingStationConfigurationUtils } from './ChargingStationConfigurationUtils';
@@ -229,6 +230,7 @@ export class ChargingStationUtils {
     delete stationTemplate.AutomaticTransactionGenerator;
     delete stationTemplate.chargeBoxSerialNumberPrefix;
     delete stationTemplate.chargePointSerialNumberPrefix;
+    delete stationTemplate.meterSerialNumberPrefix;
     return stationTemplate;
   }
 
@@ -251,7 +253,7 @@ export class ChargingStationUtils {
 
   public static createSerialNumber(
     stationTemplate: ChargingStationTemplate,
-    stationInfo: ChargingStationInfo,
+    stationInfo: ChargingStationInfo = {} as ChargingStationInfo,
     params: {
       randomSerialNumberUpperCase?: boolean;
       randomSerialNumber?: boolean;
@@ -268,24 +270,34 @@ export class ChargingStationUtils {
           upperCase: params.randomSerialNumberUpperCase,
         })
       : '';
-    stationTemplate?.chargePointSerialNumberPrefix &&
-    stationInfo &&
-    Utils.isNullOrUndefined(stationInfo?.chargePointSerialNumber)
-      ? (stationInfo.chargePointSerialNumber =
-          stationTemplate.chargePointSerialNumberPrefix + serialNumberSuffix)
-      : stationInfo && delete stationInfo.chargePointSerialNumber;
-    stationTemplate?.chargeBoxSerialNumberPrefix &&
-    stationInfo &&
-    Utils.isNullOrUndefined(stationInfo?.chargeBoxSerialNumber)
-      ? (stationInfo.chargeBoxSerialNumber =
-          stationTemplate.chargeBoxSerialNumberPrefix + serialNumberSuffix)
-      : stationInfo && delete stationInfo.chargeBoxSerialNumber;
-    stationTemplate?.meterSerialNumberPrefix &&
-    stationInfo &&
-    Utils.isNullOrUndefined(stationInfo?.meterSerialNumber)
-      ? (stationInfo.meterSerialNumber =
-          stationTemplate.meterSerialNumberPrefix + serialNumberSuffix)
-      : stationInfo && delete stationInfo.meterSerialNumber;
+    stationInfo.chargePointSerialNumber =
+      stationTemplate?.chargePointSerialNumberPrefix &&
+      stationTemplate.chargePointSerialNumberPrefix + serialNumberSuffix;
+    stationInfo.chargeBoxSerialNumber =
+      stationTemplate?.chargeBoxSerialNumberPrefix &&
+      stationTemplate.chargeBoxSerialNumberPrefix + serialNumberSuffix;
+    stationInfo.meterSerialNumber =
+      stationTemplate?.meterSerialNumberPrefix &&
+      stationTemplate.meterSerialNumberPrefix + serialNumberSuffix;
+  }
+
+  public static propagateSerialNumber(
+    stationTemplate: ChargingStationTemplate,
+    stationInfoSrc: ChargingStationInfo,
+    stationInfoDst: ChargingStationInfo = {} as ChargingStationInfo
+  ) {
+    if (!stationInfoSrc || !stationTemplate) {
+      throw new BaseError('Missing charging station template or info to propagate serial number');
+    }
+    stationTemplate?.chargePointSerialNumberPrefix && stationInfoSrc?.chargePointSerialNumber
+      ? (stationInfoDst.chargePointSerialNumber = stationInfoSrc.chargePointSerialNumber)
+      : stationInfoDst?.chargePointSerialNumber && delete stationInfoDst.chargePointSerialNumber;
+    stationTemplate?.chargeBoxSerialNumberPrefix && stationInfoSrc?.chargeBoxSerialNumber
+      ? (stationInfoDst.chargeBoxSerialNumber = stationInfoSrc.chargeBoxSerialNumber)
+      : stationInfoDst?.chargeBoxSerialNumber && delete stationInfoDst.chargeBoxSerialNumber;
+    stationTemplate?.meterSerialNumberPrefix && stationInfoSrc?.meterSerialNumber
+      ? (stationInfoDst.meterSerialNumber = stationInfoSrc.meterSerialNumber)
+      : stationInfoDst?.meterSerialNumber && delete stationInfoDst.meterSerialNumber;
   }
 
   public static getAmperageLimitationUnitDivider(stationInfo: ChargingStationInfo): number {
index 1c6415ae801ed52b0fb3c2975bf08bae64114f41..2baa72ef24ebb656ace0a380d41d844138f9d460 100644 (file)
@@ -9,6 +9,7 @@ export default interface ChargingStationInfo
     | 'powerUnit'
     | 'chargeBoxSerialNumberPrefix'
     | 'chargePointSerialNumberPrefix'
+    | 'meterSerialNumberPrefix'
   > {
   infoHash?: string;
   chargingStationId?: string;
index 31825e16b18b12996166ff480eef42178f07662f..1499e4ff11237f98df2b57b05bfc1af9fff9fec1 100644 (file)
@@ -36,15 +36,15 @@ export enum OCPP16IncomingRequestCommand {
 export type OCPP16HeartbeatRequest = EmptyObject;
 
 export interface OCPP16BootNotificationRequest extends JsonObject {
-  chargeBoxSerialNumber?: string;
+  chargePointVendor: string;
   chargePointModel: string;
   chargePointSerialNumber?: string;
-  chargePointVendor: string;
+  chargeBoxSerialNumber?: string;
   firmwareVersion?: string;
   iccid?: string;
   imsi?: string;
-  meterSerialNumber?: string;
   meterType?: string;
+  meterSerialNumber?: string;
 }
 
 export interface OCPP16StatusNotificationRequest extends JsonObject {