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

Commit

Permalink
Add persistent OCPP parameters key/value support by CS generated name
Browse files Browse the repository at this point in the history
Issue #196

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
  • Loading branch information
Jérôme Benoit committed Mar 11, 2022
1 parent 62c4bdf commit 073bd09
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 7 deletions.
11 changes: 11 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"root": true,
"env": {
"es2020": true,
"node": true,
"mocha": true
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"extends": [
"eslint:recommended",
"plugin:import/errors",
Expand Down Expand Up @@ -114,6 +123,8 @@
"files": ["**/*.ts"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
"project": "./tsconfig.json"
},
"extends": [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
],
"main": "./dist/start.js",
"lint-staged": {
"*.{js,ts}": [
"src/**/*.{js,ts}": [
"prettier --write",
"eslint --cache --fix"
]
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default {
}),
isDevelopmentBuild && istanbul(),
del({
targets: 'dist/*',
targets: ['dist/*', '!dist/assets', 'dist/assets/*.json', 'dist/assets/station-templates'],
}),
copy({
targets: [{ src: 'src/assets', dest: 'dist/' }],
Expand Down
2 changes: 2 additions & 0 deletions src/assets/station-templates/evlink.station-template.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"supervisionUrlOcppConfiguration": true,
"supervisionUrlOcppKey": "ocppcentraladdress",
"authorizationFile": "authorization-tags.json",
"baseName": "CS-EVLINK",
"chargePointModel": "MONOBLOCK",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"supervisionUrlOcppConfiguration": true,
"supervisionUrlOcppKey": "ocppcentraladdress",
"authorizationFile": "authorization-tags.json",
"baseName": "CS-SCHNEIDER",
"chargePointModel": "MONOBLOCK",
Expand Down
2 changes: 2 additions & 0 deletions src/assets/station-templates/schneider.station-template.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"supervisionUrlOcppConfiguration": true,
"supervisionUrlOcppKey": "ocppcentraladdress",
"authorizationFile": "authorization-tags.json",
"baseName": "CS-SCHNEIDER",
"chargePointModel": "MONOBLOCK",
Expand Down
72 changes: 67 additions & 5 deletions src/charging-station/ChargingStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default class ChargingStation {
public heartbeatSetInterval!: NodeJS.Timeout;
public ocppRequestService!: OCPPRequestService;
private readonly index: number;
private configurationFile!: string;
private bootNotificationRequest!: BootNotificationRequest;
private bootNotificationResponse!: BootNotificationResponse | null;
private connectorsConfigurationHash!: string;
Expand Down Expand Up @@ -594,6 +595,7 @@ export default class ChargingStation {
if (keyFound) {
const keyIndex = this.configuration.configurationKey.indexOf(keyFound);
this.configuration.configurationKey[keyIndex].value = value;
this.saveConfiguration();
} else {
logger.error(
`${this.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
Expand Down Expand Up @@ -734,7 +736,13 @@ export default class ChargingStation {

private initialize(): void {
this.stationInfo = this.buildStationInfo();
this.configuration = this.getTemplateChargingStationConfiguration();
this.configurationFile = path.join(
path.resolve(__dirname, '../'),
'assets',
'configurations',
this.stationInfo.chargingStationId + '.json'
);
this.configuration = this.getConfiguration();
delete this.stationInfo.Configuration;
this.bootNotificationRequest = {
chargePointModel: this.stationInfo.chargePointModel,
Expand Down Expand Up @@ -940,6 +948,64 @@ export default class ChargingStation {
Constants.DEFAULT_CONNECTION_TIMEOUT.toString()
);
}
this.saveConfiguration();
}

private getConfigurationFromTemplate(): ChargingStationConfiguration {
return this.stationInfo.Configuration ?? ({} as ChargingStationConfiguration);
}

private getConfigurationFromFile(): ChargingStationConfiguration | null {
let configuration: ChargingStationConfiguration = null;
if (this.configurationFile && fs.existsSync(this.configurationFile)) {
try {
const fileDescriptor = fs.openSync(this.configurationFile, 'r');
configuration = JSON.parse(
fs.readFileSync(fileDescriptor, 'utf8')
) as ChargingStationConfiguration;
fs.closeSync(fileDescriptor);
} catch (error) {
FileUtils.handleFileException(
this.logPrefix(),
'Configuration',
this.configurationFile,
error as NodeJS.ErrnoException
);
}
}
return configuration;
}

private saveConfiguration(): void {
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(),
'Configuration',
this.configurationFile,
error as NodeJS.ErrnoException
);
}
} else {
logger.error(
`${this.logPrefix()} Trying to save charging station configuration to undefined file`
);
}
}

private getConfiguration(): ChargingStationConfiguration {
let configuration: ChargingStationConfiguration = this.getConfigurationFromFile();
if (!configuration) {
configuration = this.getConfigurationFromTemplate();
}
return configuration;
}

private async onOpen(): Promise<void> {
Expand Down Expand Up @@ -1141,10 +1207,6 @@ export default class ChargingStation {
logger.error(this.logPrefix() + ' WebSocket error: %j', error);
}

private getTemplateChargingStationConfiguration(): ChargingStationConfiguration {
return this.stationInfo.Configuration ?? ({} as ChargingStationConfiguration);
}

private getAuthorizationFile(): string | undefined {
return (
this.stationInfo.authorizationFile &&
Expand Down

0 comments on commit 073bd09

Please sign in to comment.