1 import _Ajv
, { type JSONSchemaType
, type ValidateFunction
} from
'ajv';
2 import _ajvFormats from
'ajv-formats';
4 import { OCPPConstants
} from
'./OCPPConstants.js';
5 import { OCPPServiceUtils
} from
'./OCPPServiceUtils.js';
6 import { type ChargingStation
, getIdTagsFile
} from
'../../charging-station/index.js';
7 import { OCPPError
} from
'../../exception/index.js';
11 IncomingRequestCommand
,
14 } from
'../../types/index.js';
15 import { logger
, setDefaultErrorParams
} from
'../../utils/index.js';
16 type Ajv
= _Ajv
.default;
17 const Ajv
= _Ajv
.default;
18 const ajvFormats
= _ajvFormats
.default;
20 const moduleName
= 'OCPPIncomingRequestService';
22 export abstract class OCPPIncomingRequestService
{
23 private static instance
: OCPPIncomingRequestService
| null = null;
24 private readonly version
: OCPPVersion
;
25 private readonly ajv
: Ajv
;
26 private jsonValidateFunctions
: Map
<IncomingRequestCommand
, ValidateFunction
<JsonType
>>;
27 protected abstract jsonSchemas
: Map
<IncomingRequestCommand
, JSONSchemaType
<JsonType
>>;
29 protected constructor(version
: OCPPVersion
) {
30 this.version
= version
;
32 keywords
: ['javaType'],
33 multipleOfPrecision
: 2,
36 this.jsonValidateFunctions
= new Map
<IncomingRequestCommand
, ValidateFunction
<JsonType
>>();
37 this.incomingRequestHandler
= this.incomingRequestHandler
.bind(this) as <
38 ReqType
extends JsonType
,
39 // eslint-disable-next-line @typescript-eslint/no-unused-vars
40 ResType
extends JsonType
,
42 chargingStation
: ChargingStation
,
44 commandName
: IncomingRequestCommand
,
45 commandPayload
: ReqType
,
47 this.validateIncomingRequestPayload
= this.validateIncomingRequestPayload
.bind(this) as <
50 chargingStation
: ChargingStation
,
51 commandName
: IncomingRequestCommand
,
52 schema
: JSONSchemaType
<T
>,
57 public static getInstance
<T
extends OCPPIncomingRequestService
>(this: new () => T
): T
{
58 if (OCPPIncomingRequestService
.instance
=== null) {
59 OCPPIncomingRequestService
.instance
= new this();
61 return OCPPIncomingRequestService
.instance
as T
;
64 protected handleIncomingRequestError
<T
extends JsonType
>(
65 chargingStation
: ChargingStation
,
66 commandName
: IncomingRequestCommand
,
68 params
: HandleErrorParams
<T
> = { throwError
: true, consoleOut
: false },
70 setDefaultErrorParams(params
);
72 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
75 if (!params
?.throwError
&& params
?.errorResponse
) {
76 return params
?.errorResponse
;
78 if (params
?.throwError
&& !params
?.errorResponse
) {
81 if (params
?.throwError
&& params
?.errorResponse
) {
82 return params
?.errorResponse
;
86 protected validateIncomingRequestPayload
<T
extends JsonType
>(
87 chargingStation
: ChargingStation
,
88 commandName
: IncomingRequestCommand
,
89 schema
: JSONSchemaType
<T
>,
92 if (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false) {
95 const validate
= this.getJsonIncomingRequestValidateFunction
<T
>(commandName
, schema
);
96 if (validate(payload
)) {
100 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`,
104 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
105 'Incoming request PDU is invalid',
107 JSON
.stringify(validate
.errors
, undefined, 2),
111 protected handleRequestClearCache(chargingStation
: ChargingStation
): ClearCacheResponse
{
112 if (chargingStation
.idTagsCache
.deleteIdTags(getIdTagsFile(chargingStation
.stationInfo
)!)) {
113 return OCPPConstants
.OCPP_RESPONSE_ACCEPTED
;
115 return OCPPConstants
.OCPP_RESPONSE_REJECTED
;
118 private getJsonIncomingRequestValidateFunction
<T
extends JsonType
>(
119 commandName
: IncomingRequestCommand
,
120 schema
: JSONSchemaType
<T
>,
122 if (this.jsonValidateFunctions
.has(commandName
) === false) {
123 this.jsonValidateFunctions
.set(commandName
, this.ajv
.compile
<T
>(schema
).bind(this));
125 return this.jsonValidateFunctions
.get(commandName
)!;
128 // eslint-disable-next-line @typescript-eslint/no-unused-vars
129 public abstract incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
130 chargingStation
: ChargingStation
,
132 commandName
: IncomingRequestCommand
,
133 commandPayload
: ReqType
,