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
.bind(this);
35 this.validateResponsePayload
.bind(this);
38 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
39 if (OCPPResponseService
.instance
=== null) {
40 OCPPResponseService
.instance
= new this();
42 return OCPPResponseService
.instance
as T
;
45 protected validateResponsePayload
<T
extends JsonType
>(
46 chargingStation
: ChargingStation
,
47 commandName
: RequestCommand
,
48 schema
: JSONSchemaType
<T
>,
51 if (chargingStation
.getPayloadSchemaValidation() === false) {
54 const validate
= this.ajv
.compile(schema
);
55 if (validate(payload
)) {
59 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
63 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
64 'Response PDU is invalid',
66 JSON
.stringify(validate
.errors
, null, 2)
70 protected emptyResponseHandler() {
71 /* This is intentional */
74 public abstract responseHandler(
75 chargingStation
: ChargingStation
,
76 commandName
: RequestCommand
,
78 requestPayload
: JsonType