Fix timestamping at OCPP firmware update command handling
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16RequestService.ts
CommitLineData
edd13439 1// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
c8eeb62b 2
b52c969d
JB
3import fs from 'fs';
4import path from 'path';
5import { fileURLToPath } from 'url';
6
6c1761d4 7import type { JSONSchemaType } from 'ajv';
b52c969d 8
78202038 9import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
8114d10e 10import OCPPError from '../../../exception/OCPPError';
6c1761d4
JB
11import type { JsonObject, JsonType } from '../../../types/JsonType';
12import type { OCPP16MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues';
b52c969d 13import {
27782dbc
JB
14 type OCPP16BootNotificationRequest,
15 type OCPP16DataTransferRequest,
c9a4f9ea 16 type OCPP16DiagnosticsStatusNotificationRequest,
e9a4164c 17 type OCPP16FirmwareStatusNotificationRequest,
27782dbc 18 type OCPP16HeartbeatRequest,
b52c969d 19 OCPP16RequestCommand,
27782dbc 20 type OCPP16StatusNotificationRequest,
b52c969d 21} from '../../../types/ocpp/1.6/Requests';
6c1761d4 22import type {
b52c969d
JB
23 OCPP16AuthorizeRequest,
24 OCPP16StartTransactionRequest,
25 OCPP16StopTransactionRequest,
26} from '../../../types/ocpp/1.6/Transaction';
8114d10e 27import { ErrorType } from '../../../types/ocpp/ErrorType';
d270cc87 28import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
6c1761d4 29import type { RequestParams } from '../../../types/ocpp/Requests';
8114d10e 30import Constants from '../../../utils/Constants';
c0560973 31import Utils from '../../../utils/Utils';
8114d10e 32import type ChargingStation from '../../ChargingStation';
4d20f040 33import OCPPConstants from '../OCPPConstants';
8114d10e
JB
34import OCPPRequestService from '../OCPPRequestService';
35import type OCPPResponseService from '../OCPPResponseService';
c0560973 36
909dcf2d
JB
37const moduleName = 'OCPP16RequestService';
38
c0560973 39export default class OCPP16RequestService extends OCPPRequestService {
b3fc3ff5 40 protected jsonSchemas: Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>;
b52c969d 41
08f130a0 42 public constructor(ocppResponseService: OCPPResponseService) {
909dcf2d 43 if (new.target?.name === moduleName) {
06127450 44 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
9f2e3130 45 }
d270cc87 46 super(OCPPVersion.VERSION_16, ocppResponseService);
b52c969d
JB
47 this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>([
48 [
49 OCPP16RequestCommand.AUTHORIZE,
e9a4164c
JB
50 this.parseJsonSchemaFile<OCPP16AuthorizeRequest>(
51 '../../../assets/json-schemas/ocpp/1.6/Authorize.json'
52 ),
b52c969d
JB
53 ],
54 [
55 OCPP16RequestCommand.BOOT_NOTIFICATION,
e9a4164c
JB
56 this.parseJsonSchemaFile<OCPP16BootNotificationRequest>(
57 '../../../assets/json-schemas/ocpp/1.6/BootNotification.json'
58 ),
b52c969d
JB
59 ],
60 [
61 OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
e9a4164c
JB
62 this.parseJsonSchemaFile<OCPP16DiagnosticsStatusNotificationRequest>(
63 '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json'
64 ),
b52c969d
JB
65 ],
66 [
67 OCPP16RequestCommand.HEARTBEAT,
e9a4164c
JB
68 this.parseJsonSchemaFile<OCPP16HeartbeatRequest>(
69 '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json'
70 ),
b52c969d
JB
71 ],
72 [
73 OCPP16RequestCommand.METER_VALUES,
e9a4164c
JB
74 this.parseJsonSchemaFile<OCPP16MeterValuesRequest>(
75 '../../../assets/json-schemas/ocpp/1.6/MeterValues.json'
76 ),
b52c969d
JB
77 ],
78 [
79 OCPP16RequestCommand.STATUS_NOTIFICATION,
e9a4164c
JB
80 this.parseJsonSchemaFile<OCPP16StatusNotificationRequest>(
81 '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json'
82 ),
b52c969d
JB
83 ],
84 [
85 OCPP16RequestCommand.START_TRANSACTION,
e9a4164c
JB
86 this.parseJsonSchemaFile<OCPP16StartTransactionRequest>(
87 '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json'
88 ),
b52c969d
JB
89 ],
90 [
91 OCPP16RequestCommand.STOP_TRANSACTION,
e9a4164c
JB
92 this.parseJsonSchemaFile<OCPP16StopTransactionRequest>(
93 '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json'
94 ),
b52c969d 95 ],
91a7d3ea
JB
96 [
97 OCPP16RequestCommand.DATA_TRANSFER,
e9a4164c
JB
98 this.parseJsonSchemaFile<OCPP16DataTransferRequest>(
99 '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json'
100 ),
91a7d3ea 101 ],
c9a4f9ea
JB
102 [
103 OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION,
e9a4164c
JB
104 this.parseJsonSchemaFile<OCPP16FirmwareStatusNotificationRequest>(
105 '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json'
106 ),
c9a4f9ea 107 ],
b52c969d 108 ]);
9952c548 109 this.buildRequestPayload.bind(this);
9f2e3130
JB
110 }
111
6c1761d4 112 public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
08f130a0 113 chargingStation: ChargingStation,
94a464f9 114 commandName: OCPP16RequestCommand,
5cc4b63b 115 commandParams?: JsonType,
be9b0d50 116 params?: RequestParams
6c1761d4 117 ): Promise<ResponseType> {
ed6cfcff 118 if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) {
6c1761d4 119 const requestPayload = this.buildRequestPayload<RequestType>(
b52c969d
JB
120 chargingStation,
121 commandName,
122 commandParams
123 );
f22266fd 124 return (await this.sendMessage(
08f130a0 125 chargingStation,
94a464f9 126 Utils.generateUUID(),
b52c969d 127 requestPayload,
94a464f9
JB
128 commandName,
129 params
6c1761d4 130 )) as unknown as ResponseType;
94a464f9 131 }
e909d2a7 132 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
94a464f9
JB
133 throw new OCPPError(
134 ErrorType.NOT_SUPPORTED,
6c8f5d90 135 `Unsupported OCPP command '${commandName}'`,
94a464f9 136 commandName,
7369e417 137 commandParams
94a464f9 138 );
c0560973
JB
139 }
140
5cc4b63b 141 private buildRequestPayload<Request extends JsonType>(
08f130a0 142 chargingStation: ChargingStation,
78085c42 143 commandName: OCPP16RequestCommand,
5cc4b63b 144 commandParams?: JsonType
f22266fd 145 ): Request {
68c993d5 146 let connectorId: number;
cf058664 147 let energyActiveImportRegister: number;
5cc4b63b 148 commandParams = commandParams as JsonObject;
78085c42 149 switch (commandName) {
78085c42 150 case OCPP16RequestCommand.BOOT_NOTIFICATION:
78085c42 151 case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION:
22e0d48e 152 case OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION:
78085c42 153 case OCPP16RequestCommand.METER_VALUES:
78085c42 154 case OCPP16RequestCommand.STATUS_NOTIFICATION:
36c462a4
JB
155 case OCPP16RequestCommand.DATA_TRANSFER:
156 return commandParams as unknown as Request;
157 case OCPP16RequestCommand.AUTHORIZE:
78085c42 158 return {
36c462a4
JB
159 idTag: Constants.DEFAULT_IDTAG,
160 ...commandParams,
f22266fd 161 } as unknown as Request;
36c462a4
JB
162 case OCPP16RequestCommand.HEARTBEAT:
163 return OCPPConstants.OCPP_REQUEST_EMPTY as unknown as Request;
78085c42
JB
164 case OCPP16RequestCommand.START_TRANSACTION:
165 return {
36c462a4
JB
166 idTag: Constants.DEFAULT_IDTAG,
167 meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId(
168 commandParams?.connectorId as number
169 ),
170 timestamp: new Date(),
171 ...commandParams,
f22266fd 172 } as unknown as Request;
78085c42 173 case OCPP16RequestCommand.STOP_TRANSACTION:
f1e731bd 174 chargingStation.getTransactionDataMeterValues() &&
f1e731bd
JB
175 (connectorId = chargingStation.getConnectorIdByTransactionId(
176 commandParams?.transactionId as number
177 ));
36c462a4
JB
178 energyActiveImportRegister = chargingStation.getEnergyActiveImportRegisterByTransactionId(
179 commandParams?.transactionId as number,
180 true
181 );
78085c42 182 return {
36c462a4
JB
183 idTag: chargingStation.getTransactionIdTag(commandParams?.transactionId as number),
184 meterStop: energyActiveImportRegister,
185 timestamp: new Date(),
186 ...(chargingStation.getTransactionDataMeterValues() && {
187 transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues(
188 chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue,
189 OCPP16ServiceUtils.buildTransactionEndMeterValue(
190 chargingStation,
191 connectorId,
192 energyActiveImportRegister
193 )
194 ),
78085c42 195 }),
36c462a4 196 ...commandParams,
f22266fd 197 } as unknown as Request;
78085c42 198 default:
e909d2a7 199 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
78085c42
JB
200 throw new OCPPError(
201 ErrorType.NOT_SUPPORTED,
202 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
6c8f5d90 203 `Unsupported OCPP command '${commandName}'`,
78085c42 204 commandName,
7369e417 205 commandParams
78085c42
JB
206 );
207 }
208 }
e9a4164c
JB
209
210 private parseJsonSchemaFile<T extends JsonType>(relativePath: string): JSONSchemaType<T> {
211 return JSON.parse(
212 fs.readFileSync(
213 path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath),
214 'utf8'
215 )
216 ) as JSONSchemaType<T>;
217 }
c0560973 218}