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';
7 import type { IncomingRequestCommand
, JsonType
, OCPPVersion
, RequestCommand
} from
'../../types';
8 import { logger
} from
'../../utils';
10 const moduleName
= 'OCPPResponseService';
12 export abstract class OCPPResponseService
{
13 private static instance
: OCPPResponseService
| null = null;
15 public jsonIncomingRequestResponseValidateFunctions
: Map
<
16 IncomingRequestCommand
,
17 ValidateFunction
<JsonType
>
20 private readonly version
: OCPPVersion
;
21 private readonly ajv
: Ajv
;
22 private jsonRequestValidateFunctions
: Map
<RequestCommand
, ValidateFunction
<JsonType
>>;
24 public abstract jsonIncomingRequestResponseSchemas
: Map
<
25 IncomingRequestCommand
,
26 JSONSchemaType
<JsonType
>
29 protected constructor(version
: OCPPVersion
) {
30 this.version
= version
;
32 keywords
: ['javaType'],
33 multipleOfPrecision
: 2,
36 this.jsonRequestValidateFunctions
= new Map
<RequestCommand
, ValidateFunction
<JsonType
>>();
37 this.jsonIncomingRequestResponseValidateFunctions
= new Map
<
38 IncomingRequestCommand
,
39 ValidateFunction
<JsonType
>
41 this.responseHandler
= this.responseHandler
.bind(this) as <
42 ReqType
extends JsonType
,
43 ResType
extends JsonType
,
45 chargingStation
: ChargingStation
,
46 commandName
: RequestCommand
,
48 requestPayload
: ReqType
,
50 this.validateResponsePayload
= this.validateResponsePayload
.bind(this) as <T
extends JsonType
>(
51 chargingStation
: ChargingStation
,
52 commandName
: RequestCommand
,
53 schema
: JSONSchemaType
<T
>,
58 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
59 if (OCPPResponseService
.instance
=== null) {
60 OCPPResponseService
.instance
= new this();
62 return OCPPResponseService
.instance
as T
;
65 protected validateResponsePayload
<T
extends JsonType
>(
66 chargingStation
: ChargingStation
,
67 commandName
: RequestCommand
,
68 schema
: JSONSchemaType
<T
>,
71 if (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false) {
74 const validate
= this.getJsonRequestValidateFunction
<T
>(commandName
, schema
);
75 if (validate(payload
)) {
79 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
83 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
84 'Response PDU is invalid',
86 JSON
.stringify(validate
.errors
, undefined, 2),
90 protected emptyResponseHandler() {
91 /* This is intentional */
94 private getJsonRequestValidateFunction
<T
extends JsonType
>(
95 commandName
: RequestCommand
,
96 schema
: JSONSchemaType
<T
>,
98 if (this.jsonRequestValidateFunctions
.has(commandName
) === false) {
99 this.jsonRequestValidateFunctions
.set(commandName
, this.ajv
.compile
<T
>(schema
).bind(this));
101 return this.jsonRequestValidateFunctions
.get(commandName
)!;
104 public abstract responseHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
105 chargingStation
: ChargingStation
,
106 commandName
: RequestCommand
,
108 requestPayload
: ReqType
,