-import type ChargingStation from '../ChargingStation';
+import { JSONSchemaType } from 'ajv';
+import Ajv from 'ajv-draft-04';
+import ajvFormats from 'ajv-formats';
+
+import OCPPError from '../../exception/OCPPError';
import { HandleErrorParams } from '../../types/Error';
-import { IncomingRequestCommand } from '../../types/ocpp/Requests';
import { JsonType } from '../../types/JsonType';
+import { IncomingRequestCommand } from '../../types/ocpp/Requests';
import logger from '../../utils/Logger';
+import type ChargingStation from '../ChargingStation';
+import { OCPPServiceUtils } from './OCPPServiceUtils';
-export default abstract class OCPPIncomingRequestService {
- private static readonly instances: Map<string, OCPPIncomingRequestService> = new Map<
- string,
- OCPPIncomingRequestService
- >();
+const moduleName = 'OCPPIncomingRequestService';
- protected chargingStation: ChargingStation;
+export default abstract class OCPPIncomingRequestService {
+ private static instance: OCPPIncomingRequestService | null = null;
+ private ajv: Ajv;
- protected constructor(chargingStation: ChargingStation) {
- this.chargingStation = chargingStation;
+ protected constructor() {
+ this.ajv = new Ajv();
+ ajvFormats(this.ajv);
+ this.incomingRequestHandler.bind(this);
+ this.validateIncomingRequestPayload.bind(this);
}
- public static getInstance<T extends OCPPIncomingRequestService>(
- 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<T extends OCPPIncomingRequestService>(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<T>(
+ chargingStation: ChargingStation,
commandName: IncomingRequestCommand,
error: Error,
params: HandleErrorParams<T> = { throwError: true }
): T {
logger.error(
- this.chargingStation.logPrefix() + ' Incoming request command %s error: %j',
+ `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command %s error:`,
commandName,
error
);
}
}
- public abstract handleRequest(
+ protected validateIncomingRequestPayload<T extends JsonType>(
+ chargingStation: ChargingStation,
+ commandName: IncomingRequestCommand,
+ schema: JSONSchemaType<T>,
+ payload: T
+ ): boolean {
+ if (!chargingStation.getPayloadSchemaValidation()) {
+ return true;
+ }
+ const validate = this.ajv.compile(schema);
+ if (validate(payload)) {
+ return true;
+ }
+ logger.error(
+ `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: 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)
+ );
+ }
+
+ public abstract incomingRequestHandler(
+ chargingStation: ChargingStation,
messageId: string,
commandName: IncomingRequestCommand,
commandPayload: JsonType