Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
c8eeb62b | 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 | 12 | import { |
27782dbc JB |
13 | type DiagnosticsStatusNotificationRequest, |
14 | type OCPP16BootNotificationRequest, | |
15 | type OCPP16DataTransferRequest, | |
16 | type OCPP16HeartbeatRequest, | |
b52c969d | 17 | OCPP16RequestCommand, |
27782dbc | 18 | type OCPP16StatusNotificationRequest, |
b52c969d | 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'; |
d270cc87 | 26 | import { OCPPVersion } from '../../../types/ocpp/OCPPVersion'; |
6c1761d4 | 27 | import type { RequestParams } from '../../../types/ocpp/Requests'; |
8114d10e | 28 | import Constants from '../../../utils/Constants'; |
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 { |
b3fc3ff5 | 38 | protected jsonSchemas: Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>; |
b52c969d | 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 | } |
d270cc87 | 44 | super(OCPPVersion.VERSION_16, 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 | 155 | this.buildRequestPayload.bind(this); |
9f2e3130 JB |
156 | } |
157 | ||
6c1761d4 | 158 | public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>( |
08f130a0 | 159 | chargingStation: ChargingStation, |
94a464f9 | 160 | commandName: OCPP16RequestCommand, |
5cc4b63b | 161 | commandParams?: JsonType, |
be9b0d50 | 162 | params?: RequestParams |
6c1761d4 | 163 | ): Promise<ResponseType> { |
ed6cfcff | 164 | if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) { |
6c1761d4 | 165 | const requestPayload = this.buildRequestPayload<RequestType>( |
b52c969d JB |
166 | chargingStation, |
167 | commandName, | |
168 | commandParams | |
169 | ); | |
f22266fd | 170 | return (await this.sendMessage( |
08f130a0 | 171 | chargingStation, |
94a464f9 | 172 | Utils.generateUUID(), |
b52c969d | 173 | requestPayload, |
94a464f9 JB |
174 | commandName, |
175 | params | |
6c1761d4 | 176 | )) as unknown as ResponseType; |
94a464f9 | 177 | } |
e909d2a7 | 178 | // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). |
94a464f9 JB |
179 | throw new OCPPError( |
180 | ErrorType.NOT_SUPPORTED, | |
6c8f5d90 | 181 | `Unsupported OCPP command '${commandName}'`, |
94a464f9 | 182 | commandName, |
7369e417 | 183 | commandParams |
94a464f9 | 184 | ); |
c0560973 JB |
185 | } |
186 | ||
5cc4b63b | 187 | private buildRequestPayload<Request extends JsonType>( |
08f130a0 | 188 | chargingStation: ChargingStation, |
78085c42 | 189 | commandName: OCPP16RequestCommand, |
5cc4b63b | 190 | commandParams?: JsonType |
f22266fd | 191 | ): Request { |
68c993d5 | 192 | let connectorId: number; |
cf058664 | 193 | let energyActiveImportRegister: number; |
5cc4b63b | 194 | commandParams = commandParams as JsonObject; |
78085c42 JB |
195 | switch (commandName) { |
196 | case OCPP16RequestCommand.AUTHORIZE: | |
197 | return { | |
198 | ...(!Utils.isUndefined(commandParams?.idTag) | |
199 | ? { idTag: commandParams.idTag } | |
200 | : { idTag: Constants.DEFAULT_IDTAG }), | |
f22266fd | 201 | } as unknown as Request; |
78085c42 JB |
202 | case OCPP16RequestCommand.BOOT_NOTIFICATION: |
203 | return { | |
204 | chargePointModel: commandParams?.chargePointModel, | |
205 | chargePointVendor: commandParams?.chargePointVendor, | |
206 | ...(!Utils.isUndefined(commandParams?.chargeBoxSerialNumber) && { | |
207 | chargeBoxSerialNumber: commandParams.chargeBoxSerialNumber, | |
208 | }), | |
209 | ...(!Utils.isUndefined(commandParams?.chargePointSerialNumber) && { | |
210 | chargePointSerialNumber: commandParams.chargePointSerialNumber, | |
211 | }), | |
212 | ...(!Utils.isUndefined(commandParams?.firmwareVersion) && { | |
213 | firmwareVersion: commandParams.firmwareVersion, | |
214 | }), | |
215 | ...(!Utils.isUndefined(commandParams?.iccid) && { iccid: commandParams.iccid }), | |
216 | ...(!Utils.isUndefined(commandParams?.imsi) && { imsi: commandParams.imsi }), | |
217 | ...(!Utils.isUndefined(commandParams?.meterSerialNumber) && { | |
218 | meterSerialNumber: commandParams.meterSerialNumber, | |
219 | }), | |
220 | ...(!Utils.isUndefined(commandParams?.meterType) && { | |
221 | meterType: commandParams.meterType, | |
222 | }), | |
f22266fd | 223 | } as unknown as Request; |
78085c42 JB |
224 | case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION: |
225 | return { | |
226 | status: commandParams?.diagnosticsStatus, | |
f22266fd | 227 | } as unknown as Request; |
78085c42 | 228 | case OCPP16RequestCommand.HEARTBEAT: |
f22266fd | 229 | return {} as unknown as Request; |
78085c42 JB |
230 | case OCPP16RequestCommand.METER_VALUES: |
231 | return { | |
232 | connectorId: commandParams?.connectorId, | |
233 | transactionId: commandParams?.transactionId, | |
7369e417 | 234 | meterValue: commandParams?.meterValue, |
f22266fd | 235 | } as unknown as Request; |
78085c42 JB |
236 | case OCPP16RequestCommand.STATUS_NOTIFICATION: |
237 | return { | |
238 | connectorId: commandParams?.connectorId, | |
78085c42 | 239 | status: commandParams?.status, |
93b4a429 | 240 | errorCode: commandParams?.errorCode, |
f22266fd | 241 | } as unknown as Request; |
78085c42 JB |
242 | case OCPP16RequestCommand.START_TRANSACTION: |
243 | return { | |
244 | connectorId: commandParams?.connectorId, | |
245 | ...(!Utils.isUndefined(commandParams?.idTag) | |
246 | ? { idTag: commandParams?.idTag } | |
247 | : { idTag: Constants.DEFAULT_IDTAG }), | |
08f130a0 | 248 | meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId( |
78085c42 JB |
249 | commandParams?.connectorId as number |
250 | ), | |
aef5c03d | 251 | timestamp: new Date(), |
f22266fd | 252 | } as unknown as Request; |
78085c42 | 253 | case OCPP16RequestCommand.STOP_TRANSACTION: |
08f130a0 | 254 | connectorId = chargingStation.getConnectorIdByTransactionId( |
f479a792 JB |
255 | commandParams?.transactionId as number |
256 | ); | |
7acb3f7b JB |
257 | commandParams?.meterStop && |
258 | (energyActiveImportRegister = | |
259 | chargingStation.getEnergyActiveImportRegisterByTransactionId( | |
260 | commandParams?.transactionId as number, | |
261 | true | |
262 | )); | |
78085c42 JB |
263 | return { |
264 | transactionId: commandParams?.transactionId, | |
cf058664 JB |
265 | idTag: |
266 | commandParams?.idTag ?? | |
267 | chargingStation.getTransactionIdTag(commandParams?.transactionId as number), | |
268 | meterStop: commandParams?.meterStop ?? energyActiveImportRegister, | |
aef5c03d | 269 | timestamp: new Date(), |
cf058664 | 270 | reason: commandParams?.reason, |
08f130a0 | 271 | ...(chargingStation.getTransactionDataMeterValues() && { |
78085c42 | 272 | transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( |
08f130a0 | 273 | chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, |
78085c42 | 274 | OCPP16ServiceUtils.buildTransactionEndMeterValue( |
08f130a0 | 275 | chargingStation, |
68c993d5 | 276 | connectorId, |
cf058664 | 277 | (commandParams?.meterStop as number) ?? energyActiveImportRegister |
78085c42 JB |
278 | ) |
279 | ), | |
280 | }), | |
f22266fd | 281 | } as unknown as Request; |
91a7d3ea JB |
282 | case OCPP16RequestCommand.DATA_TRANSFER: |
283 | return commandParams as unknown as Request; | |
78085c42 | 284 | default: |
e909d2a7 | 285 | // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). |
78085c42 JB |
286 | throw new OCPPError( |
287 | ErrorType.NOT_SUPPORTED, | |
288 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
6c8f5d90 | 289 | `Unsupported OCPP command '${commandName}'`, |
78085c42 | 290 | commandName, |
7369e417 | 291 | commandParams |
78085c42 JB |
292 | ); |
293 | } | |
294 | } | |
c0560973 | 295 | } |