Handle Soft Stop with exception, not boolean
MODIFY
src/scheduler/tasks/CloseTransactionsInProgressTask.ts
src/scheduler/tasks/CloseTransactionsInProgressTask.ts
@@ -32,7 +32,7 @@ export default class CloseTransactionsInProgressTask extends TenantSchedulerTask
for (const transaction of transactions.result) {
try {
// Soft stop transaction
- if (await ocppService.softStopTransaction(tenant, transaction, transaction.chargeBox, transaction.siteArea)) {
+ await ocppService.softStopTransaction(tenant, transaction, transaction.chargeBox, transaction.siteArea);
result.inSuccess++;
await Logging.logInfo({
...LoggingHelper.getTransactionProperties(transaction),
@@ -43,9 +43,6 @@ export default class CloseTransactionsInProgressTask extends TenantSchedulerTask
action: ServerAction.TRANSACTION_SOFT_STOP,
detailedMessages: { transaction }
});
- } else {
- result.inError++;
- }
} catch (error) {
result.inError++;
await Logging.logError({

MODIFY
src/server/ocpp/services/OCPPService.ts
src/server/ocpp/services/OCPPService.ts
@@ -530,26 +530,55 @@ export default class OCPPService {
}
}
- public async softStopTransaction(tenant: Tenant, transaction: Transaction, chargingStation: ChargingStation, siteArea: SiteArea): Promise<boolean> {
- // Check
- if (!tenant || !transaction || !chargingStation) {
- return false;
+ public async softStopTransaction(tenant: Tenant, transaction: Transaction, chargingStation: ChargingStation, siteArea: SiteArea): Promise<void> {
+ if (!tenant) {
+ throw new BackendError({
+ ...LoggingHelper.getTransactionProperties(transaction),
+ module: MODULE_NAME, method: 'softStopTransaction',
+ message: `${Utils.buildConnectorInfo(transaction.connectorId, transaction.id)} The Tenant is mandatory`,
+ action: ServerAction.OCPP_STOP_TRANSACTION,
+ detailedMessages: { transaction, chargingStation, siteArea }
+ });
+ }
+ if (!chargingStation) {
+ throw new BackendError({
+ ...LoggingHelper.getTransactionProperties(transaction),
+ module: MODULE_NAME, method: 'softStopTransaction',
+ message: `${Utils.buildConnectorInfo(transaction.connectorId, transaction.id)} The Charging Station is mandatory`,
+ action: ServerAction.OCPP_STOP_TRANSACTION,
+ detailedMessages: { transaction, siteArea }
+ });
+ }
+ if (!transaction) {
+ throw new BackendError({
+ ...LoggingHelper.getChargingStationProperties(chargingStation),
+ module: MODULE_NAME, method: 'softStopTransaction',
+ message: `${Utils.buildConnectorInfo(transaction.connectorId, transaction.id)} The Transaction is mandatory`,
+ action: ServerAction.OCPP_STOP_TRANSACTION,
+ detailedMessages: { chargingStation, siteArea }
+ });
}
if (Utils.isTenantComponentActive(tenant, TenantComponents.ORGANIZATION) && !siteArea) {
- return false;
+ throw new BackendError({
+ ...LoggingHelper.getTransactionProperties(transaction),
+ module: MODULE_NAME, method: 'softStopTransaction',
+ message: `${Utils.buildConnectorInfo(transaction.connectorId, transaction.id)} The Site Area is mandatory`,
+ action: ServerAction.OCPP_STOP_TRANSACTION,
+ detailedMessages: { transaction, chargingStation }
+ });
}
// Set
chargingStation.siteArea = siteArea;
// Stop Transaction
const result = await this.handleStopTransaction(
{ // OCPP Header
- chargeBoxIdentity: transaction.chargeBoxID,
+ tenant: tenant,
+ tenantID: tenant.id,
chargingStation: chargingStation,
+ chargeBoxIdentity: chargingStation.id,
companyID: transaction.companyID,
siteID: transaction.siteID,
siteAreaID: transaction.siteAreaID,
- tenantID: tenant.id,
- tenant: tenant,
},
{ // OCPP Stop Transaction
transactionId: transaction.id,
@@ -578,7 +607,6 @@ export default class OCPPService {
// 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
@@ -836,17 +836,9 @@ export default class TransactionService {
});
}
// Stop Transaction
- const success = await new OCPPService(Configuration.getChargingStationConfig()).softStopTransaction(
+ try {
+ await new OCPPService(Configuration.getChargingStationConfig()).softStopTransaction(
req.tenant, transaction, chargingStation, chargingStation.siteArea);
- if (!success) {
- throw new AppError({
- ...LoggingHelper.getTransactionProperties(transaction),
- errorCode: HTTPError.GENERAL_ERROR,
- message: `${Utils.buildConnectorInfo(transaction.connectorId, transaction.id)} Transaction cannot be stopped`,
- module: MODULE_NAME, method: 'transactionSoftStop',
- user: req.user, action
- });
- }
await Logging.logInfo({
...LoggingHelper.getTransactionProperties(transaction),
tenantID: req.tenant.id,
@@ -855,6 +847,15 @@ export default class TransactionService {
message: `${Utils.buildConnectorInfo(transaction.connectorId, transaction.id)} Transaction has been soft stopped successfully`,
action, detailedMessages: { transaction }
});
+ } catch (error) {
+ throw new AppError({
+ ...LoggingHelper.getTransactionProperties(transaction),
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: `${Utils.buildConnectorInfo(transaction.connectorId, transaction.id)} Transaction cannot be soft stopped`,
+ module: MODULE_NAME, method: 'transactionSoftStop',
+ user: req.user, action
+ });
+ }
}
res.json(Constants.REST_CHARGING_STATION_COMMAND_RESPONSE_SUCCESS);
next();

Handle Soft Stop with exception, not boolean