1 import Ajv
, { type JSONSchemaType
, type ValidateFunction
} from
'ajv';
2 import ajvFormats from
'ajv-formats';
4 import { OCPPConstants
} from
'./OCPPConstants';
5 import { OCPPServiceUtils
} from
'./OCPPServiceUtils';
6 import { type ChargingStation
, getIdTagsFile
} from
'../../charging-station';
7 import { OCPPError
} from
'../../exception';
11 IncomingRequestCommand
,
15 import { logger
, setDefaultErrorParams
} from
'../../utils';
17 const moduleName
= 'OCPPIncomingRequestService';
19 export abstract class OCPPIncomingRequestService
{
20 private static instance
: OCPPIncomingRequestService
| null = null;
21 private readonly version
: OCPPVersion
;
22 private readonly ajv
: Ajv
;
23 private jsonValidateFunctions
: Map
<IncomingRequestCommand
, ValidateFunction
<JsonType
>>;
24 protected abstract jsonSchemas
: Map
<IncomingRequestCommand
, JSONSchemaType
<JsonType
>>;
26 protected constructor(version
: OCPPVersion
) {
27 this.version
= version
;
29 keywords
: ['javaType'],
30 multipleOfPrecision
: 2,
33 this.jsonValidateFunctions
= new Map
<IncomingRequestCommand
, ValidateFunction
<JsonType
>>();
34 this.incomingRequestHandler
= this.incomingRequestHandler
.bind(this) as <
35 ReqType
extends JsonType
,
36 // eslint-disable-next-line @typescript-eslint/no-unused-vars
37 ResType
extends JsonType
,
39 chargingStation
: ChargingStation
,
41 commandName
: IncomingRequestCommand
,
42 commandPayload
: ReqType
,
44 this.validateIncomingRequestPayload
= this.validateIncomingRequestPayload
.bind(this) as <
47 chargingStation
: ChargingStation
,
48 commandName
: IncomingRequestCommand
,
49 schema
: JSONSchemaType
<T
>,
54 public static getInstance
<T
extends OCPPIncomingRequestService
>(this: new () => T
): T
{
55 if (OCPPIncomingRequestService
.instance
=== null) {
56 OCPPIncomingRequestService
.instance
= new this();
58 return OCPPIncomingRequestService
.instance
as T
;
61 protected handleIncomingRequestError
<T
extends JsonType
>(
62 chargingStation
: ChargingStation
,
63 commandName
: IncomingRequestCommand
,
65 params
: HandleErrorParams
<T
> = { throwError
: true, consoleOut
: false },
67 setDefaultErrorParams(params
);
69 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
72 if (!params
?.throwError
&& params
?.errorResponse
) {
73 return params
?.errorResponse
;
75 if (params
?.throwError
&& !params
?.errorResponse
) {
78 if (params
?.throwError
&& params
?.errorResponse
) {
79 return params
?.errorResponse
;
83 protected validateIncomingRequestPayload
<T
extends JsonType
>(
84 chargingStation
: ChargingStation
,
85 commandName
: IncomingRequestCommand
,
86 schema
: JSONSchemaType
<T
>,
89 if (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false) {
92 const validate
= this.getJsonIncomingRequestValidateFunction
<T
>(commandName
, schema
);
93 if (validate(payload
)) {
97 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`,
101 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
102 'Incoming request PDU is invalid',
104 JSON
.stringify(validate
.errors
, undefined, 2),
108 protected handleRequestClearCache(chargingStation
: ChargingStation
): ClearCacheResponse
{
109 if (chargingStation
.idTagsCache
.deleteIdTags(getIdTagsFile(chargingStation
.stationInfo
)!)) {
110 return OCPPConstants
.OCPP_RESPONSE_ACCEPTED
;
112 return OCPPConstants
.OCPP_RESPONSE_REJECTED
;
115 private getJsonIncomingRequestValidateFunction
<T
extends JsonType
>(
116 commandName
: IncomingRequestCommand
,
117 schema
: JSONSchemaType
<T
>,
119 if (this.jsonValidateFunctions
.has(commandName
) === false) {
120 this.jsonValidateFunctions
.set(commandName
, this.ajv
.compile
<T
>(schema
).bind(this));
122 return this.jsonValidateFunctions
.get(commandName
)!;
125 // eslint-disable-next-line @typescript-eslint/no-unused-vars
126 public abstract incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
127 chargingStation
: ChargingStation
,
129 commandName
: IncomingRequestCommand
,
130 commandPayload
: ReqType
,