1 import Ajv
, { type JSONSchemaType
, type ValidateFunction
} from
'ajv';
2 import ajvFormats from
'ajv-formats';
4 import { OCPPServiceUtils
} from
'./OCPPServiceUtils.js';
5 import type { ChargingStation
} from
'../../charging-station/index.js';
6 import { OCPPError
} from
'../../exception/index.js';
8 IncomingRequestCommand
,
12 } from
'../../types/index.js';
13 import { logger
} from
'../../utils/index.js';
15 const moduleName
= 'OCPPResponseService';
17 export abstract class OCPPResponseService
{
18 private static instance
: OCPPResponseService
| null = null;
20 public jsonIncomingRequestResponseValidateFunctions
: Map
<
21 IncomingRequestCommand
,
22 ValidateFunction
<JsonType
>
25 private readonly version
: OCPPVersion
;
26 private readonly ajv
: Ajv
;
27 private jsonRequestValidateFunctions
: Map
<RequestCommand
, ValidateFunction
<JsonType
>>;
29 public abstract jsonIncomingRequestResponseSchemas
: Map
<
30 IncomingRequestCommand
,
31 JSONSchemaType
<JsonType
>
34 protected constructor(version
: OCPPVersion
) {
35 this.version
= version
;
37 keywords
: ['javaType'],
38 multipleOfPrecision
: 2,
41 this.jsonRequestValidateFunctions
= new Map
<RequestCommand
, ValidateFunction
<JsonType
>>();
42 this.jsonIncomingRequestResponseValidateFunctions
= new Map
<
43 IncomingRequestCommand
,
44 ValidateFunction
<JsonType
>
46 this.responseHandler
= this.responseHandler
.bind(this) as <
47 ReqType
extends JsonType
,
48 ResType
extends JsonType
,
50 chargingStation
: ChargingStation
,
51 commandName
: RequestCommand
,
53 requestPayload
: ReqType
,
55 this.validateResponsePayload
= this.validateResponsePayload
.bind(this) as <T
extends JsonType
>(
56 chargingStation
: ChargingStation
,
57 commandName
: RequestCommand
,
58 schema
: JSONSchemaType
<T
>,
63 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
64 if (OCPPResponseService
.instance
=== null) {
65 OCPPResponseService
.instance
= new this();
67 return OCPPResponseService
.instance
as T
;
70 protected validateResponsePayload
<T
extends JsonType
>(
71 chargingStation
: ChargingStation
,
72 commandName
: RequestCommand
,
73 schema
: JSONSchemaType
<T
>,
76 if (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false) {
79 const validate
= this.getJsonRequestValidateFunction
<T
>(commandName
, schema
);
80 if (validate(payload
)) {
84 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
88 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
89 'Response PDU is invalid',
91 JSON
.stringify(validate
.errors
, undefined, 2),
95 protected emptyResponseHandler() {
96 /* This is intentional */
99 private getJsonRequestValidateFunction
<T
extends JsonType
>(
100 commandName
: RequestCommand
,
101 schema
: JSONSchemaType
<T
>,
103 if (this.jsonRequestValidateFunctions
.has(commandName
) === false) {
104 this.jsonRequestValidateFunctions
.set(commandName
, this.ajv
.compile
<T
>(schema
).bind(this));
106 return this.jsonRequestValidateFunctions
.get(commandName
)!;
109 public abstract responseHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
110 chargingStation
: ChargingStation
,
111 commandName
: RequestCommand
,
113 requestPayload
: ReqType
,