1 import Ajv
, { type JSONSchemaType
} 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;
20 private readonly version
: OCPPVersion
;
21 private readonly ajv
: Ajv
;
22 public abstract jsonIncomingRequestResponseSchemas
: Map
<
23 IncomingRequestCommand
,
24 JSONSchemaType
<JsonObject
>
27 protected constructor(version
: OCPPVersion
) {
28 this.version
= version
;
30 keywords
: ['javaType'],
31 multipleOfPrecision
: 2,
34 this.responseHandler
= this.responseHandler
.bind(this) as <
35 ReqType
extends JsonType
,
36 ResType
extends JsonType
,
38 chargingStation
: ChargingStation
,
39 commandName
: RequestCommand
,
41 requestPayload
: ReqType
,
43 this.validateResponsePayload
= this.validateResponsePayload
.bind(this) as <T
extends JsonType
>(
44 chargingStation
: ChargingStation
,
45 commandName
: RequestCommand
,
46 schema
: JSONSchemaType
<T
>,
51 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
52 if (OCPPResponseService
.instance
=== null) {
53 OCPPResponseService
.instance
= new this();
55 return OCPPResponseService
.instance
as T
;
58 protected validateResponsePayload
<T
extends JsonType
>(
59 chargingStation
: ChargingStation
,
60 commandName
: RequestCommand
,
61 schema
: JSONSchemaType
<T
>,
64 if (chargingStation
.getOcppStrictCompliance() === false) {
67 const validate
= this.ajv
.compile(schema
);
68 if (validate(payload
)) {
72 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
76 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
!),
77 'Response PDU is invalid',
79 JSON
.stringify(validate
.errors
, null, 2),
83 protected emptyResponseHandler() {
84 /* This is intentional */
87 public abstract responseHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
88 chargingStation
: ChargingStation
,
89 commandName
: RequestCommand
,
91 requestPayload
: ReqType
,