X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2Focpp%2FOCPPIncomingRequestService.ts;h=28de507774a281aeeb0d32b4d17b1348b45d50c5;hb=5398cecf45b4bdab604d0e6aff8a96dcc67d5ae9;hp=4b8c40f806b6c5482cb418ec71368c125df3c2d2;hpb=b3fc3ff5bc50c2dbe20eb3ac1e681c00a022b4ee;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 4b8c40f8..28de5077 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -1,36 +1,57 @@ -import { AsyncResource } from 'async_hooks'; +import { AsyncResource } from 'node:async_hooks'; -import Ajv, { type JSONSchemaType } from 'ajv'; +import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv'; import ajvFormats from 'ajv-formats'; -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 logger from '../../utils/Logger'; -import type ChargingStation from '../ChargingStation'; +import { OCPPConstants } from './OCPPConstants'; import { OCPPServiceUtils } from './OCPPServiceUtils'; +import { type ChargingStation, getIdTagsFile } from '../../charging-station'; +import { OCPPError } from '../../exception'; +import type { + ClearCacheResponse, + HandleErrorParams, + IncomingRequestCommand, + JsonType, + OCPPVersion, +} from '../../types'; +import { logger, setDefaultErrorParams } from '../../utils'; const moduleName = 'OCPPIncomingRequestService'; -export default abstract class OCPPIncomingRequestService { +export abstract class OCPPIncomingRequestService extends AsyncResource { private static instance: OCPPIncomingRequestService | null = null; - protected asyncResource: AsyncResource; private readonly version: OCPPVersion; private readonly ajv: Ajv; - protected abstract jsonSchemas: Map>; + private jsonValidateFunctions: Map>; + protected abstract jsonSchemas: Map>; protected constructor(version: OCPPVersion) { + super(moduleName); this.version = version; this.ajv = new Ajv({ keywords: ['javaType'], multipleOfPrecision: 2, }); ajvFormats(this.ajv); - this.asyncResource = new AsyncResource(moduleName); - this.incomingRequestHandler.bind(this); - this.validateIncomingRequestPayload.bind(this); + 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 { @@ -40,15 +61,16 @@ export default abstract class OCPPIncomingRequestService { 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 + error, ); if (!params?.throwError && params?.errorResponse) { return params?.errorResponse; @@ -65,31 +87,42 @@ export default abstract class OCPPIncomingRequestService { chargingStation: ChargingStation, commandName: IncomingRequestCommand, schema: JSONSchemaType, - payload: T + payload: T, ): boolean { - if (chargingStation.getPayloadSchemaValidation() === false) { + if (chargingStation.stationInfo?.ocppStrictCompliance === false) { return true; } - const validate = this.ajv.compile(schema); + if (this.jsonValidateFunctions.has(commandName) === false) { + this.jsonValidateFunctions.set(commandName, this.ajv.compile(schema).bind(this)); + } + const validate = this.jsonValidateFunctions.get(commandName)!; if (validate(payload)) { return true; } logger.error( `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`, - validate.errors + 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), ); } - public abstract incomingRequestHandler( + protected handleRequestClearCache(chargingStation: ChargingStation): ClearCacheResponse { + if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo)!)) { + return OCPPConstants.OCPP_RESPONSE_ACCEPTED; + } + return OCPPConstants.OCPP_RESPONSE_REJECTED; + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public abstract incomingRequestHandler( chargingStation: ChargingStation, messageId: string, commandName: IncomingRequestCommand, - commandPayload: JsonType + commandPayload: ReqType, ): Promise; }