UI protocol: add OCPP heartbeat command support
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIHttpServer.ts
1 import { IncomingMessage, RequestListener, Server, ServerResponse } from 'http';
2
3 import { StatusCodes } from 'http-status-codes';
4
5 import BaseError from '../../exception/BaseError';
6 import type { UIServerConfiguration } from '../../types/ConfigurationData';
7 import {
8 ProcedureName,
9 Protocol,
10 ProtocolRequest,
11 ProtocolResponse,
12 ProtocolVersion,
13 RequestPayload,
14 ResponseStatus,
15 } from '../../types/UIProtocol';
16 import logger from '../../utils/Logger';
17 import Utils from '../../utils/Utils';
18 import { AbstractUIServer } from './AbstractUIServer';
19 import UIServiceFactory from './ui-services/UIServiceFactory';
20 import { UIServiceUtils } from './ui-services/UIServiceUtils';
21
22 const moduleName = 'UIHttpServer';
23
24 type responseHandler = { procedureName: ProcedureName; res: ServerResponse };
25
26 export default class UIHttpServer extends AbstractUIServer {
27 private readonly responseHandlers: Map<string, responseHandler>;
28
29 public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
30 super(uiServerConfiguration);
31 this.httpServer = new Server(this.requestListener.bind(this) as RequestListener);
32 this.responseHandlers = new Map<string, responseHandler>();
33 }
34
35 public start(): void {
36 if (this.httpServer.listening === false) {
37 this.httpServer.listen(this.uiServerConfiguration.options);
38 }
39 }
40
41 public stop(): void {
42 this.chargingStations.clear();
43 }
44
45 // eslint-disable-next-line @typescript-eslint/no-unused-vars
46 public sendRequest(request: ProtocolRequest): void {
47 // This is intentionally left blank
48 }
49
50 public sendResponse(response: ProtocolResponse): void {
51 const [uuid, payload] = response;
52 if (this.responseHandlers.has(uuid) === true) {
53 const { res } = this.responseHandlers.get(uuid);
54 res.writeHead(this.responseStatusToStatusCode(payload.status), {
55 'Content-Type': 'application/json',
56 });
57 res.write(JSON.stringify(payload));
58 res.end();
59 this.responseHandlers.delete(uuid);
60 } else {
61 logger.error(
62 `${this.logPrefix(moduleName, 'sendResponse')} Response for unknown request id: ${uuid}`
63 );
64 }
65 }
66
67 public logPrefix(modName?: string, methodName?: string, prefixSuffix?: string): string {
68 const logMsgPrefix = prefixSuffix ? `UI HTTP Server ${prefixSuffix}` : 'UI HTTP Server';
69 const logMsg =
70 modName && methodName ? ` ${logMsgPrefix} | ${modName}.${methodName}:` : ` ${logMsgPrefix} |`;
71 return Utils.logPrefix(logMsg);
72 }
73
74 private requestListener(req: IncomingMessage, res: ServerResponse): void {
75 if (this.authenticate(req) === false) {
76 res.setHeader('Content-Type', 'text/plain');
77 res.setHeader('WWW-Authenticate', 'Basic realm=users');
78 res.writeHead(StatusCodes.UNAUTHORIZED);
79 res.end(`${StatusCodes.UNAUTHORIZED} Unauthorized`);
80 return;
81 }
82 // Expected request URL pathname: /ui/:version/:procedureName
83 const [protocol, version, procedureName] = req.url?.split('/').slice(1) as [
84 Protocol,
85 ProtocolVersion,
86 ProcedureName
87 ];
88 const uuid = Utils.generateUUID();
89 this.responseHandlers.set(uuid, { procedureName, res });
90 try {
91 if (UIServiceUtils.isProtocolAndVersionSupported(protocol, version) === false) {
92 throw new BaseError(`Unsupported UI protocol version: '/${protocol}/${version}'`);
93 }
94 req.on('error', (error) => {
95 logger.error(
96 `${this.logPrefix(moduleName, 'requestListener.req.onerror')} Error on HTTP request:`,
97 error
98 );
99 });
100 if (this.uiServices.has(version) === false) {
101 this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
102 }
103 if (req.method === 'POST') {
104 const bodyBuffer = [];
105 req
106 .on('data', (chunk) => {
107 bodyBuffer.push(chunk);
108 })
109 .on('end', () => {
110 const body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload;
111 this.uiServices
112 .get(version)
113 .requestHandler(this.buildProtocolRequest(uuid, procedureName, body ?? {}))
114 .catch(() => {
115 this.sendResponse(
116 this.buildProtocolResponse(uuid, { status: ResponseStatus.FAILURE })
117 );
118 });
119 });
120 } else {
121 throw new BaseError(`Unsupported HTTP method: '${req.method}'`);
122 }
123 } catch (error) {
124 logger.error(
125 `${this.logPrefix(moduleName, 'requestListener')} Handle HTTP request error:`,
126 error
127 );
128 this.sendResponse(this.buildProtocolResponse(uuid, { status: ResponseStatus.FAILURE }));
129 }
130 }
131
132 private authenticate(req: IncomingMessage): boolean {
133 if (this.isBasicAuthEnabled() === true) {
134 if (this.isValidBasicAuth(req) === true) {
135 return true;
136 }
137 return false;
138 }
139 return true;
140 }
141
142 private responseStatusToStatusCode(status: ResponseStatus): StatusCodes {
143 switch (status) {
144 case ResponseStatus.SUCCESS:
145 return StatusCodes.OK;
146 case ResponseStatus.FAILURE:
147 return StatusCodes.BAD_REQUEST;
148 default:
149 return StatusCodes.INTERNAL_SERVER_ERROR;
150 }
151 }
152 }