From: Jérôme Benoit Date: Fri, 4 Dec 2020 20:38:43 +0000 (+0100) Subject: Add type sanity check to ChangeConfiguration OCPP command X-Git-Tag: v1.0.1-0~165 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=6d3a11a0544cf3d3a5f580b0e02a837c1453c0a0;p=e-mobility-charging-stations-simulator.git Add type sanity check to ChangeConfiguration OCPP command Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 1da63faa..b6c5fa2c 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -231,7 +231,7 @@ export default class ChargingStation { _getNumberOfPhases(): number { switch (this._getPowerOutType()) { case PowerOutType.AC: - return !Utils.isUndefined(this._stationInfo.numberOfPhases) ? Utils.convertToInt(this._stationInfo.numberOfPhases) : 3; + return !Utils.isUndefined(this._stationInfo.numberOfPhases) ? this._stationInfo.numberOfPhases : 3; case PowerOutType.DC: return 0; } @@ -315,7 +315,7 @@ export default class ChargingStation { logger.error(errMsg); throw Error(errMsg); } - return !Utils.isUndefined(this._stationInfo.voltageOut) ? Utils.convertToInt(this._stationInfo.voltageOut) : defaultVoltageOut; + return !Utils.isUndefined(this._stationInfo.voltageOut) ? this._stationInfo.voltageOut : defaultVoltageOut; } _getTransactionIdTag(transactionId: number): string { @@ -1147,7 +1147,7 @@ export default class ChargingStation { } async handleResponseStartTransaction(payload: StartTransactionResponse, requestPayload: StartTransactionRequest): Promise { - const connectorId = Utils.convertToInt(requestPayload.connectorId); + const connectorId = requestPayload.connectorId; if (this.getConnector(connectorId).transactionStarted) { logger.debug(this._logPrefix() + ' Trying to start a transaction on an already used connector ' + connectorId.toString() + ': %j', this.getConnector(connectorId)); return; @@ -1187,7 +1187,7 @@ export default class ChargingStation { async handleResponseStopTransaction(payload: StopTransactionResponse, requestPayload: StopTransactionRequest): Promise { let transactionConnectorId: number; for (const connector in this._connectors) { - if (Utils.convertToInt(connector) > 0 && this.getConnector(Utils.convertToInt(connector)).transactionId === Utils.convertToInt(requestPayload.transactionId)) { + if (Utils.convertToInt(connector) > 0 && this.getConnector(Utils.convertToInt(connector)).transactionId === requestPayload.transactionId) { transactionConnectorId = Utils.convertToInt(connector); break; } @@ -1259,7 +1259,7 @@ export default class ChargingStation { } async handleRequestUnlockConnector(commandPayload: UnlockConnectorRequest): Promise { - const connectorId = Utils.convertToInt(commandPayload.connectorId); + const connectorId = commandPayload.connectorId; if (connectorId === 0) { logger.error(this._logPrefix() + ' Trying to unlock connector ' + connectorId.toString()); return Constants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED; @@ -1344,6 +1344,13 @@ export default class ChargingStation { } handleRequestChangeConfiguration(commandPayload: ChangeConfigurationRequest): ChangeConfigurationResponse { + // JSON request fields type sanity check + if (!Utils.isString(commandPayload.key)) { + logger.error(`${this._logPrefix()} ChangeConfiguration request key field is not a string:`, commandPayload); + } + if (!Utils.isString(commandPayload.value)) { + logger.error(`${this._logPrefix()} ChangeConfiguration request value field is not a string:`, commandPayload); + } const keyToChange = this._getConfigurationKey(commandPayload.key); if (!keyToChange) { return Constants.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED; @@ -1399,7 +1406,7 @@ export default class ChargingStation { } async handleRequestRemoteStartTransaction(commandPayload: RemoteStartTransactionRequest): Promise { - const transactionConnectorID: number = commandPayload.connectorId ? Utils.convertToInt(commandPayload.connectorId) : 1; + const transactionConnectorID: number = commandPayload.connectorId ? commandPayload.connectorId : 1; if (this._getAuthorizeRemoteTxRequests() && this._getLocalAuthListEnabled() && this.hasAuthorizedTags()) { // Check if authorized if (this._authorizedTags.find((value) => value === commandPayload.idTag)) { @@ -1418,7 +1425,7 @@ export default class ChargingStation { } async handleRequestRemoteStopTransaction(commandPayload: RemoteStopTransactionRequest): Promise { - const transactionId = Utils.convertToInt(commandPayload.transactionId); + const transactionId = commandPayload.transactionId; for (const connector in this._connectors) { if (Utils.convertToInt(connector) > 0 && this.getConnector(Utils.convertToInt(connector)).transactionId === transactionId) { await this.sendStopTransaction(transactionId); diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 69ccc318..cfa63cae 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -106,6 +106,11 @@ export default class Utils { return Math.round(number * roundPower) / roundPower; } + static truncTo(number: number, scale: number): number { + const truncPower = Math.pow(10, scale); + return Math.trunc(number * truncPower) / truncPower; + } + static getRandomFloatRounded(max: number, min = 0, scale = 2): number { if (min) { return Utils.roundTo(Utils.getRandomFloat(max, min), scale);