1 // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
3 import type { ValidateFunction
} from
'ajv'
5 import { OCPP20ServiceUtils
} from
'./OCPP20ServiceUtils.js'
6 import type { ChargingStation
} from
'../../../charging-station/index.js'
7 import { OCPPError
} from
'../../../exception/index.js'
10 type IncomingRequestHandler
,
12 type OCPP20ClearCacheRequest
,
13 OCPP20IncomingRequestCommand
,
15 } from
'../../../types/index.js'
16 import { logger
} from
'../../../utils/index.js'
17 import { OCPPIncomingRequestService
} from
'../OCPPIncomingRequestService.js'
19 const moduleName
= 'OCPP20IncomingRequestService'
21 export class OCPP20IncomingRequestService
extends OCPPIncomingRequestService
{
22 protected jsonSchemasValidateFunction
: Map
<
23 OCPP20IncomingRequestCommand
,
24 ValidateFunction
<JsonType
>
27 private readonly incomingRequestHandlers
: Map
<
28 OCPP20IncomingRequestCommand
,
29 IncomingRequestHandler
32 public constructor () {
33 // if (new.target.name === moduleName) {
34 // throw new TypeError(`Cannot construct ${new.target.name} instances directly`)
36 super(OCPPVersion
.VERSION_20
)
37 this.incomingRequestHandlers
= new Map
<OCPP20IncomingRequestCommand
, IncomingRequestHandler
>([
38 [OCPP20IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)]
40 this.jsonSchemasValidateFunction
= new Map
<
41 OCPP20IncomingRequestCommand
,
42 ValidateFunction
<JsonType
>
45 OCPP20IncomingRequestCommand
.CLEAR_CACHE
,
48 OCPP20ServiceUtils
.parseJsonSchemaFile
<OCPP20ClearCacheRequest
>(
49 'assets/json-schemas/ocpp/2.0/ClearCacheRequest.json',
57 this.validatePayload
= this.validatePayload
.bind(this)
60 public async incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
61 chargingStation
: ChargingStation
,
63 commandName
: OCPP20IncomingRequestCommand
,
64 commandPayload
: ReqType
68 chargingStation
.stationInfo
?.ocppStrictCompliance
=== true &&
69 chargingStation
.inPendingState() &&
70 (commandName
=== OCPP20IncomingRequestCommand
.REQUEST_START_TRANSACTION
||
71 commandName
=== OCPP20IncomingRequestCommand
.REQUEST_STOP_TRANSACTION
)
74 ErrorType
.SECURITY_ERROR
,
75 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
79 )} while the charging station is in pending state on the central server`,
85 chargingStation
.isRegistered() ||
86 (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false &&
87 chargingStation
.inUnknownState())
90 this.incomingRequestHandlers
.has(commandName
) &&
91 OCPP20ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
)
94 this.validatePayload(chargingStation
, commandName
, commandPayload
)
95 // Call the method to build the response
96 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
97 response
= (await this.incomingRequestHandlers
.get(commandName
)!(
104 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
112 ErrorType
.NOT_IMPLEMENTED
,
113 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
124 ErrorType
.SECURITY_ERROR
,
125 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
129 )} while the charging station is not registered on the central server.`,
134 // Send the built response
135 await chargingStation
.ocppRequestService
.sendResponse(
143 private validatePayload (
144 chargingStation
: ChargingStation
,
145 commandName
: OCPP20IncomingRequestCommand
,
146 commandPayload
: JsonType
148 if (this.jsonSchemasValidateFunction
.has(commandName
)) {
149 return this.validateIncomingRequestPayload(chargingStation
, commandName
, commandPayload
)
152 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema validation function found for command '${commandName}' PDU validation`