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
= this.validatePayload
.bind(this) as (
44 chargingStation
: ChargingStation
,
45 commandName
: OCPP20IncomingRequestCommand
,
46 commandPayload
: JsonType
50 public async incomingRequestHandler(
51 chargingStation
: ChargingStation
,
53 commandName
: OCPP20IncomingRequestCommand
,
54 commandPayload
: JsonType
56 let response
: JsonType
;
58 chargingStation
.getOcppStrictCompliance() === true &&
59 chargingStation
.inPendingState() === true &&
60 (commandName
=== OCPP20IncomingRequestCommand
.REQUEST_START_TRANSACTION
||
61 commandName
=== OCPP20IncomingRequestCommand
.REQUEST_STOP_TRANSACTION
)
64 ErrorType
.SECURITY_ERROR
,
65 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
69 )} while the charging station is in pending state on the central server`,
75 chargingStation
.isRegistered() === true ||
76 (chargingStation
.getOcppStrictCompliance() === false &&
77 chargingStation
.inUnknownState() === true)
80 this.incomingRequestHandlers
.has(commandName
) === true &&
81 OCPP20ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
) === true
84 this.validatePayload(chargingStation
, commandName
, commandPayload
);
85 // Call the method to build the response
86 response
= await this.incomingRequestHandlers
.get(commandName
)(
93 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
101 ErrorType
.NOT_IMPLEMENTED
,
102 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
113 ErrorType
.SECURITY_ERROR
,
114 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
118 )} while the charging station is not registered on the central server.`,
123 // Send the built response
124 await chargingStation
.ocppRequestService
.sendResponse(
132 private validatePayload(
133 chargingStation
: ChargingStation
,
134 commandName
: OCPP20IncomingRequestCommand
,
135 commandPayload
: JsonType
137 if (this.jsonSchemas
.has(commandName
) === true) {
138 return this.validateIncomingRequestPayload(
141 this.jsonSchemas
.get(commandName
),
146 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation`