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';
15 import type { OCPP20BootNotificationResponse
} from
'../../../types/ocpp/2.0/Responses';
16 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
17 import { OCPPVersion
} from
'../../../types/ocpp/OCPPVersion';
18 import { RegistrationStatusEnumType
, ResponseHandler
} from
'../../../types/ocpp/Responses';
19 import logger from
'../../../utils/Logger';
20 import type ChargingStation from
'../../ChargingStation';
21 import OCPPResponseService from
'../OCPPResponseService';
22 import { OCPP20ServiceUtils
} from
'./OCPP20ServiceUtils';
24 const moduleName
= 'OCPP20ResponseService';
26 export default class OCPP20ResponseService
extends OCPPResponseService
{
27 public jsonIncomingRequestResponseSchemas
: Map
<
28 OCPP20IncomingRequestCommand
,
29 JSONSchemaType
<JsonObject
>
32 private responseHandlers
: Map
<OCPP20RequestCommand
, ResponseHandler
>;
33 private jsonSchemas
: Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonObject
>>;
35 public constructor() {
36 if (new.target
?.name
=== moduleName
) {
37 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
39 super(OCPPVersion
.VERSION_20
);
40 this.responseHandlers
= new Map
<OCPP20RequestCommand
, ResponseHandler
>([
41 [OCPP20RequestCommand
.BOOT_NOTIFICATION
, this.handleResponseBootNotification
.bind(this)],
43 this.jsonSchemas
= new Map
<OCPP20RequestCommand
, JSONSchemaType
<JsonObject
>>([
45 OCPP20RequestCommand
.BOOT_NOTIFICATION
,
49 path
.dirname(fileURLToPath(import.meta
.url
)),
50 '../../../assets/json-schemas/ocpp/2.0/BootNotificationResponse.json'
54 ) as JSONSchemaType
<OCPP20BootNotificationResponse
>,
57 this.jsonIncomingRequestResponseSchemas
= new Map();
58 this.validatePayload
.bind(this);
61 public async responseHandler(
62 chargingStation
: ChargingStation
,
63 commandName
: OCPP20RequestCommand
,
65 requestPayload
: JsonType
68 chargingStation
.isRegistered() === true ||
69 commandName
=== OCPP20RequestCommand
.BOOT_NOTIFICATION
72 this.responseHandlers
.has(commandName
) === true &&
73 OCPP20ServiceUtils
.isRequestCommandSupported(chargingStation
, commandName
) === true
76 this.validatePayload(chargingStation
, commandName
, payload
);
77 await this.responseHandlers
.get(commandName
)(chargingStation
, payload
, requestPayload
);
80 `${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`,
88 ErrorType
.NOT_IMPLEMENTED
,
89 `${commandName} is not implemented to handle response PDU ${JSON.stringify(
100 ErrorType
.SECURITY_ERROR
,
101 `${commandName} cannot be issued to handle response PDU ${JSON.stringify(
105 )} while the charging station is not registered on the central server.`,
112 private validatePayload(
113 chargingStation
: ChargingStation
,
114 commandName
: OCPP20RequestCommand
,
117 if (this.jsonSchemas
.has(commandName
) === true) {
118 return this.validateResponsePayload(
121 this.jsonSchemas
.get(commandName
),
126 `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation`
131 private handleResponseBootNotification(
132 chargingStation
: ChargingStation
,
133 payload
: OCPP20BootNotificationResponse
135 if (payload
.status === RegistrationStatusEnumType
.ACCEPTED
) {
136 // ChargingStationConfigurationUtils.addConfigurationKey(
138 // OCPP16StandardParametersKey.HeartbeatInterval,
139 // payload.interval.toString(),
141 // { overwrite: true, save: true }
143 // ChargingStationConfigurationUtils.addConfigurationKey(
145 // OCPP16StandardParametersKey.HeartBeatInterval,
146 // payload.interval.toString(),
147 // { visible: false },
148 // { overwrite: true, save: true }
150 chargingStation
.heartbeatSetInterval
151 ? chargingStation
.restartHeartbeat()
152 : chargingStation
.startHeartbeat();
154 if (Object.values(RegistrationStatusEnumType
).includes(payload
.status)) {
155 const logMsg
= `${chargingStation.logPrefix()} Charging station in '${
157 }' state on the central server`;
158 payload
.status === RegistrationStatusEnumType
.REJECTED
159 ? logger
.warn(logMsg
)
160 : logger
.info(logMsg
);
163 chargingStation
.logPrefix() +
164 ' Charging station boot notification response received: %j with undefined registration status',