using Utils.truncto() instead of toFixed()
MODIFY
src/server/ocpp/utils/OCPPUtils.ts
src/server/ocpp/utils/OCPPUtils.ts
@@ -218,9 +218,9 @@ export default class OCPPUtils {
if (pricedConsumption.cumulatedAmount) {
consumption.cumulatedAmount = pricedConsumption.cumulatedAmount;
} else {
- consumption.cumulatedAmount = Utils.truncTo(transaction.currentCumulatedPrice + consumption.amount, 6);
+ consumption.cumulatedAmount = Utils.truncTo(transaction.currentCumulatedPrice + consumption.amount, 3);
}
- transaction.currentCumulatedPrice = parseFloat(consumption.cumulatedAmount.toFixed(3));
+ transaction.currentCumulatedPrice = consumption.cumulatedAmount;
}
break;
// Stop Transaction
@@ -236,14 +236,14 @@ export default class OCPPUtils {
if (pricedConsumption.cumulatedAmount) {
consumption.cumulatedAmount = pricedConsumption.cumulatedAmount;
} else {
- consumption.cumulatedAmount = Utils.truncTo(transaction.currentCumulatedPrice + consumption.amount, 6);
+ consumption.cumulatedAmount = Utils.truncTo(transaction.currentCumulatedPrice + consumption.amount, 3);
}
- transaction.currentCumulatedPrice = parseFloat(consumption.cumulatedAmount.toFixed(3));
+ transaction.currentCumulatedPrice = consumption.cumulatedAmount;
// Update Transaction
if (!transaction.stop) {
transaction.stop = {} as TransactionStop;
}
- transaction.stop.price = Utils.truncTo(transaction.currentCumulatedPrice, 6);
+ transaction.stop.price = transaction.currentCumulatedPrice;
transaction.stop.roundedPrice = Utils.truncTo(transaction.currentCumulatedPrice, 2);
transaction.stop.priceUnit = pricedConsumption.currencyCode;
transaction.stop.pricingSource = pricedConsumption.pricingSource;

MODIFY
test/api/OCPPCommonTests.ts
test/api/OCPPCommonTests.ts
@@ -219,7 +219,6 @@ export default class OCPPCommonTests {
this.energyActiveImportEndMeterValue = this.energyActiveImportStartMeterValue + this.transactionTotalConsumptionWh;
this.transactionTotalInactivitySecs = this.energyActiveImportMeterValues.reduce(
(sum, meterValue) => (meterValue === 0 ? sum + this.meterValueIntervalSecs : sum), 0);
- this.totalPrice = this.pricekWh * (this.transactionTotalConsumptionWh / 1000);
// Tags
this.validTag = faker.random.alphaNumeric(20).toString();
this.invalidTag = faker.random.alphaNumeric(21).toString();
@@ -468,8 +467,13 @@ export default class OCPPCommonTests {
// ------------------------------------------------------------------
// Send Meter Values (except the last one which is used in Stop Transaction)
// ------------------------------------------------------------------
+ let currentCumulatedPrice = 0;
for (let index = 0; index <= this.energyActiveImportMeterValues.length - 2; index++) {
// Set new meter value
+ currentCumulatedPrice = Utils.truncTo(((this.energyActiveImportMeterValues[index] / 1000) * this.pricekWh), 3) + Utils.truncTo(currentCumulatedPrice, 3);
+ if (index === this.energyActiveImportMeterValues.length - 2) {
+ this.totalPrice = currentCumulatedPrice;
+ }
currentEnergyActiveImportMeterValue += this.energyActiveImportMeterValues[index];
// Add time
this.transactionCurrentTime = moment(this.transactionCurrentTime).add(this.meterValueIntervalSecs, 's').toDate();
@@ -505,7 +509,7 @@ export default class OCPPCommonTests {
currentTotalConsumptionWh: (currentEnergyActiveImportMeterValue - this.energyActiveImportStartMeterValue),
currentTotalDurationSecs: this.meterValueIntervalSecs * (index + 1),
currentTotalInactivitySecs: this.totalInactivities[index],
- currentCumulatedPrice: ((currentEnergyActiveImportMeterValue - this.energyActiveImportStartMeterValue) / 1000) * this.pricekWh,
+ currentCumulatedPrice: Utils.truncTo(currentCumulatedPrice, 3),
currentInactivityStatus: Utils.getInactivityStatusLevel(this.chargingStationContext.getChargingStation(),
this.newTransaction.connectorId, this.totalInactivities[index]),
});
@@ -517,7 +521,7 @@ export default class OCPPCommonTests {
currentTotalConsumptionWh: (currentEnergyActiveImportMeterValue - this.energyActiveImportStartMeterValue),
currentTotalDurationSecs: this.meterValueIntervalSecs * (index + 1),
currentTotalInactivitySecs: this.totalInactivities[index],
- currentCumulatedPrice: ((currentEnergyActiveImportMeterValue - this.energyActiveImportStartMeterValue) / 1000) * this.pricekWh,
+ currentCumulatedPrice: Utils.truncTo(currentCumulatedPrice, 3),
currentInactivityStatus: Utils.getInactivityStatusLevel(this.chargingStationContext.getChargingStation(),
this.newTransaction.connectorId, this.totalInactivities[index]),
});

using Utils.truncto() instead of toFixed()