| 1 | import { AsyncResource } from 'node:async_hooks'; |
| 2 | |
| 3 | import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv'; |
| 4 | import ajvFormats from 'ajv-formats'; |
| 5 | |
| 6 | import { OCPPConstants } from './OCPPConstants'; |
| 7 | import { OCPPServiceUtils } from './OCPPServiceUtils'; |
| 8 | import { type ChargingStation, getIdTagsFile } from '../../charging-station'; |
| 9 | import { OCPPError } from '../../exception'; |
| 10 | import type { |
| 11 | ClearCacheResponse, |
| 12 | HandleErrorParams, |
| 13 | IncomingRequestCommand, |
| 14 | JsonType, |
| 15 | OCPPVersion, |
| 16 | } from '../../types'; |
| 17 | import { logger, setDefaultErrorParams } from '../../utils'; |
| 18 | |
| 19 | const moduleName = 'OCPPIncomingRequestService'; |
| 20 | |
| 21 | export abstract class OCPPIncomingRequestService extends AsyncResource { |
| 22 | private static instance: OCPPIncomingRequestService | null = null; |
| 23 | private readonly version: OCPPVersion; |
| 24 | private readonly ajv: Ajv; |
| 25 | private jsonValidateFunctions: Map<IncomingRequestCommand, ValidateFunction<JsonType>>; |
| 26 | protected abstract jsonSchemas: Map<IncomingRequestCommand, JSONSchemaType<JsonType>>; |
| 27 | |
| 28 | protected constructor(version: OCPPVersion) { |
| 29 | super(moduleName); |
| 30 | this.version = version; |
| 31 | this.ajv = new Ajv({ |
| 32 | keywords: ['javaType'], |
| 33 | multipleOfPrecision: 2, |
| 34 | }); |
| 35 | ajvFormats(this.ajv); |
| 36 | this.jsonValidateFunctions = new Map<IncomingRequestCommand, ValidateFunction<JsonType>>(); |
| 37 | this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as < |
| 38 | ReqType extends JsonType, |
| 39 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 40 | ResType extends JsonType, |
| 41 | >( |
| 42 | chargingStation: ChargingStation, |
| 43 | messageId: string, |
| 44 | commandName: IncomingRequestCommand, |
| 45 | commandPayload: ReqType, |
| 46 | ) => Promise<void>; |
| 47 | this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as < |
| 48 | T extends JsonType, |
| 49 | >( |
| 50 | chargingStation: ChargingStation, |
| 51 | commandName: IncomingRequestCommand, |
| 52 | schema: JSONSchemaType<T>, |
| 53 | payload: T, |
| 54 | ) => boolean; |
| 55 | } |
| 56 | |
| 57 | public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T { |
| 58 | if (OCPPIncomingRequestService.instance === null) { |
| 59 | OCPPIncomingRequestService.instance = new this(); |
| 60 | } |
| 61 | return OCPPIncomingRequestService.instance as T; |
| 62 | } |
| 63 | |
| 64 | protected handleIncomingRequestError<T extends JsonType>( |
| 65 | chargingStation: ChargingStation, |
| 66 | commandName: IncomingRequestCommand, |
| 67 | error: Error, |
| 68 | params: HandleErrorParams<T> = { throwError: true, consoleOut: false }, |
| 69 | ): T | undefined { |
| 70 | setDefaultErrorParams(params); |
| 71 | logger.error( |
| 72 | `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, |
| 73 | error, |
| 74 | ); |
| 75 | if (!params?.throwError && params?.errorResponse) { |
| 76 | return params?.errorResponse; |
| 77 | } |
| 78 | if (params?.throwError && !params?.errorResponse) { |
| 79 | throw error; |
| 80 | } |
| 81 | if (params?.throwError && params?.errorResponse) { |
| 82 | return params?.errorResponse; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | protected validateIncomingRequestPayload<T extends JsonType>( |
| 87 | chargingStation: ChargingStation, |
| 88 | commandName: IncomingRequestCommand, |
| 89 | schema: JSONSchemaType<T>, |
| 90 | payload: T, |
| 91 | ): boolean { |
| 92 | if (chargingStation.stationInfo?.ocppStrictCompliance === false) { |
| 93 | return true; |
| 94 | } |
| 95 | const validate = this.getJsonIncomingRequestValidateFunction<T>(commandName, schema); |
| 96 | if (validate(payload)) { |
| 97 | return true; |
| 98 | } |
| 99 | logger.error( |
| 100 | `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`, |
| 101 | validate.errors, |
| 102 | ); |
| 103 | throw new OCPPError( |
| 104 | OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), |
| 105 | 'Incoming request PDU is invalid', |
| 106 | commandName, |
| 107 | JSON.stringify(validate.errors, undefined, 2), |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | protected handleRequestClearCache(chargingStation: ChargingStation): ClearCacheResponse { |
| 112 | if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo)!)) { |
| 113 | return OCPPConstants.OCPP_RESPONSE_ACCEPTED; |
| 114 | } |
| 115 | return OCPPConstants.OCPP_RESPONSE_REJECTED; |
| 116 | } |
| 117 | |
| 118 | private getJsonIncomingRequestValidateFunction<T extends JsonType>( |
| 119 | commandName: IncomingRequestCommand, |
| 120 | schema: JSONSchemaType<T>, |
| 121 | ) { |
| 122 | if (this.jsonValidateFunctions.has(commandName) === false) { |
| 123 | this.jsonValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this)); |
| 124 | } |
| 125 | return this.jsonValidateFunctions.get(commandName)!; |
| 126 | } |
| 127 | |
| 128 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 129 | public abstract incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>( |
| 130 | chargingStation: ChargingStation, |
| 131 | messageId: string, |
| 132 | commandName: IncomingRequestCommand, |
| 133 | commandPayload: ReqType, |
| 134 | ): Promise<void>; |
| 135 | } |