From: Jérôme Benoit Date: Fri, 13 Nov 2020 09:45:26 +0000 (+0100) Subject: Fix message buffer handling. X-Git-Tag: v1.0.1-0~201 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=63b48f776a09ac5a5ffb5b161c108e3ca625a3cb;p=e-mobility-charging-stations-simulator.git Fix message buffer handling. And typing. Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 18013db4..85a944b5 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(); } @@ -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); } }); diff --git a/src/types/CommandStatisticsData.ts b/src/types/CommandStatistics.ts similarity index 56% rename from src/types/CommandStatisticsData.ts rename to src/types/CommandStatistics.ts index edc92cc0..7a4579b7 100644 --- a/src/types/CommandStatisticsData.ts +++ b/src/types/CommandStatistics.ts @@ -1,4 +1,4 @@ -export default interface CommandStatisticsData { +export interface CommandStatisticsData { countRequest: number; countResponse: number; countError: number; @@ -8,3 +8,7 @@ export default interface CommandStatisticsData { totalTime: number; avgTime: number; } + +export default interface CommandStatistics { + [command: string]: CommandStatisticsData; +} diff --git a/src/types/ocpp/1.6/RequestResponses.ts b/src/types/ocpp/1.6/RequestResponses.ts index 95b00eb9..68bb86f8 100644 --- a/src/types/ocpp/1.6/RequestResponses.ts +++ b/src/types/ocpp/1.6/RequestResponses.ts @@ -4,7 +4,7 @@ export enum DefaultResponseStatus { } export interface DefaultRequestResponse { - status: DefaultResponseStatus + status: DefaultResponseStatus; } export enum UnlockStatus { @@ -25,5 +25,5 @@ export enum ConfigurationStatus { } export interface ConfigurationResponse { - status: ConfigurationStatus + status: ConfigurationStatus; } diff --git a/src/types/ocpp/1.6/Requests.ts b/src/types/ocpp/1.6/Requests.ts new file mode 100644 index 00000000..78123f87 --- /dev/null +++ b/src/types/ocpp/1.6/Requests.ts @@ -0,0 +1,5 @@ +import OCPPError from '../../../charging-station/OcppError'; + +export default interface Requests { + [id: string]: [(payload?, requestPayload?) => void, (error?: OCPPError) => void, object]; +} diff --git a/src/utils/Statistics.ts b/src/utils/Statistics.ts index 0f0674d1..ceef29d9 100644 --- a/src/utils/Statistics.ts +++ b/src/utils/Statistics.ts @@ -1,4 +1,5 @@ -import CommandStatisticsData from '../types/CommandStatisticsData'; +import CommandStatistics, { CommandStatisticsData } from '../types/CommandStatistics'; + import Configuration from './Configuration'; import Constants from './Constants'; import { PerformanceEntry } from 'perf_hooks'; @@ -8,12 +9,10 @@ import logger from './Logger'; export default class Statistics { private static instance: Statistics; private _objName: string; - private _commandsStatistics: { - [command: string]: CommandStatisticsData - }; + private _commandsStatistics: CommandStatistics; private constructor() { - this._commandsStatistics = {}; + this._commandsStatistics = {} as CommandStatistics; } set objName(objName: string) {