1 // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
3 import type { JSONSchemaType
} 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 jsonSchemas
: Map
<OCPP20IncomingRequestCommand
, JSONSchemaType
<JsonType
>>
23 private readonly incomingRequestHandlers
: Map
<
24 OCPP20IncomingRequestCommand
,
25 IncomingRequestHandler
28 public constructor () {
29 // if (new.target.name === moduleName) {
30 // throw new TypeError(`Cannot construct ${new.target.name} instances directly`)
32 super(OCPPVersion
.VERSION_20
)
33 this.incomingRequestHandlers
= new Map
<OCPP20IncomingRequestCommand
, IncomingRequestHandler
>([
34 [OCPP20IncomingRequestCommand
.CLEAR_CACHE
, this.handleRequestClearCache
.bind(this)]
36 this.jsonSchemas
= new Map
<OCPP20IncomingRequestCommand
, JSONSchemaType
<JsonType
>>([
38 OCPP20IncomingRequestCommand
.CLEAR_CACHE
,
39 OCPP20ServiceUtils
.parseJsonSchemaFile
<OCPP20ClearCacheRequest
>(
40 'assets/json-schemas/ocpp/2.0/ClearCacheRequest.json',
46 this.validatePayload
= this.validatePayload
.bind(this)
49 public async incomingRequestHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
50 chargingStation
: ChargingStation
,
52 commandName
: OCPP20IncomingRequestCommand
,
53 commandPayload
: ReqType
57 chargingStation
.stationInfo
?.ocppStrictCompliance
=== true &&
58 chargingStation
.inPendingState() &&
59 (commandName
=== OCPP20IncomingRequestCommand
.REQUEST_START_TRANSACTION
||
60 commandName
=== OCPP20IncomingRequestCommand
.REQUEST_STOP_TRANSACTION
)
63 ErrorType
.SECURITY_ERROR
,
64 `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
68 )} while the charging station is in pending state on the central server`,
74 chargingStation
.isRegistered() ||
75 (chargingStation
.stationInfo
?.ocppStrictCompliance
=== false &&
76 chargingStation
.inUnknownState())
79 this.incomingRequestHandlers
.has(commandName
) &&
80 OCPP20ServiceUtils
.isIncomingRequestCommandSupported(chargingStation
, commandName
)
83 this.validatePayload(chargingStation
, commandName
, commandPayload
)
84 // Call the method to build the response
85 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
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
)) {
138 return this.validateIncomingRequestPayload(
141 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
142 this.jsonSchemas
.get(commandName
)!,
147 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation`