1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import type { JSONSchemaType
} from
'ajv';
5 import type { ChargingStation
} from
'../../../charging-station';
6 import { OCPPError
} from
'../../../exception';
9 type IncomingRequestHandler
,
12 type OCPP20ClearCacheRequest
,
13 OCPP20IncomingRequestCommand
,
15 } from
'../../../types';
16 import { logger
} from
'../../../utils';
17 import { OCPP20ServiceUtils
, OCPPIncomingRequestService
} from
'../internal';
19 const moduleName
= 'OCPP20IncomingRequestService';
21 export class OCPP20IncomingRequestService
extends OCPPIncomingRequestService
{
22 protected jsonSchemas
: Map
<OCPP20IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>;
23 private incomingRequestHandlers
: Map
<OCPP20IncomingRequestCommand
, IncomingRequestHandler
>;
25 public constructor() {
26 if (new.target
?.name
=== moduleName
) {
27 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
29 super(OCPPVersion
.VERSION_20
);
30 this.incomingRequestHandlers
= new Map
<OCPP20IncomingRequestCommand
, IncomingRequestHandler
>([
31 [OCPP20IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)],
33 this.jsonSchemas
= new Map
<OCPP20IncomingRequestCommand
, JSONSchemaType
<JsonObject
>>([
35 OCPP20IncomingRequestCommand
.CLEAR_CACHE
,
36 OCPP20ServiceUtils
.parseJsonSchemaFile
<OCPP20ClearCacheRequest
>(
37 '../../../assets/json-schemas/ocpp/2.0/ClearCacheRequest.json',
43 this.validatePayload
.bind(this);
46 public async incomingRequestHandler(
47 chargingStation
: ChargingStation
,
49 commandName
: OCPP20IncomingRequestCommand
,
50 commandPayload
: JsonType
52 let response
: JsonType
;
54 chargingStation
.getOcppStrictCompliance() === true &&
55 chargingStation
.isInPendingState() === true &&
56 (commandName
=== OCPP20IncomingRequestCommand
.REQUEST_START_TRANSACTION
||
57 commandName
=== OCPP20IncomingRequestCommand
.REQUEST_STOP_TRANSACTION
)
60 ErrorType
.SECURITY_ERROR
,
61 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
65 )} while the charging station is in pending state on the central server`,
71 chargingStation
.isRegistered() === true ||
72 (chargingStation
.getOcppStrictCompliance() === false &&
73 chargingStation
.isInUnknownState() === true)
76 this.incomingRequestHandlers
.has(commandName
) === true &&
77 OCPP20ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
80 this.validatePayload(chargingStation
, commandName
, commandPayload
);
81 // Call the method to build the response
82 response
= await this.incomingRequestHandlers
.get(commandName
)(
89 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
97 ErrorType
.NOT_IMPLEMENTED
,
98 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
109 ErrorType
.SECURITY_ERROR
,
110 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
114 )} while the charging station is not registered on the central server.`,
119 // Send the built response
120 await chargingStation
.ocppRequestService
.sendResponse(
128 private validatePayload(
129 chargingStation
: ChargingStation
,
130 commandName
: OCPP20IncomingRequestCommand
,
131 commandPayload
: JsonType
133 if (this.jsonSchemas
.has(commandName
) === true) {
134 return this.validateIncomingRequestPayload(
137 this.jsonSchemas
.get(commandName
),
142 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation`