X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStation.js;h=0411a1a1f12c88bb457b4246f10d7b30131e37a9;hb=a6e68f340a9c5837c767b316dee5d5121188dc47;hp=f6ed30629d6fa813ee258534dc018814de88e07a;hpb=f7869514b1450a852ff707ba1a3beaecccea9ba7;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.js b/src/charging-station/ChargingStation.js index f6ed3062..0411a1a1 100644 --- a/src/charging-station/ChargingStation.js +++ b/src/charging-station/ChargingStation.js @@ -72,7 +72,7 @@ class ChargingStation { _getAuthorizeRemoteTxRequests() { const authorizeRemoteTxRequests = this._configuration.configurationKey.find((configElement) => configElement.key === 'AuthorizeRemoteTxRequests'); - return authorizeRemoteTxRequests ? authorizeRemoteTxRequests.value : false; + return authorizeRemoteTxRequests ? Utils.convertToBoolean(authorizeRemoteTxRequests.value) : false; } _buildChargingStation(index, stationTemplate) { @@ -138,7 +138,7 @@ class ChargingStation { }); } } else { - // At first start, send Bootnotification + // At first start, send BootNotification try { this.sendMessage(uuid(), this._bootNotificationMessage, Constants.OCPP_JSON_CALL_MESSAGE, 'BootNotification'); } catch (error) { @@ -180,10 +180,11 @@ class ChargingStation { } async onMessage(message) { - // Parse the message - const [messageType, messageId, commandName, commandPayload, errorDetails] = JSON.parse(message); - + let [messageType, messageId, commandName, commandPayload, errorDetails] = [0, '', Constants.ENTITY_CHARGING_STATION, '', '']; try { + // Parse the message + [messageType, messageId, commandName, commandPayload, errorDetails] = JSON.parse(message); + // Check the Type of message switch (messageType) { // Incoming Message @@ -298,14 +299,8 @@ class ChargingStation { // Error Message case Constants.OCPP_JSON_CALL_ERROR_MESSAGE: // Build Message - // eslint-disable-next-line no-case-declarations - const { - code, - message, - details, - } = command; - this._statistics.addMessage(`Error ${code}`); - messageToSend = JSON.stringify([messageType, messageId, code, message, details]); + this._statistics.addMessage(`Error ${command.code}`); + messageToSend = JSON.stringify([messageType, messageId, command.code ? command.code : Constants.OCPP_ERROR_GENERIC_ERROR, command.message ? command.message : '', command.details ? command.details : {}]); break; } // Check if wsConnection in ready @@ -366,7 +361,7 @@ class ChargingStation { // determine number of customized connectors let lastConnector; for (lastConnector in connectorsConfig) { - if (lastConnector === 0 && this._stationInfo.usedConnectorId0) { + if (Utils.convertToInt(lastConnector) === 0 && this._stationInfo.usedConnectorId0) { this._connectors[lastConnector] = connectorsConfig[lastConnector]; } } @@ -406,29 +401,29 @@ class ChargingStation { } handleResponseStartTransaction(payload, requestPayload) { - this._connectors[requestPayload.connectorId] = { - transactionStarted: false, - idTag: requestPayload.idTag, - }; + // Reset connector transaction related attributes + this._connectors[requestPayload.connectorId].transactionStarted = false; + this._connectors[requestPayload.connectorId].idTag = requestPayload.idTag; + if (payload.idTagInfo.status === 'Accepted') { for (const connector in this._connectors) { - if (connector === requestPayload.connectorId) { + if (Utils.convertToInt(connector) === requestPayload.connectorId) { this._connectors[connector].transactionStarted = true; this._connectors[connector].transactionId = payload.transactionId; this._connectors[connector].lastConsumptionValue = 0; this._connectors[connector].lastSoC = 0; - logger.info(this._basicFormatLog() + ' Transaction ' + this._connectors[connector].transactionId + ' STARTED on ' + this._stationInfo.name + '#' + requestPayload.connectorId); + logger.info(this._basicFormatLog() + ' Transaction ' + this._connectors[connector].transactionId + ' STARTED on ' + this._stationInfo.name + '#' + requestPayload.connectorId + ' with idTag ' + requestPayload.idTag); this.sendStatusNotification(requestPayload.connectorId, 'Charging'); - const configuredMeterInterval = this._configuration.configurationKey.find((value) => value.key === 'meterValueInterval'); + const configuredMeterValueSampleInterval = this._configuration.configurationKey.find((value) => value.key === 'MeterValueSampleInterval'); this.startMeterValues(requestPayload.connectorId, - (configuredMeterInterval ? configuredMeterInterval.value * 1000 : 60000), + (configuredMeterValueSampleInterval ? configuredMeterValueSampleInterval.value * 1000 : 60000), this); } } } else { logger.error(this._basicFormatLog() + ' Starting transaction id ' + payload.transactionId + ' REJECTED with status ' + payload.idTagInfo.status + ', idTag ' + requestPayload.idTag); for (const connector in this._connectors) { - if (connector === requestPayload.connectorId) { + if (Utils.convertToInt(connector) === requestPayload.connectorId) { this._resetTransactionOnConnector(connector); } } @@ -483,11 +478,11 @@ class ChargingStation { await this.sendError(messageId, error); } } else { - // Throw Exception + // Throw exception await this.sendError(messageId, new OCPPError(Constants.OCPP_ERROR_NOT_IMPLEMENTED, 'Not implemented', {})); throw new Error(`${commandName} is not implemented ${JSON.stringify(commandPayload, null, ' ')}`); } - // Send Response + // Send response await this.sendMessage(messageId, result, Constants.OCPP_JSON_CALL_RESULT_MESSAGE); } @@ -497,38 +492,32 @@ class ChargingStation { async handleChangeConfiguration(commandPayload) { const keyToChange = this._configuration.configurationKey.find((element) => element.key === commandPayload.key); - if (keyToChange) { - keyToChange.value = commandPayload.value; - return { - status: 'Accepted', - }; + if (keyToChange && !Utils.convertToBoolean(keyToChange.readonly)) { + const keyIndex = this._configuration.configurationKey.indexOf(keyToChange); + this._configuration.configurationKey[keyIndex].value = commandPayload.value; + return Constants.OCPP_RESPONSE_ACCEPTED; } - return { - status: 'Rejected', - }; + return Constants.OCPP_RESPONSE_REJECTED; } async handleRemoteStartTransaction(commandPayload) { const transactionConnectorID = (commandPayload.connectorId ? commandPayload.connectorId : '1'); if (this.isAuthorizationRequested() && this._authorizeRemoteTxRequests) { - // check if authorized + // Check if authorized if (this._authorizedKeys.find((value) => value === commandPayload.idTag)) { // Authorization successful start transaction setTimeout(() => this.sendStartTransaction(transactionConnectorID, commandPayload.idTag), 500); - return { - status: 'Accepted', - }; + logger.info(this._basicFormatLog() + ' Transaction remotely STARTED on ' + this._stationInfo.name + '#' + transactionConnectorID + ' with idTag ' + commandPayload.idTag); + return Constants.OCPP_RESPONSE_ACCEPTED; } // Start authorization checks - return { - status: 'Rejected', - }; + logger.error(this._basicFormatLog() + ' Remote starting transaction REJECTED with status ' + commandPayload.idTagInfo.status + ', idTag ' + commandPayload.idTag); + return Constants.OCPP_RESPONSE_REJECTED; } - // no local authorization check required => start transaction + // No local authorization check required => start transaction setTimeout(() => this.sendStartTransaction(transactionConnectorID, commandPayload.idTag), 500); - return { - status: 'Accepted', - }; + logger.info(this._basicFormatLog() + ' Transaction remotely STARTED on ' + this._stationInfo.name + '#' + transactionConnectorID + ' with idTag ' + commandPayload.idTag); + return Constants.OCPP_RESPONSE_ACCEPTED; } async sendStartTransaction(connectorID, idTag) { @@ -599,7 +588,7 @@ class ChargingStation { // Persist previous value in connector const connector = self._connectors[connectorID]; let consumption; - consumption = Utils.getRandomInt(self._stationInfo.maxPower / 3600000 * interval, 3); + consumption = Utils.getRandomInt(self._stationInfo.maxPower / 3600000 * interval, 4); if (connector && connector.lastConsumptionValue >= 0) { connector.lastConsumptionValue += consumption; } else { @@ -628,11 +617,11 @@ class ChargingStation { } async startMeterValues(connectorID, interval, self) { - // if (!this._connectors[connectorID].transactionStarted) { - // logger.debug(`${self._basicFormatLog()} Trying to start meter values on connector ID ${connectorID} with no transaction`); - // } else if (this._connectors[connectorID].transactionStarted && !this._connectors[connectorID].transactionId) { - // logger.debug(`${self._basicFormatLog()} Trying to start meter values on connector ID ${connectorID} with no transaction id`); - // } + if (!this._connectors[connectorID].transactionStarted) { + logger.debug(`${self._basicFormatLog()} Trying to start meter values on connector ID ${connectorID} with no transaction`); + } else if (this._connectors[connectorID].transactionStarted && !this._connectors[connectorID].transactionId) { + logger.debug(`${self._basicFormatLog()} Trying to start meter values on connector ID ${connectorID} with no transaction id`); + } this._connectors[connectorID].transactionInterval = setInterval(async () => { const sendMeterValues = performance.timerify(this.sendMeterValues); this._performanceObserver.observe({ @@ -648,9 +637,7 @@ class ChargingStation { this.sendStopTransaction(commandPayload.transactionId, connector); } } - return { - status: 'Accepted', - }; + return Constants.OCPP_RESPONSE_ACCEPTED; } isAuthorizationRequested() {