Allow to specify timestamp in OCPP commands payload with the UI Server
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20RequestService.ts
1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
2
3 import fs from 'fs';
4 import path from 'path';
5 import { fileURLToPath } from 'url';
6
7 import type { JSONSchemaType } from 'ajv';
8
9 import { OCPP20ServiceUtils } from './OCPP20ServiceUtils';
10 import OCPPError from '../../../exception/OCPPError';
11 import type { JsonObject, JsonType } from '../../../types/JsonType';
12 import {
13 type OCPP20BootNotificationRequest,
14 type OCPP20HeartbeatRequest,
15 OCPP20RequestCommand,
16 type OCPP20StatusNotificationRequest,
17 } from '../../../types/ocpp/2.0/Requests';
18 import { ErrorType } from '../../../types/ocpp/ErrorType';
19 import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
20 import type { RequestParams } from '../../../types/ocpp/Requests';
21 import Utils from '../../../utils/Utils';
22 import type ChargingStation from '../../ChargingStation';
23 import OCPPRequestService from '../OCPPRequestService';
24 import type OCPPResponseService from '../OCPPResponseService';
25
26 const moduleName = 'OCPP20RequestService';
27
28 export default class OCPP20RequestService extends OCPPRequestService {
29 protected jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>;
30
31 public constructor(ocppResponseService: OCPPResponseService) {
32 if (new.target?.name === moduleName) {
33 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
34 }
35 super(OCPPVersion.VERSION_20, ocppResponseService);
36 this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
37 [
38 OCPP20RequestCommand.BOOT_NOTIFICATION,
39 this.parseJsonSchemaFile<OCPP20BootNotificationRequest>(
40 '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json'
41 ),
42 ],
43 [
44 OCPP20RequestCommand.HEARTBEAT,
45 this.parseJsonSchemaFile<OCPP20HeartbeatRequest>(
46 '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json'
47 ),
48 ],
49 [
50 OCPP20RequestCommand.STATUS_NOTIFICATION,
51 this.parseJsonSchemaFile<OCPP20StatusNotificationRequest>(
52 '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json'
53 ),
54 ],
55 ]);
56 this.buildRequestPayload.bind(this);
57 }
58
59 public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
60 chargingStation: ChargingStation,
61 commandName: OCPP20RequestCommand,
62 commandParams?: JsonType,
63 params?: RequestParams
64 ): Promise<ResponseType> {
65 if (OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) {
66 const requestPayload = this.buildRequestPayload<RequestType>(
67 chargingStation,
68 commandName,
69 commandParams
70 );
71 return (await this.sendMessage(
72 chargingStation,
73 Utils.generateUUID(),
74 requestPayload,
75 commandName,
76 params
77 )) as unknown as ResponseType;
78 }
79 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
80 throw new OCPPError(
81 ErrorType.NOT_SUPPORTED,
82 `Unsupported OCPP command '${commandName}'`,
83 commandName,
84 commandParams
85 );
86 }
87
88 private buildRequestPayload<Request extends JsonType>(
89 chargingStation: ChargingStation,
90 commandName: OCPP20RequestCommand,
91 commandParams?: JsonType
92 ): Request {
93 commandParams = commandParams as JsonObject;
94 switch (commandName) {
95 case OCPP20RequestCommand.BOOT_NOTIFICATION:
96 commandParams.chargingStation = commandParams.chargingStation as JsonObject;
97 commandParams.chargingStation.modem = commandParams.chargingStation.modem as JsonObject;
98 return {
99 reason: commandParams?.reason,
100 chargingStation: {
101 model: commandParams?.chargingStation?.model,
102 vendorName: commandParams?.chargingStation?.vendorName,
103 ...(!Utils.isUndefined(commandParams?.chargingStation?.firmwareVersion) && {
104 firmwareVersion: commandParams.chargingStation?.firmwareVersion,
105 }),
106 ...(!Utils.isUndefined(commandParams?.chargingStation?.serialNumber) && {
107 serialNumber: commandParams.chargingStation?.serialNumber,
108 }),
109 ...(!Utils.isUndefined(commandParams?.chargingStation?.modem) && {
110 modem: {
111 ...(!Utils.isUndefined(commandParams?.chargingStation?.modem?.iccid) && {
112 iccid: commandParams.chargingStation.modem.iccid,
113 }),
114 ...(!Utils.isUndefined(commandParams?.chargingStation?.modem?.imsi) && {
115 imsi: commandParams.chargingStation.modem.imsi,
116 }),
117 },
118 }),
119 },
120 } as unknown as Request;
121 case OCPP20RequestCommand.HEARTBEAT:
122 return {} as unknown as Request;
123 case OCPP20RequestCommand.STATUS_NOTIFICATION:
124 return {
125 timestamp: commandParams?.timestamp ?? new Date(),
126 connectorStatus: commandParams?.connectorStatus,
127 evseId: commandParams?.evseId,
128 connectorId: commandParams?.connectorId,
129 } as unknown as Request;
130 default:
131 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
132 throw new OCPPError(
133 ErrorType.NOT_SUPPORTED,
134 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
135 `Unsupported OCPP command '${commandName}'`,
136 commandName,
137 commandParams
138 );
139 }
140 }
141
142 private parseJsonSchemaFile<T extends JsonType>(relativePath: string): JSONSchemaType<T> {
143 return JSON.parse(
144 fs.readFileSync(
145 path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath),
146 'utf8'
147 )
148 ) as JSONSchemaType<T>;
149 }
150 }