UI Server: factor out responses handling logic in abstract class
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / AbstractUIServer.ts
CommitLineData
94dc3080 1import { type IncomingMessage, Server, type ServerResponse } from 'http';
daa6505e
JB
2
3import type { WebSocket } from 'ws';
fe94fce0 4
6c1761d4 5import type { ChargingStationData } from '../../types/ChargingStationWorker';
eb3abc4f
JB
6import type { UIServerConfiguration } from '../../types/ConfigurationData';
7import {
8 AuthenticationType,
852a4c5f
JB
9 ProcedureName,
10 ProtocolRequest,
11 ProtocolResponse,
12 ProtocolVersion,
13 RequestPayload,
14 ResponsePayload,
15} from '../../types/UIProtocol';
db2336d9 16import type AbstractUIService from './ui-services/AbstractUIService';
143498c8 17import UIServiceFactory from './ui-services/UIServiceFactory';
8114d10e 18
fe94fce0 19export abstract class AbstractUIServer {
32de5a57 20 public readonly chargingStations: Map<string, ChargingStationData>;
eb3abc4f 21 protected httpServer: Server;
94dc3080 22 protected responseHandlers: Map<string, ServerResponse | WebSocket>;
5e3cb728 23 protected readonly uiServices: Map<ProtocolVersion, AbstractUIService>;
fe94fce0 24
eb3abc4f 25 public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
32de5a57 26 this.chargingStations = new Map<string, ChargingStationData>();
daa6505e 27 this.httpServer = new Server();
94dc3080 28 this.responseHandlers = new Map<string, ServerResponse | WebSocket>();
fe94fce0
JB
29 this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
30 }
31
852a4c5f
JB
32 public buildProtocolRequest(
33 id: string,
34 procedureName: ProcedureName,
35 requestPayload: RequestPayload
5e3cb728
JB
36 ): ProtocolRequest {
37 return [id, procedureName, requestPayload];
852a4c5f
JB
38 }
39
5e3cb728
JB
40 public buildProtocolResponse(id: string, responsePayload: ResponsePayload): ProtocolResponse {
41 return [id, responsePayload];
852a4c5f
JB
42 }
43
daa6505e
JB
44 public stop(): void {
45 this.chargingStations.clear();
46 }
47
143498c8
JB
48 protected registerProtocolVersionUIService(version: ProtocolVersion): void {
49 if (this.uiServices.has(version) === false) {
50 this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
51 }
52 }
53
eb3abc4f
JB
54 protected isBasicAuthEnabled(): boolean {
55 return (
56 this.uiServerConfiguration.authentication?.enabled === true &&
57 this.uiServerConfiguration.authentication?.type === AuthenticationType.BASIC_AUTH
58 );
59 }
60
61 protected isValidBasicAuth(req: IncomingMessage): boolean {
62 const authorizationHeader = req.headers.authorization ?? '';
63 const authorizationToken = authorizationHeader.split(/\s+/).pop() ?? '';
64 const authentication = Buffer.from(authorizationToken, 'base64').toString();
65 const authenticationParts = authentication.split(/:/);
66 const username = authenticationParts.shift();
67 const password = authenticationParts.join(':');
68 return (
69 this.uiServerConfiguration.authentication?.username === username &&
70 this.uiServerConfiguration.authentication?.password === password
71 );
72 }
73
fe94fce0 74 public abstract start(): void;
5e3cb728
JB
75 public abstract sendRequest(request: ProtocolRequest): void;
76 public abstract sendResponse(response: ProtocolResponse): void;
77 public abstract logPrefix(
78 moduleName?: string,
79 methodName?: string,
80 prefixSuffix?: string
81 ): string;
fe94fce0 82}