From: Jérôme Benoit Date: Mon, 12 Apr 2021 07:26:21 +0000 (+0200) Subject: Improve electric utils code. X-Git-Tag: v1.0.1-0~55 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=cb31c873ed2fd050266247f70bc31178610dffcb;p=e-mobility-charging-stations-simulator.git Improve electric utils code. Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 411c4522..15cac9b5 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -322,16 +322,16 @@ export default class ChargingStation { public addToMessageQueue(message: string): void { let dups = false; - // Handle dups in buffer + // Handle dups in message queue for (const bufferedMessage of this.messageQueue) { - // Same message + // Message already in the queue if (message === bufferedMessage) { dups = true; break; } } if (!dups) { - // Buffer message + // Queue message this.messageQueue.push(message); } } diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 4e828453..e9f7ee6d 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -2,8 +2,8 @@ import { AuthorizeRequest, OCPP16AuthorizeResponse, OCPP16StartTransactionRespon import { HeartbeatRequest, OCPP16BootNotificationRequest, OCPP16IncomingRequestCommand, OCPP16RequestCommand, StatusNotificationRequest } from '../../../types/ocpp/1.6/Requests'; import { MeterValue, MeterValueLocation, MeterValuePhase, MeterValueUnit, MeterValuesRequest, OCPP16MeterValueMeasurand, OCPP16SampledValue } from '../../../types/ocpp/1.6/MeterValues'; +import { ACElectricUtils } from '../../../utils/ElectricUtils'; import Constants from '../../../utils/Constants'; -import ElectricUtils from '../../../utils/ElectricUtils'; import MeasurandValues from '../../../types/MeasurandValues'; import { MessageType } from '../../../types/ocpp/MessageType'; import { OCPP16BootNotificationResponse } from '../../../types/ocpp/1.6/Responses'; @@ -226,7 +226,7 @@ export default class OCPP16RequestService extends OCPPRequestService { let maxAmperage: number; switch (self.chargingStation.getPowerOutType()) { case PowerOutType.AC: - maxAmperage = ElectricUtils.ampPerPhaseFromPower(self.chargingStation.getNumberOfPhases(), self.chargingStation.stationInfo.maxPower / self.chargingStation.stationInfo.powerDivider, self.chargingStation.getVoltageOut()); + maxAmperage = ACElectricUtils.amperagePerPhaseFromPower(self.chargingStation.getNumberOfPhases(), self.chargingStation.stationInfo.maxPower / self.chargingStation.stationInfo.powerDivider, self.chargingStation.getVoltageOut()); if (Utils.isUndefined(meterValuesTemplate[index].value)) { currentMeasurandValues.L1 = Utils.getRandomFloatRounded(maxAmperage); currentMeasurandValues.L2 = 0; @@ -239,7 +239,7 @@ export default class OCPP16RequestService extends OCPPRequestService { } break; case PowerOutType.DC: - maxAmperage = ElectricUtils.ampTotalFromPower(self.chargingStation.stationInfo.maxPower / self.chargingStation.stationInfo.powerDivider, self.chargingStation.getVoltageOut()); + maxAmperage = ACElectricUtils.amperageTotalFromPower(self.chargingStation.stationInfo.maxPower / self.chargingStation.stationInfo.powerDivider, self.chargingStation.getVoltageOut()); if (Utils.isUndefined(meterValuesTemplate[index].value)) { currentMeasurandValues.allPhases = Utils.getRandomFloatRounded(maxAmperage); } diff --git a/src/utils/ElectricUtils.ts b/src/utils/ElectricUtils.ts index dba823ab..5adf4bc7 100644 --- a/src/utils/ElectricUtils.ts +++ b/src/utils/ElectricUtils.ts @@ -1,10 +1,12 @@ /** * Rationale: https://wiki.piment-noir.org/doku.php/en:cs:modelling_multi-phased_electrical_system_interconnexion + */ + +/** * Targeted to AC related values calculation. - * To use for DC, always consider cosPhi = 1 and do not use per phase helpers */ -export default class ElectricUtils { - static ampTotal(nbOfPhases: number, Iph: number): number { +export class ACElectricUtils { + static amperageTotal(nbOfPhases: number, Iph: number): number { return nbOfPhases * Iph; } @@ -17,23 +19,40 @@ export default class ElectricUtils { } static powerTotal(nbOfPhases: number, V: number, Iph: number, cosPhi = 1): number { - return nbOfPhases * ElectricUtils.powerPerPhase(V, Iph, cosPhi); + return nbOfPhases * ACElectricUtils.powerPerPhase(V, Iph, cosPhi); } - static ampTotalFromPower(P: number, V: number, cosPhi = 1): number { - const power = P / (V * cosPhi); + static amperageTotalFromPower(P: number, V: number, cosPhi = 1): number { + const amperage = P / (V * cosPhi); if (cosPhi === 1 && P % V === 0) { - return power; + return amperage; } - return Math.round(power); + return Math.round(amperage); } - static ampPerPhaseFromPower(nbOfPhases: number, P: number, V: number, cosPhi = 1): number { - const power = ElectricUtils.ampTotalFromPower(P, V, cosPhi); - const powerPerPhase = power / nbOfPhases; - if (power % nbOfPhases === 0) { - return powerPerPhase; + static amperagePerPhaseFromPower(nbOfPhases: number, P: number, V: number, cosPhi = 1): number { + const amperage = ACElectricUtils.amperageTotalFromPower(P, V, cosPhi); + const amperagePerPhase = amperage / nbOfPhases; + if (amperage % nbOfPhases === 0) { + return amperagePerPhase; } - return Math.round(powerPerPhase); + return Math.round(amperagePerPhase); + } +} + +/** + * Targeted to DC related values calculation. + */ +export class DCElectricUtils { + static power(V: number, I: number): number { + return V * I; + } + + static amperage(P: number, V: number): number { + const amperage = P / V; + if (P % V === 0) { + return amperage; + } + return Math.round(amperage); } }