From 7cb5b17fac198fe3ecd009ac4c692c0d88dd051e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 1 Oct 2022 22:19:10 +0200 Subject: [PATCH] UI protocol: cleanup version handling code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 31 ---------------- src/charging-station/ChargingStationUtils.ts | 35 +++++++++++++++++++ .../ocpp/1.6/OCPP16IncomingRequestService.ts | 5 +-- .../ui-server/UIHttpServer.ts | 5 +-- .../ui-server/UIWebSocketServer.ts | 6 ++-- .../ui-server/ui-services/UIServiceUtils.ts | 17 ++++----- 6 files changed, 53 insertions(+), 46 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 457e3468..4e11e9e2 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -579,37 +579,6 @@ export default class ChargingStation { } } - public setChargingProfile(connectorId: number, cp: ChargingProfile): void { - if (Utils.isNullOrUndefined(this.getConnectorStatus(connectorId).chargingProfiles)) { - logger.error( - `${this.logPrefix()} Trying to set a charging profile on connectorId ${connectorId} with an uninitialized charging profiles array attribute, applying deferred initialization` - ); - this.getConnectorStatus(connectorId).chargingProfiles = []; - } - if (Array.isArray(this.getConnectorStatus(connectorId).chargingProfiles) === false) { - logger.error( - `${this.logPrefix()} Trying to set a charging profile on connectorId ${connectorId} with an improper attribute type for the charging profiles array, applying proper type initialization` - ); - this.getConnectorStatus(connectorId).chargingProfiles = []; - } - let cpReplaced = false; - if (!Utils.isEmptyArray(this.getConnectorStatus(connectorId).chargingProfiles)) { - this.getConnectorStatus(connectorId).chargingProfiles?.forEach( - (chargingProfile: ChargingProfile, index: number) => { - if ( - chargingProfile.chargingProfileId === cp.chargingProfileId || - (chargingProfile.stackLevel === cp.stackLevel && - chargingProfile.chargingProfilePurpose === cp.chargingProfilePurpose) - ) { - this.getConnectorStatus(connectorId).chargingProfiles[index] = cp; - cpReplaced = true; - } - } - ); - } - !cpReplaced && this.getConnectorStatus(connectorId).chargingProfiles?.push(cp); - } - public resetConnectorStatus(connectorId: number): void { this.getConnectorStatus(connectorId).idTagLocalAuthorized = false; this.getConnectorStatus(connectorId).idTagAuthorized = false; diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 24556120..e378fdec 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -289,6 +289,41 @@ export class ChargingStationUtils { return unitDivider; } + public static setChargingProfile( + chargingStation: ChargingStation, + connectorId: number, + cp: ChargingProfile + ): void { + if (Utils.isNullOrUndefined(chargingStation.getConnectorStatus(connectorId).chargingProfiles)) { + logger.error( + `${chargingStation.logPrefix()} Trying to set a charging profile on connectorId ${connectorId} with an uninitialized charging profiles array attribute, applying deferred initialization` + ); + chargingStation.getConnectorStatus(connectorId).chargingProfiles = []; + } + if (Array.isArray(chargingStation.getConnectorStatus(connectorId).chargingProfiles) === false) { + logger.error( + `${chargingStation.logPrefix()} Trying to set a charging profile on connectorId ${connectorId} with an improper attribute type for the charging profiles array, applying proper type initialization` + ); + chargingStation.getConnectorStatus(connectorId).chargingProfiles = []; + } + let cpReplaced = false; + if (!Utils.isEmptyArray(chargingStation.getConnectorStatus(connectorId).chargingProfiles)) { + chargingStation + .getConnectorStatus(connectorId) + .chargingProfiles?.forEach((chargingProfile: ChargingProfile, index: number) => { + if ( + chargingProfile.chargingProfileId === cp.chargingProfileId || + (chargingProfile.stackLevel === cp.stackLevel && + chargingProfile.chargingProfilePurpose === cp.chargingProfilePurpose) + ) { + chargingStation.getConnectorStatus(connectorId).chargingProfiles[index] = cp; + cpReplaced = true; + } + }); + } + !cpReplaced && chargingStation.getConnectorStatus(connectorId).chargingProfiles?.push(cp); + } + /** * Charging profiles should already be sorted by connectorId and stack level (highest stack level has priority) * diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index f951fc77..7c449ca5 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -569,7 +569,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ) { return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; } - chargingStation.setChargingProfile( + ChargingStationUtils.setChargingProfile( + chargingStation, commandPayload.connectorId, commandPayload.csChargingProfiles ); @@ -911,7 +912,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer cp: OCPP16ChargingProfile ): boolean { if (cp && cp.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE) { - chargingStation.setChargingProfile(connectorId, cp); + ChargingStationUtils.setChargingProfile(chargingStation, connectorId, cp); logger.debug( `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}, dump their stack: %j`, chargingStation.getConnectorStatus(connectorId).chargingProfiles diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index 4b911efd..7b777b86 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -89,8 +89,9 @@ export default class UIHttpServer extends AbstractUIServer { const uuid = Utils.generateUUID(); this.responseHandlers.set(uuid, res); try { - if (UIServiceUtils.isProtocolAndVersionSupported(protocol, version) === false) { - throw new BaseError(`Unsupported UI protocol version: '/${protocol}/${version}'`); + const fullProtocol = `${protocol}${version}`; + if (UIServiceUtils.isProtocolAndVersionSupported(fullProtocol) === false) { + throw new BaseError(`Unsupported UI protocol version: '${fullProtocol}'`); } this.registerProtocolVersionUIService(version); req.on('error', (error) => { diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index 83d074cf..4ac5d235 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -28,16 +28,16 @@ export default class UIWebSocketServer extends AbstractUIServer { public start(): void { this.webSocketServer.on('connection', (ws: WebSocket, req: IncomingMessage): void => { - const [protocol, version] = UIServiceUtils.getProtocolAndVersion(ws.protocol); - if (UIServiceUtils.isProtocolAndVersionSupported(protocol, version) === false) { + if (UIServiceUtils.isProtocolAndVersionSupported(ws.protocol) === false) { logger.error( `${this.logPrefix( moduleName, 'start.server.onconnection' - )} Unsupported UI protocol version: '${protocol}${version}'` + )} Unsupported UI protocol version: '${ws.protocol}'` ); ws.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR); } + const [, version] = UIServiceUtils.getProtocolAndVersion(ws.protocol); this.registerProtocolVersionUIService(version); ws.on('message', (rawData) => { const request = this.validateRawDataRequest(rawData); diff --git a/src/charging-station/ui-server/ui-services/UIServiceUtils.ts b/src/charging-station/ui-server/ui-services/UIServiceUtils.ts index a3cb11b1..b6137886 100644 --- a/src/charging-station/ui-server/ui-services/UIServiceUtils.ts +++ b/src/charging-station/ui-server/ui-services/UIServiceUtils.ts @@ -11,6 +11,7 @@ export class UIServiceUtils { public static handleProtocols = ( protocols: Set, + // eslint-disable-next-line @typescript-eslint/no-unused-vars request: IncomingMessage ): string | false => { let protocol: Protocol; @@ -19,8 +20,7 @@ export class UIServiceUtils { return false; } for (const fullProtocol of protocols) { - [protocol, version] = UIServiceUtils.getProtocolAndVersion(fullProtocol); - if (UIServiceUtils.isProtocolAndVersionSupported(protocol, version) === true) { + if (UIServiceUtils.isProtocolAndVersionSupported(fullProtocol) === true) { return fullProtocol; } } @@ -32,12 +32,13 @@ export class UIServiceUtils { return false; }; - public static isProtocolAndVersionSupported = ( - protocol: Protocol, - version: ProtocolVersion - ): boolean => - Object.values(Protocol).includes(protocol) === true && - Object.values(ProtocolVersion).includes(version) === true; + public static isProtocolAndVersionSupported = (protocolStr: string): boolean => { + const [protocol, version] = UIServiceUtils.getProtocolAndVersion(protocolStr); + return ( + Object.values(Protocol).includes(protocol) === true && + Object.values(ProtocolVersion).includes(version) === true + ); + }; public static getProtocolAndVersion = (protocolStr: string): [Protocol, ProtocolVersion] => { const protocolIndex = protocolStr.indexOf(Protocol.UI); -- 2.34.1