UI Server: fix write after end
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / AbstractUIServer.ts
1 import { type IncomingMessage, Server } from 'http';
2 import type { Socket } from 'net';
3
4 import type { WebSocket } from 'ws';
5
6 import type { ChargingStationData } from '../../types/ChargingStationWorker';
7 import type { UIServerConfiguration } from '../../types/ConfigurationData';
8 import {
9 AuthenticationType,
10 ProcedureName,
11 ProtocolRequest,
12 ProtocolResponse,
13 ProtocolVersion,
14 RequestPayload,
15 ResponsePayload,
16 } from '../../types/UIProtocol';
17 import type AbstractUIService from './ui-services/AbstractUIService';
18 import UIServiceFactory from './ui-services/UIServiceFactory';
19
20 export abstract class AbstractUIServer {
21 public readonly chargingStations: Map<string, ChargingStationData>;
22 protected httpServer: Server;
23 protected sockets: Set<Socket | WebSocket>;
24 protected readonly uiServices: Map<ProtocolVersion, AbstractUIService>;
25
26 public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
27 this.chargingStations = new Map<string, ChargingStationData>();
28 this.httpServer = new Server();
29 this.sockets = new Set<Socket>();
30 this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
31 }
32
33 public buildProtocolRequest(
34 id: string,
35 procedureName: ProcedureName,
36 requestPayload: RequestPayload
37 ): ProtocolRequest {
38 return [id, procedureName, requestPayload];
39 }
40
41 public buildProtocolResponse(id: string, responsePayload: ResponsePayload): ProtocolResponse {
42 return [id, responsePayload];
43 }
44
45 public stop(): void {
46 this.chargingStations.clear();
47 }
48
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
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
75 public abstract start(): void;
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;
83 }