X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPIncomingRequestService.ts;h=5eaddae90976cef25602583412ebd02e899909bb;hb=13a6f27c10768faa05acf33fd8e0637511d49e3e;hp=41c0e49bc844ab0c7073a1d254ad38cb50c9ba55;hpb=27f08ad31f3e69c0681005edc6e9fec49d0450c2;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 41c0e49b..5eaddae9 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -1,65 +1,86 @@ -import { AsyncResource } from 'async_hooks'; +import _Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv' +import _ajvFormats from 'ajv-formats' -import Ajv, { type JSONSchemaType } from 'ajv'; -import ajvFormats from 'ajv-formats'; +import { OCPPConstants } from './OCPPConstants.js' +import { OCPPServiceUtils } from './OCPPServiceUtils.js' +import { type ChargingStation, getIdTagsFile } from '../../charging-station/index.js' +import { OCPPError } from '../../exception/index.js' +import type { + ClearCacheResponse, + HandleErrorParams, + IncomingRequestCommand, + JsonType, + OCPPVersion +} from '../../types/index.js' +import { logger, setDefaultErrorParams } from '../../utils/index.js' +type Ajv = _Ajv.default +// eslint-disable-next-line @typescript-eslint/no-redeclare +const Ajv = _Ajv.default +const ajvFormats = _ajvFormats.default -import OCPPConstants from './OCPPConstants'; -import { OCPPServiceUtils } from './OCPPServiceUtils'; -import OCPPError from '../../exception/OCPPError'; -import type { HandleErrorParams } from '../../types/Error'; -import type { JsonObject, JsonType } from '../../types/JsonType'; -import type { OCPPVersion } from '../../types/ocpp/OCPPVersion'; -import type { IncomingRequestCommand } from '../../types/ocpp/Requests'; -import type { ClearCacheResponse } from '../../types/ocpp/Responses'; -import logger from '../../utils/Logger'; -import type ChargingStation from '../ChargingStation'; -import { ChargingStationUtils } from '../ChargingStationUtils'; +const moduleName = 'OCPPIncomingRequestService' -const moduleName = 'OCPPIncomingRequestService'; +export abstract class OCPPIncomingRequestService { + private static instance: OCPPIncomingRequestService | null = null + private readonly version: OCPPVersion + private readonly ajv: Ajv + private readonly jsonValidateFunctions: Map> + protected abstract jsonSchemas: Map> -export default abstract class OCPPIncomingRequestService extends AsyncResource { - private static instance: OCPPIncomingRequestService | null = null; - private readonly version: OCPPVersion; - private readonly ajv: Ajv; - protected abstract jsonSchemas: Map>; - - protected constructor(version: OCPPVersion) { - super(moduleName); - this.version = version; + protected constructor (version: OCPPVersion) { + this.version = version this.ajv = new Ajv({ keywords: ['javaType'], - multipleOfPrecision: 2, - }); - ajvFormats(this.ajv); - this.incomingRequestHandler.bind(this); - this.validateIncomingRequestPayload.bind(this); + multipleOfPrecision: 2 + }) + ajvFormats(this.ajv) + this.jsonValidateFunctions = new Map>() + this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as < + ReqType extends JsonType, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + ResType extends JsonType + >( + chargingStation: ChargingStation, + messageId: string, + commandName: IncomingRequestCommand, + commandPayload: ReqType + ) => Promise + this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as < + T extends JsonType + >( + chargingStation: ChargingStation, + commandName: IncomingRequestCommand, + schema: JSONSchemaType, + payload: T + ) => boolean } public static getInstance(this: new () => T): T { if (OCPPIncomingRequestService.instance === null) { - OCPPIncomingRequestService.instance = new this(); + OCPPIncomingRequestService.instance = new this() } - return OCPPIncomingRequestService.instance as T; + return OCPPIncomingRequestService.instance as T } - protected handleIncomingRequestError( + protected handleIncomingRequestError( chargingStation: ChargingStation, commandName: IncomingRequestCommand, error: Error, - params: HandleErrorParams = { throwError: true } + params: HandleErrorParams = { throwError: true, consoleOut: false } ): T | undefined { + setDefaultErrorParams(params) logger.error( `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, error - ); - if (!params?.throwError && params?.errorResponse) { - return params?.errorResponse; + ) + if (params.throwError === false && params.errorResponse != null) { + return params.errorResponse } - if (params?.throwError && !params?.errorResponse) { - throw error; + if (params.throwError === true && params.errorResponse == null) { + throw error } - if (params?.throwError && params?.errorResponse) { - return params?.errorResponse; + if (params.throwError === true && params.errorResponse != null) { + return params.errorResponse } } @@ -69,36 +90,49 @@ export default abstract class OCPPIncomingRequestService extends AsyncResource { schema: JSONSchemaType, payload: T ): boolean { - if (chargingStation.getPayloadSchemaValidation() === false) { - return true; + if (chargingStation.stationInfo?.ocppStrictCompliance === false) { + return true } - const validate = this.ajv.compile(schema); + const validate = this.getJsonIncomingRequestValidateFunction(commandName, schema) if (validate(payload)) { - return true; + return true } logger.error( `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`, validate.errors - ); + ) throw new OCPPError( OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), 'Incoming request PDU is invalid', commandName, - JSON.stringify(validate.errors, null, 2) - ); + JSON.stringify(validate.errors, undefined, 2) + ) + } + + protected handleRequestClearCache (chargingStation: ChargingStation): ClearCacheResponse { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo!)!)) { + return OCPPConstants.OCPP_RESPONSE_ACCEPTED + } + return OCPPConstants.OCPP_RESPONSE_REJECTED } - protected handleRequestClearCache(chargingStation: ChargingStation): ClearCacheResponse { - chargingStation.authorizedTagsCache.deleteAuthorizedTags( - ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo) - ); - return OCPPConstants.OCPP_RESPONSE_ACCEPTED; + private getJsonIncomingRequestValidateFunction( + commandName: IncomingRequestCommand, + schema: JSONSchemaType + ): ValidateFunction { + if (!this.jsonValidateFunctions.has(commandName)) { + this.jsonValidateFunctions.set(commandName, this.ajv.compile(schema).bind(this)) + } + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return this.jsonValidateFunctions.get(commandName)! } - public abstract incomingRequestHandler( + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public abstract incomingRequestHandler( chargingStation: ChargingStation, messageId: string, commandName: IncomingRequestCommand, - commandPayload: JsonType - ): Promise; + commandPayload: ReqType + ): Promise }