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 | ||
7 | import { JSONSchemaType } from 'ajv'; | |
8 | ||
8114d10e | 9 | import OCPPError from '../../../exception/OCPPError'; |
5cc4b63b | 10 | import { JsonObject, JsonType } from '../../../types/JsonType'; |
b52c969d JB |
11 | import { OCPP16MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues'; |
12 | import { | |
13 | DiagnosticsStatusNotificationRequest, | |
14 | OCPP16BootNotificationRequest, | |
15 | OCPP16HeartbeatRequest, | |
16 | OCPP16RequestCommand, | |
17 | OCPP16StatusNotificationRequest, | |
18 | } from '../../../types/ocpp/1.6/Requests'; | |
19 | import { | |
20 | OCPP16AuthorizeRequest, | |
21 | OCPP16StartTransactionRequest, | |
22 | OCPP16StopTransactionRequest, | |
23 | } from '../../../types/ocpp/1.6/Transaction'; | |
8114d10e | 24 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
be9b0d50 | 25 | import { RequestParams } from '../../../types/ocpp/Requests'; |
8114d10e | 26 | import Constants from '../../../utils/Constants'; |
b52c969d | 27 | import logger from '../../../utils/Logger'; |
c0560973 | 28 | import Utils from '../../../utils/Utils'; |
8114d10e | 29 | import type ChargingStation from '../../ChargingStation'; |
65554cc3 | 30 | import { ChargingStationUtils } from '../../ChargingStationUtils'; |
8114d10e JB |
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 | ], | |
142 | ]); | |
9f2e3130 JB |
143 | } |
144 | ||
5cc4b63b | 145 | public async requestHandler<Request extends JsonType, Response extends JsonType>( |
08f130a0 | 146 | chargingStation: ChargingStation, |
94a464f9 | 147 | commandName: OCPP16RequestCommand, |
5cc4b63b | 148 | commandParams?: JsonType, |
be9b0d50 | 149 | params?: RequestParams |
f22266fd | 150 | ): Promise<Response> { |
ada189a8 | 151 | if (ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation)) { |
b52c969d JB |
152 | const requestPayload = this.buildRequestPayload<Request>( |
153 | chargingStation, | |
154 | commandName, | |
155 | commandParams | |
156 | ); | |
157 | if (this.jsonSchemas.has(commandName)) { | |
158 | this.validateRequestPayload( | |
159 | chargingStation, | |
160 | commandName, | |
161 | this.jsonSchemas.get(commandName), | |
162 | requestPayload | |
163 | ); | |
164 | } else { | |
165 | logger.warn( | |
166 | `${chargingStation.logPrefix()} ${moduleName}.requestHandler: No JSON schema found for command ${commandName} PDU validation` | |
167 | ); | |
168 | } | |
f22266fd | 169 | return (await this.sendMessage( |
08f130a0 | 170 | chargingStation, |
94a464f9 | 171 | Utils.generateUUID(), |
b52c969d | 172 | requestPayload, |
94a464f9 JB |
173 | commandName, |
174 | params | |
f22266fd | 175 | )) as unknown as Response; |
94a464f9 JB |
176 | } |
177 | throw new OCPPError( | |
178 | ErrorType.NOT_SUPPORTED, | |
ada189a8 | 179 | `${moduleName}.requestHandler: Unsupported OCPP command '${commandName}'`, |
94a464f9 | 180 | commandName, |
7369e417 | 181 | commandParams |
94a464f9 | 182 | ); |
c0560973 JB |
183 | } |
184 | ||
5cc4b63b | 185 | private buildRequestPayload<Request extends JsonType>( |
08f130a0 | 186 | chargingStation: ChargingStation, |
78085c42 | 187 | commandName: OCPP16RequestCommand, |
5cc4b63b | 188 | commandParams?: JsonType |
f22266fd | 189 | ): Request { |
68c993d5 | 190 | let connectorId: number; |
5cc4b63b | 191 | commandParams = commandParams as JsonObject; |
78085c42 JB |
192 | switch (commandName) { |
193 | case OCPP16RequestCommand.AUTHORIZE: | |
194 | return { | |
195 | ...(!Utils.isUndefined(commandParams?.idTag) | |
196 | ? { idTag: commandParams.idTag } | |
197 | : { idTag: Constants.DEFAULT_IDTAG }), | |
f22266fd | 198 | } as unknown as Request; |
78085c42 JB |
199 | case OCPP16RequestCommand.BOOT_NOTIFICATION: |
200 | return { | |
201 | chargePointModel: commandParams?.chargePointModel, | |
202 | chargePointVendor: commandParams?.chargePointVendor, | |
203 | ...(!Utils.isUndefined(commandParams?.chargeBoxSerialNumber) && { | |
204 | chargeBoxSerialNumber: commandParams.chargeBoxSerialNumber, | |
205 | }), | |
206 | ...(!Utils.isUndefined(commandParams?.chargePointSerialNumber) && { | |
207 | chargePointSerialNumber: commandParams.chargePointSerialNumber, | |
208 | }), | |
209 | ...(!Utils.isUndefined(commandParams?.firmwareVersion) && { | |
210 | firmwareVersion: commandParams.firmwareVersion, | |
211 | }), | |
212 | ...(!Utils.isUndefined(commandParams?.iccid) && { iccid: commandParams.iccid }), | |
213 | ...(!Utils.isUndefined(commandParams?.imsi) && { imsi: commandParams.imsi }), | |
214 | ...(!Utils.isUndefined(commandParams?.meterSerialNumber) && { | |
215 | meterSerialNumber: commandParams.meterSerialNumber, | |
216 | }), | |
217 | ...(!Utils.isUndefined(commandParams?.meterType) && { | |
218 | meterType: commandParams.meterType, | |
219 | }), | |
f22266fd | 220 | } as unknown as Request; |
78085c42 JB |
221 | case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION: |
222 | return { | |
223 | status: commandParams?.diagnosticsStatus, | |
f22266fd | 224 | } as unknown as Request; |
78085c42 | 225 | case OCPP16RequestCommand.HEARTBEAT: |
f22266fd | 226 | return {} as unknown as Request; |
78085c42 JB |
227 | case OCPP16RequestCommand.METER_VALUES: |
228 | return { | |
229 | connectorId: commandParams?.connectorId, | |
230 | transactionId: commandParams?.transactionId, | |
7369e417 | 231 | meterValue: commandParams?.meterValue, |
f22266fd | 232 | } as unknown as Request; |
78085c42 JB |
233 | case OCPP16RequestCommand.STATUS_NOTIFICATION: |
234 | return { | |
235 | connectorId: commandParams?.connectorId, | |
78085c42 | 236 | status: commandParams?.status, |
93b4a429 | 237 | errorCode: commandParams?.errorCode, |
f22266fd | 238 | } as unknown as Request; |
78085c42 JB |
239 | case OCPP16RequestCommand.START_TRANSACTION: |
240 | return { | |
241 | connectorId: commandParams?.connectorId, | |
242 | ...(!Utils.isUndefined(commandParams?.idTag) | |
243 | ? { idTag: commandParams?.idTag } | |
244 | : { idTag: Constants.DEFAULT_IDTAG }), | |
08f130a0 | 245 | meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId( |
78085c42 JB |
246 | commandParams?.connectorId as number |
247 | ), | |
248 | timestamp: new Date().toISOString(), | |
f22266fd | 249 | } as unknown as Request; |
78085c42 | 250 | case OCPP16RequestCommand.STOP_TRANSACTION: |
08f130a0 | 251 | connectorId = chargingStation.getConnectorIdByTransactionId( |
f479a792 JB |
252 | commandParams?.transactionId as number |
253 | ); | |
78085c42 JB |
254 | return { |
255 | transactionId: commandParams?.transactionId, | |
256 | ...(!Utils.isUndefined(commandParams?.idTag) && { idTag: commandParams.idTag }), | |
257 | meterStop: commandParams?.meterStop, | |
258 | timestamp: new Date().toISOString(), | |
259 | ...(commandParams?.reason && { reason: commandParams.reason }), | |
08f130a0 | 260 | ...(chargingStation.getTransactionDataMeterValues() && { |
78085c42 | 261 | transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( |
08f130a0 | 262 | chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, |
78085c42 | 263 | OCPP16ServiceUtils.buildTransactionEndMeterValue( |
08f130a0 | 264 | chargingStation, |
68c993d5 | 265 | connectorId, |
78085c42 JB |
266 | commandParams?.meterStop as number |
267 | ) | |
268 | ), | |
269 | }), | |
f22266fd | 270 | } as unknown as Request; |
78085c42 JB |
271 | default: |
272 | throw new OCPPError( | |
273 | ErrorType.NOT_SUPPORTED, | |
274 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
ada189a8 | 275 | `${moduleName}.buildRequestPayload: Unsupported OCPP command '${commandName}'`, |
78085c42 | 276 | commandName, |
7369e417 | 277 | commandParams |
78085c42 JB |
278 | ); |
279 | } | |
280 | } | |
c0560973 | 281 | } |