From 3ec1073715e995f84b1e2fbb147f85a6c7dc8264 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 11 Sep 2021 22:21:39 +0200 Subject: [PATCH] Cleanup configuration files watching code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Still buggy with worker threads Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 60 +++++++++++++------------ src/utils/Configuration.ts | 13 +++--- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 2f1bbc3e..2f2bc338 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -237,7 +237,7 @@ export default class ChargingStation { if (!Constants.SUPPORTED_MEASURANDS.includes(sampledValueTemplates[index]?.measurand ?? MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER)) { logger.warn(`${this.logPrefix()} Unsupported MeterValues measurand ${measurand} ${phase ? `on phase ${phase} ` : ''}in template on connectorId ${connectorId}`); } else if (phase && sampledValueTemplates[index]?.phase === phase && sampledValueTemplates[index]?.measurand === measurand - && this.getConfigurationKey(StandardParametersKey.MeterValuesSampledData).value.includes(measurand)) { + && this.getConfigurationKey(StandardParametersKey.MeterValuesSampledData).value.includes(measurand)) { return sampledValueTemplates[index]; } else if (!phase && !sampledValueTemplates[index].phase && sampledValueTemplates[index]?.measurand === measurand && this.getConfigurationKey(StandardParametersKey.MeterValuesSampledData).value.includes(measurand)) { @@ -866,7 +866,7 @@ export default class ChargingStation { } if (this.automaticTransactionGeneration.timeToStop) { // The ATG might sleep - void this.automaticTransactionGeneration.start(); + this.automaticTransactionGeneration.start().catch(() => { }); } } } @@ -982,14 +982,15 @@ export default class ChargingStation { const authorizationFile = this.getAuthorizationFile(); if (authorizationFile) { try { - fs.watch(authorizationFile).on('change', () => { - try { - logger.debug(this.logPrefix() + ' Authorization file ' + authorizationFile + ' have changed, reload'); - // Initialize authorizedTags - this.authorizedTags = this.getAuthorizedTags(); - console.log('here'); - } catch (error) { - logger.error(this.logPrefix() + ' Authorization file monitoring error: %j', error); + fs.watch(authorizationFile, (event, filename) => { + if (filename && event === 'change') { + try { + logger.debug(this.logPrefix() + ' Authorization file ' + authorizationFile + ' have changed, reload'); + // Initialize authorizedTags + this.authorizedTags = this.getAuthorizedTags(); + } catch (error) { + logger.error(this.logPrefix() + ' Authorization file monitoring error: %j', error); + } } }); } catch (error) { @@ -1002,26 +1003,27 @@ export default class ChargingStation { private startStationTemplateFileMonitoring(): void { try { - // eslint-disable-next-line @typescript-eslint/no-misused-promises - fs.watch(this.stationTemplateFile).on('change', async (): Promise => { - try { - logger.debug(this.logPrefix() + ' Template file ' + this.stationTemplateFile + ' have changed, reload'); - // Initialize - this.initialize(); - // Restart the ATG - if (!this.stationInfo.AutomaticTransactionGenerator.enable && - this.automaticTransactionGeneration) { - await this.automaticTransactionGeneration.stop(); - } - this.startAutomaticTransactionGenerator(); - if (this.getEnableStatistics()) { - this.performanceStatistics.restart(); - } else { - this.performanceStatistics.stop(); + fs.watch(this.stationTemplateFile, async (event, filename): Promise => { + if (filename && event === 'change') { + try { + logger.debug(this.logPrefix() + ' Template file ' + this.stationTemplateFile + ' have changed, reload'); + // Initialize + this.initialize(); + // Restart the ATG + if (!this.stationInfo.AutomaticTransactionGenerator.enable && + this.automaticTransactionGeneration) { + await this.automaticTransactionGeneration.stop(); + } + this.startAutomaticTransactionGenerator(); + if (this.getEnableStatistics()) { + this.performanceStatistics.restart(); + } else { + this.performanceStatistics.stop(); + } + // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed + } catch (error) { + logger.error(this.logPrefix() + ' Charging station template file monitoring error: %j', error); } - // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed - } catch (error) { - logger.error(this.logPrefix() + ' Charging station template file monitoring error: %j', error); } }); } catch (error) { diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index f7fe64bf..f10350eb 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -162,12 +162,13 @@ export default class Configuration { private static getConfigurationFileWatcher(): fs.FSWatcher { try { - // eslint-disable-next-line @typescript-eslint/no-misused-promises - return fs.watch(Configuration.configurationFilePath).on('change', async (): Promise => { - // Nullify to force configuration file reading - Configuration.configuration = null; - if (!Configuration.isUndefined(Configuration.configurationChangeCallback)) { - await Configuration.configurationChangeCallback(); + return fs.watch(Configuration.configurationFilePath, async (event, filename): Promise => { + if (filename && event === 'change') { + // Nullify to force configuration file reading + Configuration.configuration = null; + if (!Configuration.isUndefined(Configuration.configurationChangeCallback)) { + await Configuration.configurationChangeCallback(); + } } }); } catch (error) { -- 2.34.1