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'
41 this.validatePayload
.bind(this);
44 public async incomingRequestHandler(
45 chargingStation
: ChargingStation
,
47 commandName
: OCPP20IncomingRequestCommand
,
48 commandPayload
: JsonType
50 let response
: JsonType
;
52 chargingStation
.getOcppStrictCompliance() === true &&
53 chargingStation
.isInPendingState() === true &&
54 (commandName
=== OCPP20IncomingRequestCommand
.REQUEST_START_TRANSACTION
||
55 commandName
=== OCPP20IncomingRequestCommand
.REQUEST_STOP_TRANSACTION
)
58 ErrorType
.SECURITY_ERROR
,
59 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
63 )} while the charging station is in pending state on the central server`,
69 chargingStation
.isRegistered() === true ||
70 (chargingStation
.getOcppStrictCompliance() === false &&
71 chargingStation
.isInUnknownState() === true)
74 this.incomingRequestHandlers
.has(commandName
) === true &&
75 OCPP20ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
78 this.validatePayload(chargingStation
, commandName
, commandPayload
);
79 // Call the method to build the response
80 response
= await this.incomingRequestHandlers
.get(commandName
)(
87 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
95 ErrorType
.NOT_IMPLEMENTED
,
96 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
107 ErrorType
.SECURITY_ERROR
,
108 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
112 )} while the charging station is not registered on the central server.`,
117 // Send the built response
118 await chargingStation
.ocppRequestService
.sendResponse(
126 private validatePayload(
127 chargingStation
: ChargingStation
,
128 commandName
: OCPP20IncomingRequestCommand
,
129 commandPayload
: JsonType
131 if (this.jsonSchemas
.has(commandName
) === true) {
132 return this.validateIncomingRequestPayload(
135 this.jsonSchemas
.get(commandName
),
140 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation`