Skip to content

Commit

Permalink
Set minion lock status when setting new lock #108
Browse files Browse the repository at this point in the history
  • Loading branch information
haimkastner committed Mar 4, 2020
1 parent 2ee78f1 commit bd7652e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions backend/api-errors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
- { 9501 : 'Get current version fail'}
- { 10501 : 'Get supported devices from commands repo fail'}
- { 11501 : 'Fetch rf commands commands repo fail'}
- { 12501 : 'Setting calibration failed'}


- { 1503 : 'Connection to device fail.'}
Expand Down
57 changes: 46 additions & 11 deletions backend/src/business-layer/minionsBl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,19 +295,54 @@ export class MinionsBl {
minion.calibration = minionCalibrate;

/**
* Save timeout update in Dal for next app running.
* .
*/
this.minionsDal.updateMinionCalibrate(minionId, minionCalibrate).catch((error: ErrorResponse) => {
logger.warn(`Fail to update minion ${minionId} auto turn off ${error.message}`);
});
try {
/** Save calibration update in Dal for next calibration activation */
this.minionsDal.updateMinionCalibrate(minionId, minionCalibrate);

/**
* Send minion feed update
*/
this.minionFeed.next({
event: 'update',
minion,
});
/**
* Send minion feed update
*/
this.minionFeed.next({
event: 'update',
minion,
});

// Change minion status only if the current is violated the new lock
if (minionCalibrate.calibrationMode === 'AUTO' || !minionCalibrate.calibrationCycleMinutes) {
return;
}

const currentStatus = minion.minionStatus[minion.minionType].status;

const statusToSet = DeepCopy<MinionStatus>(minion.minionStatus);
let needToSetStatus = false;

if (currentStatus === 'on' && minionCalibrate.calibrationMode === 'LOCK_OFF') {
statusToSet[minion.minionType].status = 'off';
needToSetStatus = true;
}

if (currentStatus === 'off' && minionCalibrate.calibrationMode === 'LOCK_ON') {
statusToSet[minion.minionType].status = 'on';
needToSetStatus = true;
}

if (needToSetStatus) {
try {
await this.setMinionStatus(minionId, statusToSet);
} catch (error) {
logger.warn(`[MinionsBL] Failed to change minion "${minionId}" status, according to the new lock`);
}
}
} catch (error) {
logger.warn(`Fail to update minion ${minionId} auto turn off ${error.message}`);
throw {
responseCode: 12501,
message: 'Setting calibration failed',
} as ErrorResponse;
}
}

/**
Expand Down

0 comments on commit bd7652e

Please sign in to comment.