build(deps-dev): apply updates
[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.js'
7 import { UIServiceFactory } from './ui-services/UIServiceFactory.js'
8 import { BaseError } from '../../exception/index.js'
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/index.js'
21
22 export abstract class AbstractUIServer {
23 public readonly chargingStations: Map<string, ChargingStationData>
24 public readonly chargingStationTemplates: Set<string>
25 protected readonly httpServer: Server | Http2Server
26 protected readonly responseHandlers: Map<string, ServerResponse | WebSocket>
27 protected readonly uiServices: Map<ProtocolVersion, AbstractUIService>
28
29 public constructor (protected readonly uiServerConfiguration: UIServerConfiguration) {
30 this.chargingStations = new Map<string, ChargingStationData>()
31 this.chargingStationTemplates = new Set<string>()
32 switch (this.uiServerConfiguration.version) {
33 case ApplicationProtocolVersion.VERSION_11:
34 this.httpServer = new Server()
35 break
36 case ApplicationProtocolVersion.VERSION_20:
37 this.httpServer = createServer()
38 break
39 default:
40 throw new BaseError(
41 `Unsupported application protocol version ${this.uiServerConfiguration.version}`
42 )
43 }
44 this.responseHandlers = new Map<string, ServerResponse | WebSocket>()
45 this.uiServices = new Map<ProtocolVersion, AbstractUIService>()
46 }
47
48 public buildProtocolRequest (
49 id: string,
50 procedureName: ProcedureName,
51 requestPayload: RequestPayload
52 ): ProtocolRequest {
53 return [id, procedureName, requestPayload]
54 }
55
56 public buildProtocolResponse (id: string, responsePayload: ResponsePayload): ProtocolResponse {
57 return [id, responsePayload]
58 }
59
60 public stop (): void {
61 this.stopHttpServer()
62 this.chargingStations.clear()
63 this.chargingStationTemplates.clear()
64 }
65
66 public async sendInternalRequest (request: ProtocolRequest): Promise<ProtocolResponse> {
67 const protocolVersion = ProtocolVersion['0.0.1']
68 this.registerProtocolVersionUIService(protocolVersion)
69 return await (this.uiServices
70 .get(protocolVersion)
71 ?.requestHandler(request) as Promise<ProtocolResponse>)
72 }
73
74 public hasResponseHandler (id: string): boolean {
75 return this.responseHandlers.has(id)
76 }
77
78 protected startHttpServer (): void {
79 if (!this.httpServer.listening) {
80 this.httpServer.listen(this.uiServerConfiguration.options)
81 }
82 }
83
84 protected registerProtocolVersionUIService (version: ProtocolVersion): void {
85 if (!this.uiServices.has(version)) {
86 this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this))
87 }
88 }
89
90 protected authenticate (req: IncomingMessage, next: (err?: Error) => void): void {
91 if (this.isBasicAuthEnabled()) {
92 if (!this.isValidBasicAuth(req)) {
93 next(new BaseError('Unauthorized'))
94 }
95 next()
96 }
97 next()
98 }
99
100 private stopHttpServer (): void {
101 if (this.httpServer.listening) {
102 this.httpServer.close()
103 }
104 }
105
106 private isBasicAuthEnabled (): boolean {
107 return (
108 this.uiServerConfiguration.authentication?.enabled === true &&
109 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
110 this.uiServerConfiguration.authentication.type === AuthenticationType.BASIC_AUTH
111 )
112 }
113
114 private isValidBasicAuth (req: IncomingMessage): boolean {
115 const authorizationHeader = req.headers.authorization ?? ''
116 const authorizationToken = authorizationHeader.split(/\s+/).pop() ?? ''
117 const authentication = Buffer.from(authorizationToken, 'base64').toString()
118 const authenticationParts = authentication.split(/:/)
119 const username = authenticationParts.shift()
120 const password = authenticationParts.join(':')
121 return (
122 this.uiServerConfiguration.authentication?.username === username &&
123 this.uiServerConfiguration.authentication?.password === password
124 )
125 }
126
127 public abstract start (): void
128 public abstract sendRequest (request: ProtocolRequest): void
129 public abstract sendResponse (response: ProtocolResponse): void
130 public abstract logPrefix (moduleName?: string, methodName?: string, prefixSuffix?: string): string
131 }