+ }
+ )
+ }
+
+ private messageHandler (
+ msg: ChargingStationWorkerMessage<ChargingStationWorkerMessageData>
+ ): void {
+ // logger.debug(
+ // `${this.logPrefix()} ${moduleName}.messageHandler: Worker channel message received: ${JSON.stringify(
+ // msg,
+ // undefined,
+ // 2
+ // )}`
+ // )
+ try {
+ switch (msg.event) {
+ case ChargingStationWorkerMessageEvents.added:
+ this.emit(ChargingStationWorkerMessageEvents.added, msg.data)
+ break
+ case ChargingStationWorkerMessageEvents.started:
+ this.emit(ChargingStationWorkerMessageEvents.started, msg.data)
+ break
+ case ChargingStationWorkerMessageEvents.stopped:
+ this.emit(ChargingStationWorkerMessageEvents.stopped, msg.data)
+ break
+ case ChargingStationWorkerMessageEvents.updated:
+ this.emit(ChargingStationWorkerMessageEvents.updated, msg.data)
+ break
+ case ChargingStationWorkerMessageEvents.performanceStatistics:
+ this.emit(ChargingStationWorkerMessageEvents.performanceStatistics, msg.data)
+ break
+ case ChargingStationWorkerMessageEvents.addedWorkerElement:
+ this.emit(ChargingStationWorkerMessageEvents.addWorkerElement, msg.data)
+ break
+ case ChargingStationWorkerMessageEvents.workerElementError:
+ this.emit(ChargingStationWorkerMessageEvents.workerElementError, msg.data)
+ break
+ default:
+ throw new BaseError(
+ `Unknown charging station worker event: '${
+ msg.event
+ }' received with data: ${JSON.stringify(msg.data, undefined, 2)}`
+ )
+ }
+ } catch (error) {
+ logger.error(
+ `${this.logPrefix()} ${moduleName}.messageHandler: Error occurred while handling '${
+ msg.event
+ }' event:`,
+ error
+ )
+ }
+ }
+
+ private readonly workerEventAdded = (data: ChargingStationData): void => {
+ this.uiServer?.chargingStations.set(data.stationInfo.hashId, data)
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ ++this.chargingStationsByTemplate.get(data.stationInfo.templateName)!.added
+ logger.info(
+ `${this.logPrefix()} ${moduleName}.workerEventAdded: Charging station ${
+ data.stationInfo.chargingStationId
+ } (hashId: ${data.stationInfo.hashId}) added (${
+ this.numberOfAddedChargingStations
+ } added from ${this.numberOfConfiguredChargingStations} configured charging station(s))`
+ )
+ }
+
+ private readonly workerEventStarted = (data: ChargingStationData): void => {
+ this.uiServer?.chargingStations.set(data.stationInfo.hashId, data)
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ ++this.chargingStationsByTemplate.get(data.stationInfo.templateName)!.started
+ logger.info(
+ `${this.logPrefix()} ${moduleName}.workerEventStarted: Charging station ${
+ data.stationInfo.chargingStationId
+ } (hashId: ${data.stationInfo.hashId}) started (${
+ this.numberOfStartedChargingStations
+ } started from ${this.numberOfAddedChargingStations} added charging station(s))`
+ )
+ }
+
+ private readonly workerEventStopped = (data: ChargingStationData): void => {
+ this.uiServer?.chargingStations.set(data.stationInfo.hashId, data)
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ --this.chargingStationsByTemplate.get(data.stationInfo.templateName)!.started
+ logger.info(
+ `${this.logPrefix()} ${moduleName}.workerEventStopped: Charging station ${
+ data.stationInfo.chargingStationId
+ } (hashId: ${data.stationInfo.hashId}) stopped (${
+ this.numberOfStartedChargingStations
+ } started from ${this.numberOfAddedChargingStations} added charging station(s))`
+ )
+ }
+
+ private readonly workerEventUpdated = (data: ChargingStationData): void => {
+ this.uiServer?.chargingStations.set(data.stationInfo.hashId, data)
+ }
+
+ private readonly workerEventPerformanceStatistics = (data: Statistics): void => {
+ // eslint-disable-next-line @typescript-eslint/unbound-method
+ if (isAsyncFunction(this.storage?.storePerformanceStatistics)) {
+ (
+ this.storage.storePerformanceStatistics as (
+ performanceStatistics: Statistics
+ ) => Promise<void>
+ )(data).catch(Constants.EMPTY_FUNCTION)
+ } else {
+ (this.storage?.storePerformanceStatistics as (performanceStatistics: Statistics) => void)(
+ data
+ )
+ }
+ }
+
+ 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 = parse(stationTemplateUrl.file).name
+ this.chargingStationsByTemplate.set(templateName, {
+ configured: stationTemplateUrl.numberOfStations,
+ added: 0,
+ started: 0,
+ lastIndex: 0
+ })
+ this.uiServer?.chargingStationTemplates.add(templateName)
+ }
+ if (this.chargingStationsByTemplate.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)
+ }
+ if (
+ this.numberOfConfiguredChargingStations === 0 &&
+ Configuration.getConfigurationSection<UIServerConfiguration>(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)
+ }
+ this.initializedCounters = true
+ }
+ }
+
+ public async addChargingStation (
+ index: number,
+ stationTemplateFile: string,
+ options?: ChargingStationOptions
+ ): Promise<void> {
+ await this.workerImplementation?.addElement({
+ index,
+ templateFile: join(
+ dirname(fileURLToPath(import.meta.url)),
+ 'assets',
+ 'station-templates',
+ stationTemplateFile
+ ),
+ options
+ })
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.chargingStationsByTemplate.get(parse(stationTemplateFile).name)!.lastIndex = max(
+ index,
+ this.chargingStationsByTemplate.get(parse(stationTemplateFile).name)?.lastIndex ?? -Infinity
+ )
+ }
+
+ private gracefulShutdown (): void {
+ this.stop()
+ .then(() => {
+ console.info(chalk.green('Graceful shutdown'))
+ this.uiServer?.stop()
+ this.waitChargingStationsStopped()
+ .then(() => {
+ exit(exitCodes.succeeded)
+ })
+ .catch(() => {
+ exit(exitCodes.gracefulShutdownError)
+ })
+ })
+ .catch(error => {
+ console.error(chalk.red('Error while shutdowning charging stations simulator: '), error)
+ exit(exitCodes.gracefulShutdownError)
+ })