X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStationUtils.ts;h=efa8921c645707a293ecbd7a2b60dcb3de1d84c4;hb=910c3f0cae3d7e9a9fb82608c3a26df37ff19b29;hp=ed12aa4acf697bf01c351e9d4cc0857c77c763e4;hpb=43ee4373fc8c85473b7d5c192da79f6d53d6c8ee;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index ed12aa4a..efa8921c 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -1,34 +1,37 @@ import crypto from 'node:crypto'; -import path from 'path'; -import { fileURLToPath } from 'url'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import moment from 'moment'; -import type ChargingStation from './ChargingStation'; -import BaseError from '../exception/BaseError'; -import type { ChargingStationInfo } from '../types/ChargingStationInfo'; +import type { ChargingStation } from './internal'; +import { BaseError } from '../exception'; import { AmpereUnits, + type BootNotificationRequest, + BootReasonEnumType, + type ChargingProfile, + ChargingProfileKindType, + ChargingRateUnitType, + type ChargingSchedulePeriod, + type ChargingStationInfo, type ChargingStationTemplate, CurrentType, + type OCPP16BootNotificationRequest, + type OCPP20BootNotificationRequest, + OCPPVersion, + RecurrencyKindType, Voltage, -} from '../types/ChargingStationTemplate'; -import { ChargingProfileKindType, RecurrencyKindType } from '../types/ocpp/1.6/ChargingProfile'; -import type { OCPP16BootNotificationRequest } from '../types/ocpp/1.6/Requests'; -import { BootReasonEnumType, type OCPP20BootNotificationRequest } from '../types/ocpp/2.0/Requests'; +} from '../types'; import { - type ChargingProfile, - ChargingRateUnitType, - type ChargingSchedulePeriod, -} from '../types/ocpp/ChargingProfile'; -import { OCPPVersion } from '../types/ocpp/OCPPVersion'; -import type { BootNotificationRequest } from '../types/ocpp/Requests'; -import { WorkerProcessType } from '../types/Worker'; -import Configuration from '../utils/Configuration'; -import Constants from '../utils/Constants'; -import { ACElectricUtils, DCElectricUtils } from '../utils/ElectricUtils'; -import logger from '../utils/Logger'; -import Utils from '../utils/Utils'; + ACElectricUtils, + Configuration, + Constants, + DCElectricUtils, + Utils, + logger, +} from '../utils'; +import { WorkerProcessType } from '../worker'; const moduleName = 'ChargingStationUtils'; @@ -43,15 +46,13 @@ export class ChargingStationUtils { ): string { // In case of multiple instances: add instance index to charging station id const instanceIndex = process.env.CF_INSTANCE_INDEX ?? 0; - const idSuffix = stationTemplate.nameSuffix ?? ''; - const idStr = '000000000' + index.toString(); + const idSuffix = stationTemplate?.nameSuffix ?? ''; + const idStr = `000000000${index.toString()}`; return stationTemplate?.fixedName ? stationTemplate.baseName - : stationTemplate.baseName + - '-' + - instanceIndex.toString() + - idStr.substring(idStr.length - 4) + - idSuffix; + : `${stationTemplate.baseName}-${instanceIndex.toString()}${idStr.substring( + idStr.length - 4 + )}${idSuffix}`; } public static getHashId(index: number, stationTemplate: ChargingStationTemplate): string { @@ -80,8 +81,10 @@ export class ChargingStationUtils { return crypto .createHash(Constants.DEFAULT_HASH_ALGORITHM) .update( - JSON.stringify(chargingStationInfo) + - ChargingStationUtils.getChargingStationId(index, stationTemplate) + `${JSON.stringify(chargingStationInfo)}${ChargingStationUtils.getChargingStationId( + index, + stationTemplate + )}` ) .digest('hex'); } @@ -112,7 +115,7 @@ export class ChargingStationUtils { public static getConfiguredNumberOfConnectors(stationTemplate: ChargingStationTemplate): number { let configuredMaxConnectors: number; - if (Utils.isEmptyArray(stationTemplate.numberOfConnectors) === false) { + if (Utils.isNotEmptyArray(stationTemplate.numberOfConnectors) === true) { const numberOfConnectors = stationTemplate.numberOfConnectors as number[]; configuredMaxConnectors = numberOfConnectors[Math.floor(Utils.secureRandom() * numberOfConnectors.length)]; @@ -191,13 +194,13 @@ export class ChargingStationUtils { } public static workerPoolInUse(): boolean { - return [WorkerProcessType.DYNAMIC_POOL, WorkerProcessType.STATIC_POOL].includes( + return [WorkerProcessType.dynamicPool, WorkerProcessType.staticPool].includes( Configuration.getWorker().processType ); } public static workerDynamicPoolInUse(): boolean { - return Configuration.getWorker().processType === WorkerProcessType.DYNAMIC_POOL; + return Configuration.getWorker().processType === WorkerProcessType.dynamicPool; } public static warnDeprecatedTemplateKey( @@ -210,7 +213,7 @@ export class ChargingStationUtils { if (!Utils.isUndefined(template[key])) { logger.warn( `${logPrefix} Deprecated template key '${key}' usage in file '${templateFile}'${ - logMsgToAppend && '. ' + logMsgToAppend + Utils.isNotEmptyString(logMsgToAppend) && `. ${logMsgToAppend}` }` ); } @@ -268,15 +271,19 @@ export class ChargingStationUtils { upperCase: params.randomSerialNumberUpperCase, }) : ''; - stationInfo.chargePointSerialNumber = - stationTemplate?.chargePointSerialNumberPrefix && - stationTemplate.chargePointSerialNumberPrefix + serialNumberSuffix; - stationInfo.chargeBoxSerialNumber = - stationTemplate?.chargeBoxSerialNumberPrefix && - stationTemplate.chargeBoxSerialNumberPrefix + serialNumberSuffix; - stationInfo.meterSerialNumber = - stationTemplate?.meterSerialNumberPrefix && - stationTemplate.meterSerialNumberPrefix + serialNumberSuffix; + stationInfo.chargePointSerialNumber = Utils.isNotEmptyString( + stationTemplate?.chargePointSerialNumberPrefix + ) + ? `${stationTemplate.chargePointSerialNumberPrefix}${serialNumberSuffix}` + : undefined; + stationInfo.chargeBoxSerialNumber = Utils.isNotEmptyString( + stationTemplate?.chargeBoxSerialNumberPrefix + ) + ? `${stationTemplate.chargeBoxSerialNumberPrefix}${serialNumberSuffix}` + : undefined; + stationInfo.meterSerialNumber = Utils.isNotEmptyString(stationTemplate?.meterSerialNumberPrefix) + ? `${stationTemplate.meterSerialNumberPrefix}${serialNumberSuffix}` + : undefined; } public static propagateSerialNumber( @@ -325,23 +332,23 @@ export class ChargingStationUtils { // Get charging profiles for connector and sort by stack level chargingProfiles = chargingStation .getConnectorStatus(connectorId) - .chargingProfiles.sort((a, b) => b.stackLevel - a.stackLevel); + ?.chargingProfiles?.sort((a, b) => b.stackLevel - a.stackLevel); // Get profiles on connector 0 - if (chargingStation.getConnectorStatus(0).chargingProfiles) { + if (chargingStation.getConnectorStatus(0)?.chargingProfiles) { chargingProfiles.push( ...chargingStation .getConnectorStatus(0) .chargingProfiles.sort((a, b) => b.stackLevel - a.stackLevel) ); } - if (!Utils.isEmptyArray(chargingProfiles)) { + if (Utils.isNotEmptyArray(chargingProfiles)) { const result = ChargingStationUtils.getLimitFromChargingProfiles( chargingProfiles, chargingStation.logPrefix() ); if (!Utils.isNullOrUndefined(result)) { - limit = result.limit; - matchingChargingProfile = result.matchingChargingProfile; + limit = result?.limit; + matchingChargingProfile = result?.matchingChargingProfile; switch (chargingStation.getCurrentOutType()) { case CurrentType.AC: limit =