1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
3 import { AuthorizeRequest
, OCPP16AuthorizationStatus
, OCPP16AuthorizeResponse
, OCPP16StartTransactionResponse
, OCPP16StopTransactionResponse
, StartTransactionRequest
, StopTransactionRequest
} from
'../../../types/ocpp/1.6/Transaction';
4 import { HeartbeatRequest
, OCPP16RequestCommand
, StatusNotificationRequest
} from
'../../../types/ocpp/1.6/Requests';
5 import { HeartbeatResponse
, OCPP16BootNotificationResponse
, OCPP16RegistrationStatus
, StatusNotificationResponse
} from
'../../../types/ocpp/1.6/Responses';
6 import { MeterValuesRequest
, MeterValuesResponse
} from
'../../../types/ocpp/1.6/MeterValues';
8 import ChargingStation from
'../../ChargingStation';
9 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
10 import { OCPP16ChargePointStatus
} from
'../../../types/ocpp/1.6/ChargePointStatus';
11 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
12 import { OCPP16StandardParametersKey
} from
'../../../types/ocpp/1.6/Configuration';
13 import OCPPError from
'../OCPPError';
14 import OCPPResponseService from
'../OCPPResponseService';
15 import { ResponseHandler
} from
'../../../types/ocpp/Responses';
16 import Utils from
'../../../utils/Utils';
17 import logger from
'../../../utils/Logger';
19 export default class OCPP16ResponseService
extends OCPPResponseService
{
20 private responseHandlers
: Map
<OCPP16RequestCommand
, ResponseHandler
>;
22 constructor(chargingStation
: ChargingStation
) {
23 super(chargingStation
);
24 this.responseHandlers
= new Map
<OCPP16RequestCommand
, ResponseHandler
>([
25 [OCPP16RequestCommand
.BOOT_NOTIFICATION
, this.handleResponseBootNotification
.bind(this)],
26 [OCPP16RequestCommand
.HEARTBEAT
, this.handleResponseHeartbeat
.bind(this)],
27 [OCPP16RequestCommand
.AUTHORIZE
, this.handleResponseAuthorize
.bind(this)],
28 [OCPP16RequestCommand
.START_TRANSACTION
, this.handleResponseStartTransaction
.bind(this)],
29 [OCPP16RequestCommand
.STOP_TRANSACTION
, this.handleResponseStopTransaction
.bind(this)],
30 [OCPP16RequestCommand
.STATUS_NOTIFICATION
, this.handleResponseStatusNotification
.bind(this)],
31 [OCPP16RequestCommand
.METER_VALUES
, this.handleResponseMeterValues
.bind(this)]
35 public async handleResponse(commandName
: OCPP16RequestCommand
, payload
: Record
<string, unknown
> | string, requestPayload
: Record
<string, unknown
>): Promise
<void> {
36 if (this.responseHandlers
.has(commandName
)) {
38 await this.responseHandlers
.get(commandName
)(payload
, requestPayload
);
40 logger
.error(this.chargingStation
.logPrefix() + ' Handle request response error: %j', error
);
45 throw new OCPPError(ErrorType
.NOT_IMPLEMENTED
, `${commandName} is not implemented to handle request response payload ${JSON.stringify(payload, null, 2)}`, commandName
);
49 private handleResponseBootNotification(payload
: OCPP16BootNotificationResponse
): void {
50 if (payload
.status === OCPP16RegistrationStatus
.ACCEPTED
) {
51 this.chargingStation
.addConfigurationKey(OCPP16StandardParametersKey
.HeartBeatInterval
, payload
.interval
.toString());
52 this.chargingStation
.addConfigurationKey(OCPP16StandardParametersKey
.HeartbeatInterval
, payload
.interval
.toString(), { visible
: false });
53 this.chargingStation
.heartbeatSetInterval
? this.chargingStation
.restartHeartbeat() : this.chargingStation
.startHeartbeat();
54 } else if (payload
.status === OCPP16RegistrationStatus
.PENDING
) {
55 logger
.info(this.chargingStation
.logPrefix() + ' Charging station in pending state on the central server');
57 logger
.warn(this.chargingStation
.logPrefix() + ' Charging station rejected by the central server');
61 private handleResponseHeartbeat(payload
: HeartbeatResponse
, requestPayload
: HeartbeatRequest
): void {
62 logger
.debug(this.chargingStation
.logPrefix() + ' Heartbeat response received: %j to Heartbeat request: %j', payload
, requestPayload
);
65 private handleResponseAuthorize(payload
: OCPP16AuthorizeResponse
, requestPayload
: AuthorizeRequest
): void {
66 let authorizeConnectorId
: number;
67 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
68 if (connectorId
> 0 && this.chargingStation
.getConnectorStatus(connectorId
)?.authorizeIdTag
=== requestPayload
.idTag
) {
69 authorizeConnectorId
= connectorId
;
73 if (payload
.idTagInfo
.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
74 this.chargingStation
.getConnectorStatus(authorizeConnectorId
).idTagAuthorized
= true;
75 logger
.debug(`${this.chargingStation.logPrefix()} IdTag ${requestPayload.idTag} authorized on connector ${authorizeConnectorId}`);
77 this.chargingStation
.getConnectorStatus(authorizeConnectorId
).idTagAuthorized
= false;
78 delete this.chargingStation
.getConnectorStatus(authorizeConnectorId
).authorizeIdTag
;
79 logger
.debug(`${this.chargingStation.logPrefix()} IdTag ${requestPayload.idTag} refused with status ${payload.idTagInfo.status} on connector ${authorizeConnectorId}`);
83 private async handleResponseStartTransaction(payload
: OCPP16StartTransactionResponse
, requestPayload
: StartTransactionRequest
): Promise
<void> {
84 const connectorId
= requestPayload
.connectorId
;
86 let transactionConnectorId
: number;
87 for (const id
of this.chargingStation
.connectors
.keys()) {
88 if (id
> 0 && id
=== connectorId
) {
89 transactionConnectorId
= id
;
93 if (!transactionConnectorId
) {
94 logger
.error(this.chargingStation
.logPrefix() + ' Trying to start a transaction on a non existing connector Id ' + connectorId
.toString());
97 if (this.chargingStation
.getConnectorStatus(connectorId
).transactionRemoteStarted
&& this.chargingStation
.getAuthorizeRemoteTxRequests()
98 && this.chargingStation
.getLocalAuthListEnabled() && this.chargingStation
.hasAuthorizedTags() && !this.chargingStation
.getConnectorStatus(connectorId
).idTagLocalAuthorized
) {
99 logger
.error(this.chargingStation
.logPrefix() + ' Trying to start a transaction with a not local authorized idTag ' + this.chargingStation
.getConnectorStatus(connectorId
).localAuthorizeIdTag
+ ' on connector Id ' + connectorId
.toString());
100 await this.resetConnectorOnStartTransactionError(connectorId
);
103 if (this.chargingStation
.getConnectorStatus(connectorId
).transactionRemoteStarted
&& this.chargingStation
.getAuthorizeRemoteTxRequests()
104 && this.chargingStation
.getMayAuthorizeAtRemoteStart() && !this.chargingStation
.getConnectorStatus(connectorId
).idTagLocalAuthorized
105 && !this.chargingStation
.getConnectorStatus(connectorId
).idTagAuthorized
) {
106 logger
.error(this.chargingStation
.logPrefix() + ' Trying to start a transaction with a not authorized idTag ' + this.chargingStation
.getConnectorStatus(connectorId
).authorizeIdTag
+ ' on connector Id ' + connectorId
.toString());
107 await this.resetConnectorOnStartTransactionError(connectorId
);
110 if (this.chargingStation
.getConnectorStatus(connectorId
).idTagAuthorized
&& this.chargingStation
.getConnectorStatus(connectorId
).authorizeIdTag
!== requestPayload
.idTag
) {
111 logger
.error(this.chargingStation
.logPrefix() + ' Trying to start a transaction with an idTag ' + requestPayload
.idTag
+ ' different from the authorize request one ' + this.chargingStation
.getConnectorStatus(connectorId
).authorizeIdTag
+ ' on connector Id ' + connectorId
.toString());
112 await this.resetConnectorOnStartTransactionError(connectorId
);
115 if (this.chargingStation
.getConnectorStatus(connectorId
).idTagLocalAuthorized
116 && this.chargingStation
.getConnectorStatus(connectorId
).localAuthorizeIdTag
!== requestPayload
.idTag
) {
117 logger
.error(this.chargingStation
.logPrefix() + ' Trying to start a transaction with an idTag ' + requestPayload
.idTag
+ ' different from the local authorized one ' + this.chargingStation
.getConnectorStatus(connectorId
).localAuthorizeIdTag
+ ' on connector Id ' + connectorId
.toString());
118 await this.resetConnectorOnStartTransactionError(connectorId
);
121 if (this.chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
) {
122 logger
.debug(this.chargingStation
.logPrefix() + ' Trying to start a transaction on an already used connector ' + connectorId
.toString() + ': %j', this.chargingStation
.getConnectorStatus(connectorId
));
125 if (this.chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.AVAILABLE
126 && this.chargingStation
.getConnectorStatus(connectorId
)?.status !== OCPP16ChargePointStatus
.PREPARING
) {
127 logger
.error(`${this.chargingStation.logPrefix()} Trying to start a transaction on connector ${connectorId.toString()} with status ${this.chargingStation.getConnectorStatus(connectorId)?.status}`);
130 if (!Number.isInteger(payload
.transactionId
)) {
131 logger
.warn(`${this.chargingStation.logPrefix()} Trying to start a transaction on connector ${connectorId.toString()} with a non integer transaction Id ${payload.transactionId}, converting to integer`);
132 payload
.transactionId
= Utils
.convertToInt(payload
.transactionId
);
135 if (payload
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
136 this.chargingStation
.getConnectorStatus(connectorId
).transactionStarted
= true;
137 this.chargingStation
.getConnectorStatus(connectorId
).transactionId
= payload
.transactionId
;
138 this.chargingStation
.getConnectorStatus(connectorId
).transactionIdTag
= requestPayload
.idTag
;
139 this.chargingStation
.getConnectorStatus(connectorId
).transactionEnergyActiveImportRegisterValue
= 0;
140 this.chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
= OCPP16ServiceUtils
.buildTransactionBeginMeterValue(this.chargingStation
, connectorId
,
141 requestPayload
.meterStart
);
142 this.chargingStation
.getBeginEndMeterValues() && await this.chargingStation
.ocppRequestService
.sendTransactionBeginMeterValues(connectorId
, payload
.transactionId
,
143 this.chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
);
144 await this.chargingStation
.ocppRequestService
.sendStatusNotification(connectorId
, OCPP16ChargePointStatus
.CHARGING
);
145 this.chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.CHARGING
;
146 logger
.info(this.chargingStation
.logPrefix() + ' Transaction ' + payload
.transactionId
.toString() + ' STARTED on ' + this.chargingStation
.stationInfo
.chargingStationId
+ '#' + connectorId
.toString() + ' for idTag ' + requestPayload
.idTag
);
147 if (this.chargingStation
.stationInfo
.powerSharedByConnectors
) {
148 this.chargingStation
.stationInfo
.powerDivider
++;
150 const configuredMeterValueSampleInterval
= this.chargingStation
.getConfigurationKey(OCPP16StandardParametersKey
.MeterValueSampleInterval
);
151 this.chargingStation
.startMeterValues(connectorId
, configuredMeterValueSampleInterval
? Utils
.convertToInt(configuredMeterValueSampleInterval
.value
) * 1000 : 60000);
153 logger
.warn(this.chargingStation
.logPrefix() + ' Starting transaction id ' + payload
.transactionId
.toString() + ' REJECTED with status ' + payload
?.idTagInfo
?.status + ', idTag ' + requestPayload
.idTag
);
154 await this.resetConnectorOnStartTransactionError(connectorId
);
158 private async resetConnectorOnStartTransactionError(connectorId
: number): Promise
<void> {
159 this.chargingStation
.resetConnectorStatus(connectorId
);
160 if (this.chargingStation
.getConnectorStatus(connectorId
).status !== OCPP16ChargePointStatus
.AVAILABLE
) {
161 await this.chargingStation
.ocppRequestService
.sendStatusNotification(connectorId
, OCPP16ChargePointStatus
.AVAILABLE
);
162 this.chargingStation
.getConnectorStatus(connectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
166 private async handleResponseStopTransaction(payload
: OCPP16StopTransactionResponse
, requestPayload
: StopTransactionRequest
): Promise
<void> {
167 let transactionConnectorId
: number;
168 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
169 if (connectorId
> 0 && this.chargingStation
.getConnectorStatus(connectorId
)?.transactionId
=== requestPayload
.transactionId
) {
170 transactionConnectorId
= connectorId
;
174 if (!transactionConnectorId
) {
175 logger
.error(this.chargingStation
.logPrefix() + ' Trying to stop a non existing transaction ' + requestPayload
.transactionId
.toString());
178 if (payload
.idTagInfo
?.status === OCPP16AuthorizationStatus
.ACCEPTED
) {
179 (this.chargingStation
.getBeginEndMeterValues() && this.chargingStation
.getOutOfOrderEndMeterValues())
180 && await this.chargingStation
.ocppRequestService
.sendTransactionEndMeterValues(transactionConnectorId
, requestPayload
.transactionId
,
181 OCPP16ServiceUtils
.buildTransactionEndMeterValue(this.chargingStation
, transactionConnectorId
, requestPayload
.meterStop
));
182 if (!this.chargingStation
.isChargingStationAvailable() || !this.chargingStation
.isConnectorAvailable(transactionConnectorId
)) {
183 await this.chargingStation
.ocppRequestService
.sendStatusNotification(transactionConnectorId
, OCPP16ChargePointStatus
.UNAVAILABLE
);
184 this.chargingStation
.getConnectorStatus(transactionConnectorId
).status = OCPP16ChargePointStatus
.UNAVAILABLE
;
186 await this.chargingStation
.ocppRequestService
.sendStatusNotification(transactionConnectorId
, OCPP16ChargePointStatus
.AVAILABLE
);
187 this.chargingStation
.getConnectorStatus(transactionConnectorId
).status = OCPP16ChargePointStatus
.AVAILABLE
;
189 if (this.chargingStation
.stationInfo
.powerSharedByConnectors
) {
190 this.chargingStation
.stationInfo
.powerDivider
--;
192 logger
.info(this.chargingStation
.logPrefix() + ' Transaction ' + requestPayload
.transactionId
.toString() + ' STOPPED on ' + this.chargingStation
.stationInfo
.chargingStationId
+ '#' + transactionConnectorId
.toString());
193 this.chargingStation
.resetConnectorStatus(transactionConnectorId
);
195 logger
.warn(this.chargingStation
.logPrefix() + ' Stopping transaction id ' + requestPayload
.transactionId
.toString() + ' REJECTED with status ' + payload
.idTagInfo
?.status);
199 private handleResponseStatusNotification(payload
: StatusNotificationRequest
, requestPayload
: StatusNotificationResponse
): void {
200 logger
.debug(this.chargingStation
.logPrefix() + ' Status notification response received: %j to StatusNotification request: %j', payload
, requestPayload
);
203 private handleResponseMeterValues(payload
: MeterValuesRequest
, requestPayload
: MeterValuesResponse
): void {
204 logger
.debug(this.chargingStation
.logPrefix() + ' MeterValues response received: %j to MeterValues request: %j', payload
, requestPayload
);