| 1 | // Partial Copyright Jerome Benoit. 2021. 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 | DiagnosticsStatusNotificationRequest, |
| 14 | OCPP16BootNotificationRequest, |
| 15 | OCPP16HeartbeatRequest, |
| 16 | OCPP16RequestCommand, |
| 17 | OCPP16StatusNotificationRequest, |
| 18 | } from '../../../types/ocpp/1.6/Requests'; |
| 19 | import type { |
| 20 | OCPP16AuthorizeRequest, |
| 21 | OCPP16StartTransactionRequest, |
| 22 | OCPP16StopTransactionRequest, |
| 23 | } from '../../../types/ocpp/1.6/Transaction'; |
| 24 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
| 25 | import type { RequestParams } from '../../../types/ocpp/Requests'; |
| 26 | import Constants from '../../../utils/Constants'; |
| 27 | import logger from '../../../utils/Logger'; |
| 28 | import Utils from '../../../utils/Utils'; |
| 29 | import type ChargingStation from '../../ChargingStation'; |
| 30 | import { ChargingStationUtils } from '../../ChargingStationUtils'; |
| 31 | import OCPPRequestService from '../OCPPRequestService'; |
| 32 | import type OCPPResponseService from '../OCPPResponseService'; |
| 33 | import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; |
| 34 | |
| 35 | const moduleName = 'OCPP16RequestService'; |
| 36 | |
| 37 | export default class OCPP16RequestService extends OCPPRequestService { |
| 38 | private jsonSchemas: Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>; |
| 39 | |
| 40 | public constructor(ocppResponseService: OCPPResponseService) { |
| 41 | if (new.target?.name === moduleName) { |
| 42 | throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); |
| 43 | } |
| 44 | super(ocppResponseService); |
| 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 | ]); |
| 143 | this.buildRequestPayload.bind(this); |
| 144 | this.validatePayload.bind(this); |
| 145 | } |
| 146 | |
| 147 | public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>( |
| 148 | chargingStation: ChargingStation, |
| 149 | commandName: OCPP16RequestCommand, |
| 150 | commandParams?: JsonType, |
| 151 | params?: RequestParams |
| 152 | ): Promise<ResponseType> { |
| 153 | if (ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation)) { |
| 154 | const requestPayload = this.buildRequestPayload<RequestType>( |
| 155 | chargingStation, |
| 156 | commandName, |
| 157 | commandParams |
| 158 | ); |
| 159 | this.validatePayload(chargingStation, commandName, requestPayload); |
| 160 | return (await this.sendMessage( |
| 161 | chargingStation, |
| 162 | Utils.generateUUID(), |
| 163 | requestPayload, |
| 164 | commandName, |
| 165 | params |
| 166 | )) as unknown as ResponseType; |
| 167 | } |
| 168 | throw new OCPPError( |
| 169 | ErrorType.NOT_SUPPORTED, |
| 170 | `Unsupported OCPP command '${commandName}'`, |
| 171 | commandName, |
| 172 | commandParams |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | private buildRequestPayload<Request extends JsonType>( |
| 177 | chargingStation: ChargingStation, |
| 178 | commandName: OCPP16RequestCommand, |
| 179 | commandParams?: JsonType |
| 180 | ): Request { |
| 181 | let connectorId: number; |
| 182 | commandParams = commandParams as JsonObject; |
| 183 | switch (commandName) { |
| 184 | case OCPP16RequestCommand.AUTHORIZE: |
| 185 | return { |
| 186 | ...(!Utils.isUndefined(commandParams?.idTag) |
| 187 | ? { idTag: commandParams.idTag } |
| 188 | : { idTag: Constants.DEFAULT_IDTAG }), |
| 189 | } as unknown as Request; |
| 190 | case OCPP16RequestCommand.BOOT_NOTIFICATION: |
| 191 | return { |
| 192 | chargePointModel: commandParams?.chargePointModel, |
| 193 | chargePointVendor: commandParams?.chargePointVendor, |
| 194 | ...(!Utils.isUndefined(commandParams?.chargeBoxSerialNumber) && { |
| 195 | chargeBoxSerialNumber: commandParams.chargeBoxSerialNumber, |
| 196 | }), |
| 197 | ...(!Utils.isUndefined(commandParams?.chargePointSerialNumber) && { |
| 198 | chargePointSerialNumber: commandParams.chargePointSerialNumber, |
| 199 | }), |
| 200 | ...(!Utils.isUndefined(commandParams?.firmwareVersion) && { |
| 201 | firmwareVersion: commandParams.firmwareVersion, |
| 202 | }), |
| 203 | ...(!Utils.isUndefined(commandParams?.iccid) && { iccid: commandParams.iccid }), |
| 204 | ...(!Utils.isUndefined(commandParams?.imsi) && { imsi: commandParams.imsi }), |
| 205 | ...(!Utils.isUndefined(commandParams?.meterSerialNumber) && { |
| 206 | meterSerialNumber: commandParams.meterSerialNumber, |
| 207 | }), |
| 208 | ...(!Utils.isUndefined(commandParams?.meterType) && { |
| 209 | meterType: commandParams.meterType, |
| 210 | }), |
| 211 | } as unknown as Request; |
| 212 | case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION: |
| 213 | return { |
| 214 | status: commandParams?.diagnosticsStatus, |
| 215 | } as unknown as Request; |
| 216 | case OCPP16RequestCommand.HEARTBEAT: |
| 217 | return {} as unknown as Request; |
| 218 | case OCPP16RequestCommand.METER_VALUES: |
| 219 | return { |
| 220 | connectorId: commandParams?.connectorId, |
| 221 | transactionId: commandParams?.transactionId, |
| 222 | meterValue: commandParams?.meterValue, |
| 223 | } as unknown as Request; |
| 224 | case OCPP16RequestCommand.STATUS_NOTIFICATION: |
| 225 | return { |
| 226 | connectorId: commandParams?.connectorId, |
| 227 | status: commandParams?.status, |
| 228 | errorCode: commandParams?.errorCode, |
| 229 | } as unknown as Request; |
| 230 | case OCPP16RequestCommand.START_TRANSACTION: |
| 231 | return { |
| 232 | connectorId: commandParams?.connectorId, |
| 233 | ...(!Utils.isUndefined(commandParams?.idTag) |
| 234 | ? { idTag: commandParams?.idTag } |
| 235 | : { idTag: Constants.DEFAULT_IDTAG }), |
| 236 | meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId( |
| 237 | commandParams?.connectorId as number |
| 238 | ), |
| 239 | timestamp: new Date().toISOString(), |
| 240 | } as unknown as Request; |
| 241 | case OCPP16RequestCommand.STOP_TRANSACTION: |
| 242 | connectorId = chargingStation.getConnectorIdByTransactionId( |
| 243 | commandParams?.transactionId as number |
| 244 | ); |
| 245 | return { |
| 246 | transactionId: commandParams?.transactionId, |
| 247 | ...(!Utils.isUndefined(commandParams?.idTag) && { idTag: commandParams.idTag }), |
| 248 | meterStop: commandParams?.meterStop, |
| 249 | timestamp: new Date().toISOString(), |
| 250 | ...(commandParams?.reason && { reason: commandParams.reason }), |
| 251 | ...(chargingStation.getTransactionDataMeterValues() && { |
| 252 | transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( |
| 253 | chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, |
| 254 | OCPP16ServiceUtils.buildTransactionEndMeterValue( |
| 255 | chargingStation, |
| 256 | connectorId, |
| 257 | commandParams?.meterStop as number |
| 258 | ) |
| 259 | ), |
| 260 | }), |
| 261 | } as unknown as Request; |
| 262 | default: |
| 263 | throw new OCPPError( |
| 264 | ErrorType.NOT_SUPPORTED, |
| 265 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
| 266 | `Unsupported OCPP command '${commandName}'`, |
| 267 | commandName, |
| 268 | commandParams |
| 269 | ); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | private validatePayload<Request extends JsonType>( |
| 274 | chargingStation: ChargingStation, |
| 275 | commandName: OCPP16RequestCommand, |
| 276 | requestPayload: Request |
| 277 | ): boolean { |
| 278 | if (this.jsonSchemas.has(commandName)) { |
| 279 | return this.validateRequestPayload( |
| 280 | chargingStation, |
| 281 | commandName, |
| 282 | this.jsonSchemas.get(commandName), |
| 283 | requestPayload |
| 284 | ); |
| 285 | } |
| 286 | logger.warn( |
| 287 | `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation` |
| 288 | ); |
| 289 | return false; |
| 290 | } |
| 291 | } |