RequestCommand,
type Response,
StandardParametersKey,
+ type Status,
type StatusNotificationRequest,
type StatusNotificationResponse,
StopTransactionReason,
Configuration,
Constants,
DCElectricUtils,
- ErrorUtils,
- FileUtils,
- MessageChannelUtils,
Utils,
buildChargingStationAutomaticTransactionGeneratorConfiguration,
buildConnectorsStatus,
buildEvsesStatus,
+ buildStartedMessage,
+ buildStoppedMessage,
+ buildUpdatedMessage,
+ handleFileException,
logger,
+ watchJsonFile,
} from '../utils';
export class ChargingStation {
}
this.openWSConnection();
// Monitor charging station template file
- this.templateFileWatcher = FileUtils.watchJsonFile(
+ this.templateFileWatcher = watchJsonFile(
this.templateFile,
FileType.ChargingStationTemplate,
this.logPrefix(),
}
);
this.started = true;
- parentPort?.postMessage(MessageChannelUtils.buildStartedMessage(this));
+ parentPort?.postMessage(buildStartedMessage(this));
this.starting = false;
} else {
logger.warn(`${this.logPrefix()} Charging station is already starting...`);
delete this.bootNotificationResponse;
this.started = false;
this.saveConfiguration();
- parentPort?.postMessage(MessageChannelUtils.buildStoppedMessage(this));
+ parentPort?.postMessage(buildStoppedMessage(this));
this.stopping = false;
} else {
logger.warn(`${this.logPrefix()} Charging station is already stopping...`);
terminateOpened: false,
}
): void {
- options.handshakeTimeout = options?.handshakeTimeout ?? this.getConnectionTimeout() * 1000;
+ options = { handshakeTimeout: this.getConnectionTimeout() * 1000, ...options };
params = { ...{ closeOpened: false, terminateOpened: false }, ...params };
if (this.started === false && this.starting === false) {
logger.warn(
public getAutomaticTransactionGeneratorConfiguration():
| AutomaticTransactionGeneratorConfiguration
| undefined {
+ let automaticTransactionGeneratorConfiguration:
+ | AutomaticTransactionGeneratorConfiguration
+ | undefined;
const automaticTransactionGeneratorConfigurationFromFile =
this.getConfigurationFromFile()?.automaticTransactionGenerator;
- if (automaticTransactionGeneratorConfigurationFromFile) {
- return automaticTransactionGeneratorConfigurationFromFile;
+ if (
+ this.getAutomaticTransactionGeneratorPersistentConfiguration() &&
+ automaticTransactionGeneratorConfigurationFromFile
+ ) {
+ automaticTransactionGeneratorConfiguration =
+ automaticTransactionGeneratorConfigurationFromFile;
+ } else {
+ automaticTransactionGeneratorConfiguration =
+ this.getTemplateFromFile()?.AutomaticTransactionGenerator;
}
- return this.getTemplateFromFile()?.AutomaticTransactionGenerator;
+ return {
+ ...Constants.DEFAULT_ATG_CONFIGURATION,
+ ...automaticTransactionGeneratorConfiguration,
+ };
+ }
+
+ public getAutomaticTransactionGeneratorStatuses(): Status[] | undefined {
+ return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses;
}
public startAutomaticTransactionGenerator(connectorIds?: number[]): void {
} else {
this.automaticTransactionGenerator?.start();
}
- this.saveChargingStationAutomaticTransactionGeneratorConfiguration();
- parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
+ this.saveAutomaticTransactionGeneratorConfiguration();
+ parentPort?.postMessage(buildUpdatedMessage(this));
}
public stopAutomaticTransactionGenerator(connectorIds?: number[]): void {
} else {
this.automaticTransactionGenerator?.stop();
}
- this.saveChargingStationAutomaticTransactionGeneratorConfiguration();
- parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
+ this.saveAutomaticTransactionGeneratorConfiguration();
+ parentPort?.postMessage(buildUpdatedMessage(this));
}
public async stopTransactionOnConnector(
this.templateFileHash = template.templateHash;
}
} catch (error) {
- ErrorUtils.handleFileException(
+ handleFileException(
this.templateFile,
FileType.ChargingStationTemplate,
error as NodeJS.ErrnoException,
private getStationInfoFromTemplate(): ChargingStationInfo {
const stationTemplate: ChargingStationTemplate | undefined = this.getTemplateFromFile();
- ChargingStationUtils.checkTemplateFile(stationTemplate, this.logPrefix(), this.templateFile);
+ ChargingStationUtils.checkTemplate(stationTemplate, this.logPrefix(), this.templateFile);
ChargingStationUtils.warnTemplateKeysDeprecation(
stationTemplate,
this.logPrefix(),
return this.stationInfo?.stationInfoPersistentConfiguration ?? true;
}
+ private getAutomaticTransactionGeneratorPersistentConfiguration(): boolean {
+ return this.stationInfo?.automaticTransactionGeneratorPersistentConfiguration ?? true;
+ }
+
private handleUnsupportedVersion(version: OCPPVersion) {
const errorMsg = `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
logger.error(`${this.logPrefix()} ${errorMsg}`);
private initialize(): void {
const stationTemplate = this.getTemplateFromFile();
- ChargingStationUtils.checkTemplateFile(stationTemplate, this.logPrefix(), this.templateFile);
+ ChargingStationUtils.checkTemplate(stationTemplate, this.logPrefix(), this.templateFile);
this.configurationFile = path.join(
path.dirname(this.templateFile.replace('station-templates', 'configurations')),
`${ChargingStationUtils.getHashId(this.index, stationTemplate)}.json`
);
const chargingStationConfiguration = this.getConfigurationFromFile();
- const featureFlag = false;
if (
- featureFlag &&
+ chargingStationConfiguration?.stationInfo?.templateHash === stationTemplate?.templateHash &&
(chargingStationConfiguration?.connectorsStatus || chargingStationConfiguration?.evsesStatus)
) {
this.initializeConnectorsOrEvsesFromFile(chargingStationConfiguration);
if (stationTemplate?.Evses) {
const evsesConfigHash = crypto
.createHash(Constants.DEFAULT_HASH_ALGORITHM)
- .update(`${JSON.stringify(stationTemplate?.Evses)}`)
+ .update(JSON.stringify(stationTemplate?.Evses))
.digest('hex');
const evsesConfigChanged =
this.evses?.size !== 0 && this.evsesConfigurationHash !== evsesConfigHash;
this.configurationFileHash = configuration.configurationHash;
}
} catch (error) {
- ErrorUtils.handleFileException(
+ handleFileException(
this.configurationFile,
FileType.ChargingStationConfiguration,
error as NodeJS.ErrnoException,
return configuration;
}
- private saveChargingStationAutomaticTransactionGeneratorConfiguration(): void {
- this.saveConfiguration();
+ private saveAutomaticTransactionGeneratorConfiguration(): void {
+ if (this.getAutomaticTransactionGeneratorPersistentConfiguration()) {
+ this.saveConfiguration();
+ }
}
private saveConnectorsStatus() {
Utils.cloneObject<ChargingStationConfiguration>(this.getConfigurationFromFile()) ?? {};
if (this.getStationInfoPersistentConfiguration() && this.stationInfo) {
configurationData.stationInfo = this.stationInfo;
+ } else {
+ delete configurationData.stationInfo;
}
if (this.getOcppPersistentConfiguration() && this.ocppConfiguration?.configurationKey) {
configurationData.configurationKey = this.ocppConfiguration.configurationKey;
+ } else {
+ delete configurationData.configurationKey;
}
configurationData = merge<ChargingStationConfiguration>(
configurationData,
buildChargingStationAutomaticTransactionGeneratorConfiguration(this)
);
+ if (
+ !this.getAutomaticTransactionGeneratorPersistentConfiguration() ||
+ !this.getAutomaticTransactionGeneratorConfiguration()
+ ) {
+ delete configurationData.automaticTransactionGenerator;
+ }
if (this.connectors.size > 0) {
configurationData.connectorsStatus = buildConnectorsStatus(this);
+ } else {
+ delete configurationData.connectorsStatus;
}
if (this.evses.size > 0) {
configurationData.evsesStatus = buildEvsesStatus(this);
+ } else {
+ delete configurationData.evsesStatus;
}
delete configurationData.configurationHash;
const configurationHash = crypto
.createHash(Constants.DEFAULT_HASH_ALGORITHM)
- .update(JSON.stringify(configurationData))
+ .update(
+ JSON.stringify({
+ stationInfo: configurationData.stationInfo,
+ configurationKey: configurationData.configurationKey,
+ automaticTransactionGenerator: configurationData.automaticTransactionGenerator,
+ } as ChargingStationConfiguration)
+ )
.digest('hex');
if (this.configurationFileHash !== configurationHash) {
AsyncLock.acquire(AsyncLockType.configuration)
this.configurationFileHash = configurationHash;
})
.catch((error) => {
- ErrorUtils.handleFileException(
+ handleFileException(
this.configurationFile,
FileType.ChargingStationConfiguration,
error as NodeJS.ErrnoException,
);
}
} catch (error) {
- ErrorUtils.handleFileException(
+ handleFileException(
this.configurationFile,
FileType.ChargingStationConfiguration,
error as NodeJS.ErrnoException,
skipBufferingOnError: true,
});
if (this.isRegistered() === false) {
- this.getRegistrationMaxRetries() !== -1 && registrationRetryCount++;
+ this.getRegistrationMaxRetries() !== -1 && ++registrationRetryCount;
await Utils.sleep(
this?.bootNotificationResponse?.interval
? this.bootNotificationResponse.interval * 1000
}
this.wsConnectionRestarted = false;
this.autoReconnectRetryCount = 0;
- parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
+ parentPort?.postMessage(buildUpdatedMessage(this));
} else {
logger.warn(
`${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
this.started === true && (await this.reconnect());
break;
}
- parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
+ parentPort?.postMessage(buildUpdatedMessage(this));
}
private getCachedRequest(messageType: MessageType, messageId: string): CachedRequest | undefined {
logger.error(`${this.logPrefix()} ${errorMsg}`);
throw new OCPPError(ErrorType.PROTOCOL_ERROR, errorMsg);
}
- parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
+ parentPort?.postMessage(buildUpdatedMessage(this));
} else {
throw new OCPPError(ErrorType.PROTOCOL_ERROR, 'Incoming message is not an array', null, {
request,
this.autoReconnectRetryCount < this.getAutoReconnectMaxRetries() ||
this.getAutoReconnectMaxRetries() === -1
) {
- this.autoReconnectRetryCount++;
+ ++this.autoReconnectRetryCount;
const reconnectDelay = this.getReconnectExponentialDelay()
? Utils.exponentialDelay(this.autoReconnectRetryCount)
: this.getConnectionTimeout() * 1000;