1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
4 import path from
'path';
5 import { fileURLToPath
} from
'url';
7 import type { JSONSchemaType
} from
'ajv';
9 import OCPPError from
'../../../exception/OCPPError';
10 import type { JsonObject
, JsonType
} from
'../../../types/JsonType';
12 OCPP20IncomingRequestCommand
,
14 } from
'../../../types/ocpp/2.0/Requests';
16 OCPP20BootNotificationResponse
,
17 OCPP20ClearCacheResponse
,
18 OCPP20HeartbeatResponse
,
19 OCPP20StatusNotificationResponse
,
20 } from
'../../../types/ocpp/2.0/Responses';
21 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
22 import { OCPPVersion
} from
'../../../types/ocpp/OCPPVersion';
23 import { RegistrationStatusEnumType
, ResponseHandler
} from
'../../../types/ocpp/Responses';
24 import logger from
'../../../utils/Logger';
25 import type ChargingStation from
'../../ChargingStation';
26 import OCPPResponseService from
'../OCPPResponseService';
27 import { OCPP20ServiceUtils
} from
'./OCPP20ServiceUtils';
29 const moduleName
= 'OCPP20ResponseService';
31 export default class OCPP20ResponseService
extends OCPPResponseService
{
32 public jsonIncomingRequestResponseSchemas
: Map
<
33 OCPP20IncomingRequestCommand
,
34 JSONSchemaType
<JsonObject
>
37 private responseHandlers
: Map
<OCPP20RequestCommand
, ResponseHandler
>;
38 private jsonSchemas
: Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonObject
>>;
40 public constructor() {
41 if (new.target
?.name
=== moduleName
) {
42 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
44 super(OCPPVersion
.VERSION_20
);
45 this.responseHandlers
= new Map
<OCPP20RequestCommand
, ResponseHandler
>([
46 [OCPP20RequestCommand
.BOOT_NOTIFICATION
, this.handleResponseBootNotification
.bind(this)],
47 [OCPP20RequestCommand
.HEARTBEAT
, this.emptyResponseHandler
.bind(this)],
48 [OCPP20RequestCommand
.STATUS_NOTIFICATION
, this.emptyResponseHandler
.bind(this)],
50 this.jsonSchemas
= new Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonObject
>>([
52 OCPP20RequestCommand
.BOOT_NOTIFICATION
,
56 path
.dirname(fileURLToPath(import.meta
.url
)),
57 '../../../assets/json-schemas/ocpp/2.0/BootNotificationResponse.json'
61 ) as JSONSchemaType
<OCPP20BootNotificationResponse
>,
64 OCPP20RequestCommand
.HEARTBEAT
,
68 path
.dirname(fileURLToPath(import.meta
.url
)),
69 '../../../assets/json-schemas/ocpp/2.0/HeartbeatResponse.json'
73 ) as JSONSchemaType
<OCPP20HeartbeatResponse
>,
76 OCPP20RequestCommand
.STATUS_NOTIFICATION
,
80 path
.dirname(fileURLToPath(import.meta
.url
)),
81 '../../../assets/json-schemas/ocpp/2.0/StatusNotificationResponse.json'
85 ) as JSONSchemaType
<OCPP20StatusNotificationResponse
>,
88 this.jsonIncomingRequestResponseSchemas
= new Map([
90 OCPP20IncomingRequestCommand
.CLEAR_CACHE
,
94 path
.dirname(fileURLToPath(import.meta
.url
)),
95 '../../../assets/json-schemas/ocpp/2.0/ClearCacheResponse.json'
99 ) as JSONSchemaType
<OCPP20ClearCacheResponse
>,
102 this.validatePayload
.bind(this);
105 public async responseHandler(
106 chargingStation
: ChargingStation
,
107 commandName
: OCPP20RequestCommand
,
109 requestPayload
: JsonType
112 chargingStation
.isRegistered() === true ||
113 commandName
=== OCPP20RequestCommand
.BOOT_NOTIFICATION
116 this.responseHandlers
.has(commandName
) === true &&
117 OCPP20ServiceUtils
.isRequestCommandSupported(chargingStation
, commandName
) === true
120 this.validatePayload(chargingStation
, commandName
, payload
);
121 await this.responseHandlers
.get(commandName
)(chargingStation
, payload
, requestPayload
);
124 `${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`,
132 ErrorType
.NOT_IMPLEMENTED
,
133 `${commandName} is not implemented to handle response PDU ${JSON.stringify(
144 ErrorType
.SECURITY_ERROR
,
145 `${commandName} cannot be issued to handle response PDU ${JSON.stringify(
149 )} while the charging station is not registered on the central server.`,
156 private validatePayload(
157 chargingStation
: ChargingStation
,
158 commandName
: OCPP20RequestCommand
,
161 if (this.jsonSchemas
.has(commandName
) === true) {
162 return this.validateResponsePayload(
165 this.jsonSchemas
.get(commandName
),
170 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation`
175 private handleResponseBootNotification(
176 chargingStation
: ChargingStation
,
177 payload
: OCPP20BootNotificationResponse
179 if (payload
.status === RegistrationStatusEnumType
.ACCEPTED
) {
180 // ChargingStationConfigurationUtils.addConfigurationKey(
182 // OCPP16StandardParametersKey.HeartbeatInterval,
183 // payload.interval.toString(),
185 // { overwrite: true, save: true }
187 // ChargingStationConfigurationUtils.addConfigurationKey(
189 // OCPP16StandardParametersKey.HeartBeatInterval,
190 // payload.interval.toString(),
191 // { visible: false },
192 // { overwrite: true, save: true }
194 chargingStation
.heartbeatSetInterval
195 ? chargingStation
.restartHeartbeat()
196 : chargingStation
.startHeartbeat();
198 if (Object.values(RegistrationStatusEnumType
).includes(payload
.status)) {
199 const logMsg
= `${chargingStation.logPrefix()} Charging station in '${
201 }' state on the central server`;
202 payload
.status === RegistrationStatusEnumType
.REJECTED
203 ? logger
.warn(logMsg
)
204 : logger
.info(logMsg
);
207 chargingStation
.logPrefix() +
208 ' Charging station boot notification response received: %j with undefined registration status',