3b1e269556338a958568f2d4e16217a756716ddb
1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import type { JSONSchemaType
} from
'ajv';
5 import OCPPError from
'../../../exception/OCPPError';
6 import type { JsonObject
, JsonType
} from
'../../../types/JsonType';
7 import type { OCPP20RequestCommand
} from
'../../../types/ocpp/2.0/Requests';
8 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
9 import type { RequestParams
} from
'../../../types/ocpp/Requests';
10 import logger from
'../../../utils/Logger';
11 import Utils from
'../../../utils/Utils';
12 import type ChargingStation from
'../../ChargingStation';
13 import OCPPRequestService from
'../OCPPRequestService';
14 import type OCPPResponseService from
'../OCPPResponseService';
15 import { OCPP20ServiceUtils
} from
'./OCPP20ServiceUtils';
17 const moduleName
= 'OCPP20RequestService';
19 export default class OCPP20RequestService
extends OCPPRequestService
{
20 private jsonSchemas
: Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonObject
>>;
22 public constructor(ocppResponseService
: OCPPResponseService
) {
23 if (new.target
?.name
=== moduleName
) {
24 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
26 super(ocppResponseService
);
27 this.jsonSchemas
= new Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonObject
>>();
28 this.buildRequestPayload
.bind(this);
29 this.validatePayload
.bind(this);
32 public async requestHandler
<RequestType
extends JsonType
, ResponseType
extends JsonType
>(
33 chargingStation
: ChargingStation
,
34 commandName
: OCPP20RequestCommand
,
35 commandParams
?: JsonType
,
36 params
?: RequestParams
37 ): Promise
<ResponseType
> {
38 if (OCPP20ServiceUtils
.isRequestCommandSupported(chargingStation
, commandName
) === true) {
39 const requestPayload
= this.buildRequestPayload
<RequestType
>(
44 this.validatePayload(chargingStation
, commandName
, requestPayload
);
45 return (await this.sendMessage(
51 )) as unknown
as ResponseType
;
53 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
55 ErrorType
.NOT_SUPPORTED
,
56 `Unsupported OCPP command '${commandName}'`,
62 private buildRequestPayload
<Request
extends JsonType
>(
63 chargingStation
: ChargingStation
,
64 commandName
: OCPP20RequestCommand
,
65 commandParams
?: JsonType
67 commandParams
= commandParams
as JsonObject
;
68 switch (commandName
) {
70 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
72 ErrorType
.NOT_SUPPORTED
,
73 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
74 `Unsupported OCPP command '${commandName}'`,
81 private validatePayload
<Request
extends JsonType
>(
82 chargingStation
: ChargingStation
,
83 commandName
: OCPP20RequestCommand
,
84 requestPayload
: Request
86 if (this.jsonSchemas
.has(commandName
)) {
87 return this.validateRequestPayload(
90 this.jsonSchemas
.get(commandName
),
95 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation`