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
.getOcppStrictCompliance() === false) {
74 if (this.jsonRequestValidateFunctions
.has(commandName
) === false) {
75 this.jsonRequestValidateFunctions
.set(commandName
, this.ajv
.compile
<T
>(schema
).bind(this));
77 const validate
= this.jsonRequestValidateFunctions
.get(commandName
)!;
78 if (validate(payload
)) {
82 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
86 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
!),
87 'Response PDU is invalid',
89 JSON
.stringify(validate
.errors
, undefined, 2),
93 protected emptyResponseHandler() {
94 /* This is intentional */
97 public abstract responseHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
98 chargingStation
: ChargingStation
,
99 commandName
: RequestCommand
,
101 requestPayload
: ReqType
,