X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16IncomingRequestService.ts;h=c870fbc14107624b0f942a09e5c9430c6e3144d6;hb=6c1761d470507ea23d186be61b94ca7375c5144a;hp=3238722568d9cc92bae51cd1476a1e90a10d0b77;hpb=b52c969dc8bfbd4bc2a2b6f1fc74e3868c4a091d;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 32387225..c870fbc1 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -4,12 +4,12 @@ import fs from 'fs'; import path from 'path'; import { URL, fileURLToPath } from 'url'; -import { JSONSchemaType } from 'ajv'; +import type { JSONSchemaType } from 'ajv'; import { Client, FTPResponse } from 'basic-ftp'; import tar from 'tar'; import OCPPError from '../../../exception/OCPPError'; -import { JsonObject, JsonType } from '../../../types/JsonType'; +import type { JsonObject, JsonType } from '../../../types/JsonType'; import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode'; import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; import { @@ -21,7 +21,7 @@ import { OCPP16SupportedFeatureProfiles, } from '../../../types/ocpp/1.6/Configuration'; import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus'; -import { +import type { OCPP16MeterValuesRequest, OCPP16MeterValuesResponse, } from '../../../types/ocpp/1.6/MeterValues'; @@ -47,7 +47,7 @@ import { SetChargingProfileRequest, UnlockConnectorRequest, } from '../../../types/ocpp/1.6/Requests'; -import { +import type { ChangeAvailabilityResponse, ChangeConfigurationResponse, ClearChargingProfileResponse, @@ -71,10 +71,10 @@ import { OCPP16StopTransactionRequest, OCPP16StopTransactionResponse, } from '../../../types/ocpp/1.6/Transaction'; -import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration'; +import type { OCPPConfigurationKey } from '../../../types/ocpp/Configuration'; import { ErrorType } from '../../../types/ocpp/ErrorType'; -import { IncomingRequestHandler } from '../../../types/ocpp/Requests'; -import { DefaultResponse } from '../../../types/ocpp/Responses'; +import type { IncomingRequestHandler } from '../../../types/ocpp/Requests'; +import type { DefaultResponse } from '../../../types/ocpp/Responses'; import Constants from '../../../utils/Constants'; import logger from '../../../utils/Logger'; import Utils from '../../../utils/Utils'; @@ -276,6 +276,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ) as JSONSchemaType, ], ]); + this.validatePayload.bind(this); } public async incomingRequestHandler( @@ -311,18 +312,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ChargingStationUtils.isIncomingRequestCommandSupported(commandName, chargingStation) ) { try { - if (this.jsonSchemas.has(commandName)) { - this.validateIncomingRequestPayload( - chargingStation, - commandName, - this.jsonSchemas.get(commandName), - commandPayload - ); - } else { - logger.warn( - `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: No JSON schema found for command ${commandName} PDU validation` - ); - } + this.validatePayload(chargingStation, commandName, commandPayload); // Call the method to build the response response = await this.incomingRequestHandlers.get(commandName)( chargingStation, @@ -330,7 +320,10 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ); } catch (error) { // Log - logger.error(chargingStation.logPrefix() + ' Handle request error: %j', error); + logger.error( + `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`, + error + ); throw error; } } else { @@ -367,6 +360,25 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ); } + private validatePayload( + chargingStation: ChargingStation, + commandName: OCPP16IncomingRequestCommand, + commandPayload: JsonType + ): boolean { + if (this.jsonSchemas.has(commandName)) { + return this.validateIncomingRequestPayload( + chargingStation, + commandName, + this.jsonSchemas.get(commandName), + commandPayload + ); + } + logger.warn( + `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation` + ); + return false; + } + // Simulate charging station restart private handleRequestReset( chargingStation: ChargingStation,