UI protocol: cleanup version handling code
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 1 Oct 2022 20:19:10 +0000 (22:19 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 1 Oct 2022 20:19:10 +0000 (22:19 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ChargingStationUtils.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ui-server/UIHttpServer.ts
src/charging-station/ui-server/UIWebSocketServer.ts
src/charging-station/ui-server/ui-services/UIServiceUtils.ts

index 457e3468704553150fb6527f80dabf6cd07a480d..4e11e9e27f806f800577217204b3c9fe1571d8f6 100644 (file)
@@ -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;
index 24556120b4b9db9862dfae478ce3fcb468e2a309..e378fdec87bd26832ce1abf4ebe2cddd53b8a1b9 100644 (file)
@@ -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)
    *
index f951fc77145cceda22e15f77b0f873da8a901e12..7c449ca55e82fadccba0956b95c6ebf4ba953f87 100644 (file)
@@ -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
index 4b911efd96b2d97aaf338fb4dd618e23064a934d..7b777b86040c3cba972d886da3df9a11dbab999a 100644 (file)
@@ -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) => {
index 83d074cf3414edd9afd1d53b3b6abe64adfda735..4ac5d23561abcdd9e25a29a8fd12d22d37ba18d8 100644 (file)
@@ -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);
index a3cb11b14ac9b177a0ca19ff0565ad3b9be5fa5c..b6137886d76c105fda10f5cd667f7c37e7e94dc5 100644 (file)
@@ -11,6 +11,7 @@ export class UIServiceUtils {
 
   public static handleProtocols = (
     protocols: Set<string>,
+    // 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);