Move the UI WS protocol version handling is protocol header
[e-mobility-charging-stations-simulator.git] / src / charging-station / UIWebSocketServices / UIServiceUtils.ts
CommitLineData
4198ad5c
JB
1import { Protocol, ProtocolVersion } from '../../types/UIProtocol';
2
3import { IncomingMessage } from 'http';
4import Utils from '../../utils/Utils';
5import logger from '../../utils/Logger';
6
7export class UIServiceUtils {
8 public static handleProtocols = (protocols: Set<string>, request: IncomingMessage): string | false => {
9 let protocolIndex: number;
10 let protocol: Protocol;
11 let version: ProtocolVersion;
12 for (const fullProtocol of protocols) {
13 protocolIndex = fullProtocol.indexOf(Protocol.UI);
14 protocol = fullProtocol.substring(protocolIndex, protocolIndex + Protocol.UI.length) as Protocol;
15 version = fullProtocol.substring(protocolIndex + Protocol.UI.length) as ProtocolVersion;
16 if (Object.values(Protocol).includes(protocol) && Object.values(ProtocolVersion).includes(version)) {
17 return fullProtocol;
18 }
19 }
20 logger.error(`${Utils.logPrefix('WebSocket Server:')} Unsupported protocol: ${protocol} or protocol version: ${version}`);
21 return false;
22 };
23}