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 { JsonType
} from
'../../types/JsonType';
7 import { RequestCommand
} from
'../../types/ocpp/Requests';
8 import logger from
'../../utils/Logger';
9 import type ChargingStation from
'../ChargingStation';
10 import { OCPPServiceUtils
} from
'./OCPPServiceUtils';
12 const moduleName
= 'OCPPResponseService';
14 export default abstract class OCPPResponseService
{
15 private static instance
: OCPPResponseService
| null = null;
18 protected constructor() {
23 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
24 if (!OCPPResponseService
.instance
) {
25 OCPPResponseService
.instance
= new this();
27 return OCPPResponseService
.instance
as T
;
30 protected validateResponsePayload
<T
extends JsonType
>(
31 chargingStation
: ChargingStation
,
32 commandName
: RequestCommand
,
33 schema
: JSONSchemaType
<T
>,
36 if (!chargingStation
.getPayloadSchemaValidation()) {
39 const validate
= this.ajv
.compile(schema
);
40 if (validate(payload
)) {
44 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Response PDU is invalid: %j`,
48 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
49 'Response PDU is invalid',
51 JSON
.stringify(validate
.errors
, null, 2)
55 // eslint-disable-next-line @typescript-eslint/no-empty-function
56 protected emptyResponseHandler() {}
58 public abstract responseHandler(
59 chargingStation
: ChargingStation
,
60 commandName
: RequestCommand
,
62 requestPayload
: JsonType