1 import { JSONSchemaType
} from
'ajv';
2 import Ajv from
'ajv-draft-04';
3 import ajvFormats from
'ajv-formats';
5 import OCPPError from
'../../exception/OCPPError';
6 import { HandleErrorParams
} from
'../../types/Error';
7 import { JsonType
} from
'../../types/JsonType';
8 import { ErrorType
} from
'../../types/ocpp/ErrorType';
9 import { IncomingRequestCommand
} from
'../../types/ocpp/Requests';
10 import logger from
'../../utils/Logger';
11 import type ChargingStation from
'../ChargingStation';
13 const moduleName
= 'OCPPIncomingRequestService';
15 export default abstract class OCPPIncomingRequestService
{
16 private static instance
: OCPPIncomingRequestService
| null = null;
19 protected constructor() {
24 public static getInstance
<T
extends OCPPIncomingRequestService
>(this: new () => T
): T
{
25 if (!OCPPIncomingRequestService
.instance
) {
26 OCPPIncomingRequestService
.instance
= new this();
28 return OCPPIncomingRequestService
.instance
as T
;
31 protected handleIncomingRequestError
<T
>(
32 chargingStation
: ChargingStation
,
33 commandName
: IncomingRequestCommand
,
35 params
: HandleErrorParams
<T
> = { throwError
: true }
38 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command %s error: %j`,
42 if (!params
?.throwError
&& params
?.errorResponse
) {
43 return params
?.errorResponse
;
45 if (params
?.throwError
&& !params
?.errorResponse
) {
48 if (params
?.throwError
&& params
?.errorResponse
) {
49 return params
?.errorResponse
;
53 protected validateIncomingRequestPayload
<T
extends JsonType
>(
54 chargingStation
: ChargingStation
,
55 commandName
: IncomingRequestCommand
,
56 schema
: JSONSchemaType
<T
>,
59 if (!chargingStation
.getPayloadSchemaValidation()) {
62 const validate
= this.ajv
.compile(schema
);
63 if (validate(payload
)) {
67 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Incoming request PDU is invalid: %j`,
71 ErrorType
.FORMATION_VIOLATION
,
72 'Incoming request PDU is invalid',
74 JSON
.stringify(validate
.errors
, null, 2)
78 public abstract incomingRequestHandler(
79 chargingStation
: ChargingStation
,
81 commandName
: IncomingRequestCommand
,
82 commandPayload
: JsonType