X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16RequestService.ts;h=4fa0190e84224464f92b8d2e4cda4cb35bc68b7a;hb=03ebf4c1db6ba11903b42e56692ed3d8538ba1d3;hp=29ea6f393288efd0afaf539b2b8d4351340da56a;hpb=b52c969dc8bfbd4bc2a2b6f1fc74e3868c4a091d;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 29ea6f39..4fa0190e 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -4,11 +4,11 @@ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; -import { JSONSchemaType } from 'ajv'; +import type { JSONSchemaType } from 'ajv'; import OCPPError from '../../../exception/OCPPError'; -import { JsonObject, JsonType } from '../../../types/JsonType'; -import { OCPP16MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues'; +import type { JsonObject, JsonType } from '../../../types/JsonType'; +import type { OCPP16MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues'; import { DiagnosticsStatusNotificationRequest, OCPP16BootNotificationRequest, @@ -16,13 +16,13 @@ import { OCPP16RequestCommand, OCPP16StatusNotificationRequest, } from '../../../types/ocpp/1.6/Requests'; -import { +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'; @@ -140,43 +140,34 @@ export default class OCPP16RequestService extends OCPPRequestService { ) 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 { + ): Promise { if (ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation)) { - const requestPayload = this.buildRequestPayload( + const requestPayload = this.buildRequestPayload( chargingStation, commandName, commandParams ); - if (this.jsonSchemas.has(commandName)) { - this.validateRequestPayload( - chargingStation, - commandName, - this.jsonSchemas.get(commandName), - requestPayload - ); - } else { - logger.warn( - `${chargingStation.logPrefix()} ${moduleName}.requestHandler: No JSON schema found for command ${commandName} PDU validation` - ); - } + this.validatePayload(chargingStation, commandName, requestPayload); return (await this.sendMessage( chargingStation, Utils.generateUUID(), 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, commandParams ); @@ -225,15 +216,6 @@ export default class OCPP16RequestService extends OCPPRequestService { case OCPP16RequestCommand.HEARTBEAT: return {} as unknown as Request; case OCPP16RequestCommand.METER_VALUES: - // Sanity check - if (!Array.isArray(commandParams?.meterValue)) { - throw new OCPPError( - ErrorType.TYPE_CONSTRAINT_VIOLATION, - `${moduleName}.buildRequestPayload ${commandName}: Invalid array type for meterValue PDU field`, - commandName, - commandParams - ); - } return { connectorId: commandParams?.connectorId, transactionId: commandParams?.transactionId, @@ -281,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, 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; + } }