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
, addConfigurationKey
} from
'../../../charging-station/index.js'
7 import { OCPPError
} from
'../../../exception/index.js'
11 type OCPP20BootNotificationResponse
,
12 type OCPP20ClearCacheResponse
,
13 type OCPP20HeartbeatResponse
,
14 OCPP20IncomingRequestCommand
,
15 OCPP20OptionalVariableName
,
17 type OCPP20StatusNotificationResponse
,
19 RegistrationStatusEnumType
,
21 } from
'../../../types/index.js'
22 import { logger
} from
'../../../utils/index.js'
23 import { OCPPResponseService
} from
'../OCPPResponseService.js'
25 const moduleName
= 'OCPP20ResponseService'
27 export class OCPP20ResponseService
extends OCPPResponseService
{
28 public jsonIncomingRequestResponseSchemas
: Map
<
29 OCPP20IncomingRequestCommand
,
30 JSONSchemaType
<JsonType
>
33 private readonly responseHandlers
: Map
<OCPP20RequestCommand
, ResponseHandler
>
34 private readonly jsonSchemas
: Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonType
>>
36 public constructor () {
37 // if (new.target.name === moduleName) {
38 // throw new TypeError(`Cannot construct ${new.target.name} instances directly`)
40 super(OCPPVersion
.VERSION_20
)
41 this.responseHandlers
= new Map
<OCPP20RequestCommand
, ResponseHandler
>([
43 OCPP20RequestCommand
.BOOT_NOTIFICATION
,
44 this.handleResponseBootNotification
.bind(this) as ResponseHandler
46 [OCPP20RequestCommand
.HEARTBEAT
, this.emptyResponseHandler
],
47 [OCPP20RequestCommand
.STATUS_NOTIFICATION
, this.emptyResponseHandler
]
49 this.jsonSchemas
= new Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonType
>>([
51 OCPP20RequestCommand
.BOOT_NOTIFICATION
,
52 OCPP20ServiceUtils
.parseJsonSchemaFile
<OCPP20BootNotificationResponse
>(
53 'assets/json-schemas/ocpp/2.0/BootNotificationResponse.json',
59 OCPP20RequestCommand
.HEARTBEAT
,
60 OCPP20ServiceUtils
.parseJsonSchemaFile
<OCPP20HeartbeatResponse
>(
61 'assets/json-schemas/ocpp/2.0/HeartbeatResponse.json',
67 OCPP20RequestCommand
.STATUS_NOTIFICATION
,
68 OCPP20ServiceUtils
.parseJsonSchemaFile
<OCPP20StatusNotificationResponse
>(
69 'assets/json-schemas/ocpp/2.0/StatusNotificationResponse.json',
75 this.jsonIncomingRequestResponseSchemas
= new Map([
77 OCPP20IncomingRequestCommand
.CLEAR_CACHE
,
78 OCPP20ServiceUtils
.parseJsonSchemaFile
<OCPP20ClearCacheResponse
>(
79 'assets/json-schemas/ocpp/2.0/ClearCacheResponse.json',
85 this.validatePayload
= this.validatePayload
.bind(this)
88 public async responseHandler
<ReqType
extends JsonType
, ResType
extends JsonType
>(
89 chargingStation
: ChargingStation
,
90 commandName
: OCPP20RequestCommand
,
92 requestPayload
: ReqType
94 if (chargingStation
.isRegistered() || commandName
=== OCPP20RequestCommand
.BOOT_NOTIFICATION
) {
96 this.responseHandlers
.has(commandName
) &&
97 OCPP20ServiceUtils
.isRequestCommandSupported(chargingStation
, commandName
)
100 this.validatePayload(chargingStation
, commandName
, payload
)
101 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
102 await this.responseHandlers
.get(commandName
)!(chargingStation
, payload
, requestPayload
)
105 `${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`,
113 ErrorType
.NOT_IMPLEMENTED
,
114 `${commandName} is not implemented to handle response PDU ${JSON.stringify(
125 ErrorType
.SECURITY_ERROR
,
126 `${commandName} cannot be issued to handle response PDU ${JSON.stringify(
130 )} while the charging station is not registered on the central server.`,
137 private validatePayload (
138 chargingStation
: ChargingStation
,
139 commandName
: OCPP20RequestCommand
,
142 if (this.jsonSchemas
.has(commandName
)) {
143 return this.validateResponsePayload(
146 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
147 this.jsonSchemas
.get(commandName
)!,
152 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation`
157 private handleResponseBootNotification (
158 chargingStation
: ChargingStation
,
159 payload
: OCPP20BootNotificationResponse
161 if (payload
.status === RegistrationStatusEnumType
.ACCEPTED
) {
164 OCPP20OptionalVariableName
.HeartbeatInterval
,
165 payload
.interval
.toString(),
167 { overwrite
: true, save
: true }
169 OCPP20ServiceUtils
.startHeartbeatInterval(chargingStation
, payload
.interval
)
171 if (Object.values(RegistrationStatusEnumType
).includes(payload
.status)) {
172 const logMsg
= `${chargingStation.logPrefix()} Charging station in '${
174 }' state on the central server`
175 payload
.status === RegistrationStatusEnumType
.REJECTED
176 ? logger
.warn(logMsg
)
177 : logger
.info(logMsg
)
180 `${chargingStation.logPrefix()} Charging station boot notification response received: %j with undefined registration status`,