UI protocol: add meter values command
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / ui-services / UIServiceUtils.ts
index a824adaf2b052415f8980be108d1424fad8a7e10..c019982854c22d73bcc446ca3a2ecc56caa1aca1 100644 (file)
@@ -13,17 +13,14 @@ export class UIServiceUtils {
     protocols: Set<string>,
     request: IncomingMessage
   ): string | false => {
-    let protocolIndex: number;
     let protocol: Protocol;
     let version: ProtocolVersion;
+    if (protocols.size === 0) {
+      return false;
+    }
     for (const fullProtocol of protocols) {
-      protocolIndex = fullProtocol.indexOf(Protocol.UI);
-      protocol = fullProtocol.substring(
-        protocolIndex,
-        protocolIndex + Protocol.UI.length
-      ) as Protocol;
-      version = fullProtocol.substring(protocolIndex + Protocol.UI.length) as ProtocolVersion;
-      if (UIServiceUtils.isProtocolSupported(protocol, version) === true) {
+      [protocol, version] = UIServiceUtils.getProtocolAndVersion(fullProtocol);
+      if (UIServiceUtils.isProtocolAndVersionSupported(protocol, version) === true) {
         return fullProtocol;
       }
     }
@@ -35,9 +32,22 @@ export class UIServiceUtils {
     return false;
   };
 
-  public static isProtocolSupported = (protocol: Protocol, version: ProtocolVersion): boolean =>
+  public static isProtocolAndVersionSupported = (
+    protocol: Protocol,
+    version: ProtocolVersion
+  ): boolean =>
     Object.values(Protocol).includes(protocol) && Object.values(ProtocolVersion).includes(version);
 
+  public static getProtocolAndVersion = (protocolStr: string): [Protocol, ProtocolVersion] => {
+    const protocolIndex = protocolStr.indexOf(Protocol.UI);
+    const protocol = protocolStr.substring(
+      protocolIndex,
+      protocolIndex + Protocol.UI.length
+    ) as Protocol;
+    const version = protocolStr.substring(protocolIndex + Protocol.UI.length) as ProtocolVersion;
+    return [protocol, version];
+  };
+
   public static isLoopback(address: string): boolean {
     const isLoopbackRegExp = new RegExp(
       // eslint-disable-next-line no-useless-escape