1 import { AsyncResource
} from
'node:async_hooks';
3 import Ajv
, { type JSONSchemaType
} 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
,
18 import { logger
, setDefaultErrorParams
} from
'../../utils';
20 const moduleName
= 'OCPPIncomingRequestService';
22 export abstract class OCPPIncomingRequestService
extends AsyncResource
{
23 private static instance
: OCPPIncomingRequestService
| null = null;
24 private readonly version
: OCPPVersion
;
25 private readonly ajv
: Ajv
;
26 protected abstract jsonSchemas
: Map
<IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
28 protected constructor(version
: OCPPVersion
) {
30 this.version
= version
;
32 keywords
: ['javaType'],
33 multipleOfPrecision
: 2,
36 this.incomingRequestHandler
= this.incomingRequestHandler
.bind(this) as <
37 ReqType
extends JsonType
,
38 // eslint-disable-next-line @typescript-eslint/no-unused-vars
39 ResType
extends JsonType
,
41 chargingStation
: ChargingStation
,
43 commandName
: IncomingRequestCommand
,
44 commandPayload
: ReqType
,
46 this.validateIncomingRequestPayload
= this.validateIncomingRequestPayload
.bind(this) as <
49 chargingStation
: ChargingStation
,
50 commandName
: IncomingRequestCommand
,
51 schema
: JSONSchemaType
<T
>,
56 public static getInstance
<T
extends OCPPIncomingRequestService
>(this: new () => T
): T
{
57 if (OCPPIncomingRequestService
.instance
=== null) {
58 OCPPIncomingRequestService
.instance
= new this();
60 return OCPPIncomingRequestService
.instance
as T
;
63 protected handleIncomingRequestError
<T
extends JsonType
>(
64 chargingStation
: ChargingStation
,
65 commandName
: IncomingRequestCommand
,
67 params
: HandleErrorParams
<T
> = { throwError
: true, consoleOut
: false },
69 setDefaultErrorParams(params
);
71 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
74 if (!params
?.throwError
&& params
?.errorResponse
) {
75 return params
?.errorResponse
;
77 if (params
?.throwError
&& !params
?.errorResponse
) {
80 if (params
?.throwError
&& params
?.errorResponse
) {
81 return params
?.errorResponse
;
85 protected validateIncomingRequestPayload
<T
extends JsonType
>(
86 chargingStation
: ChargingStation
,
87 commandName
: IncomingRequestCommand
,
88 schema
: JSONSchemaType
<T
>,
91 if (chargingStation
.getOcppStrictCompliance() === false) {
94 const validate
= this.ajv
.compile(schema
);
95 if (validate(payload
)) {
99 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`,
103 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
!),
104 'Incoming request PDU is invalid',
106 JSON
.stringify(validate
.errors
, undefined, 2),
110 protected handleRequestClearCache(chargingStation
: ChargingStation
): ClearCacheResponse
{
111 if (chargingStation
.idTagsCache
.deleteIdTags(getIdTagsFile(chargingStation
.stationInfo
)!)) {
112 return OCPPConstants
.OCPP_RESPONSE_ACCEPTED
;
114 return OCPPConstants
.OCPP_RESPONSE_REJECTED
;
117 // eslint-disable-next-line @typescript-eslint/no-unused-vars
118 public abstract incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
119 chargingStation
: ChargingStation
,
121 commandName
: IncomingRequestCommand
,
122 commandPayload
: ReqType
,