1 import { type IncomingMessage
, Server
, type ServerResponse
} from
'node:http';
3 import type { WebSocket
} from
'ws';
5 import type { AbstractUIService
} from
'./ui-services/AbstractUIService';
6 import { UIServiceFactory
} from
'./ui-services/UIServiceFactory';
7 import { BaseError
} from
'../../exception';
10 type ChargingStationData
,
13 type ProtocolResponse
,
17 type UIServerConfiguration
,
20 export abstract class AbstractUIServer
{
21 public readonly chargingStations
: Map
<string, ChargingStationData
>;
22 protected readonly httpServer
: Server
;
23 protected readonly responseHandlers
: Map
<string, ServerResponse
| 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.responseHandlers
= new Map
<string, ServerResponse
| WebSocket
>();
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 public async sendInternalRequest(request
: ProtocolRequest
): Promise
<ProtocolResponse
> {
50 const protocolVersion
= ProtocolVersion
['0.0.1'];
51 this.registerProtocolVersionUIService(protocolVersion
);
52 return this.uiServices
.get(protocolVersion
)?.requestHandler(request
);
55 public hasResponseHandler(id
: string): boolean {
56 return this.responseHandlers
.has(id
);
59 protected startHttpServer(): void {
60 if (this.httpServer
.listening
=== false) {
61 this.httpServer
.listen(this.uiServerConfiguration
.options
);
65 protected registerProtocolVersionUIService(version
: ProtocolVersion
): void {
66 if (this.uiServices
.has(version
) === false) {
67 this.uiServices
.set(version
, UIServiceFactory
.getUIServiceImplementation(version
, this));
71 protected authenticate(req
: IncomingMessage
, next
: (err
?: Error) => void): void {
72 if (this.isBasicAuthEnabled() === true) {
73 if (this.isValidBasicAuth(req
) === false) {
74 next(new BaseError('Unauthorized'));
81 private isBasicAuthEnabled(): boolean {
83 this.uiServerConfiguration
.authentication
?.enabled
=== true &&
84 this.uiServerConfiguration
.authentication
?.type === AuthenticationType
.BASIC_AUTH
88 private isValidBasicAuth(req
: IncomingMessage
): boolean {
89 const authorizationHeader
= req
.headers
.authorization
?? '';
90 const authorizationToken
= authorizationHeader
.split(/\s
+/).pop() ?? '';
91 const authentication
= Buffer
.from(authorizationToken
, 'base64').toString();
92 const authenticationParts
= authentication
.split(/:/);
93 const username
= authenticationParts
.shift();
94 const password
= authenticationParts
.join(':');
96 this.uiServerConfiguration
.authentication
?.username
=== username
&&
97 this.uiServerConfiguration
.authentication
?.password
=== password
101 public abstract start(): void;
102 public abstract sendRequest(request
: ProtocolRequest
): void;
103 public abstract sendResponse(response
: ProtocolResponse
): void;
104 public abstract logPrefix(
107 prefixSuffix
?: string