From 63b48f776a09ac5a5ffb5b161c108e3ca625a3cb Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 13 Nov 2020 10:45:26 +0100 Subject: [PATCH] Fix message buffer handling. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit And typing. Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 18 ++++++++++-------- ...dStatisticsData.ts => CommandStatistics.ts} | 6 +++++- src/types/ocpp/1.6/RequestResponses.ts | 4 ++-- src/types/ocpp/1.6/Requests.ts | 5 +++++ src/utils/Statistics.ts | 9 ++++----- 5 files changed, 26 insertions(+), 16 deletions(-) rename src/types/{CommandStatisticsData.ts => CommandStatistics.ts} (56%) create mode 100644 src/types/ocpp/1.6/Requests.ts 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) { -- 2.34.1