From: Jérôme Benoit Date: Sat, 12 Jun 2021 16:53:31 +0000 (+0200) Subject: Fix power unit in kW handling in charging station template X-Git-Tag: v1.0.15~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=510f0fa5461cada6ca7c0e2aea525674fb00f079;p=e-mobility-charging-stations-simulator.git Fix power unit in kW handling in charging station template Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 50323127..ac704ea8 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1,6 +1,6 @@ import { BootNotificationResponse, RegistrationStatus } from '../types/ocpp/Responses'; import ChargingStationConfiguration, { ConfigurationKey } from '../types/ChargingStationConfiguration'; -import ChargingStationTemplate, { CurrentOutType, VoltageOut } from '../types/ChargingStationTemplate'; +import ChargingStationTemplate, { CurrentOutType, PowerUnits, VoltageOut } from '../types/ChargingStationTemplate'; import Connectors, { Connector } from '../types/Connectors'; import { PerformanceObserver, performance } from 'perf_hooks'; import Requests, { AvailabilityType, BootNotificationRequest, IncomingRequest, IncomingRequestCommand } from '../types/ocpp/Requests'; @@ -395,12 +395,18 @@ export default class ChargingStation { } catch (error) { FileUtils.handleFileException(this.logPrefix(), 'Template', this.stationTemplateFile, error); } - const stationInfo: ChargingStationInfo = stationTemplateFromFile || {} as ChargingStationInfo; + const stationInfo: ChargingStationInfo = stationTemplateFromFile ?? {} as ChargingStationInfo; if (!Utils.isEmptyArray(stationTemplateFromFile.power)) { stationTemplateFromFile.power = stationTemplateFromFile.power as number[]; - stationInfo.maxPower = stationTemplateFromFile.power[Math.floor(Math.random() * stationTemplateFromFile.power.length)]; + const powerArrayRandomIndex = Math.floor(Math.random() * stationTemplateFromFile.power.length); + stationInfo.maxPower = stationTemplateFromFile.powerUnit === PowerUnits.KILO_WATT + ? stationTemplateFromFile.power[powerArrayRandomIndex] * 1000 + : stationTemplateFromFile.power[powerArrayRandomIndex]; } else { - stationInfo.maxPower = stationTemplateFromFile.power as number; + stationTemplateFromFile.power = stationTemplateFromFile.power as number; + stationInfo.maxPower = stationTemplateFromFile.powerUnit === PowerUnits.KILO_WATT + ? (stationTemplateFromFile.power) * 1000 + : stationTemplateFromFile.power; } stationInfo.chargingStationId = this.getChargingStationId(stationTemplateFromFile); stationInfo.resetTime = stationTemplateFromFile.resetTime ? stationTemplateFromFile.resetTime * 1000 : Constants.CHARGING_STATION_DEFAULT_RESET_TIME; diff --git a/src/charging-station/OcppError.ts b/src/charging-station/OcppError.ts index ae86c0a1..6780b60b 100644 --- a/src/charging-station/OcppError.ts +++ b/src/charging-station/OcppError.ts @@ -7,9 +7,9 @@ export default class OCPPError extends Error { constructor(code: string, message: string, details?: any) { super(message); - this.code = code || ErrorType.GENERIC_ERROR; - this.message = message || ''; - this.details = details || {}; + this.code = code ?? ErrorType.GENERIC_ERROR; + this.message = message ?? ''; + this.details = details ?? {}; Object.setPrototypeOf(this, OCPPError.prototype); // For instanceof diff --git a/src/types/ChargingStationInfo.ts b/src/types/ChargingStationInfo.ts index 033dcb51..732bf00a 100644 --- a/src/types/ChargingStationInfo.ts +++ b/src/types/ChargingStationInfo.ts @@ -3,6 +3,6 @@ import ChargingStationTemplate from './ChargingStationTemplate'; export default interface ChargingStationInfo extends ChargingStationTemplate { chargingStationId?: string; chargeBoxSerialNumber?: string; - maxPower?: number; + maxPower?: number; // Always in Watt powerDivider?: number; }