Call the same soft stop transaction in endpoint service
MODIFY
src/scheduler/tasks/CloseTransactionsInProgressTask.ts
src/scheduler/tasks/CloseTransactionsInProgressTask.ts
@@ -5,12 +5,10 @@ import LockingHelper from '../../locking/LockingHelper';
import LockingManager from '../../locking/LockingManager';
import Logging from '../../utils/Logging';
import OCPPService from '../../server/ocpp/services/OCPPService';
-import OCPPUtils from '../../server/ocpp/utils/OCPPUtils';
import { ServerAction } from '../../types/Server';
import Tenant from '../../types/Tenant';
import TenantSchedulerTask from '../TenantSchedulerTask';
import TransactionStorage from '../../storage/mongodb/TransactionStorage';
-import User from '../../types/User';
import Utils from '../../utils/Utils';
const MODULE_NAME = 'CleanTransactionsInProgressTask';
@@ -24,7 +22,6 @@ export default class CloseTransactionsInProgressTask extends TenantSchedulerTask
inError: 0,
inSuccess: 0,
};
- let authUser: { user: User, alternateUser: User };
const startTime = new Date().getTime();
// Instantiate the OCPPService
const ocppService = new OCPPService(Configuration.getChargingStationConfig());
@@ -36,24 +33,9 @@ export default class CloseTransactionsInProgressTask extends TenantSchedulerTask
// Soft stop transaction
if (await ocppService.softStopTransaction(tenant, transaction, transaction.chargeBox, transaction.siteArea)) {
result.inSuccess++;
- continue;
- }
- if (transaction.chargeBox) {
- // Transaction is stopped by central system?
- authUser = await ocppService.checkAuthorizeStopTransactionAndGetUsers(
- tenant, transaction.chargeBox, transaction, transaction.tagID, true);
+ } else {
+ result.inError++;
}
- // Update Transaction with Stop Transaction and Stop MeterValues
- OCPPUtils.updateTransactionWithStopTransaction(transaction, transaction.chargeBox, {
- transactionId: transaction.id,
- chargeBoxID: transaction.chargeBoxID,
- idTag: transaction.tagID,
- timestamp: Utils.convertToDate(transaction.lastConsumption ? transaction.lastConsumption.timestamp : transaction.timestamp).toISOString(),
- meterStop: transaction.lastConsumption ? transaction.lastConsumption.value : transaction.meterStart
- }, authUser?.user, authUser?.alternateUser, transaction.tagID, true);
- // Save the transaction
- await TransactionStorage.saveTransaction(tenant, transaction);
- result.inSuccess++;
} catch (error) {
result.inError++;
await Logging.logError({

MODIFY
src/server/ocpp/services/OCPPService.ts
src/server/ocpp/services/OCPPService.ts
@@ -556,7 +556,25 @@ export default class OCPPService {
},
true
);
- return (result.idTagInfo?.status === OCPPAuthorizationStatus.ACCEPTED);
+ if (result.idTagInfo?.status !== OCPPAuthorizationStatus.ACCEPTED) {
+ let authUser: { user: User, alternateUser: User };
+ if (transaction.chargeBox) {
+ // Transaction is stopped by central system?
+ authUser = await this.checkAuthorizeStopTransactionAndGetUsers(
+ tenant, transaction.chargeBox, transaction, transaction.tagID, true);
+ }
+ // Update Transaction with Stop Transaction and Stop MeterValues
+ OCPPUtils.updateTransactionWithStopTransaction(transaction, transaction.chargeBox, {
+ transactionId: transaction.id,
+ chargeBoxID: transaction.chargeBoxID,
+ idTag: transaction.tagID,
+ timestamp: Utils.convertToDate(transaction.lastConsumption ? transaction.lastConsumption.timestamp : transaction.timestamp).toISOString(),
+ meterStop: transaction.lastConsumption ? transaction.lastConsumption.value : transaction.meterStart
+ }, authUser?.user, authUser?.alternateUser, transaction.tagID, true);
+ // Save the transaction
+ await TransactionStorage.saveTransaction(tenant, transaction);
+ }
+ return true;
}
public async checkAuthorizeStopTransactionAndGetUsers(tenant: Tenant, chargingStation: ChargingStation, transaction: Transaction,

MODIFY
src/server/rest/v1/service/TransactionService.ts
src/server/rest/v1/service/TransactionService.ts
@@ -349,25 +349,34 @@ export default class TransactionService {
const chargingStation = await ChargingStationStorage.getChargingStation(req.tenant, transaction.chargeBoxID, { withSiteArea: true });
UtilsService.assertObjectExists(action, chargingStation, `Charging Station ID '${transaction.chargeBoxID}' does not exist`,
MODULE_NAME, 'handleTransactionSoftStop', req.user);
+ // Check connector
+ const connector = Utils.getConnectorFromID(chargingStation, transaction.connectorId);
+ if (!connector) {
+ throw new AppError({
+ ...LoggingHelper.getChargingStationProperties(chargingStation),
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: `${Utils.buildConnectorInfo(transaction.connectorId, transaction.id)} The Connector ID has not been found`,
+ module: MODULE_NAME, method: 'handleTransactionSoftStop',
+ user: req.user, action: action
+ });
+ }
// Check if already stopped
if (transaction.stop) {
// Clear Connector
+ if (connector.currentTransactionID === transaction.id) {
OCPPUtils.clearChargingStationConnectorRuntimeData(chargingStation, transaction.connectorId);
- // Save Connectors
await ChargingStationStorage.saveChargingStationConnectors(req.tenant, chargingStation.id, chargingStation.connectors);
+ }
await Logging.logInfo({
tenantID: req.user.tenantID,
user: req.user, actionOnUser: transaction.userID,
module: MODULE_NAME, method: 'handleTransactionSoftStop',
- message: `${Utils.buildConnectorInfo(transaction.connectorId, transaction.id)} Transaction has already been stopped and connector has been cleaned`,
+ message: `${Utils.buildConnectorInfo(transaction.connectorId, transaction.id)} Transaction has already been stopped`,
action: action,
});
} else {
- // Charging Station must be active
- if (!chargingStation.inactive) {
- // Check connector
- const connector = Utils.getConnectorFromID(chargingStation, transaction.connectorId);
- if (connector.currentTransactionID === transaction.id) {
+ // Transaction is still ongoing
+ if (!chargingStation.inactive && connector.currentTransactionID === transaction.id) {
throw new AppError({
...LoggingHelper.getChargingStationProperties(chargingStation),
errorCode: HTTPError.GENERAL_ERROR,
@@ -376,28 +385,10 @@ export default class TransactionService {
user: req.user, action: action
});
}
- }
// Stop Transaction
- const result = await new OCPPService(Configuration.getChargingStationConfig()).handleStopTransaction(
- { // OCPP Headers
- chargeBoxIdentity: chargingStation.id,
- chargingStation: chargingStation,
- companyID: chargingStation.companyID,
- siteID: chargingStation.siteID,
- siteAreaID: chargingStation.siteAreaID,
- tenantID: req.user.tenantID,
- tenant: req.tenant,
- },
- { // OCPP Stop Transaction
- transactionId: transactionId,
- chargeBoxID: chargingStation.id,
- idTag: req.user.tagIDs[0],
- timestamp: Utils.convertToDate(transaction.lastConsumption ? transaction.lastConsumption.timestamp : transaction.timestamp).toISOString(),
- meterStop: transaction.lastConsumption ? transaction.lastConsumption.value : transaction.meterStart
- },
- true
- );
- if (result.idTagInfo?.status !== OCPPAuthorizationStatus.ACCEPTED) {
+ const success = await new OCPPService(Configuration.getChargingStationConfig()).softStopTransaction(
+ req.tenant, transaction, chargingStation, chargingStation.siteArea);
+ if (!success) {
throw new AppError({
...LoggingHelper.getChargingStationProperties(chargingStation),
errorCode: HTTPError.GENERAL_ERROR,
@@ -413,7 +404,7 @@ export default class TransactionService {
...LoggingHelper.getChargingStationProperties(chargingStation),
message: `${Utils.buildConnectorInfo(transaction.connectorId, transaction.id)} Transaction has been stopped successfully`,
action: action,
- detailedMessages: { result }
+ detailedMessages: { transaction }
});
}
res.json(Constants.REST_RESPONSE_SUCCESS);

MODIFY
src/storage/mongodb/TransactionStorage.ts
src/storage/mongodb/TransactionStorage.ts
@@ -755,6 +755,7 @@ export default class TransactionStorage {
DatabaseUtils.pushRenameDatabaseIDToNumber(aggregation);
// Convert Object ID to string
DatabaseUtils.pushConvertObjectIDToString(aggregation, 'userID');
+ DatabaseUtils.pushConvertObjectIDToString(aggregation, 'companyID');
DatabaseUtils.pushConvertObjectIDToString(aggregation, 'siteID');
DatabaseUtils.pushConvertObjectIDToString(aggregation, 'siteAreaID');
DatabaseUtils.pushConvertObjectIDToString(aggregation, 'stop.userID');

Call the same soft stop transaction in endpoint service