import ChargingStationInfo from '../types/ChargingStationInfo';
import Configuration from '../utils/Configuration';
import Constants from '../utils/Constants';
+import FileUtils from '../utils/FileUtils';
import { MessageType } from '../types/ocpp/MessageType';
import { MeterValueMeasurand } from '../types/ocpp/MeterValues';
import OCPP16IncomingRequestService from './ocpp/1.6/OCCP16IncomingRequestService';
public resetTransactionOnConnector(connectorId: number): void {
this.initTransactionOnConnector(connectorId);
- if (this.getConnector(connectorId)?.transactionSetInterval) {
- clearInterval(this.getConnector(connectorId).transactionSetInterval);
- }
+ this.stopMeterValues(connectorId);
}
public addToMessageQueue(message: string): void {
stationTemplateFromFile = JSON.parse(fs.readFileSync(fileDescriptor, 'utf8')) as ChargingStationTemplate;
fs.closeSync(fileDescriptor);
} catch (error) {
- logger.error('Template file ' + this.stationTemplateFile + ' loading error: %j', error);
- throw error;
+ FileUtils.handleFileException(this.logPrefix(), 'Template', this.stationTemplateFile, error);
}
const stationInfo: ChargingStationInfo = stationTemplateFromFile || {} as ChargingStationInfo;
if (!Utils.isEmptyArray(stationTemplateFromFile.power)) {
authorizedTags = JSON.parse(fs.readFileSync(fileDescriptor, 'utf8')) as string[];
fs.closeSync(fileDescriptor);
} catch (error) {
- logger.error(this.logPrefix() + ' Authorization file ' + authorizationFile + ' loading error: %j', error);
- throw error;
+ FileUtils.handleFileException(this.logPrefix(), 'Authorization', authorizationFile, error);
}
} else {
logger.info(this.logPrefix() + ' No authorization file given in template file ' + this.stationTemplateFile);
}
}
// Start the ATG
+ this.startAutomaticTransactionGenerator();
+ if (this.getEnableStatistics()) {
+ this.performanceStatistics.start();
+ }
+ }
+
+ private startAutomaticTransactionGenerator() {
if (this.stationInfo.AutomaticTransactionGenerator.enable) {
if (!this.automaticTransactionGeneration) {
this.automaticTransactionGeneration = new AutomaticTransactionGenerator(this);
void this.automaticTransactionGeneration.start();
}
}
- if (this.getEnableStatistics()) {
- this.performanceStatistics.start();
- }
}
private async stopMessageSequence(reason: StopTransactionReason = StopTransactionReason.NONE): Promise<void> {
}
private startWebSocketPing(): void {
- const webSocketPingInterval: number = this.getConfigurationKey(StandardParametersKey.WebSocketPingInterval) ? Utils.convertToInt(this.getConfigurationKey(StandardParametersKey.WebSocketPingInterval).value) : 0;
+ const webSocketPingInterval: number = this.getConfigurationKey(StandardParametersKey.WebSocketPingInterval)
+ ? Utils.convertToInt(this.getConfigurationKey(StandardParametersKey.WebSocketPingInterval).value)
+ : 0;
if (webSocketPingInterval > 0 && !this.webSocketPingSetInterval) {
this.webSocketPingSetInterval = setInterval(() => {
if (this.isWebSocketOpen()) {
}
private openWSConnection(options?: WebSocket.ClientOptions, forceCloseOpened = false): void {
- if (Utils.isUndefined(options)) {
- options = {} as WebSocket.ClientOptions;
- }
- if (Utils.isUndefined(options?.handshakeTimeout)) {
- options.handshakeTimeout = this.getConnectionTimeout() * 1000;
- }
+ options ?? {} as WebSocket.ClientOptions;
+ options?.handshakeTimeout ?? this.getConnectionTimeout() * 1000;
if (this.isWebSocketOpen() && forceCloseOpened) {
this.wsConnection.close();
}
logger.info(this.logPrefix() + ' Will communicate through URL ' + this.supervisionUrl);
}
+ private stopMeterValues(connectorId: number) {
+ if (this.getConnector(connectorId)?.transactionSetInterval) {
+ clearInterval(this.getConnector(connectorId).transactionSetInterval);
+ }
+ }
+
private startAuthorizationFileMonitoring(): void {
- fs.watch(this.getAuthorizationFile()).on('change', (e) => {
+ const authorizationFile = this.getAuthorizationFile();
+ if (authorizationFile) {
try {
- logger.debug(this.logPrefix() + ' Authorization file ' + this.getAuthorizationFile() + ' have changed, reload');
- // Initialize authorizedTags
- this.authorizedTags = this.getAuthorizedTags();
+ fs.watch(authorizationFile).on('change', (e) => {
+ 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) {
- logger.error(this.logPrefix() + ' Authorization file monitoring error: %j', error);
+ FileUtils.handleFileException(this.logPrefix(), 'Authorization', authorizationFile, error);
}
- });
+ } else {
+ logger.info(this.logPrefix() + ' No authorization file given in template file ' + this.stationTemplateFile + '. Not monitoring changes');
+ }
}
private startStationTemplateFileMonitoring(): void {
+ try {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
- fs.watch(this.stationTemplateFile).on('change', async (e): Promise<void> => {
- try {
- logger.debug(this.logPrefix() + ' Template file ' + this.stationTemplateFile + ' have changed, reload');
- // Initialize
- this.initialize();
- // Stop the ATG
- if (!this.stationInfo.AutomaticTransactionGenerator.enable &&
+ fs.watch(this.stationTemplateFile).on('change', async (e): Promise<void> => {
+ try {
+ logger.debug(this.logPrefix() + ' Template file ' + this.stationTemplateFile + ' have changed, reload');
+ // Initialize
+ this.initialize();
+ // Stop the ATG
+ if (!this.stationInfo.AutomaticTransactionGenerator.enable &&
this.automaticTransactionGeneration) {
- await this.automaticTransactionGeneration.stop();
- }
- // Start the ATG
- if (this.stationInfo.AutomaticTransactionGenerator.enable) {
- if (!this.automaticTransactionGeneration) {
- this.automaticTransactionGeneration = new AutomaticTransactionGenerator(this);
- }
- if (this.automaticTransactionGeneration.timeToStop) {
- // The ATG might sleep
- void this.automaticTransactionGeneration.start();
+ await this.automaticTransactionGeneration.stop();
}
+ // Start the ATG
+ this.startAutomaticTransactionGenerator();
+ // 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) {
+ FileUtils.handleFileException(this.logPrefix(), 'Template', this.stationTemplateFile, error);
+ }
}
private getReconnectExponentialDelay(): boolean | undefined {
this.stationInfo.AutomaticTransactionGenerator.stopOnConnectionFailure &&
this.automaticTransactionGeneration &&
!this.automaticTransactionGeneration.timeToStop) {
- this.automaticTransactionGeneration.stop().catch(() => { });
+ await this.automaticTransactionGeneration.stop();
}
if (this.autoReconnectRetryCount < this.getAutoReconnectMaxRetries() || this.getAutoReconnectMaxRetries() === -1) {
this.autoReconnectRetryCount++;