Send Remote Stop in case of unknow session
MODIFY
src/server/ocpp/services/OCPPService.ts
src/server/ocpp/services/OCPPService.ts
@@ -2183,11 +2183,33 @@ export default class OCPPService {
}
const transaction = await TransactionStorage.getTransaction(tenant, meterValues.transactionId, { withUser: true, withTag: true, withCar: true });
if (!transaction) {
- // Try a Remote Stop the Transaction
+ // Abort the ongoing Transaction
if (meterValues.transactionId) {
- // Get the OCPP Client
- const chargingStationClient = await ChargingStationClientFactory.getChargingStationClient(tenant, chargingStation);
- if (!chargingStationClient) {
+ await this.abortOngoingTransactionInMeterValues(tenant, chargingStation, headers, meterValues);
+ }
+ // Unkown Transaction
+ throw new BackendError({
+ source: chargingStation.id,
+ module: MODULE_NAME, method: 'getTransactionFromMeterValues',
+ message: `${Utils.buildConnectorInfo(meterValues.connectorId, meterValues.transactionId)} Transaction does not exist`,
+ action: ServerAction.OCPP_METER_VALUES,
+ detailedMessages: { headers, meterValues }
+ });
+ }
+ // Transaction finished
+ if (transaction?.stop) {
+ // Abort the ongoing Transaction
+ await this.abortOngoingTransactionInMeterValues(tenant, chargingStation, headers, meterValues);
+ throw new BackendError({
+ source: chargingStation.id,
+ module: MODULE_NAME, method: 'getTransactionFromMeterValues',
+ message: `${Utils.buildConnectorInfo(meterValues.connectorId, meterValues.transactionId)} Transaction has already been stopped`,
+ action: ServerAction.OCPP_METER_VALUES,
+ detailedMessages: { headers, transaction, meterValues }
+ });
+ }
+ // Received Meter Values after the Transaction End Meter Value
+ if (transaction.transactionEndReceived) {
await Logging.logWarning({
tenantID: tenant.id,
siteID: chargingStation.siteID,
@@ -2196,29 +2218,36 @@ export default class OCPPService {
chargingStationID: chargingStation.id,
source: chargingStation.id,
module: MODULE_NAME, method: 'getTransactionFromMeterValues',
- action: ServerAction.OCPP_STOP_TRANSACTION,
- message: `${Utils.buildConnectorInfo(meterValues.connectorId, meterValues.transactionId)} Charging Station is not connected to the backend, cannot send a Remote Stop Transaction on an unknown ongoing Transaction`,
+ action: ServerAction.OCPP_METER_VALUES,
+ message: `${Utils.buildConnectorInfo(meterValues.connectorId, meterValues.transactionId)} Meter Values received after the 'Transaction.End'`,
detailedMessages: { headers, meterValues }
});
- } else {
- // Send Remote Stop
- const result = await chargingStationClient.remoteStopTransaction({
- transactionId: meterValues.transactionId
- });
- if (result.status === OCPPRemoteStartStopStatus.ACCEPTED) {
- await Logging.logInfo({
+ }
+ return transaction;
+ }
+
+ private async abortOngoingTransactionInMeterValues(tenant: Tenant, chargingStation: ChargingStation, headers: OCPPHeader, meterValues: OCPPMeterValuesRequest) {
+ // Get the OCPP Client
+ const chargingStationClient = await ChargingStationClientFactory.getChargingStationClient(tenant, chargingStation);
+ if (!chargingStationClient) {
+ await Logging.logError({
tenantID: tenant.id,
siteID: chargingStation.siteID,
siteAreaID: chargingStation.siteAreaID,
companyID: chargingStation.companyID,
chargingStationID: chargingStation.id,
source: chargingStation.id,
- module: MODULE_NAME, method: 'getTransactionFromMeterValues',
- action: ServerAction.OCPP_STOP_TRANSACTION,
- message: `${Utils.buildConnectorInfo(meterValues.connectorId, meterValues.transactionId)} Transaction with unknown ID has been automatically remotely stopped`,
+ module: MODULE_NAME, method: 'abortOngoingTransactionInMeterValues',
+ action: ServerAction.OCPP_METER_VALUES,
+ message: `${Utils.buildConnectorInfo(meterValues.connectorId, meterValues.transactionId)} Charging Station is not connected to the backend, cannot send a Remote Stop Transaction on an ongoing Transaction`,
detailedMessages: { headers, meterValues }
});
} else {
+ // Send Remote Stop
+ const result = await chargingStationClient.remoteStopTransaction({
+ transactionId: meterValues.transactionId
+ });
+ if (result.status === OCPPRemoteStartStopStatus.ACCEPTED) {
await Logging.logWarning({
tenantID: tenant.id,
siteID: chargingStation.siteID,
@@ -2226,48 +2255,26 @@ export default class OCPPService {
companyID: chargingStation.companyID,
chargingStationID: chargingStation.id,
source: chargingStation.id,
- module: MODULE_NAME, method: 'getTransactionFromMeterValues',
- action: ServerAction.OCPP_STOP_TRANSACTION,
- message: `${Utils.buildConnectorInfo(meterValues.connectorId, meterValues.transactionId)} Cannot send a Remote Stop Transaction on an unknown ongoing Transaction`,
- detailedMessages: { headers, meterValues }
- });
- }
- }
- }
- // Unkown Transaction
- throw new BackendError({
- source: chargingStation.id,
- module: MODULE_NAME, method: 'getTransactionFromMeterValues',
- message: `${Utils.buildConnectorInfo(meterValues.connectorId, meterValues.transactionId)} Transaction does not exist`,
+ module: MODULE_NAME, method: 'abortOngoingTransactionInMeterValues',
action: ServerAction.OCPP_METER_VALUES,
+ message: `${Utils.buildConnectorInfo(meterValues.connectorId, meterValues.transactionId)} Transaction has been automatically remotely stopped`,
detailedMessages: { headers, meterValues }
});
- // Transaction finished!
- } else if (transaction.stop) {
- throw new BackendError({
- source: chargingStation.id,
- module: MODULE_NAME, method: 'getTransactionFromMeterValues',
- message: `${Utils.buildConnectorInfo(meterValues.connectorId, meterValues.transactionId)} Transaction is already stopped`,
- action: ServerAction.OCPP_METER_VALUES,
- detailedMessages: { headers, transaction, meterValues }
- });
- }
- // Received Meter Values after the Transaction End Meter Value
- if (transaction.transactionEndReceived) {
- await Logging.logWarning({
+ } else {
+ await Logging.logError({
tenantID: tenant.id,
siteID: chargingStation.siteID,
siteAreaID: chargingStation.siteAreaID,
companyID: chargingStation.companyID,
chargingStationID: chargingStation.id,
source: chargingStation.id,
- module: MODULE_NAME, method: 'getTransactionFromMeterValues',
+ module: MODULE_NAME, method: 'abortOngoingTransactionInMeterValues',
action: ServerAction.OCPP_METER_VALUES,
- message: `${Utils.buildConnectorInfo(meterValues.connectorId, meterValues.transactionId)} Meter Values received after the 'Transaction.End'`,
+ message: `${Utils.buildConnectorInfo(meterValues.connectorId, meterValues.transactionId)} Cannot send a Remote Stop Transaction on an unknown ongoing Transaction`,
detailedMessages: { headers, meterValues }
});
}
- return transaction;
+ }
}
private async getTransactionFromStopTransaction(tenant: Tenant, chargingStation: ChargingStation,

Send Remote Stop in case of unknow session