1 import { type IncomingMessage
, Server
} from
'http';
2 import type { Socket
} from
'net';
4 import type { WebSocket
} from
'ws';
6 import type { ChargingStationData
} from
'../../types/ChargingStationWorker';
7 import type { UIServerConfiguration
} from
'../../types/ConfigurationData';
16 } from
'../../types/UIProtocol';
17 import type AbstractUIService from
'./ui-services/AbstractUIService';
18 import UIServiceFactory from
'./ui-services/UIServiceFactory';
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
>;
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
>();
33 public buildProtocolRequest(
35 procedureName
: ProcedureName
,
36 requestPayload
: RequestPayload
38 return [id
, procedureName
, requestPayload
];
41 public buildProtocolResponse(id
: string, responsePayload
: ResponsePayload
): ProtocolResponse
{
42 return [id
, responsePayload
];
46 this.chargingStations
.clear();
49 protected registerProtocolVersionUIService(version
: ProtocolVersion
): void {
50 if (this.uiServices
.has(version
) === false) {
51 this.uiServices
.set(version
, UIServiceFactory
.getUIServiceImplementation(version
, this));
55 protected isBasicAuthEnabled(): boolean {
57 this.uiServerConfiguration
.authentication
?.enabled
=== true &&
58 this.uiServerConfiguration
.authentication
?.type === AuthenticationType
.BASIC_AUTH
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(':');
70 this.uiServerConfiguration
.authentication
?.username
=== username
&&
71 this.uiServerConfiguration
.authentication
?.password
=== password
75 public abstract start(): void;
76 public abstract sendRequest(request
: ProtocolRequest
): void;
77 public abstract sendResponse(response
: ProtocolResponse
): void;
78 public abstract logPrefix(