fix: ensure charging stations are stopped if UI server is disabled at
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / AbstractUIServer.ts
1 import { type IncomingMessage, Server, type ServerResponse } from 'node:http';
2 import { type Http2Server, createServer } from 'node:http2';
3
4 import type { WebSocket } from 'ws';
5
6 import type { AbstractUIService } from './ui-services/AbstractUIService';
7 import { UIServiceFactory } from './ui-services/UIServiceFactory';
8 import { BaseError } from '../../exception';
9 import {
10 ApplicationProtocolVersion,
11 AuthenticationType,
12 type ChargingStationData,
13 type ProcedureName,
14 type ProtocolRequest,
15 type ProtocolResponse,
16 ProtocolVersion,
17 type RequestPayload,
18 type ResponsePayload,
19 type UIServerConfiguration,
20 } from '../../types';
21
22 export abstract class AbstractUIServer {
23 public readonly chargingStations: Map<string, ChargingStationData>;
24 protected readonly httpServer: Server | Http2Server;
25 protected readonly responseHandlers: Map<string, ServerResponse | WebSocket>;
26 protected readonly uiServices: Map<ProtocolVersion, AbstractUIService>;
27
28 public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
29 this.chargingStations = new Map<string, ChargingStationData>();
30 switch (this.uiServerConfiguration.version) {
31 case ApplicationProtocolVersion.VERSION_11:
32 this.httpServer = new Server();
33 break;
34 case ApplicationProtocolVersion.VERSION_20:
35 this.httpServer = createServer();
36 break;
37 default:
38 throw new BaseError(
39 `Unsupported application protocol version ${this.uiServerConfiguration.version}`,
40 );
41 }
42 this.responseHandlers = new Map<string, ServerResponse | WebSocket>();
43 this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
44 }
45
46 public buildProtocolRequest(
47 id: string,
48 procedureName: ProcedureName,
49 requestPayload: RequestPayload,
50 ): ProtocolRequest {
51 return [id, procedureName, requestPayload];
52 }
53
54 public buildProtocolResponse(id: string, responsePayload: ResponsePayload): ProtocolResponse {
55 return [id, responsePayload];
56 }
57
58 public stop(): void {
59 this.stopHttpServer();
60 this.chargingStations.clear();
61 }
62
63 public async sendInternalRequest(request: ProtocolRequest): Promise<ProtocolResponse> {
64 const protocolVersion = ProtocolVersion['0.0.1'];
65 this.registerProtocolVersionUIService(protocolVersion);
66 return this.uiServices
67 .get(protocolVersion)
68 ?.requestHandler(request) as Promise<ProtocolResponse>;
69 }
70
71 public hasResponseHandler(id: string): boolean {
72 return this.responseHandlers.has(id);
73 }
74
75 protected startHttpServer(): void {
76 if (this.httpServer.listening === false) {
77 this.httpServer.listen(this.uiServerConfiguration.options);
78 }
79 }
80
81 protected registerProtocolVersionUIService(version: ProtocolVersion): void {
82 if (this.uiServices.has(version) === false) {
83 this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
84 }
85 }
86
87 protected authenticate(req: IncomingMessage, next: (err?: Error) => void): void {
88 if (this.isBasicAuthEnabled() === true) {
89 if (this.isValidBasicAuth(req) === false) {
90 next(new BaseError('Unauthorized'));
91 }
92 next();
93 }
94 next();
95 }
96
97 private stopHttpServer(): void {
98 if (this.httpServer.listening === true) {
99 this.httpServer.close();
100 }
101 }
102
103 private isBasicAuthEnabled(): boolean {
104 return (
105 this.uiServerConfiguration.authentication?.enabled === true &&
106 this.uiServerConfiguration.authentication?.type === AuthenticationType.BASIC_AUTH
107 );
108 }
109
110 private isValidBasicAuth(req: IncomingMessage): boolean {
111 const authorizationHeader = req.headers.authorization ?? '';
112 const authorizationToken = authorizationHeader.split(/\s+/).pop() ?? '';
113 const authentication = Buffer.from(authorizationToken, 'base64').toString();
114 const authenticationParts = authentication.split(/:/);
115 const username = authenticationParts.shift();
116 const password = authenticationParts.join(':');
117 return (
118 this.uiServerConfiguration.authentication?.username === username &&
119 this.uiServerConfiguration.authentication?.password === password
120 );
121 }
122
123 public abstract start(): void;
124 public abstract sendRequest(request: ProtocolRequest): void;
125 public abstract sendResponse(response: ProtocolResponse): void;
126 public abstract logPrefix(
127 moduleName?: string,
128 methodName?: string,
129 prefixSuffix?: string,
130 ): string;
131 }