Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Add CS template tunable to enable persistent OCPP configuration
Browse files Browse the repository at this point in the history
Reference: #196

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
  • Loading branch information
Jérôme Benoit committed Mar 12, 2022
1 parent a59737e commit e8e865e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 37 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ But the modifications to test have to be done to the files in the build result d
| supervisionUrlOcppKey | | 'ConnectionUrl' | string | The vendor string that will be used as a vendor OCPP parameter key to set the supervision URL |
| ocppVersion | 1.6 | 1.6 | string | OCPP version |
| ocppProtocol | json | json | string | OCPP protocol |
| ocppStrictCompliance | true/false | true | boolean | Strict adherence to the OCPP version and protocol specifications |
| ocppStrictCompliance | true/false | false | boolean | Strict adherence to the OCPP version and protocol specifications |
| ocppPersistentConfiguration | true/false | true | boolean | Enable persistent OCPP parameters by charging station autogenerated name |
| wsOptions | | {} | ClientOptions & ClientRequestArgs | [ws](https://github.com/websockets/ws) and node.js [http](https://nodejs.org/api/http.html) clients options intersection |
| authorizationFile | | '' | string | RFID tags list file relative to src/assets path |
| baseName | | '' | string | base name to build charging stations name |
Expand Down
72 changes: 42 additions & 30 deletions src/charging-station/ChargingStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,15 @@ export default class ChargingStation {
}
}
);
FileUtils.watchJsonFile<ChargingStationConfiguration>(
this.logPrefix(),
FileType.ChargingStationConfiguration,
this.configurationFile,
this.configuration
);
// FIXME: triggered by saveConfiguration()
// if (this.getOcppPersistentConfiguration()) {
// FileUtils.watchJsonFile<ChargingStationConfiguration>(
// this.logPrefix(),
// FileType.ChargingStationConfiguration,
// this.configurationFile,
// this.configuration
// );
// }
// Handle WebSocket message
this.wsConnection.on(
'message',
Expand Down Expand Up @@ -718,14 +721,14 @@ export default class ChargingStation {
}
}

private getSupervisionUrlOcppKey(): string {
return this.stationInfo.supervisionUrlOcppKey ?? VendorDefaultParametersKey.ConnectionUrl;
}

private getSupervisionUrlOcppConfiguration(): boolean {
return this.stationInfo.supervisionUrlOcppConfiguration ?? false;
}

private getSupervisionUrlOcppKey(): string {
return this.stationInfo.supervisionUrlOcppKey ?? VendorDefaultParametersKey.ConnectionUrl;
}

private getChargingStationId(stationTemplate: ChargingStationTemplate): string {
// In case of multiple instances: add instance index to charging station id
const instanceIndex = process.env.CF_INSTANCE_INDEX ?? 0;
Expand Down Expand Up @@ -795,6 +798,10 @@ export default class ChargingStation {
return this.stationInfo.ocppVersion ? this.stationInfo.ocppVersion : OCPPVersion.VERSION_16;
}

private getOcppPersistentConfiguration(): boolean {
return this.stationInfo.ocppPersistentConfiguration ?? true;
}

private handleUnsupportedVersion(version: OCPPVersion) {
const errMsg = `${this.logPrefix()} Unsupported protocol version '${version}' configured in template file ${
this.stationTemplateFile
Expand All @@ -807,8 +814,7 @@ export default class ChargingStation {
this.stationInfo = this.buildStationInfo();
this.configurationFile = path.join(
path.resolve(__dirname, '../'),
'assets',
'configurations',
'assets/configurations',
this.stationInfo.chargingStationId + '.json'
);
this.configuration = this.getConfiguration();
Expand Down Expand Up @@ -1025,7 +1031,11 @@ export default class ChargingStation {

private getConfigurationFromFile(): ChargingStationConfiguration | null {
let configuration: ChargingStationConfiguration = null;
if (this.configurationFile && fs.existsSync(this.configurationFile)) {
if (
this.getOcppPersistentConfiguration() &&
this.configurationFile &&
fs.existsSync(this.configurationFile)
) {
try {
configuration = JSON.parse(
fs.readFileSync(this.configurationFile, 'utf8')
Expand All @@ -1043,26 +1053,28 @@ export default class ChargingStation {
}

private saveConfiguration(): void {
if (this.configurationFile) {
try {
if (!fs.existsSync(path.dirname(this.configurationFile))) {
fs.mkdirSync(path.dirname(this.configurationFile), { recursive: true });
if (this.getOcppPersistentConfiguration()) {
if (this.configurationFile) {
try {
if (!fs.existsSync(path.dirname(this.configurationFile))) {
fs.mkdirSync(path.dirname(this.configurationFile), { recursive: true });
}
const fileDescriptor = fs.openSync(this.configurationFile, 'w');
fs.writeFileSync(fileDescriptor, JSON.stringify(this.configuration, null, 2));
fs.closeSync(fileDescriptor);
} catch (error) {
FileUtils.handleFileException(
this.logPrefix(),
FileType.ChargingStationConfiguration,
this.configurationFile,
error as NodeJS.ErrnoException
);
}
const fileDescriptor = fs.openSync(this.configurationFile, 'w');
fs.writeFileSync(fileDescriptor, JSON.stringify(this.configuration, null, 2));
fs.closeSync(fileDescriptor);
} catch (error) {
FileUtils.handleFileException(
this.logPrefix(),
FileType.ChargingStationConfiguration,
this.configurationFile,
error as NodeJS.ErrnoException
} else {
logger.error(
`${this.logPrefix()} Trying to save charging station configuration to undefined file`
);
}
} else {
logger.error(
`${this.logPrefix()} Trying to save charging station configuration to undefined file`
);
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ export default class OCPP16ResponseService extends OCPPResponseService {
this.chargingStation.getConnectorStatus(authorizeConnectorId).idTagAuthorized = false;
delete this.chargingStation.getConnectorStatus(authorizeConnectorId).authorizeIdTag;
logger.debug(
`${this.chargingStation.logPrefix()} IdTag ${requestPayload.idTag} refused with status ${
`${this.chargingStation.logPrefix()} IdTag ${requestPayload.idTag} refused with status '${
payload.idTagInfo.status
} on connector ${authorizeConnectorId}`
}' on connector ${authorizeConnectorId}`
);
}
}
Expand Down Expand Up @@ -360,9 +360,9 @@ export default class OCPP16ResponseService extends OCPPResponseService {
this.chargingStation.logPrefix() +
' Starting transaction id ' +
payload.transactionId.toString() +
' REJECTED with status ' +
" REJECTED with status '" +
payload?.idTagInfo?.status +
', idTag ' +
"', idTag " +
requestPayload.idTag
);
await this.resetConnectorOnStartTransactionError(connectorId);
Expand Down Expand Up @@ -463,8 +463,9 @@ export default class OCPP16ResponseService extends OCPPResponseService {
this.chargingStation.logPrefix() +
' Stopping transaction id ' +
requestPayload.transactionId.toString() +
' REJECTED with status ' +
payload.idTagInfo?.status
" REJECTED with status '" +
payload.idTagInfo?.status +
"'"
);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/types/ChargingStationTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default interface ChargingStationTemplate {
ocppVersion?: OCPPVersion;
ocppProtocol?: OCPPProtocol;
ocppStrictCompliance?: boolean;
ocppPersistentConfiguration?: boolean;
wsOptions?: ClientOptions & ClientRequestArgs;
authorizationFile?: string;
baseName: string;
Expand Down

0 comments on commit e8e865e

Please sign in to comment.