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 payloadValidateFunctions
: Map
<OCPP20IncomingRequestCommand
, ValidateFunction
<JsonType
>>
24 private readonly incomingRequestHandlers
: Map
<
25 OCPP20IncomingRequestCommand
,
26 IncomingRequestHandler
29 public constructor () {
30 // if (new.target.name === moduleName) {
31 // throw new TypeError(`Cannot construct ${new.target.name} instances directly`)
33 super(OCPPVersion
.VERSION_20
)
34 this.incomingRequestHandlers
= new Map
<OCPP20IncomingRequestCommand
, IncomingRequestHandler
>([
35 [OCPP20IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)]
37 this.payloadValidateFunctions
= new Map
<
38 OCPP20IncomingRequestCommand
,
39 ValidateFunction
<JsonType
>
42 OCPP20IncomingRequestCommand
.CLEAR_CACHE
,
45 OCPP20ServiceUtils
.parseJsonSchemaFile
<OCPP20ClearCacheRequest
>(
46 'assets/json-schemas/ocpp/2.0/ClearCacheRequest.json',
54 this.validatePayload
= this.validatePayload
.bind(this)
57 public async incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
58 chargingStation
: ChargingStation
,
60 commandName
: OCPP20IncomingRequestCommand
,
61 commandPayload
: ReqType
65 chargingStation
.stationInfo
?.ocppStrictCompliance
=== true &&
66 chargingStation
.inPendingState() &&
67 (commandName
=== OCPP20IncomingRequestCommand
.REQUEST_START_TRANSACTION
||
68 commandName
=== OCPP20IncomingRequestCommand
.REQUEST_STOP_TRANSACTION
)
71 ErrorType
.SECURITY_ERROR
,
72 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
76 )} while the charging station is in pending state on the central server`,
82 chargingStation
.isRegistered() ||
83 (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false &&
84 chargingStation
.inUnknownState())
87 this.incomingRequestHandlers
.has(commandName
) &&
88 OCPP20ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
)
91 this.validatePayload(chargingStation
, commandName
, commandPayload
)
92 // Call the method to build the response
93 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
94 response
= (await this.incomingRequestHandlers
.get(commandName
)!(
98 this.emit(commandName
, chargingStation
, commandPayload
, response
)
102 `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`,
110 ErrorType
.NOT_IMPLEMENTED
,
111 `${commandName} is not implemented to handle request PDU ${JSON.stringify(
122 ErrorType
.SECURITY_ERROR
,
123 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
127 )} while the charging station is not registered on the central server.`,
132 // Send the built response
133 await chargingStation
.ocppRequestService
.sendResponse(
141 private validatePayload (
142 chargingStation
: ChargingStation
,
143 commandName
: OCPP20IncomingRequestCommand
,
144 commandPayload
: JsonType
146 if (this.payloadValidateFunctions
.has(commandName
)) {
147 return this.validateIncomingRequestPayload(chargingStation
, commandName
, commandPayload
)
150 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema validation function found for command '${commandName}' PDU validation`