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