1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import type { JSONSchemaType
} from
'ajv';
5 import { OCPP20ServiceUtils
} from
'./OCPP20ServiceUtils';
6 import { OCPPError
} from
'../../../exception';
9 type IncomingRequestHandler
,
12 type OCPP20ClearCacheRequest
,
13 OCPP20IncomingRequestCommand
,
15 } from
'../../../types';
16 import { logger
} from
'../../../utils/Logger';
17 import type { ChargingStation
} from
'../../ChargingStation';
18 import { OCPPIncomingRequestService
} from
'../OCPPIncomingRequestService';
20 const moduleName
= 'OCPP20IncomingRequestService';
22 export class OCPP20IncomingRequestService
extends OCPPIncomingRequestService
{
23 protected jsonSchemas
: Map
<OCPP20IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
24 private incomingRequestHandlers
: Map
<OCPP20IncomingRequestCommand
, IncomingRequestHandler
>;
26 public constructor() {
27 if (new.target
?.name
=== moduleName
) {
28 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
30 super(OCPPVersion
.VERSION_20
);
31 this.incomingRequestHandlers
= new Map
<OCPP20IncomingRequestCommand
, IncomingRequestHandler
>([
32 [OCPP20IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
34 this.jsonSchemas
= new Map
<OCPP20IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
36 OCPP20IncomingRequestCommand
.CLEAR_CACHE
,
37 OCPP20ServiceUtils
.parseJsonSchemaFile
<OCPP20ClearCacheRequest
>(
38 '../../../assets/json-schemas/ocpp/2.0/ClearCacheRequest.json',
44 this.validatePayload
.bind(this);
47 public async incomingRequestHandler(
48 chargingStation
: ChargingStation
,
50 commandName
: OCPP20IncomingRequestCommand
,
51 commandPayload
: JsonType
53 let response
: JsonType
;
55 chargingStation
.getOcppStrictCompliance() === true &&
56 chargingStation
.isInPendingState() === true &&
57 (commandName
=== OCPP20IncomingRequestCommand
.REQUEST_START_TRANSACTION
||
58 commandName
=== OCPP20IncomingRequestCommand
.REQUEST_STOP_TRANSACTION
)
61 ErrorType
.SECURITY_ERROR
,
62 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
66 )} while the charging station is in pending state on the central server`,
72 chargingStation
.isRegistered() === true ||
73 (chargingStation
.getOcppStrictCompliance() === false &&
74 chargingStation
.isInUnknownState() === true)
77 this.incomingRequestHandlers
.has(commandName
) === true &&
78 OCPP20ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
81 this.validatePayload(chargingStation
, commandName
, commandPayload
);
82 // Call the method to build the response
83 response
= await this.incomingRequestHandlers
.get(commandName
)(
90 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
98 ErrorType
.NOT_IMPLEMENTED
,
99 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
110 ErrorType
.SECURITY_ERROR
,
111 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
115 )} while the charging station is not registered on the central server.`,
120 // Send the built response
121 await chargingStation
.ocppRequestService
.sendResponse(
129 private validatePayload(
130 chargingStation
: ChargingStation
,
131 commandName
: OCPP20IncomingRequestCommand
,
132 commandPayload
: JsonType
134 if (this.jsonSchemas
.has(commandName
) === true) {
135 return this.validateIncomingRequestPayload(
138 this.jsonSchemas
.get(commandName
),
143 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation`