From 143498c8a5507fbf23c13a032cf9c5ebf285675d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 6 Sep 2022 23:02:23 +0200 Subject: [PATCH] UI server: factor services registration MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ui-server/AbstractUIServer.ts | 7 +++++++ src/charging-station/ui-server/UIHttpServer.ts | 5 +---- src/charging-station/ui-server/UIWebSocketServer.ts | 5 +---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/charging-station/ui-server/AbstractUIServer.ts b/src/charging-station/ui-server/AbstractUIServer.ts index a55d0070..48151dc4 100644 --- a/src/charging-station/ui-server/AbstractUIServer.ts +++ b/src/charging-station/ui-server/AbstractUIServer.ts @@ -12,6 +12,7 @@ import { ResponsePayload, } from '../../types/UIProtocol'; import type AbstractUIService from './ui-services/AbstractUIService'; +import UIServiceFactory from './ui-services/UIServiceFactory'; export abstract class AbstractUIServer { public readonly chargingStations: Map; @@ -35,6 +36,12 @@ export abstract class AbstractUIServer { return [id, responsePayload]; } + protected registerProtocolVersionUIService(version: ProtocolVersion): void { + if (this.uiServices.has(version) === false) { + this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this)); + } + } + protected isBasicAuthEnabled(): boolean { return ( this.uiServerConfiguration.authentication?.enabled === true && diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index ce52ed4b..4db72bf7 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -16,7 +16,6 @@ import { import logger from '../../utils/Logger'; import Utils from '../../utils/Utils'; import { AbstractUIServer } from './AbstractUIServer'; -import UIServiceFactory from './ui-services/UIServiceFactory'; import { UIServiceUtils } from './ui-services/UIServiceUtils'; const moduleName = 'UIHttpServer'; @@ -97,9 +96,7 @@ export default class UIHttpServer extends AbstractUIServer { error ); }); - if (this.uiServices.has(version) === false) { - this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this)); - } + this.registerProtocolVersionUIService(version); if (req.method === 'POST') { const bodyBuffer = []; req diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index ea54bec0..f789d396 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -11,7 +11,6 @@ import { WebSocketCloseEventStatusCode } from '../../types/WebSocket'; import logger from '../../utils/Logger'; import Utils from '../../utils/Utils'; import { AbstractUIServer } from './AbstractUIServer'; -import UIServiceFactory from './ui-services/UIServiceFactory'; import { UIServiceUtils } from './ui-services/UIServiceUtils'; const moduleName = 'UIWebSocketServer'; @@ -40,9 +39,7 @@ export default class UIWebSocketServer extends AbstractUIServer { ); ws.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR); } - if (this.uiServices.has(version) === false) { - this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this)); - } + this.registerProtocolVersionUIService(version); ws.on('message', (rawData) => { const request = this.validateRawDataRequest(rawData); if (request === false) { -- 2.34.1