Commit | Line | Data |
---|---|---|
c8eeb62b JB |
1 | // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. |
2 | ||
b52c969d JB |
3 | import fs from 'fs'; |
4 | import path from 'path'; | |
5 | import { fileURLToPath } from 'url'; | |
6 | ||
6c1761d4 | 7 | import type { JSONSchemaType } from 'ajv'; |
b52c969d | 8 | |
8114d10e | 9 | import OCPPError from '../../../exception/OCPPError'; |
6c1761d4 JB |
10 | import type { JsonObject, JsonType } from '../../../types/JsonType'; |
11 | import type { OCPP16MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues'; | |
b52c969d JB |
12 | import { |
13 | DiagnosticsStatusNotificationRequest, | |
14 | OCPP16BootNotificationRequest, | |
91a7d3ea | 15 | OCPP16DataTransferRequest, |
b52c969d JB |
16 | OCPP16HeartbeatRequest, |
17 | OCPP16RequestCommand, | |
18 | OCPP16StatusNotificationRequest, | |
19 | } from '../../../types/ocpp/1.6/Requests'; | |
6c1761d4 | 20 | import type { |
b52c969d JB |
21 | OCPP16AuthorizeRequest, |
22 | OCPP16StartTransactionRequest, | |
23 | OCPP16StopTransactionRequest, | |
24 | } from '../../../types/ocpp/1.6/Transaction'; | |
8114d10e | 25 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
6c1761d4 | 26 | import type { RequestParams } from '../../../types/ocpp/Requests'; |
8114d10e | 27 | import Constants from '../../../utils/Constants'; |
b52c969d | 28 | import logger from '../../../utils/Logger'; |
c0560973 | 29 | import Utils from '../../../utils/Utils'; |
8114d10e JB |
30 | import type ChargingStation from '../../ChargingStation'; |
31 | import OCPPRequestService from '../OCPPRequestService'; | |
32 | import type OCPPResponseService from '../OCPPResponseService'; | |
33 | import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; | |
c0560973 | 34 | |
909dcf2d JB |
35 | const moduleName = 'OCPP16RequestService'; |
36 | ||
c0560973 | 37 | export default class OCPP16RequestService extends OCPPRequestService { |
b52c969d JB |
38 | private jsonSchemas: Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>; |
39 | ||
08f130a0 | 40 | public constructor(ocppResponseService: OCPPResponseService) { |
909dcf2d | 41 | if (new.target?.name === moduleName) { |
06127450 | 42 | throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); |
9f2e3130 | 43 | } |
08f130a0 | 44 | super(ocppResponseService); |
b52c969d JB |
45 | this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>([ |
46 | [ | |
47 | OCPP16RequestCommand.AUTHORIZE, | |
48 | JSON.parse( | |
49 | fs.readFileSync( | |
50 | path.resolve( | |
51 | path.dirname(fileURLToPath(import.meta.url)), | |
52 | '../../../assets/json-schemas/ocpp/1.6/Authorize.json' | |
53 | ), | |
54 | 'utf8' | |
55 | ) | |
56 | ) as JSONSchemaType<OCPP16AuthorizeRequest>, | |
57 | ], | |
58 | [ | |
59 | OCPP16RequestCommand.BOOT_NOTIFICATION, | |
60 | JSON.parse( | |
61 | fs.readFileSync( | |
62 | path.resolve( | |
63 | path.dirname(fileURLToPath(import.meta.url)), | |
64 | '../../../assets/json-schemas/ocpp/1.6/BootNotification.json' | |
65 | ), | |
66 | 'utf8' | |
67 | ) | |
68 | ) as JSONSchemaType<OCPP16BootNotificationRequest>, | |
69 | ], | |
70 | [ | |
71 | OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, | |
72 | JSON.parse( | |
73 | fs.readFileSync( | |
74 | path.resolve( | |
75 | path.dirname(fileURLToPath(import.meta.url)), | |
76 | '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json' | |
77 | ), | |
78 | 'utf8' | |
79 | ) | |
80 | ) as JSONSchemaType<DiagnosticsStatusNotificationRequest>, | |
81 | ], | |
82 | [ | |
83 | OCPP16RequestCommand.HEARTBEAT, | |
84 | JSON.parse( | |
85 | fs.readFileSync( | |
86 | path.resolve( | |
87 | path.dirname(fileURLToPath(import.meta.url)), | |
88 | '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json' | |
89 | ), | |
90 | 'utf8' | |
91 | ) | |
92 | ) as JSONSchemaType<OCPP16HeartbeatRequest>, | |
93 | ], | |
94 | [ | |
95 | OCPP16RequestCommand.METER_VALUES, | |
96 | JSON.parse( | |
97 | fs.readFileSync( | |
98 | path.resolve( | |
99 | path.dirname(fileURLToPath(import.meta.url)), | |
100 | '../../../assets/json-schemas/ocpp/1.6/MeterValues.json' | |
101 | ), | |
102 | 'utf8' | |
103 | ) | |
104 | ) as JSONSchemaType<OCPP16MeterValuesRequest>, | |
105 | ], | |
106 | [ | |
107 | OCPP16RequestCommand.STATUS_NOTIFICATION, | |
108 | JSON.parse( | |
109 | fs.readFileSync( | |
110 | path.resolve( | |
111 | path.dirname(fileURLToPath(import.meta.url)), | |
112 | '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json' | |
113 | ), | |
114 | 'utf8' | |
115 | ) | |
116 | ) as JSONSchemaType<OCPP16StatusNotificationRequest>, | |
117 | ], | |
118 | [ | |
119 | OCPP16RequestCommand.START_TRANSACTION, | |
120 | JSON.parse( | |
121 | fs.readFileSync( | |
122 | path.resolve( | |
123 | path.dirname(fileURLToPath(import.meta.url)), | |
124 | '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json' | |
125 | ), | |
126 | 'utf8' | |
127 | ) | |
128 | ) as JSONSchemaType<OCPP16StartTransactionRequest>, | |
129 | ], | |
130 | [ | |
131 | OCPP16RequestCommand.STOP_TRANSACTION, | |
132 | JSON.parse( | |
133 | fs.readFileSync( | |
134 | path.resolve( | |
135 | path.dirname(fileURLToPath(import.meta.url)), | |
136 | '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json' | |
137 | ), | |
138 | 'utf8' | |
139 | ) | |
140 | ) as JSONSchemaType<OCPP16StopTransactionRequest>, | |
141 | ], | |
91a7d3ea JB |
142 | [ |
143 | OCPP16RequestCommand.DATA_TRANSFER, | |
144 | JSON.parse( | |
145 | fs.readFileSync( | |
146 | path.resolve( | |
147 | path.dirname(fileURLToPath(import.meta.url)), | |
148 | '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json' | |
149 | ), | |
150 | 'utf8' | |
151 | ) | |
152 | ) as JSONSchemaType<OCPP16DataTransferRequest>, | |
153 | ], | |
b52c969d | 154 | ]); |
9952c548 JB |
155 | this.buildRequestPayload.bind(this); |
156 | this.validatePayload.bind(this); | |
9f2e3130 JB |
157 | } |
158 | ||
6c1761d4 | 159 | public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>( |
08f130a0 | 160 | chargingStation: ChargingStation, |
94a464f9 | 161 | commandName: OCPP16RequestCommand, |
5cc4b63b | 162 | commandParams?: JsonType, |
be9b0d50 | 163 | params?: RequestParams |
6c1761d4 | 164 | ): Promise<ResponseType> { |
ed6cfcff | 165 | if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) { |
6c1761d4 | 166 | const requestPayload = this.buildRequestPayload<RequestType>( |
b52c969d JB |
167 | chargingStation, |
168 | commandName, | |
169 | commandParams | |
170 | ); | |
9c5c4195 | 171 | this.validatePayload(chargingStation, commandName, requestPayload); |
f22266fd | 172 | return (await this.sendMessage( |
08f130a0 | 173 | chargingStation, |
94a464f9 | 174 | Utils.generateUUID(), |
b52c969d | 175 | requestPayload, |
94a464f9 JB |
176 | commandName, |
177 | params | |
6c1761d4 | 178 | )) as unknown as ResponseType; |
94a464f9 | 179 | } |
e909d2a7 | 180 | // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). |
94a464f9 JB |
181 | throw new OCPPError( |
182 | ErrorType.NOT_SUPPORTED, | |
6c8f5d90 | 183 | `Unsupported OCPP command '${commandName}'`, |
94a464f9 | 184 | commandName, |
7369e417 | 185 | commandParams |
94a464f9 | 186 | ); |
c0560973 JB |
187 | } |
188 | ||
5cc4b63b | 189 | private buildRequestPayload<Request extends JsonType>( |
08f130a0 | 190 | chargingStation: ChargingStation, |
78085c42 | 191 | commandName: OCPP16RequestCommand, |
5cc4b63b | 192 | commandParams?: JsonType |
f22266fd | 193 | ): Request { |
68c993d5 | 194 | let connectorId: number; |
cf058664 | 195 | let energyActiveImportRegister: number; |
5cc4b63b | 196 | commandParams = commandParams as JsonObject; |
78085c42 JB |
197 | switch (commandName) { |
198 | case OCPP16RequestCommand.AUTHORIZE: | |
199 | return { | |
200 | ...(!Utils.isUndefined(commandParams?.idTag) | |
201 | ? { idTag: commandParams.idTag } | |
202 | : { idTag: Constants.DEFAULT_IDTAG }), | |
f22266fd | 203 | } as unknown as Request; |
78085c42 JB |
204 | case OCPP16RequestCommand.BOOT_NOTIFICATION: |
205 | return { | |
206 | chargePointModel: commandParams?.chargePointModel, | |
207 | chargePointVendor: commandParams?.chargePointVendor, | |
208 | ...(!Utils.isUndefined(commandParams?.chargeBoxSerialNumber) && { | |
209 | chargeBoxSerialNumber: commandParams.chargeBoxSerialNumber, | |
210 | }), | |
211 | ...(!Utils.isUndefined(commandParams?.chargePointSerialNumber) && { | |
212 | chargePointSerialNumber: commandParams.chargePointSerialNumber, | |
213 | }), | |
214 | ...(!Utils.isUndefined(commandParams?.firmwareVersion) && { | |
215 | firmwareVersion: commandParams.firmwareVersion, | |
216 | }), | |
217 | ...(!Utils.isUndefined(commandParams?.iccid) && { iccid: commandParams.iccid }), | |
218 | ...(!Utils.isUndefined(commandParams?.imsi) && { imsi: commandParams.imsi }), | |
219 | ...(!Utils.isUndefined(commandParams?.meterSerialNumber) && { | |
220 | meterSerialNumber: commandParams.meterSerialNumber, | |
221 | }), | |
222 | ...(!Utils.isUndefined(commandParams?.meterType) && { | |
223 | meterType: commandParams.meterType, | |
224 | }), | |
f22266fd | 225 | } as unknown as Request; |
78085c42 JB |
226 | case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION: |
227 | return { | |
228 | status: commandParams?.diagnosticsStatus, | |
f22266fd | 229 | } as unknown as Request; |
78085c42 | 230 | case OCPP16RequestCommand.HEARTBEAT: |
f22266fd | 231 | return {} as unknown as Request; |
78085c42 JB |
232 | case OCPP16RequestCommand.METER_VALUES: |
233 | return { | |
234 | connectorId: commandParams?.connectorId, | |
235 | transactionId: commandParams?.transactionId, | |
7369e417 | 236 | meterValue: commandParams?.meterValue, |
f22266fd | 237 | } as unknown as Request; |
78085c42 JB |
238 | case OCPP16RequestCommand.STATUS_NOTIFICATION: |
239 | return { | |
240 | connectorId: commandParams?.connectorId, | |
78085c42 | 241 | status: commandParams?.status, |
93b4a429 | 242 | errorCode: commandParams?.errorCode, |
f22266fd | 243 | } as unknown as Request; |
78085c42 JB |
244 | case OCPP16RequestCommand.START_TRANSACTION: |
245 | return { | |
246 | connectorId: commandParams?.connectorId, | |
247 | ...(!Utils.isUndefined(commandParams?.idTag) | |
248 | ? { idTag: commandParams?.idTag } | |
249 | : { idTag: Constants.DEFAULT_IDTAG }), | |
08f130a0 | 250 | meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId( |
78085c42 JB |
251 | commandParams?.connectorId as number |
252 | ), | |
253 | timestamp: new Date().toISOString(), | |
f22266fd | 254 | } as unknown as Request; |
78085c42 | 255 | case OCPP16RequestCommand.STOP_TRANSACTION: |
08f130a0 | 256 | connectorId = chargingStation.getConnectorIdByTransactionId( |
f479a792 JB |
257 | commandParams?.transactionId as number |
258 | ); | |
7acb3f7b JB |
259 | commandParams?.meterStop && |
260 | (energyActiveImportRegister = | |
261 | chargingStation.getEnergyActiveImportRegisterByTransactionId( | |
262 | commandParams?.transactionId as number, | |
263 | true | |
264 | )); | |
78085c42 JB |
265 | return { |
266 | transactionId: commandParams?.transactionId, | |
cf058664 JB |
267 | idTag: |
268 | commandParams?.idTag ?? | |
269 | chargingStation.getTransactionIdTag(commandParams?.transactionId as number), | |
270 | meterStop: commandParams?.meterStop ?? energyActiveImportRegister, | |
78085c42 | 271 | timestamp: new Date().toISOString(), |
cf058664 | 272 | reason: commandParams?.reason, |
08f130a0 | 273 | ...(chargingStation.getTransactionDataMeterValues() && { |
78085c42 | 274 | transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( |
08f130a0 | 275 | chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, |
78085c42 | 276 | OCPP16ServiceUtils.buildTransactionEndMeterValue( |
08f130a0 | 277 | chargingStation, |
68c993d5 | 278 | connectorId, |
cf058664 | 279 | (commandParams?.meterStop as number) ?? energyActiveImportRegister |
78085c42 JB |
280 | ) |
281 | ), | |
282 | }), | |
f22266fd | 283 | } as unknown as Request; |
91a7d3ea JB |
284 | case OCPP16RequestCommand.DATA_TRANSFER: |
285 | return commandParams as unknown as Request; | |
78085c42 | 286 | default: |
e909d2a7 | 287 | // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). |
78085c42 JB |
288 | throw new OCPPError( |
289 | ErrorType.NOT_SUPPORTED, | |
290 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
6c8f5d90 | 291 | `Unsupported OCPP command '${commandName}'`, |
78085c42 | 292 | commandName, |
7369e417 | 293 | commandParams |
78085c42 JB |
294 | ); |
295 | } | |
296 | } | |
9c5c4195 JB |
297 | |
298 | private validatePayload<Request extends JsonType>( | |
299 | chargingStation: ChargingStation, | |
300 | commandName: OCPP16RequestCommand, | |
301 | requestPayload: Request | |
302 | ): boolean { | |
303 | if (this.jsonSchemas.has(commandName)) { | |
304 | return this.validateRequestPayload( | |
305 | chargingStation, | |
306 | commandName, | |
307 | this.jsonSchemas.get(commandName), | |
308 | requestPayload | |
309 | ); | |
310 | } | |
311 | logger.warn( | |
ee307db5 | 312 | `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation` |
9c5c4195 JB |
313 | ); |
314 | return false; | |
315 | } | |
c0560973 | 316 | } |