1 import { type IncomingMessage
, Server
, type ServerResponse
} from
'node:http';
2 import { type Http2Server
, createServer
} from
'node:http2';
4 import type { WebSocket
} from
'ws';
6 import type { AbstractUIService
} from
'./ui-services/AbstractUIService';
7 import { UIServiceFactory
} from
'./ui-services/UIServiceFactory';
8 import { BaseError
} from
'../../exception';
10 ApplicationProtocolVersion
,
12 type ChargingStationData
,
15 type ProtocolResponse
,
19 type UIServerConfiguration
,
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
>;
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();
34 case ApplicationProtocolVersion
.VERSION_20
:
35 this.httpServer
= createServer();
39 `Unsupported application protocol version ${this.uiServerConfiguration.version}`,
42 this.responseHandlers
= new Map
<string, ServerResponse
| WebSocket
>();
43 this.uiServices
= new Map
<ProtocolVersion
, AbstractUIService
>();
46 public buildProtocolRequest(
48 procedureName
: ProcedureName
,
49 requestPayload
: RequestPayload
,
51 return [id
, procedureName
, requestPayload
];
54 public buildProtocolResponse(id
: string, responsePayload
: ResponsePayload
): ProtocolResponse
{
55 return [id
, responsePayload
];
59 this.chargingStations
.clear();
62 public async sendInternalRequest(request
: ProtocolRequest
): Promise
<ProtocolResponse
> {
63 const protocolVersion
= ProtocolVersion
['0.0.1'];
64 this.registerProtocolVersionUIService(protocolVersion
);
65 return this.uiServices
67 ?.requestHandler(request
) as Promise
<ProtocolResponse
>;
70 public hasResponseHandler(id
: string): boolean {
71 return this.responseHandlers
.has(id
);
74 protected startHttpServer(): void {
75 if (this.httpServer
.listening
=== false) {
76 this.httpServer
.listen(this.uiServerConfiguration
.options
);
80 protected registerProtocolVersionUIService(version
: ProtocolVersion
): void {
81 if (this.uiServices
.has(version
) === false) {
82 this.uiServices
.set(version
, UIServiceFactory
.getUIServiceImplementation(version
, this));
86 protected authenticate(req
: IncomingMessage
, next
: (err
?: Error) => void): void {
87 if (this.isBasicAuthEnabled() === true) {
88 if (this.isValidBasicAuth(req
) === false) {
89 next(new BaseError('Unauthorized'));
96 private isBasicAuthEnabled(): boolean {
98 this.uiServerConfiguration
.authentication
?.enabled
=== true &&
99 this.uiServerConfiguration
.authentication
?.type === AuthenticationType
.BASIC_AUTH
103 private isValidBasicAuth(req
: IncomingMessage
): boolean {
104 const authorizationHeader
= req
.headers
.authorization
?? '';
105 const authorizationToken
= authorizationHeader
.split(/\s
+/).pop() ?? '';
106 const authentication
= Buffer
.from(authorizationToken
, 'base64').toString();
107 const authenticationParts
= authentication
.split(/:/);
108 const username
= authenticationParts
.shift();
109 const password
= authenticationParts
.join(':');
111 this.uiServerConfiguration
.authentication
?.username
=== username
&&
112 this.uiServerConfiguration
.authentication
?.password
=== password
116 public abstract start(): void;
117 public abstract sendRequest(request
: ProtocolRequest
): void;
118 public abstract sendResponse(response
: ProtocolResponse
): void;
119 public abstract logPrefix(
122 prefixSuffix
?: string,