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 { JsonObject
, JsonType
} from
'../../types/JsonType';
9 import type { OCPPVersion
} from
'../../types/ocpp/OCPPVersion';
10 import type { IncomingRequestCommand
} from
'../../types/ocpp/Requests';
11 import type { ClearCacheResponse
} from
'../../types/ocpp/Responses';
12 import logger from
'../../utils/Logger';
13 import type ChargingStation from
'../ChargingStation';
14 import { ChargingStationUtils
} from
'../ChargingStationUtils';
15 import OCPPConstants from
'./OCPPConstants';
16 import { OCPPServiceUtils
} from
'./OCPPServiceUtils';
18 const moduleName
= 'OCPPIncomingRequestService';
20 export default abstract class OCPPIncomingRequestService
{
21 private static instance
: OCPPIncomingRequestService
| null = null;
22 protected asyncResource
: AsyncResource
;
23 private readonly version
: OCPPVersion
;
24 private readonly ajv
: Ajv
;
25 protected abstract jsonSchemas
: Map
<IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
27 protected constructor(version
: OCPPVersion
) {
28 this.version
= version
;
30 keywords
: ['javaType'],
31 multipleOfPrecision
: 2,
34 this.asyncResource
= new AsyncResource(moduleName
);
35 this.incomingRequestHandler
.bind(this);
36 this.validateIncomingRequestPayload
.bind(this);
39 public static getInstance
<T
extends OCPPIncomingRequestService
>(this: new () => T
): T
{
40 if (OCPPIncomingRequestService
.instance
=== null) {
41 OCPPIncomingRequestService
.instance
= new this();
43 return OCPPIncomingRequestService
.instance
as T
;
46 protected handleIncomingRequestError
<T
>(
47 chargingStation
: ChargingStation
,
48 commandName
: IncomingRequestCommand
,
50 params
: HandleErrorParams
<T
> = { throwError
: true }
53 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
56 if (!params
?.throwError
&& params
?.errorResponse
) {
57 return params
?.errorResponse
;
59 if (params
?.throwError
&& !params
?.errorResponse
) {
62 if (params
?.throwError
&& params
?.errorResponse
) {
63 return params
?.errorResponse
;
67 protected validateIncomingRequestPayload
<T
extends JsonType
>(
68 chargingStation
: ChargingStation
,
69 commandName
: IncomingRequestCommand
,
70 schema
: JSONSchemaType
<T
>,
73 if (chargingStation
.getPayloadSchemaValidation() === false) {
76 const validate
= this.ajv
.compile(schema
);
77 if (validate(payload
)) {
81 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`,
85 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
86 'Incoming request PDU is invalid',
88 JSON
.stringify(validate
.errors
, null, 2)
92 protected handleRequestClearCache(chargingStation
: ChargingStation
): ClearCacheResponse
{
93 chargingStation
.authorizedTagsCache
.deleteAuthorizedTags(
94 ChargingStationUtils
.getAuthorizationFile(chargingStation
.stationInfo
)
96 return OCPPConstants
.OCPP_RESPONSE_ACCEPTED
;
99 public abstract incomingRequestHandler(
100 chargingStation
: ChargingStation
,
102 commandName
: IncomingRequestCommand
,
103 commandPayload
: JsonType