1 import { AsyncResource
} from
'async_hooks';
3 import Ajv
, { type JSONSchemaType
} from
'ajv';
4 import ajvFormats from
'ajv-formats';
6 import OCPPError from
'../../exception/OCPPError';
7 import type { HandleErrorParams
} from
'../../types/Error';
8 import type { JsonType
} from
'../../types/JsonType';
9 import type { OCPPVersion
} from
'../../types/ocpp/OCPPVersion';
10 import type { IncomingRequestCommand
} from
'../../types/ocpp/Requests';
11 import logger from
'../../utils/Logger';
12 import type ChargingStation from
'../ChargingStation';
13 import { OCPPServiceUtils
} from
'./OCPPServiceUtils';
15 const moduleName
= 'OCPPIncomingRequestService';
17 export default abstract class OCPPIncomingRequestService
{
18 private static instance
: OCPPIncomingRequestService
| null = null;
19 protected asyncResource
: AsyncResource
;
20 private readonly version
: OCPPVersion
;
21 private readonly ajv
: Ajv
;
23 protected constructor(version
: OCPPVersion
) {
24 this.version
= version
;
27 this.asyncResource
= new AsyncResource(moduleName
);
28 this.incomingRequestHandler
.bind(this);
29 this.validateIncomingRequestPayload
.bind(this);
32 public static getInstance
<T
extends OCPPIncomingRequestService
>(this: new () => T
): T
{
33 if (OCPPIncomingRequestService
.instance
=== null) {
34 OCPPIncomingRequestService
.instance
= new this();
36 return OCPPIncomingRequestService
.instance
as T
;
39 protected handleIncomingRequestError
<T
>(
40 chargingStation
: ChargingStation
,
41 commandName
: IncomingRequestCommand
,
43 params
: HandleErrorParams
<T
> = { throwError
: true }
46 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
49 if (!params
?.throwError
&& params
?.errorResponse
) {
50 return params
?.errorResponse
;
52 if (params
?.throwError
&& !params
?.errorResponse
) {
55 if (params
?.throwError
&& params
?.errorResponse
) {
56 return params
?.errorResponse
;
60 protected validateIncomingRequestPayload
<T
extends JsonType
>(
61 chargingStation
: ChargingStation
,
62 commandName
: IncomingRequestCommand
,
63 schema
: JSONSchemaType
<T
>,
66 if (chargingStation
.getPayloadSchemaValidation() === false) {
69 const validate
= this.ajv
.compile(schema
);
70 if (validate(payload
)) {
74 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Incoming request PDU is invalid: %j`,
78 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
79 'Incoming request PDU is invalid',
81 JSON
.stringify(validate
.errors
, null, 2)
85 public abstract incomingRequestHandler(
86 chargingStation
: ChargingStation
,
88 commandName
: IncomingRequestCommand
,
89 commandPayload
: JsonType