From 510f0fa5461cada6ca7c0e2aea525674fb00f079 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 12 Jun 2021 18:53:31 +0200 Subject: [PATCH] Fix power unit in kW handling in charging station template MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 14 ++++++++++---- src/charging-station/OcppError.ts | 6 +++--- src/types/ChargingStationInfo.ts | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) 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; } -- 2.34.1