From: Jérôme Benoit Date: Wed, 20 Mar 2024 08:53:39 +0000 (+0000) Subject: Merge pull request #1016 from SAP/dependabot/npm_and_yarn/types/node-20.11.30 X-Git-Tag: v1.3.1~26 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4ceb893765426217c05ecea31db3977ac324aaae;hp=ff4d9e2f49870474a2a05ab6bf8472cf2bd5cbf6;p=e-mobility-charging-stations-simulator.git Merge pull request #1016 from SAP/dependabot/npm_and_yarn/types/node-20.11.30 build(deps-dev): bump @types/node from 20.11.29 to 20.11.30 --- diff --git a/bundle.js b/bundle.js index 2d4bc863..4443f985 100644 --- a/bundle.js +++ b/bundle.js @@ -22,6 +22,7 @@ await build({ 'basic-ftp', 'chalk', 'date-fns', + 'date-fns/*', 'http-status-codes', 'logform', 'mnemonist', diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 545e8889..34ac90f3 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -64,7 +64,6 @@ export class Bootstrap extends EventEmitter { private storage?: Storage private readonly templateStatistics: Map private readonly version: string = version - private initializedCounters: boolean private started: boolean private starting: boolean private stopping: boolean @@ -82,12 +81,14 @@ export class Bootstrap extends EventEmitter { this.starting = false this.stopping = false this.uiServerStarted = false + this.templateStatistics = new Map() this.uiServer = UIServerFactory.getUIServerImplementation( Configuration.getConfigurationSection(ConfigurationSection.uiServer) ) - this.templateStatistics = new Map() - this.initializedCounters = false this.initializeCounters() + this.initializeWorkerImplementation( + Configuration.getConfigurationSection(ConfigurationSection.worker) + ) Configuration.configurationChangeCallback = async () => { if (isMainThread) { await Bootstrap.getInstance().restart() @@ -174,12 +175,12 @@ export class Bootstrap extends EventEmitter { ) } ) - this.initializeCounters() - const workerConfiguration = Configuration.getConfigurationSection( - ConfigurationSection.worker - ) - this.initializeWorkerImplementation(workerConfiguration) - await this.workerImplementation?.start() + // eslint-disable-next-line @typescript-eslint/unbound-method + if (isAsyncFunction(this.workerImplementation?.start)) { + await this.workerImplementation.start() + } else { + (this.workerImplementation?.start as () => void)() + } const performanceStorageConfiguration = Configuration.getConfigurationSection( ConfigurationSection.performanceStorage @@ -220,6 +221,9 @@ export class Bootstrap extends EventEmitter { ) } } + const workerConfiguration = Configuration.getConfigurationSection( + ConfigurationSection.worker + ) console.info( chalk.green( `Charging stations simulator ${ @@ -269,10 +273,8 @@ export class Bootstrap extends EventEmitter { console.error(chalk.red('Error while waiting for charging stations to stop: '), error) } await this.workerImplementation?.stop() - delete this.workerImplementation this.removeAllListeners() this.uiServer.clearCaches() - this.initializedCounters = false await this.storage?.close() delete this.storage this.started = false @@ -295,6 +297,11 @@ export class Bootstrap extends EventEmitter { this.uiServer.stop() this.uiServerStarted = false } + this.initializeCounters() + // FIXME: initialize worker implementation only if the worker section has changed + this.initializeWorkerImplementation( + Configuration.getConfigurationSection(ConfigurationSection.worker) + ) await this.start() } @@ -356,7 +363,9 @@ export class Bootstrap extends EventEmitter { elementsPerWorker, poolOptions: { messageHandler: this.messageHandler.bind(this) as MessageHandler, - workerOptions: { resourceLimits: workerConfiguration.resourceLimits } + ...(workerConfiguration.resourceLimits != null && { + workerOptions: { resourceLimits: workerConfiguration.resourceLimits } + }) } } ) @@ -487,47 +496,44 @@ export class Bootstrap extends EventEmitter { } private initializeCounters (): void { - if (!this.initializedCounters) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const stationTemplateUrls = Configuration.getStationTemplateUrls()! - if (isNotEmptyArray(stationTemplateUrls)) { - for (const stationTemplateUrl of stationTemplateUrls) { - const templateName = buildTemplateName(stationTemplateUrl.file) - this.templateStatistics.set(templateName, { - configured: stationTemplateUrl.numberOfStations, - added: 0, - started: 0, - indexes: new Set() - }) - this.uiServer.chargingStationTemplates.add(templateName) - } - if (this.templateStatistics.size !== stationTemplateUrls.length) { - console.error( - chalk.red( - "'stationTemplateUrls' contains duplicate entries, please check your configuration" - ) - ) - exit(exitCodes.duplicateChargingStationTemplateUrls) - } - } else { - console.error( - chalk.red("'stationTemplateUrls' not defined or empty, please check your configuration") - ) - exit(exitCodes.missingChargingStationsConfiguration) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const stationTemplateUrls = Configuration.getStationTemplateUrls()! + if (isNotEmptyArray(stationTemplateUrls)) { + for (const stationTemplateUrl of stationTemplateUrls) { + const templateName = buildTemplateName(stationTemplateUrl.file) + this.templateStatistics.set(templateName, { + configured: stationTemplateUrl.numberOfStations, + added: 0, + started: 0, + indexes: new Set() + }) + this.uiServer.chargingStationTemplates.add(templateName) } - if ( - this.numberOfConfiguredChargingStations === 0 && - Configuration.getConfigurationSection(ConfigurationSection.uiServer) - .enabled !== true - ) { + if (this.templateStatistics.size !== stationTemplateUrls.length) { console.error( chalk.red( - "'stationTemplateUrls' has no charging station enabled and UI server is disabled, please check your configuration" + "'stationTemplateUrls' contains duplicate entries, please check your configuration" ) ) - exit(exitCodes.noChargingStationTemplates) + exit(exitCodes.duplicateChargingStationTemplateUrls) } - this.initializedCounters = true + } else { + console.error( + chalk.red("'stationTemplateUrls' not defined or empty, please check your configuration") + ) + exit(exitCodes.missingChargingStationsConfiguration) + } + if ( + this.numberOfConfiguredChargingStations === 0 && + Configuration.getConfigurationSection(ConfigurationSection.uiServer) + .enabled !== true + ) { + console.error( + chalk.red( + "'stationTemplateUrls' has no charging station enabled and UI server is disabled, please check your configuration" + ) + ) + exit(exitCodes.noChargingStationTemplates) } } diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 29aedcba..e41ea5ec 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1191,15 +1191,6 @@ export class ChargingStation extends EventEmitter { } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'` ) } - stationInfo.firmwareUpgrade = mergeDeepRight( - { - versionUpgrade: { - step: 1 - }, - reset: true - }, - stationTemplate.firmwareUpgrade ?? {} - ) if (stationTemplate.resetTime != null) { stationInfo.resetTime = secondsToMilliseconds(stationTemplate.resetTime) } @@ -1244,7 +1235,7 @@ export class ChargingStation extends EventEmitter { stationInfoFromFile.templateHash === stationInfoFromTemplate.templateHash ) { return setChargingStationOptions( - { ...Constants.DEFAULT_STATION_INFO, ...stationInfoFromFile }, + mergeDeepRight(Constants.DEFAULT_STATION_INFO, stationInfoFromFile), options ) } @@ -1255,7 +1246,7 @@ export class ChargingStation extends EventEmitter { stationInfoFromTemplate ) return setChargingStationOptions( - { ...Constants.DEFAULT_STATION_INFO, ...stationInfoFromTemplate }, + mergeDeepRight(Constants.DEFAULT_STATION_INFO, stationInfoFromTemplate), options ) } diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index 916cf499..735a0b39 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -974,7 +974,7 @@ export const prepareChargingProfileKind = ( if (connectorStatus?.transactionStarted === true) { chargingProfile.chargingSchedule.startSchedule = connectorStatus.transactionStart } - // FIXME: Handle relative charging profile duration + // FIXME: handle relative charging profile duration break } return true diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index e1fd11c7..9356785d 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -513,22 +513,22 @@ export class Configuration { private static warnDeprecatedConfigurationKey ( key: string, - sectionName?: string, + configurationSection?: ConfigurationSection, logMsgToAppend = '' ): void { if ( - sectionName != null && - Configuration.getConfigurationData()?.[sectionName as keyof ConfigurationData] != null && + configurationSection != null && + Configuration.getConfigurationData()?.[configurationSection as keyof ConfigurationData] != + null && ( - Configuration.getConfigurationData()?.[sectionName as keyof ConfigurationData] as Record< - string, - unknown - > + Configuration.getConfigurationData()?.[ + configurationSection as keyof ConfigurationData + ] as Record )[key] != null ) { console.error( `${chalk.green(logPrefix())} ${chalk.red( - `Deprecated configuration key '${key}' usage in section '${sectionName}'${ + `Deprecated configuration key '${key}' usage in section '${configurationSection}'${ logMsgToAppend.trim().length > 0 ? `. ${logMsgToAppend}` : '' }` )}` diff --git a/src/utils/Constants.ts b/src/utils/Constants.ts index a5e46f60..3746c4fd 100644 --- a/src/utils/Constants.ts +++ b/src/utils/Constants.ts @@ -34,6 +34,12 @@ export class Constants { useConnectorId0: true, ocppVersion: OCPPVersion.VERSION_16, firmwareVersionPattern: Constants.SEMVER_PATTERN, + firmwareUpgrade: { + versionUpgrade: { + step: 1 + }, + reset: true + }, ocppPersistentConfiguration: true, stationInfoPersistentConfiguration: true, automaticTransactionGeneratorPersistentConfiguration: true, diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 9a10e6f2..907d0bd0 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -39,7 +39,7 @@ export abstract class WorkerAbstract { /** * Starts the worker pool/set. */ - public abstract start (): Promise + public abstract start (): void | Promise /** * Stops the worker pool/set. */ diff --git a/src/worker/WorkerConstants.ts b/src/worker/WorkerConstants.ts index 28e9927d..ded5c891 100644 --- a/src/worker/WorkerConstants.ts +++ b/src/worker/WorkerConstants.ts @@ -22,6 +22,7 @@ export const DEFAULT_WORKER_OPTIONS: WorkerOptions = Object.freeze({ poolMaxSize: DEFAULT_POOL_MAX_SIZE, elementsPerWorker: DEFAULT_ELEMENTS_PER_WORKER, poolOptions: { + startWorkers: false, enableEvents: true, restartWorkerOnError: true, errorHandler: defaultErrorHandler, diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index 0bdb2619..20432973 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -42,8 +42,8 @@ export class WorkerDynamicPool extends WorkerAbstract { } /** @inheritDoc */ - public async start (): Promise { - // This is intentional + public start (): void { + this.pool.start() } /** @inheritDoc */ diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 833c939f..67da00d8 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,5 +1,7 @@ import { isMainThread } from 'node:worker_threads' +import { mergeDeepRight } from 'rambda' + import type { WorkerAbstract } from './WorkerAbstract.js' import { DEFAULT_WORKER_OPTIONS } from './WorkerConstants.js' import { WorkerDynamicPool } from './WorkerDynamicPool.js' @@ -21,7 +23,7 @@ export class WorkerFactory { if (!isMainThread) { throw new Error('Cannot get a worker implementation outside the main thread') } - workerOptions = { ...DEFAULT_WORKER_OPTIONS, ...workerOptions } + workerOptions = mergeDeepRight(DEFAULT_WORKER_OPTIONS, workerOptions ?? {}) let workerImplementation: WorkerAbstract switch (workerProcessType) { case WorkerProcessType.workerSet: diff --git a/src/worker/WorkerFixedPool.ts b/src/worker/WorkerFixedPool.ts index 4eafa1d5..22290666 100644 --- a/src/worker/WorkerFixedPool.ts +++ b/src/worker/WorkerFixedPool.ts @@ -41,8 +41,8 @@ export class WorkerFixedPool extends WorkerAbstract { } /** @inheritDoc */ - public async start (): Promise { - // This is intentional + public start (): void { + this.pool.start() } /** @inheritDoc */ diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index 1ad551ef..d0f80b6e 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -99,7 +99,6 @@ export class WorkerSet extends WorkerAbstract { this.emitter?.emit(WorkerSetEvents.stopped, this.info) this.started = false this.emitter?.emitDestroy() - this.emitter?.removeAllListeners() } /** @inheritDoc */