Handling of OCPP custom param in backend
MODIFY
src/server/ocpp/utils/OCPPUtils.ts
src/server/ocpp/utils/OCPPUtils.ts
@@ -1476,7 +1476,7 @@ export default class OCPPUtils {
}
public static async requestAndSaveChargingStationOcppParameters(tenantID: string, chargingStation: ChargingStation,
- forceUpdateOcppParametersWithTemplate = false, customParameter?: OCPPCustomConfigurationParam): Promise<OCPPChangeConfigurationCommandResult> {
+ forceUpdateOcppParametersWithTemplate = false): Promise<OCPPChangeConfigurationCommandResult> {
try {
// Get the OCPP Configuration
const ocppConfiguration = await OCPPUtils.requestChargingStationOcppParameters(tenantID, chargingStation, {});
@@ -1489,35 +1489,29 @@ export default class OCPPUtils {
message: 'Command sent with success',
detailedMessages: { ocppConfiguration }
});
- // Create Conf
+ // Set Conf
const chargingStationOcppParameters: ChargingStationOcppParameters = {
id: chargingStation.id,
configuration: ocppConfiguration.configurationKey,
timestamp: new Date()
};
+ // No: Get it from DB
const ocppParametersFromDB = await ChargingStationStorage.getOcppParameters(tenantID, chargingStation.id);
- // Set default?
+ // Configuration found in Charging Station
if (!chargingStationOcppParameters.configuration) {
- // Check if there is an already existing config in DB
if (ocppParametersFromDB.count === 0) {
// No config at all: Set default OCPP configuration
chargingStationOcppParameters.configuration = Constants.DEFAULT_OCPP_16_CONFIGURATION;
} else {
- // Set DB
+ // Set from DB
chargingStationOcppParameters.configuration = ocppParametersFromDB.result;
}
}
- // Reverify all fetched Parameters
- chargingStationOcppParameters.configuration.forEach((fetchedParam) => {
- const dbParam = ocppParametersFromDB.result.find((param) => param.key === fetchedParam.key);
- if (dbParam && dbParam.custom) {
- fetchedParam.custom = dbParam.custom;
- }
- // If new param, set custom status
- if (!dbParam && customParameter && customParameter.key === fetchedParam.key) {
- fetchedParam.custom = customParameter.custom;
+ // Add the existing custom params
+ const customParams = ocppParametersFromDB.result.filter((customParam) => customParam.custom);
+ if (!Utils.isEmptyArray(customParams)) {
+ chargingStationOcppParameters.configuration = chargingStationOcppParameters.configuration.concat(customParams);
}
- });
// Save config
await ChargingStationStorage.saveOcppParameters(tenantID, chargingStationOcppParameters);
// Check OCPP Configuration

MODIFY
src/server/rest/service/ChargingStationService.ts
src/server/rest/service/ChargingStationService.ts
import { Action, Entity } from '../../../types/Authorization';
-import ChargingStation, { Command, OCPPParams, StaticLimitAmps } from '../../../types/ChargingStation';
+import ChargingStation, { ChargingStationOcppParameters, Command, OCPPParams, StaticLimitAmps } from '../../../types/ChargingStation';
import { HTTPAuthError, HTTPError } from '../../../types/HTTPError';
import { NextFunction, Request, Response } from 'express';
import { OCPPConfigurationStatus, OCPPGetCompositeScheduleCommandResult, OCPPStatus } from '../../../types/ocpp/OCPPClient';
@@ -38,7 +38,6 @@ import TransactionStorage from '../../../storage/mongodb/TransactionStorage';
import UserToken from '../../../types/UserToken';
import Utils from '../../../utils/Utils';
import UtilsService from './UtilsService';
-import fs from 'fs';
const MODULE_NAME = 'ChargingStationService';
@@ -1320,18 +1319,42 @@ export default class ChargingStationService {
user: user,
action: action,
module: MODULE_NAME, method: 'handleChargingStationCommand',
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
message: `Reboot is required due to change of OCPP Parameter '${params.key}' to '${params.value}'`,
detailedMessages: { result }
});
}
- // Refresh Configuration
+ // Custom param?
if (params.custom) {
- await OCPPUtils.requestAndSaveChargingStationOcppParameters(tenantID, chargingStation, false, {
- key: params.key,
- value: params.value,
- custom: params.custom
- });
+ // Get OCPP Parameters from DB
+ const ocppParametersFromDB = await ChargingStationStorage.getOcppParameters(tenantID, chargingStation.id);
+ // Set new structure
+ const chargingStationOcppParameters: ChargingStationOcppParameters = {
+ id: chargingStation.id,
+ configuration: ocppParametersFromDB.result,
+ timestamp: new Date()
+ };
+ // Search for existing Custom param
+ const foundOcppParam = chargingStationOcppParameters.configuration.find((ocppParam) => ocppParam.key === params.key);
+ if (foundOcppParam) {
+ // Update param
+ foundOcppParam.value = params.value;
+ // Save config
+ if (foundOcppParam.custom) {
+ // Save
+ await ChargingStationStorage.saveOcppParameters(tenantID, chargingStationOcppParameters);
+ } else {
+ // Not a custom param: Refresh the whole OCPP Parameters
+ await OCPPUtils.requestAndSaveChargingStationOcppParameters(tenantID, chargingStation);
+ }
+ } else {
+ // Add custom param
+ chargingStationOcppParameters.configuration.push(params);
+ // Save
+ await ChargingStationStorage.saveOcppParameters(tenantID, chargingStationOcppParameters);
+ }
} else {
+ // Refresh the whole OCPP Parameters
await OCPPUtils.requestAndSaveChargingStationOcppParameters(tenantID, chargingStation);
}
// Check update with Vendor

MODIFY
src/server/rest/service/security/ChargingStationSecurity.ts
src/server/rest/service/security/ChargingStationSecurity.ts
@@ -436,6 +436,9 @@ export default class ChargingStationSecurity {
if (Utils.objectHasProperty(request.args, 'value')) {
filteredRequest.args.value = sanitize(request.args.value);
}
+ if (Utils.objectHasProperty(request.args, 'custom')) {
+ filteredRequest.args.custom = UtilsSecurity.filterBoolean(request.args.custom);
+ }
if (Utils.objectHasProperty(request.args, 'connectorId')) {
filteredRequest.args.connectorId = sanitize(request.args.connectorId);
}

Handling of OCPP custom param in backend