X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16RequestService.ts;h=4fa0190e84224464f92b8d2e4cda4cb35bc68b7a;hb=03ebf4c1db6ba11903b42e56692ed3d8538ba1d3;hp=2a2d4dfcf8f74e1217901348cbebe9d5b8a53199;hpb=8114d10e3893e96bb725ce2fca9744429ee4b75b;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 2a2d4dfc..4fa0190e 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -1,13 +1,33 @@ // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +import type { JSONSchemaType } from 'ajv'; + import OCPPError from '../../../exception/OCPPError'; -import { JsonObject, JsonType } from '../../../types/JsonType'; -import { OCPP16RequestCommand } from '../../../types/ocpp/1.6/Requests'; +import type { JsonObject, JsonType } from '../../../types/JsonType'; +import type { OCPP16MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues'; +import { + DiagnosticsStatusNotificationRequest, + OCPP16BootNotificationRequest, + OCPP16HeartbeatRequest, + OCPP16RequestCommand, + OCPP16StatusNotificationRequest, +} from '../../../types/ocpp/1.6/Requests'; +import type { + OCPP16AuthorizeRequest, + OCPP16StartTransactionRequest, + OCPP16StopTransactionRequest, +} from '../../../types/ocpp/1.6/Transaction'; import { ErrorType } from '../../../types/ocpp/ErrorType'; -import { RequestParams } from '../../../types/ocpp/Requests'; +import type { RequestParams } from '../../../types/ocpp/Requests'; import Constants from '../../../utils/Constants'; +import logger from '../../../utils/Logger'; import Utils from '../../../utils/Utils'; import type ChargingStation from '../../ChargingStation'; +import { ChargingStationUtils } from '../../ChargingStationUtils'; import OCPPRequestService from '../OCPPRequestService'; import type OCPPResponseService from '../OCPPResponseService'; import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; @@ -15,33 +35,141 @@ import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; const moduleName = 'OCPP16RequestService'; export default class OCPP16RequestService extends OCPPRequestService { + private jsonSchemas: Map>; + public constructor(ocppResponseService: OCPPResponseService) { if (new.target?.name === moduleName) { throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); } super(ocppResponseService); + this.jsonSchemas = new Map>([ + [ + OCPP16RequestCommand.AUTHORIZE, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/Authorize.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.BOOT_NOTIFICATION, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/BootNotification.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/DiagnosticsStatusNotification.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.HEARTBEAT, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/Heartbeat.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/MeterValues.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/StatusNotification.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/StartTransaction.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/StopTransaction.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + ]); + this.buildRequestPayload.bind(this); + this.validatePayload.bind(this); } - public async requestHandler( + public async requestHandler( chargingStation: ChargingStation, commandName: OCPP16RequestCommand, commandParams?: JsonType, params?: RequestParams - ): Promise { - if (Object.values(OCPP16RequestCommand).includes(commandName)) { + ): Promise { + if (ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation)) { + const requestPayload = this.buildRequestPayload( + chargingStation, + commandName, + commandParams + ); + this.validatePayload(chargingStation, commandName, requestPayload); return (await this.sendMessage( chargingStation, Utils.generateUUID(), - this.buildRequestPayload(chargingStation, commandName, commandParams), + requestPayload, commandName, params - )) as unknown as Response; + )) as unknown as ResponseType; } throw new OCPPError( ErrorType.NOT_SUPPORTED, - `${moduleName}.requestHandler: Unsupported OCPP command ${commandName}`, + `Unsupported OCPP command '${commandName}'`, commandName, - { commandName } + commandParams ); } @@ -91,9 +219,7 @@ export default class OCPP16RequestService extends OCPPRequestService { return { connectorId: commandParams?.connectorId, transactionId: commandParams?.transactionId, - meterValue: Array.isArray(commandParams?.meterValue) - ? commandParams?.meterValue - : [commandParams?.meterValue], + meterValue: commandParams?.meterValue, } as unknown as Request; case OCPP16RequestCommand.STATUS_NOTIFICATION: return { @@ -137,10 +263,29 @@ export default class OCPP16RequestService extends OCPPRequestService { throw new OCPPError( ErrorType.NOT_SUPPORTED, // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - `${moduleName}.buildRequestPayload: Unsupported OCPP command: ${commandName}`, + `Unsupported OCPP command '${commandName}'`, commandName, - { commandName } + commandParams ); } } + + private validatePayload( + chargingStation: ChargingStation, + commandName: OCPP16RequestCommand, + requestPayload: Request + ): boolean { + if (this.jsonSchemas.has(commandName)) { + return this.validateRequestPayload( + chargingStation, + commandName, + this.jsonSchemas.get(commandName), + requestPayload + ); + } + logger.warn( + `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation` + ); + return false; + } }