X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16ResponseService.ts;h=dc68dd74beb7b2b342af9bd56c5ee0f6b5586bd3;hb=1c3cfd4f4074733491c8e9ceee59c044a0d0800b;hp=9093187d38a23c10f527932eca65702cc206acac;hpb=ada189a839743451d477d5b3bddc698f2634d799;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 9093187d..dc68dd74 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -1,7 +1,13 @@ // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +import { JSONSchemaType } from 'ajv'; + import OCPPError from '../../../exception/OCPPError'; -import { JsonType } from '../../../types/JsonType'; +import { JsonObject, JsonType } from '../../../types/JsonType'; import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode'; import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration'; @@ -15,7 +21,9 @@ import { OCPP16StatusNotificationRequest, } from '../../../types/ocpp/1.6/Requests'; import { + DiagnosticsStatusNotificationResponse, OCPP16BootNotificationResponse, + OCPP16HeartbeatResponse, OCPP16RegistrationStatus, OCPP16StatusNotificationResponse, } from '../../../types/ocpp/1.6/Responses'; @@ -42,6 +50,7 @@ const moduleName = 'OCPP16ResponseService'; export default class OCPP16ResponseService extends OCPPResponseService { private responseHandlers: Map; + private jsonSchemas: Map>; public constructor() { if (new.target?.name === moduleName) { @@ -50,13 +59,113 @@ export default class OCPP16ResponseService extends OCPPResponseService { super(); this.responseHandlers = new Map([ [OCPP16RequestCommand.BOOT_NOTIFICATION, this.handleResponseBootNotification.bind(this)], - [OCPP16RequestCommand.HEARTBEAT, this.handleResponseHeartbeat.bind(this)], + [OCPP16RequestCommand.HEARTBEAT, this.emptyResponseHandler.bind(this)], [OCPP16RequestCommand.AUTHORIZE, this.handleResponseAuthorize.bind(this)], [OCPP16RequestCommand.START_TRANSACTION, this.handleResponseStartTransaction.bind(this)], [OCPP16RequestCommand.STOP_TRANSACTION, this.handleResponseStopTransaction.bind(this)], - [OCPP16RequestCommand.STATUS_NOTIFICATION, this.handleResponseStatusNotification.bind(this)], - [OCPP16RequestCommand.METER_VALUES, this.handleResponseMeterValues.bind(this)], + [OCPP16RequestCommand.STATUS_NOTIFICATION, this.emptyResponseHandler.bind(this)], + [OCPP16RequestCommand.METER_VALUES, this.emptyResponseHandler.bind(this)], + [OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, this.emptyResponseHandler.bind(this)], + ]); + this.jsonSchemas = new Map>([ + [ + OCPP16RequestCommand.BOOT_NOTIFICATION, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/BootNotificationResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.HEARTBEAT, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/HeartbeatResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.AUTHORIZE, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/AuthorizeResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.START_TRANSACTION, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/StartTransactionResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.STOP_TRANSACTION, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/StopTransactionResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.STATUS_NOTIFICATION, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/StatusNotificationResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.METER_VALUES, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/MeterValuesResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotificationResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], ]); + this.validatePayload.bind(this); } public async responseHandler( @@ -71,16 +180,17 @@ export default class OCPP16ResponseService extends OCPPResponseService { ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation) ) { try { + this.validatePayload(chargingStation, commandName, payload); await this.responseHandlers.get(commandName)(chargingStation, payload, requestPayload); } catch (error) { - logger.error(chargingStation.logPrefix() + ' Handle request response error: %j', error); + logger.error(chargingStation.logPrefix() + ' Handle request response error:', error); throw error; } } else { // Throw exception throw new OCPPError( ErrorType.NOT_IMPLEMENTED, - `${commandName} is not implemented to handle request response payload ${JSON.stringify( + `${commandName} is not implemented to handle request response PDU ${JSON.stringify( payload, null, 2 @@ -92,7 +202,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { } else { throw new OCPPError( ErrorType.SECURITY_ERROR, - `${commandName} cannot be issued to handle request response payload ${JSON.stringify( + `${commandName} cannot be issued to handle request response PDU ${JSON.stringify( payload, null, 2 @@ -103,6 +213,25 @@ export default class OCPP16ResponseService extends OCPPResponseService { } } + private validatePayload( + chargingStation: ChargingStation, + commandName: OCPP16RequestCommand, + payload: JsonType + ): boolean { + if (this.jsonSchemas.has(commandName)) { + return this.validateResponsePayload( + chargingStation, + commandName, + this.jsonSchemas.get(commandName), + payload + ); + } + logger.warn( + `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation` + ); + return false; + } + private handleResponseBootNotification( chargingStation: ChargingStation, payload: OCPP16BootNotificationResponse @@ -142,9 +271,6 @@ export default class OCPP16ResponseService extends OCPPResponseService { } } - // eslint-disable-next-line @typescript-eslint/no-empty-function - private handleResponseHeartbeat(): void {} - private handleResponseAuthorize( chargingStation: ChargingStation, payload: OCPP16AuthorizeResponse, @@ -471,10 +597,4 @@ export default class OCPP16ResponseService extends OCPPResponseService { ); } } - - // eslint-disable-next-line @typescript-eslint/no-empty-function - private handleResponseStatusNotification(): void {} - - // eslint-disable-next-line @typescript-eslint/no-empty-function - private handleResponseMeterValues(): void {} }