X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStation.ts;h=e509c0288118e0b59c7380a4e78f8680e0af6fc6;hb=edfb206c5a6a309cbe5a8e6bf3c3f52ab57d96fc;hp=d783b281406eade406906c95389f1894c9b4a2c7;hpb=524d9cb333fd690edb702c88517f79b3f3d6813e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index d783b281..e509c028 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -11,10 +11,11 @@ import { ChargePointErrorCode } from '../types/ocpp/1.6/ChargePointErrorCode'; import { ChargePointStatus } from '../types/ocpp/1.6/ChargePointStatus'; import ChargingStationInfo from '../types/ChargingStationInfo'; import Configuration from '../utils/Configuration'; -import Constants from '../utils/Constants.js'; +import Constants from '../utils/Constants'; import ElectricUtils from '../utils/ElectricUtils'; import MeasurandValues from '../types/MeasurandValues'; -import OCPPError from './OcppError.js'; +import OCPPError from './OcppError'; +import Requests from '../types/ocpp/1.6/Requests'; import Statistics from '../utils/Statistics'; import Utils from '../utils/Utils'; import WebSocket from 'ws'; @@ -44,8 +45,8 @@ export default class ChargingStation { private _autoReconnectRetryCount: number; private _autoReconnectMaxRetries: number; private _autoReconnectTimeout: number; - private _requests: { [id: string]: [(payload?, requestPayload?) => void, (error?: OCPPError) => void, object] }; - private _messageQueue: any[]; + private _requests: Requests; + private _messageQueue: string[]; private _automaticTransactionGeneration: AutomaticTransactionGenerator; private _authorizedTags: string[]; private _heartbeatInterval: number; @@ -56,7 +57,7 @@ export default class ChargingStation { constructor(index: number, stationTemplateFile: string) { this._index = index; this._stationTemplateFile = stationTemplateFile; - this._connectors = {}; + this._connectors = {} as Connectors; this._initialize(); this._hasStopped = false; @@ -65,8 +66,8 @@ export default class ChargingStation { this._autoReconnectMaxRetries = Configuration.getAutoReconnectMaxRetries(); // -1 for unlimited this._autoReconnectTimeout = Configuration.getAutoReconnectTimeout() * 1000; // Ms, zero for disabling - this._requests = {}; - this._messageQueue = []; + this._requests = {} as Requests; + this._messageQueue = [] as string[]; this._authorizedTags = this._loadAndGetAuthorizedTags(); } @@ -487,7 +488,7 @@ export default class ChargingStation { } _reconnect(error): void { - logger.error(this._logPrefix() + ' Socket: abnormally closed %j', error); + logger.error(this._logPrefix() + ' Socket: abnormally closed: %j', error); // Stop the ATG if needed if (this._stationInfo.AutomaticTransactionGenerator.enable && this._stationInfo.AutomaticTransactionGenerator.stopOnConnectionFailure && @@ -519,8 +520,9 @@ export default class ChargingStation { if (this._hasSocketRestarted) { this._startMessageSequence(); if (!Utils.isEmptyArray(this._messageQueue)) { - this._messageQueue.forEach((message) => { + this._messageQueue.forEach((message, index) => { if (this._wsConnection && this._wsConnection.readyState === WebSocket.OPEN) { + this._messageQueue.splice(index, 1); this._wsConnection.send(message); } }); @@ -530,28 +532,28 @@ export default class ChargingStation { this._hasSocketRestarted = false; } - onError(error): void { - switch (error) { + onError(errorEvent): void { + switch (errorEvent) { case 'ECONNREFUSED': this._hasSocketRestarted = true; - this._reconnect(error); + this._reconnect(errorEvent); break; default: - logger.error(this._logPrefix() + ' Socket error: %j', error); + logger.error(this._logPrefix() + ' Socket error: %j', errorEvent); break; } } - onClose(error): void { - switch (error) { + onClose(closeEvent): void { + switch (closeEvent) { case 1000: // Normal close case 1005: - logger.info(this._logPrefix() + ' Socket normally closed %j', error); + logger.info(this._logPrefix() + ' Socket normally closed: %j', closeEvent); this._autoReconnectRetryCount = 0; break; default: // Abnormal close this._hasSocketRestarted = true; - this._reconnect(error); + this._reconnect(closeEvent); break; } } @@ -560,11 +562,11 @@ export default class ChargingStation { logger.debug(this._logPrefix() + ' Has received a WS ping (rfc6455) from the server'); } - async onMessage(message): Promise { + async onMessage(messageEvent): Promise { let [messageType, messageId, commandName, commandPayload, errorDetails] = [0, '', Constants.ENTITY_CHARGING_STATION, '', '']; try { // Parse the message - [messageType, messageId, commandName, commandPayload, errorDetails] = JSON.parse(message); + [messageType, messageId, commandName, commandPayload, errorDetails] = JSON.parse(messageEvent); // Check the Type of message switch (messageType) { @@ -618,7 +620,7 @@ export default class ChargingStation { } } catch (error) { // Log - logger.error('%s Incoming message %j processing error %s on request content type %s', this._logPrefix(), message, error, this._requests[messageId]); + logger.error('%s Incoming message %j processing error %s on request content type %s', this._logPrefix(), messageEvent, error, this._requests[messageId]); // Send error await this.sendError(messageId, error, commandName); } @@ -998,7 +1000,7 @@ export default class ChargingStation { if (self.getEnableStatistics()) { self._statistics.addMessage(commandName, messageType); } - logger.debug(`${self._logPrefix()} Error %j occurred when calling command %s with parameters %j`, error, commandName, commandParams); + logger.debug(`${self._logPrefix()} Error: %j occurred when calling command %s with parameters: %j`, error, commandName, commandParams); // Build Exception // eslint-disable-next-line no-empty-function self._requests[messageId] = [() => { }, () => { }, {}]; // Properly format the request @@ -1048,7 +1050,7 @@ export default class ChargingStation { handleResponseStartTransaction(payload: StartTransactionResponse, requestPayload): void { const connectorId = Utils.convertToInt(requestPayload.connectorId); if (this.getConnector(connectorId).transactionStarted) { - logger.debug(this._logPrefix() + ' Try to start a transaction on an already used connector ' + connectorId.toString() + ': %j', this.getConnector(connectorId)); + logger.debug(this._logPrefix() + ' Trying to start a transaction on an already used connector ' + connectorId.toString() + ': %j', this.getConnector(connectorId)); return; } @@ -1060,7 +1062,7 @@ export default class ChargingStation { } } if (!transactionConnectorId) { - logger.error(this._logPrefix() + ' Try to start a transaction on a non existing connector Id ' + connectorId.toString()); + logger.error(this._logPrefix() + ' Trying to start a transaction on a non existing connector Id ' + connectorId.toString()); return; } if (payload.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { @@ -1092,7 +1094,7 @@ export default class ChargingStation { } } if (!transactionConnectorId) { - logger.error(this._logPrefix() + ' Try to stop a non existing transaction ' + requestPayload.transactionId); + logger.error(this._logPrefix() + ' Trying to stop a non existing transaction ' + requestPayload.transactionId); return; } if (payload.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { @@ -1156,7 +1158,7 @@ export default class ChargingStation { async handleRequestUnlockConnector(commandPayload): Promise { const connectorId = Utils.convertToInt(commandPayload.connectorId); if (connectorId === 0) { - logger.error(this._logPrefix() + ' Try to unlock connector ' + connectorId.toString()); + logger.error(this._logPrefix() + ' Trying to unlock connector ' + connectorId.toString()); return Constants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED; } if (this.getConnector(connectorId).transactionStarted) { @@ -1297,7 +1299,7 @@ export default class ChargingStation { return Constants.OCPP_RESPONSE_ACCEPTED; } } - logger.info(this._logPrefix() + ' Try to stop remotely a non existing transaction ' + transactionId.toString()); + logger.info(this._logPrefix() + ' Trying to remote stop a non existing transaction ' + transactionId.toString()); return Constants.OCPP_RESPONSE_REJECTED; } }