X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPIncomingRequestService.ts;h=4b27f9d82f8de91b38bd1a19c4a826ebe6800d6b;hb=1b2acf4e9c00cc7272ec7769b4e82113d61f64fb;hp=b5f607f534abb3e98a8dfa0c1eb8842740fc88c6;hpb=5cc4b63bd363814ddc657bd4c9b407e5eb3150cb;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index b5f607f5..4b27f9d8 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -1,40 +1,76 @@ -import type ChargingStation from '../ChargingStation'; -import { HandleErrorParams } from '../../types/Error'; -import { IncomingRequestCommand } from '../../types/ocpp/Requests'; -import { JsonType } from '../../types/JsonType'; -import logger from '../../utils/Logger'; - -export default abstract class OCPPIncomingRequestService { - private static readonly instances: Map = new Map< - string, - OCPPIncomingRequestService - >(); - - protected chargingStation: ChargingStation; - - protected constructor(chargingStation: ChargingStation) { - this.chargingStation = chargingStation; +import { AsyncResource } from 'node:async_hooks'; + +import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv'; +import ajvFormats from 'ajv-formats'; + +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 abstract class OCPPIncomingRequestService extends AsyncResource { + private static instance: OCPPIncomingRequestService | null = null; + private readonly version: OCPPVersion; + private readonly ajv: Ajv; + 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.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 (chargingStation: ChargingStation) => T, - chargingStation: ChargingStation - ): T { - if (!OCPPIncomingRequestService.instances.has(chargingStation.hashId)) { - OCPPIncomingRequestService.instances.set(chargingStation.hashId, new this(chargingStation)); + public static getInstance(this: new () => T): T { + if (OCPPIncomingRequestService.instance === null) { + OCPPIncomingRequestService.instance = new this(); } - return OCPPIncomingRequestService.instances.get(chargingStation.hashId) as T; + return OCPPIncomingRequestService.instance as T; } - protected handleIncomingRequestError( + protected handleIncomingRequestError( + chargingStation: ChargingStation, commandName: IncomingRequestCommand, error: Error, - params: HandleErrorParams = { throwError: true } - ): T { + params: HandleErrorParams = { throwError: true, consoleOut: false }, + ): T | undefined { + setDefaultErrorParams(params); logger.error( - this.chargingStation.logPrefix() + ' Incoming request command %s error: %j', - commandName, - error + `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, + error, ); if (!params?.throwError && params?.errorResponse) { return params?.errorResponse; @@ -47,9 +83,53 @@ export default abstract class OCPPIncomingRequestService { } } - public abstract incomingRequestHandler( + protected validateIncomingRequestPayload( + chargingStation: ChargingStation, + commandName: IncomingRequestCommand, + schema: JSONSchemaType, + payload: T, + ): boolean { + if (chargingStation.stationInfo?.ocppStrictCompliance === false) { + return true; + } + const validate = this.getJsonIncomingRequestValidateFunction(commandName, schema); + if (validate(payload)) { + 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, undefined, 2), + ); + } + + protected handleRequestClearCache(chargingStation: ChargingStation): ClearCacheResponse { + if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo)!)) { + return OCPPConstants.OCPP_RESPONSE_ACCEPTED; + } + return OCPPConstants.OCPP_RESPONSE_REJECTED; + } + + private getJsonIncomingRequestValidateFunction( + commandName: IncomingRequestCommand, + schema: JSONSchemaType, + ) { + if (this.jsonValidateFunctions.has(commandName) === false) { + this.jsonValidateFunctions.set(commandName, this.ajv.compile(schema).bind(this)); + } + return this.jsonValidateFunctions.get(commandName)!; + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public abstract incomingRequestHandler( + chargingStation: ChargingStation, messageId: string, commandName: IncomingRequestCommand, - commandPayload: JsonType + commandPayload: ReqType, ): Promise; }