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';
14 type Ajv
= _Ajv
.default;
15 const Ajv
= _Ajv
.default;
16 const ajvFormats
= _ajvFormats
.default;
18 const moduleName
= 'OCPPResponseService';
20 export abstract class OCPPResponseService
{
21 private static instance
: OCPPResponseService
| null = null;
23 public jsonIncomingRequestResponseValidateFunctions
: Map
<
24 IncomingRequestCommand
,
25 ValidateFunction
<JsonType
>
28 private readonly version
: OCPPVersion
;
29 private readonly ajv
: Ajv
;
30 private jsonRequestValidateFunctions
: Map
<RequestCommand
, ValidateFunction
<JsonType
>>;
32 public abstract jsonIncomingRequestResponseSchemas
: Map
<
33 IncomingRequestCommand
,
34 JSONSchemaType
<JsonType
>
37 protected constructor(version
: OCPPVersion
) {
38 this.version
= version
;
40 keywords
: ['javaType'],
41 multipleOfPrecision
: 2,
44 this.jsonRequestValidateFunctions
= new Map
<RequestCommand
, ValidateFunction
<JsonType
>>();
45 this.jsonIncomingRequestResponseValidateFunctions
= new Map
<
46 IncomingRequestCommand
,
47 ValidateFunction
<JsonType
>
49 this.responseHandler
= this.responseHandler
.bind(this) as <
50 ReqType
extends JsonType
,
51 ResType
extends JsonType
,
53 chargingStation
: ChargingStation
,
54 commandName
: RequestCommand
,
56 requestPayload
: ReqType
,
58 this.validateResponsePayload
= this.validateResponsePayload
.bind(this) as <T
extends JsonType
>(
59 chargingStation
: ChargingStation
,
60 commandName
: RequestCommand
,
61 schema
: JSONSchemaType
<T
>,
66 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
67 if (OCPPResponseService
.instance
=== null) {
68 OCPPResponseService
.instance
= new this();
70 return OCPPResponseService
.instance
as T
;
73 protected validateResponsePayload
<T
extends JsonType
>(
74 chargingStation
: ChargingStation
,
75 commandName
: RequestCommand
,
76 schema
: JSONSchemaType
<T
>,
79 if (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false) {
82 const validate
= this.getJsonRequestValidateFunction
<T
>(commandName
, schema
);
83 if (validate(payload
)) {
87 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
91 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
92 'Response PDU is invalid',
94 JSON
.stringify(validate
.errors
, undefined, 2),
98 protected emptyResponseHandler() {
99 /* This is intentional */
102 private getJsonRequestValidateFunction
<T
extends JsonType
>(
103 commandName
: RequestCommand
,
104 schema
: JSONSchemaType
<T
>,
106 if (this.jsonRequestValidateFunctions
.has(commandName
) === false) {
107 this.jsonRequestValidateFunctions
.set(commandName
, this.ajv
.compile
<T
>(schema
).bind(this));
109 return this.jsonRequestValidateFunctions
.get(commandName
)!;
112 public abstract responseHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
113 chargingStation
: ChargingStation
,
114 commandName
: RequestCommand
,
116 requestPayload
: ReqType
,