adcdacaeeef0e0a6d128211dad74514e196215f2
1 import Ajv
, { type JSONSchemaType
} from
'ajv';
2 import ajvFormats from
'ajv-formats';
4 import OCPPError from
'../../exception/OCPPError';
5 import type { JsonType
} from
'../../types/JsonType';
6 import type { OCPPVersion
} from
'../../types/ocpp/OCPPVersion';
7 import type { RequestCommand
} from
'../../types/ocpp/Requests';
8 import logger from
'../../utils/Logger';
9 import type ChargingStation from
'../ChargingStation';
10 import { OCPPServiceUtils
} from
'./OCPPServiceUtils';
12 const moduleName
= 'OCPPResponseService';
14 export default abstract class OCPPResponseService
{
15 private static instance
: OCPPResponseService
| null = null;
16 private readonly version
: OCPPVersion
;
17 private readonly ajv
: Ajv
;
19 protected constructor(version
: OCPPVersion
) {
20 this.version
= version
;
22 multipleOfPrecision
: 2,
25 this.responseHandler
.bind(this);
26 this.validateResponsePayload
.bind(this);
29 public static getInstance
<T
extends OCPPResponseService
>(this: new () => T
): T
{
30 if (OCPPResponseService
.instance
=== null) {
31 OCPPResponseService
.instance
= new this();
33 return OCPPResponseService
.instance
as T
;
36 protected validateResponsePayload
<T
extends JsonType
>(
37 chargingStation
: ChargingStation
,
38 commandName
: RequestCommand
,
39 schema
: JSONSchemaType
<T
>,
42 if (chargingStation
.getPayloadSchemaValidation() === false) {
45 const validate
= this.ajv
.compile(schema
);
46 if (validate(payload
)) {
50 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
54 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
55 'Response PDU is invalid',
57 JSON
.stringify(validate
.errors
, null, 2)
61 protected emptyResponseHandler() {
62 /* This is intentional */
65 public abstract responseHandler(
66 chargingStation
: ChargingStation
,
67 commandName
: RequestCommand
,
69 requestPayload
: JsonType