-import type ChargingStation from '../ChargingStation';
-import { IncomingRequestCommand } from '../../types/ocpp/Requests';
-import { JsonType } from '../../types/JsonType';
-import logger from '../../utils/Logger';
+import { AsyncResource } from 'node:async_hooks';
-export default abstract class OCPPIncomingRequestService {
- private static readonly instances: Map<string, OCPPIncomingRequestService> = new Map<string, OCPPIncomingRequestService>();
- protected chargingStation: ChargingStation;
+import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv';
+import ajvFormats from 'ajv-formats';
- protected constructor(chargingStation: ChargingStation) {
- this.chargingStation = 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 abstract class OCPPIncomingRequestService extends AsyncResource {
+ private static instance: OCPPIncomingRequestService | null = null;
+ private readonly version: OCPPVersion;
+ private readonly ajv: Ajv;
+ private jsonValidateFunctions: Map<IncomingRequestCommand, ValidateFunction<JsonType>>;
+ protected abstract jsonSchemas: Map<IncomingRequestCommand, JSONSchemaType<JsonType>>;
+
+ protected constructor(version: OCPPVersion) {
+ super(moduleName);
+ this.version = version;
+ this.ajv = new Ajv({
+ keywords: ['javaType'],
+ multipleOfPrecision: 2,
+ });
+ ajvFormats(this.ajv);
+ this.jsonValidateFunctions = new Map<IncomingRequestCommand, ValidateFunction<JsonType>>();
+ 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<void>;
+ this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as <
+ T extends JsonType,
+ >(
+ chargingStation: ChargingStation,
+ commandName: IncomingRequestCommand,
+ schema: JSONSchemaType<T>,
+ payload: T,
+ ) => boolean;
+ }
+
+ public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T {
+ if (OCPPIncomingRequestService.instance === null) {
+ OCPPIncomingRequestService.instance = new this();
+ }
+ return OCPPIncomingRequestService.instance as T;
+ }
+
+ protected handleIncomingRequestError<T extends JsonType>(
+ chargingStation: ChargingStation,
+ commandName: IncomingRequestCommand,
+ error: Error,
+ params: HandleErrorParams<T> = { 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 && !params?.errorResponse) {
+ throw error;
+ }
+ if (params?.throwError && params?.errorResponse) {
+ return params?.errorResponse;
+ }
+ }
+
+ protected validateIncomingRequestPayload<T extends JsonType>(
+ chargingStation: ChargingStation,
+ commandName: IncomingRequestCommand,
+ schema: JSONSchemaType<T>,
+ payload: T,
+ ): boolean {
+ if (chargingStation.stationInfo?.ocppStrictCompliance === false) {
+ return true;
+ }
+ const validate = this.getJsonIncomingRequestValidateFunction<T>(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),
+ );
}
- public static getInstance<T extends OCPPIncomingRequestService>(this: new (chargingStation: ChargingStation) => T, chargingStation: ChargingStation): T {
- if (!OCPPIncomingRequestService.instances.has(chargingStation.id)) {
- OCPPIncomingRequestService.instances.set(chargingStation.id, new this(chargingStation));
+ protected handleRequestClearCache(chargingStation: ChargingStation): ClearCacheResponse {
+ if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo)!)) {
+ return OCPPConstants.OCPP_RESPONSE_ACCEPTED;
}
- return OCPPIncomingRequestService.instances.get(chargingStation.id) as T;
+ return OCPPConstants.OCPP_RESPONSE_REJECTED;
}
- protected handleIncomingRequestError<T>(commandName: IncomingRequestCommand, error: Error, errorOcppResponse?: T): T {
- logger.error(this.chargingStation.logPrefix() + ' Incoming request command %s error: %j', commandName, error);
- if (errorOcppResponse) {
- return errorOcppResponse;
+ private getJsonIncomingRequestValidateFunction<T extends JsonType>(
+ commandName: IncomingRequestCommand,
+ schema: JSONSchemaType<T>,
+ ) {
+ if (this.jsonValidateFunctions.has(commandName) === false) {
+ this.jsonValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this));
}
- throw error;
+ return this.jsonValidateFunctions.get(commandName)!;
}
- public abstract handleRequest(messageId: string, commandName: IncomingRequestCommand, commandPayload: JsonType): Promise<void>;
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ public abstract incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>(
+ chargingStation: ChargingStation,
+ messageId: string,
+ commandName: IncomingRequestCommand,
+ commandPayload: ReqType,
+ ): Promise<void>;
}