From 852a4c5f07886502e278d44fda06efdfa8c711d8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 27 Aug 2022 12:09:23 +0200 Subject: [PATCH] UI Server: dedupe some code in helpers MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- rollup.config.mjs | 1 + .../ChargingStationWorkerBroadcastChannel.ts | 4 +--- .../UIServiceWorkerBroadcastChannel.ts | 5 +---- .../WorkerBroadcastChannel.ts | 13 ++++++++++- .../ui-server/AbstractUIServer.ts | 21 +++++++++++++++++- .../ui-server/UIHttpServer.ts | 22 +++++-------------- .../ui-services/AbstractUIService.ts | 19 ++++------------ 7 files changed, 44 insertions(+), 41 deletions(-) diff --git a/rollup.config.mjs b/rollup.config.mjs index 2dc4f5c7..effe368d 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -43,6 +43,7 @@ export default { 'crypto', 'fs', 'http', + 'http-status-codes', 'mnemonist/lru-map-with-delete', 'moment', 'mongodb', diff --git a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts index 9de2cfb2..42448275 100644 --- a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts @@ -38,9 +38,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca if (this.isResponse(messageEvent.data)) { return; } - if (Array.isArray(messageEvent.data) === false) { - throw new BaseError('Worker broadcast channel protocol request is not an array'); - } + this.validateMessageEvent(messageEvent); const [uuid, command, requestPayload] = messageEvent.data as BroadcastChannelRequest; diff --git a/src/charging-station/UIServiceWorkerBroadcastChannel.ts b/src/charging-station/UIServiceWorkerBroadcastChannel.ts index a94f2b31..3916f626 100644 --- a/src/charging-station/UIServiceWorkerBroadcastChannel.ts +++ b/src/charging-station/UIServiceWorkerBroadcastChannel.ts @@ -1,4 +1,3 @@ -import BaseError from '../exception/BaseError'; import { BroadcastChannelResponse, MessageEvent } from '../types/WorkerBroadcastChannel'; import logger from '../utils/Logger'; import type AbstractUIService from './ui-server/ui-services/AbstractUIService'; @@ -20,9 +19,7 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan if (this.isRequest(messageEvent.data)) { return; } - if (Array.isArray(messageEvent.data) === false) { - throw new BaseError('Worker broadcast channel protocol response is not an array'); - } + this.validateMessageEvent(messageEvent); const [uuid, responsePayload] = messageEvent.data as BroadcastChannelResponse; this.uiService.sendResponse(uuid, responsePayload); diff --git a/src/charging-station/WorkerBroadcastChannel.ts b/src/charging-station/WorkerBroadcastChannel.ts index 12a877c1..ea89dc3d 100644 --- a/src/charging-station/WorkerBroadcastChannel.ts +++ b/src/charging-station/WorkerBroadcastChannel.ts @@ -1,6 +1,11 @@ import { BroadcastChannel } from 'worker_threads'; -import { BroadcastChannelRequest, BroadcastChannelResponse } from '../types/WorkerBroadcastChannel'; +import BaseError from '../exception/BaseError'; +import { + BroadcastChannelRequest, + BroadcastChannelResponse, + MessageEvent, +} from '../types/WorkerBroadcastChannel'; export default abstract class WorkerBroadcastChannel extends BroadcastChannel { protected constructor() { @@ -22,4 +27,10 @@ export default abstract class WorkerBroadcastChannel extends BroadcastChannel { protected isResponse(message: any): boolean { return Array.isArray(message) && message.length === 2; } + + protected validateMessageEvent(messageEvent: MessageEvent): void { + if (Array.isArray(messageEvent.data) === false) { + throw new BaseError('Worker broadcast channel protocol message event data is not an array'); + } + } } diff --git a/src/charging-station/ui-server/AbstractUIServer.ts b/src/charging-station/ui-server/AbstractUIServer.ts index 65847d85..b8e7c46c 100644 --- a/src/charging-station/ui-server/AbstractUIServer.ts +++ b/src/charging-station/ui-server/AbstractUIServer.ts @@ -3,7 +3,14 @@ import { Server as HttpServer } from 'http'; import WebSocket from 'ws'; import { ChargingStationData } from '../../types/ChargingStationWorker'; -import { ProtocolVersion } from '../../types/UIProtocol'; +import { + ProcedureName, + ProtocolRequest, + ProtocolResponse, + ProtocolVersion, + RequestPayload, + ResponsePayload, +} from '../../types/UIProtocol'; import type AbstractUIService from './ui-services/AbstractUIService'; export abstract class AbstractUIServer { @@ -16,6 +23,18 @@ export abstract class AbstractUIServer { this.uiServices = new Map(); } + public buildProtocolRequest( + id: string, + procedureName: ProcedureName, + requestPayload: RequestPayload + ): string { + return JSON.stringify([id, procedureName, requestPayload] as ProtocolRequest); + } + + public buildProtocolResponse(id: string, responsePayload: ResponsePayload): string { + return JSON.stringify([id, responsePayload] as ProtocolResponse); + } + public abstract start(): void; public abstract stop(): void; public abstract sendRequest(request: string): void; diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index a9b62063..6fb8a6ad 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -7,11 +7,9 @@ import { ServerOptions } from '../../types/ConfigurationData'; import { ProcedureName, Protocol, - ProtocolRequest, ProtocolResponse, ProtocolVersion, RequestPayload, - ResponsePayload, ResponseStatus, } from '../../types/UIProtocol'; import Configuration from '../../utils/Configuration'; @@ -101,9 +99,11 @@ export default class UIHttpServer extends AbstractUIServer { const body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload; this.uiServices .get(version) - .requestHandler(this.buildRequest(uuid, procedureName, body ?? {})) + .requestHandler(this.buildProtocolRequest(uuid, procedureName, body ?? {})) .catch(() => { - this.sendResponse(this.buildResponse(uuid, { status: ResponseStatus.FAILURE })); + this.sendResponse( + this.buildProtocolResponse(uuid, { status: ResponseStatus.FAILURE }) + ); }); }); } else { @@ -114,22 +114,10 @@ export default class UIHttpServer extends AbstractUIServer { `${this.logPrefix(moduleName, 'requestListener')} Handle HTTP request error:`, error ); - this.sendResponse(this.buildResponse(uuid, { status: ResponseStatus.FAILURE })); + this.sendResponse(this.buildProtocolResponse(uuid, { status: ResponseStatus.FAILURE })); } } - private buildRequest( - id: string, - procedureName: ProcedureName, - requestPayload: RequestPayload - ): string { - return JSON.stringify([id, procedureName, requestPayload] as ProtocolRequest); - } - - private buildResponse(id: string, responsePayload: ResponsePayload): string { - return JSON.stringify([id, responsePayload] as ProtocolResponse); - } - private responseStatusToStatusCode(status: ResponseStatus): StatusCodes { switch (status) { case ResponseStatus.SUCCESS: diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index 24307992..2c541cde 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -7,7 +7,6 @@ import { ProcedureName, ProtocolRequest, ProtocolRequestHandler, - ProtocolResponse, ProtocolVersion, RequestPayload, ResponsePayload, @@ -80,29 +79,19 @@ export default abstract class AbstractUIService { procedureName: ProcedureName, requestPayload: RequestPayload ): void { - this.uiServer.sendRequest(this.buildProtocolRequest(messageId, procedureName, requestPayload)); + this.uiServer.sendRequest( + this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload) + ); } public sendResponse(messageId: string, responsePayload: ResponsePayload): void { - this.uiServer.sendResponse(this.buildProtocolResponse(messageId, responsePayload)); + this.uiServer.sendResponse(this.uiServer.buildProtocolResponse(messageId, responsePayload)); } public logPrefix(modName: string, methodName: string): string { return this.uiServer.logPrefix(modName, methodName); } - private buildProtocolRequest( - messageId: string, - procedureName: ProcedureName, - requestPayload: RequestPayload - ): string { - return JSON.stringify([messageId, procedureName, requestPayload] as ProtocolRequest); - } - - private buildProtocolResponse(messageId: string, responsePayload: ResponsePayload): string { - return JSON.stringify([messageId, responsePayload] as ProtocolResponse); - } - // Validate the raw data received from the UI server private requestValidation(rawData: RawData | JsonType): ProtocolRequest { // logger.debug( -- 2.34.1