1 import { AsyncResource
} from
'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 OCPPError from
'../../exception/OCPPError';
9 import type { HandleErrorParams
} from
'../../types/Error';
10 import type { JsonObject
, JsonType
} from
'../../types/JsonType';
11 import type { OCPPVersion
} from
'../../types/ocpp/OCPPVersion';
12 import type { IncomingRequestCommand
} from
'../../types/ocpp/Requests';
13 import type { ClearCacheResponse
} from
'../../types/ocpp/Responses';
14 import logger from
'../../utils/Logger';
15 import type ChargingStation from
'../ChargingStation';
16 import { ChargingStationUtils
} from
'../ChargingStationUtils';
18 const moduleName
= 'OCPPIncomingRequestService';
20 export default abstract class OCPPIncomingRequestService
extends AsyncResource
{
21 private static instance
: OCPPIncomingRequestService
| null = null;
22 private readonly version
: OCPPVersion
;
23 private readonly ajv
: Ajv
;
24 protected abstract jsonSchemas
: Map
<IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
26 protected constructor(version
: OCPPVersion
) {
28 this.version
= version
;
30 keywords
: ['javaType'],
31 multipleOfPrecision
: 2,
34 this.incomingRequestHandler
.bind(this);
35 this.validateIncomingRequestPayload
.bind(this);
38 public static getInstance
<T
extends OCPPIncomingRequestService
>(this: new () => T
): T
{
39 if (OCPPIncomingRequestService
.instance
=== null) {
40 OCPPIncomingRequestService
.instance
= new this();
42 return OCPPIncomingRequestService
.instance
as T
;
45 protected handleIncomingRequestError
<T
>(
46 chargingStation
: ChargingStation
,
47 commandName
: IncomingRequestCommand
,
49 params
: HandleErrorParams
<T
> = { throwError
: true }
52 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
55 if (!params
?.throwError
&& params
?.errorResponse
) {
56 return params
?.errorResponse
;
58 if (params
?.throwError
&& !params
?.errorResponse
) {
61 if (params
?.throwError
&& params
?.errorResponse
) {
62 return params
?.errorResponse
;
66 protected validateIncomingRequestPayload
<T
extends JsonType
>(
67 chargingStation
: ChargingStation
,
68 commandName
: IncomingRequestCommand
,
69 schema
: JSONSchemaType
<T
>,
72 if (chargingStation
.getPayloadSchemaValidation() === false) {
75 const validate
= this.ajv
.compile(schema
);
76 if (validate(payload
)) {
80 `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`,
84 OCPPServiceUtils
.ajvErrorsToErrorType(validate
.errors
),
85 'Incoming request PDU is invalid',
87 JSON
.stringify(validate
.errors
, null, 2)
91 protected handleRequestClearCache(chargingStation
: ChargingStation
): ClearCacheResponse
{
92 chargingStation
.authorizedTagsCache
.deleteAuthorizedTags(
93 ChargingStationUtils
.getAuthorizationFile(chargingStation
.stationInfo
)
95 return OCPPConstants
.OCPP_RESPONSE_ACCEPTED
;
98 public abstract incomingRequestHandler(
99 chargingStation
: ChargingStation
,
101 commandName
: IncomingRequestCommand
,
102 commandPayload
: JsonType