Skip to content

Commit

Permalink
Add factory resetting to back-end
Browse files Browse the repository at this point in the history
  • Loading branch information
João Neto authored and jcorde committed Jul 8, 2019
1 parent e3e55ee commit 6b6b62e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 14 deletions.
9 changes: 9 additions & 0 deletions app/controllers/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ var update = function update(req, res, next) {
logger.debug(util.inspect(errReboot));
}
});
} else if (state === STATES.FACTORY_RESET) {
res.json(req.body);
systemSvc = new SystemService();
systemSvc.factoryReset(function onFactoryReset(errFactoryReset) {
if (errFactoryReset) {
logger.warn('Failed to factory resetting');
logger.debug(util.inspect(errFactoryReset));
}
});
} else {
res.json(req.body);
}
Expand Down
3 changes: 3 additions & 0 deletions app/models/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var mongoose = require('mongoose');

var STATES = {
REBOOTING: 'rebooting',
FACTORY_RESET: 'factory-reset',
CONFIGURATION_CLOUD: 'configuration-cloud',
CONFIGURATION_CLOUD_SECURITY: 'configuration-cloud-security',
CONFIGURATION_USER: 'configuration-user',
Expand Down Expand Up @@ -32,6 +33,7 @@ STATE_TRANSITIONS[STATES.REBOOTING] = [
STATES.CONFIGURATION_CLOUD,
STATES.CONFIGURATION_USER,
STATES.CONFIGURATION_GATEWAY,
STATES.FACTORY_RESET,
STATES.READY
];
STATE_TRANSITIONS[STATES.CONFIGURATION_CLOUD] = [
Expand All @@ -58,6 +60,7 @@ STATE_TRANSITIONS[STATES.CONFIGURATION_GATEWAY] = [
];
STATE_TRANSITIONS[STATES.READY] = [
STATES.REBOOTING,
STATES.FACTORY_RESET,
STATES.CONFIGURATION_CLOUD,
STATES.CONFIGURATION_USER,
STATES.CONFIGURATION_GATEWAY
Expand Down
7 changes: 7 additions & 0 deletions app/services/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ function canTransitionToRebooting(from, done) {
done(null, isAllowedTransition(from, stateModel.STATES.REBOOTING));
}

function canTransitionToFactoryReset(from, done) {
done(null, isAllowedTransition(from, stateModel.STATES.FACTORY_RESET));
}

function canTransitionToConfigurationCloud(from, done) {
done(null, isAllowedTransition(from, stateModel.STATES.CONFIGURATION_CLOUD));
}
Expand Down Expand Up @@ -101,6 +105,9 @@ function canTransition(from, to, done) {
case stateModel.STATES.REBOOTING:
canTransitionToRebooting(from, done);
break;
case stateModel.STATES.FACTORY_RESET:
canTransitionToFactoryReset(from, done);
break;
case stateModel.STATES.CONFIGURATION_CLOUD:
canTransitionToConfigurationCloud(from, done);
break;
Expand Down
40 changes: 40 additions & 0 deletions app/services/system.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
var util = require('util');

var exec = require('child_process').exec;
var bus = require('../dbus');
var logger = require('../logger');

var SERVICE_NAME = 'br.org.cesar.knot.control';
var FACTORY_RESET_INTERFACE_NAME = 'br.org.cesar.knot.control.Reset';
var OBJECT_PATH = '/br/org/cesar/knot/control';

var SystemServiceError = function SystemServiceError(message, code) {
this.name = 'SystemServiceError';
this.message = message;
this.code = code;
this.stack = (new Error()).stack;
};

var SystemService = function SystemService() {
};
Expand All @@ -7,6 +22,31 @@ SystemService.prototype.reboot = function reboot(done) {
exec('reboot', done);
};

var logError = function logError(err) { // eslint-disable-line vars-on-top
logger.warn('Error communicating with gateway control service');
logger.debug(util.inspect(err));
};

SystemService.prototype.factoryReset = function factoryReset(done) {
bus.getInterface(SERVICE_NAME, OBJECT_PATH, FACTORY_RESET_INTERFACE_NAME, function onInterface(getInterfaceErr, iface) { // eslint-disable-line max-len
if (getInterfaceErr) {
logError(getInterfaceErr);
done(new SystemServiceError(getInterfaceErr.message, 500));
return;
}

iface.FactoryReset(function onFactoryReset(factoryResetErr) { // eslint-disable-line new-cap
if (factoryResetErr) {
logError(factoryResetErr);
done(new SystemServiceError(factoryResetErr.message, 500));
return;
}

done();
});
});
};

module.exports = {
SystemService: SystemService
};
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b6b62e

Please sign in to comment.