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