Add a sanity check at worker implementation options check
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16ServiceUtils.ts
index 34a65de8f1ab0c379d0ee95af02f78f5d52afd1d..4913bd9b18cf891d2d162f0812cb4ac2f9a1e933 100644 (file)
@@ -1,3 +1,5 @@
+// Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
+
 import { MeterValueContext, MeterValueLocation, MeterValueUnit, OCPP16MeterValue, OCPP16MeterValueMeasurand, OCPP16MeterValuePhase, OCPP16SampledValue } from '../../../types/ocpp/1.6/MeterValues';
 
 import ChargingStation from '../../ChargingStation';
@@ -10,19 +12,19 @@ export class OCPP16ServiceUtils {
     if (Utils.isUndefined(chargingStation.stationInfo.powerDivider)) {
       const errMsg = `${chargingStation.logPrefix()} MeterValues measurand ${measurandType ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: powerDivider is undefined`;
       logger.error(errMsg);
-      throw Error(errMsg);
+      throw new Error(errMsg);
     } else if (chargingStation.stationInfo?.powerDivider <= 0) {
       const errMsg = `${chargingStation.logPrefix()} MeterValues measurand ${measurandType ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: powerDivider have zero or below value ${chargingStation.stationInfo.powerDivider}`;
       logger.error(errMsg);
-      throw Error(errMsg);
+      throw new Error(errMsg);
     }
   }
 
   public static buildSampledValue(sampledValueTemplate: SampledValueTemplate, value: number, context?: MeterValueContext, phase?: OCPP16MeterValuePhase): OCPP16SampledValue {
-    const sampledValueValue = value ?? (sampledValueTemplate.value ?? null);
-    const sampledValueContext = context ?? (sampledValueTemplate.context ?? null);
-    const sampledValueLocation = sampledValueTemplate.location ?? OCPP16ServiceUtils.getMeasurandDefaultLocation(sampledValueTemplate.measurand ?? null);
-    const sampledValuePhase = phase ?? (sampledValueTemplate.phase ?? null);
+    const sampledValueValue = value ?? (sampledValueTemplate?.value ?? null);
+    const sampledValueContext = context ?? (sampledValueTemplate?.context ?? null);
+    const sampledValueLocation = sampledValueTemplate?.location ?? OCPP16ServiceUtils.getMeasurandDefaultLocation(sampledValueTemplate?.measurand ?? null);
+    const sampledValuePhase = phase ?? (sampledValueTemplate?.phase ?? null);
     return {
       ...!Utils.isNullOrUndefined(sampledValueTemplate.unit) && { unit: sampledValueTemplate.unit },
       ...!Utils.isNullOrUndefined(sampledValueContext) && { context: sampledValueContext },