1 import Ajv
, { type JSONSchemaType
} from
'ajv';
2 import AjvDraft04 from
'ajv-draft-04';
3 import ajvFormats from
'ajv-formats';
5 import OCPPError from
'../../exception/OCPPError';
6 import type { JsonType
} from
'../../types/JsonType';
7 import { OCPPVersion
} from
'../../types/ocpp/OCPPVersion';
8 import type { RequestCommand
} from
'../../types/ocpp/Requests';
9 import logger from
'../../utils/Logger';
10 import type ChargingStation from
'../ChargingStation';
11 import { OCPPServiceUtils
} from
'./OCPPServiceUtils';
13 const moduleName
= 'OCPPResponseService';
15 export default abstract class OCPPResponseService
{
16 private static instance
: OCPPResponseService
| null = null;
17 private readonly version
: OCPPVersion
;
18 private readonly ajv
: Ajv
;
20 protected constructor(version
: OCPPVersion
) {
21 this.version
= version
;
22 switch (this.version
) {
23 case OCPPVersion
.VERSION_16
:
24 this.ajv
= new AjvDraft04();
26 case OCPPVersion
.VERSION_20
:
27 case OCPPVersion
.VERSION_201
:
32 this.responseHandler
.bind(this);
33 this.validateResponsePayload
.bind(this);
36 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
37 if (OCPPResponseService
.instance
=== null) {
38 OCPPResponseService
.instance
= new this();
40 return OCPPResponseService
.instance
as T
;
43 protected validateResponsePayload
<T
extends JsonType
>(
44 chargingStation
: ChargingStation
,
45 commandName
: RequestCommand
,
46 schema
: JSONSchemaType
<T
>,
49 if (chargingStation
.getPayloadSchemaValidation() === false) {
52 const validate
= this.ajv
.compile(schema
);
53 if (validate(payload
)) {
57 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Response PDU is invalid: %j`,
61 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
62 'Response PDU is invalid',
64 JSON
.stringify(validate
.errors
, null, 2)
68 protected emptyResponseHandler() {
69 /* This is intentional */
72 public abstract responseHandler(
73 chargingStation
: ChargingStation
,
74 commandName
: RequestCommand
,
76 requestPayload
: JsonType