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/OCPPError';
7 import type { JsonObject
, JsonType
} from
'../../../types/JsonType';
9 type OCPP20ClearCacheRequest
,
10 OCPP20IncomingRequestCommand
,
11 } from
'../../../types/ocpp/2.0/Requests';
12 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
13 import { OCPPVersion
} from
'../../../types/ocpp/OCPPVersion';
14 import type { IncomingRequestHandler
} from
'../../../types/ocpp/Requests';
15 import logger from
'../../../utils/Logger';
16 import type ChargingStation from
'../../ChargingStation';
17 import OCPPIncomingRequestService from
'../OCPPIncomingRequestService';
19 const moduleName
= 'OCPP20IncomingRequestService';
21 export default 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`