1 import Ajv
, { type JSONSchemaType
, type ValidateFunction
} from
'ajv';
2 import ajvFormats from
'ajv-formats';
4 import { OCPPServiceUtils
} from
'./OCPPServiceUtils';
5 import type { ChargingStation
} from
'../../charging-station';
6 import { OCPPError
} from
'../../exception';
8 IncomingRequestCommand
,
14 import { logger
} from
'../../utils';
16 const moduleName
= 'OCPPResponseService';
18 export abstract class OCPPResponseService
{
19 private static instance
: OCPPResponseService
| null = null;
21 public jsonIncomingRequestResponseValidateFunctions
: Map
<
22 IncomingRequestCommand
,
23 ValidateFunction
<JsonObject
>
26 private readonly version
: OCPPVersion
;
27 private readonly ajv
: Ajv
;
28 private jsonRequestValidateFunctions
: Map
<RequestCommand
, ValidateFunction
<JsonObject
>>;
30 public abstract jsonIncomingRequestResponseSchemas
: Map
<
31 IncomingRequestCommand
,
32 JSONSchemaType
<JsonObject
>
35 protected constructor(version
: OCPPVersion
) {
36 this.version
= version
;
38 keywords
: ['javaType'],
39 multipleOfPrecision
: 2,
42 this.jsonRequestValidateFunctions
= new Map
<RequestCommand
, ValidateFunction
<JsonObject
>>();
43 this.jsonIncomingRequestResponseValidateFunctions
= new Map
<
44 IncomingRequestCommand
,
45 ValidateFunction
<JsonObject
>
47 this.responseHandler
= this.responseHandler
.bind(this) as <
48 ReqType
extends JsonType
,
49 ResType
extends JsonType
,
51 chargingStation
: ChargingStation
,
52 commandName
: RequestCommand
,
54 requestPayload
: ReqType
,
56 this.validateResponsePayload
= this.validateResponsePayload
.bind(this) as <T
extends JsonType
>(
57 chargingStation
: ChargingStation
,
58 commandName
: RequestCommand
,
59 schema
: JSONSchemaType
<T
>,
64 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
65 if (OCPPResponseService
.instance
=== null) {
66 OCPPResponseService
.instance
= new this();
68 return OCPPResponseService
.instance
as T
;
71 protected validateResponsePayload
<T
extends JsonType
>(
72 chargingStation
: ChargingStation
,
73 commandName
: RequestCommand
,
74 schema
: JSONSchemaType
<T
>,
77 if (chargingStation
.getOcppStrictCompliance() === false) {
80 if (this.jsonRequestValidateFunctions
.has(commandName
) === false) {
81 this.jsonRequestValidateFunctions
.set(
83 this.ajv
.compile
<JsonObject
>(schema
).bind(this),
86 const validate
= this.jsonRequestValidateFunctions
.get(commandName
)!;
87 if (validate(payload
)) {
91 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
95 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
!),
96 'Response PDU is invalid',
98 JSON
.stringify(validate
.errors
, undefined, 2),
102 protected emptyResponseHandler() {
103 /* This is intentional */
106 public abstract responseHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
107 chargingStation
: ChargingStation
,
108 commandName
: RequestCommand
,
110 requestPayload
: ReqType
,