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 { OCPP20IncomingRequestCommand
} from
'../../../types/ocpp/2.0/Requests';
8 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
9 import type { IncomingRequestHandler
} from
'../../../types/ocpp/Requests';
10 import logger from
'../../../utils/Logger';
11 import type ChargingStation from
'../../ChargingStation';
12 import OCPPIncomingRequestService from
'../OCPPIncomingRequestService';
13 import { OCPP20ServiceUtils
} from
'./OCPP20ServiceUtils';
15 const moduleName
= 'OCPP20IncomingRequestService';
17 export default class OCPP20IncomingRequestService
extends OCPPIncomingRequestService
{
18 private incomingRequestHandlers
: Map
<OCPP20IncomingRequestCommand
, IncomingRequestHandler
>;
19 private jsonSchemas
: Map
<OCPP20IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
21 public constructor() {
22 if (new.target
?.name
=== moduleName
) {
23 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
26 this.incomingRequestHandlers
= new Map
<OCPP20IncomingRequestCommand
, IncomingRequestHandler
>();
27 this.jsonSchemas
= new Map
<OCPP20IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>();
28 this.validatePayload
.bind(this);
31 public async incomingRequestHandler(
32 chargingStation
: ChargingStation
,
34 commandName
: OCPP20IncomingRequestCommand
,
35 commandPayload
: JsonType
37 let response
: JsonType
;
39 chargingStation
.getOcppStrictCompliance() === true &&
40 chargingStation
.isInPendingState() === true /* &&
41 (commandName === OCPP20IncomingRequestCommand.REMOTE_START_TRANSACTION ||
42 commandName === OCPP20IncomingRequestCommand.REMOTE_STOP_TRANSACTION ) */
45 ErrorType
.SECURITY_ERROR
,
46 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
50 )} while the charging station is in pending state on the central server`,
56 chargingStation
.isRegistered() === true ||
57 (chargingStation
.getOcppStrictCompliance() === false &&
58 chargingStation
.isInUnknownState() === true)
61 this.incomingRequestHandlers
.has(commandName
) === true &&
62 OCPP20ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
65 this.validatePayload(chargingStation
, commandName
, commandPayload
);
66 // Call the method to build the response
67 response
= await this.incomingRequestHandlers
.get(commandName
)(
74 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
82 ErrorType
.NOT_IMPLEMENTED
,
83 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
94 ErrorType
.SECURITY_ERROR
,
95 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
99 )} while the charging station is not registered on the central server.`,
104 // Send the built response
105 await chargingStation
.ocppRequestService
.sendResponse(
113 private validatePayload(
114 chargingStation
: ChargingStation
,
115 commandName
: OCPP20IncomingRequestCommand
,
116 commandPayload
: JsonType
118 if (this.jsonSchemas
.has(commandName
)) {
119 return this.validateIncomingRequestPayload(
122 this.jsonSchemas
.get(commandName
),
127 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation`