1 import Ajv
, { type JSONSchemaType
} from
'ajv';
2 import ajvFormats from
'ajv-formats';
4 import { OCPPServiceUtils
} from
'./internal';
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 chargingStation
: ChargingStation
,
36 commandName
: RequestCommand
,
38 requestPayload
: JsonType
40 this.validateResponsePayload
= this.validateResponsePayload
.bind(this) as <T
extends JsonType
>(
41 chargingStation
: ChargingStation
,
42 commandName
: RequestCommand
,
43 schema
: JSONSchemaType
<T
>,
48 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
49 if (OCPPResponseService
.instance
=== null) {
50 OCPPResponseService
.instance
= new this();
52 return OCPPResponseService
.instance
as T
;
55 protected validateResponsePayload
<T
extends JsonType
>(
56 chargingStation
: ChargingStation
,
57 commandName
: RequestCommand
,
58 schema
: JSONSchemaType
<T
>,
61 if (chargingStation
.getPayloadSchemaValidation() === false) {
64 const validate
= this.ajv
.compile(schema
);
65 if (validate(payload
)) {
69 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
73 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
74 'Response PDU is invalid',
76 JSON
.stringify(validate
.errors
, null, 2)
80 protected emptyResponseHandler() {
81 /* This is intentional */
84 public abstract responseHandler(
85 chargingStation
: ChargingStation
,
86 commandName
: RequestCommand
,
88 requestPayload
: JsonType