1 import { AsyncResource
} from
'node:async_hooks';
3 import Ajv
, { type JSONSchemaType
, type ValidateFunction
} from
'ajv';
4 import ajvFormats from
'ajv-formats';
6 import { OCPPConstants
} from
'./OCPPConstants';
7 import { OCPPServiceUtils
} from
'./OCPPServiceUtils';
8 import { type ChargingStation
, getIdTagsFile
} from
'../../charging-station';
9 import { OCPPError
} from
'../../exception';
13 IncomingRequestCommand
,
17 import { logger
, setDefaultErrorParams
} from
'../../utils';
19 const moduleName
= 'OCPPIncomingRequestService';
21 export abstract class OCPPIncomingRequestService
extends AsyncResource
{
22 private static instance
: OCPPIncomingRequestService
| null = null;
23 private readonly version
: OCPPVersion
;
24 private readonly ajv
: Ajv
;
25 private jsonValidateFunctions
: Map
<IncomingRequestCommand
, ValidateFunction
<JsonType
>>;
26 protected abstract jsonSchemas
: Map
<IncomingRequestCommand
, JSONSchemaType
<JsonType
>>;
28 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
.getOcppStrictCompliance() === false) {
95 if (this.jsonValidateFunctions
.has(commandName
) === false) {
96 this.jsonValidateFunctions
.set(commandName
, this.ajv
.compile
<T
>(schema
).bind(this));
98 const validate
= this.jsonValidateFunctions
.get(commandName
)!;
99 if (validate(payload
)) {
103 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`,
107 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
108 'Incoming request PDU is invalid',
110 JSON
.stringify(validate
.errors
, undefined, 2),
114 protected handleRequestClearCache(chargingStation
: ChargingStation
): ClearCacheResponse
{
115 if (chargingStation
.idTagsCache
.deleteIdTags(getIdTagsFile(chargingStation
.stationInfo
)!)) {
116 return OCPPConstants
.OCPP_RESPONSE_ACCEPTED
;
118 return OCPPConstants
.OCPP_RESPONSE_REJECTED
;
121 // eslint-disable-next-line @typescript-eslint/no-unused-vars
122 public abstract incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
123 chargingStation
: ChargingStation
,
125 commandName
: IncomingRequestCommand
,
126 commandPayload
: ReqType
,