1 import Ajv
, { type JSONSchemaType
} from
'ajv';
2 import ajvFormats from
'ajv-formats';
4 import OCPPError from
'../../exception/OCPPError';
5 import type { JsonObject
, JsonType
} from
'../../types/JsonType';
6 import type { OCPPVersion
} from
'../../types/ocpp/OCPPVersion';
7 import type { IncomingRequestCommand
, 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;
16 private readonly version
: OCPPVersion
;
17 private readonly ajv
: Ajv
;
18 public abstract jsonIncomingRequestResponseSchemas
: Map
<
19 IncomingRequestCommand
,
20 JSONSchemaType
<JsonObject
>
23 protected constructor(version
: OCPPVersion
) {
24 this.version
= version
;
26 keywords
: ['javaType'],
27 multipleOfPrecision
: 2,
30 this.responseHandler
.bind(this);
31 this.validateResponsePayload
.bind(this);
34 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
35 if (OCPPResponseService
.instance
=== null) {
36 OCPPResponseService
.instance
= new this();
38 return OCPPResponseService
.instance
as T
;
41 protected validateResponsePayload
<T
extends JsonType
>(
42 chargingStation
: ChargingStation
,
43 commandName
: RequestCommand
,
44 schema
: JSONSchemaType
<T
>,
47 if (chargingStation
.getPayloadSchemaValidation() === false) {
50 const validate
= this.ajv
.compile(schema
);
51 if (validate(payload
)) {
55 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
59 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
60 'Response PDU is invalid',
62 JSON
.stringify(validate
.errors
, null, 2)
66 protected emptyResponseHandler() {
67 /* This is intentional */
70 public abstract responseHandler(
71 chargingStation
: ChargingStation
,
72 commandName
: RequestCommand
,
74 requestPayload
: JsonType