Fix energy calculation in metervalues
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 5 Oct 2021 08:12:30 +0000 (10:12 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 5 Oct 2021 08:12:30 +0000 (10:12 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/ocpp/1.6/OCPP16RequestService.ts

index a41a9731b7099a6be9ff135f135fcf6ba43f40ed..4daf9628b905aa08ceecffa78b8cc6abf3e35fd2 100644 (file)
@@ -309,26 +309,25 @@ export default class OCPP16RequestService extends OCPPRequestService {
       if (energySampledValueTemplate) {
         OCPP16ServiceUtils.checkMeasurandPowerDivider(this.chargingStation, energySampledValueTemplate.measurand);
         const unitDivider = energySampledValueTemplate?.unit === MeterValueUnit.KILO_WATT_HOUR ? 1000 : 1;
-        const energyMeasurandValue = energySampledValueTemplate.value
+        const maxEnergyRounded = Utils.roundTo(((this.chargingStation.stationInfo.maxPower / this.chargingStation.stationInfo.powerDivider) * interval) / (3600 * 1000), 2);
+        const energyValueRounded = energySampledValueTemplate.value
           // Cumulate the fluctuated value around the static one
           ? Utils.getRandomFloatFluctuatedRounded(parseInt(energySampledValueTemplate.value), energySampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT)
-          : Utils.getRandomInteger(this.chargingStation.stationInfo.maxPower / (this.chargingStation.stationInfo.powerDivider * 3600000) * interval);
+          : Utils.getRandomFloatRounded(maxEnergyRounded);
         // Persist previous value on connector
         if (connector && !Utils.isNullOrUndefined(connector.energyActiveImportRegisterValue) && connector.energyActiveImportRegisterValue >= 0 &&
             !Utils.isNullOrUndefined(connector.transactionEnergyActiveImportRegisterValue) && connector.transactionEnergyActiveImportRegisterValue >= 0) {
-          connector.energyActiveImportRegisterValue += energyMeasurandValue;
-          connector.transactionEnergyActiveImportRegisterValue += energyMeasurandValue;
+          connector.energyActiveImportRegisterValue += energyValueRounded;
+          connector.transactionEnergyActiveImportRegisterValue += energyValueRounded;
         } else {
           connector.energyActiveImportRegisterValue = 0;
           connector.transactionEnergyActiveImportRegisterValue = 0;
         }
         meterValue.sampledValue.push(OCPP16ServiceUtils.buildSampledValue(energySampledValueTemplate,
-          Utils.roundTo(this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) / unitDivider, 4)));
+          Utils.roundTo(this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) / unitDivider, 2)));
         const sampledValuesIndex = meterValue.sampledValue.length - 1;
-        const maxEnergy = Math.round(this.chargingStation.stationInfo.maxPower * 3600 / (this.chargingStation.stationInfo.powerDivider * interval));
-        const maxEnergyRounded = Utils.roundTo(maxEnergy / unitDivider, 4);
-        if (Utils.convertToFloat(meterValue.sampledValue[sampledValuesIndex].value) > maxEnergyRounded || debug) {
-          logger.error(`${this.chargingStation.logPrefix()} MeterValues measurand ${meterValue.sampledValue[sampledValuesIndex].measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${meterValue.sampledValue[sampledValuesIndex].value}/${maxEnergyRounded}`);
+        if (energyValueRounded > maxEnergyRounded || debug) {
+          logger.error(`${this.chargingStation.logPrefix()} MeterValues measurand ${meterValue.sampledValue[sampledValuesIndex].measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${energyValueRounded}/${maxEnergyRounded}, duration: ${Utils.roundTo(interval / (3600 * 1000), 4)}h`);
         }
       }
       const payload: MeterValuesRequest = {