+ messageHandler: this.messageHandler.bind(this) as MessageHandler<Worker>,
+ workerOptions: { resourceLimits: workerConfiguration.resourceLimits }
+ }
+ }
+ )
+ }
+
+ 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)