From 6b6b62e7f6487df76f4ea673b106858ffe365de2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Neto?= Date: Thu, 4 Jul 2019 01:49:55 -0300 Subject: [PATCH] Add factory resetting to back-end --- app/controllers/state.js | 9 +++++++++ app/models/state.js | 3 +++ app/services/state.js | 7 +++++++ app/services/system.js | 40 ++++++++++++++++++++++++++++++++++++++++ package-lock.json | 28 ++++++++++++++-------------- 5 files changed, 73 insertions(+), 14 deletions(-) diff --git a/app/controllers/state.js b/app/controllers/state.js index 40837fac..4ab378b7 100644 --- a/app/controllers/state.js +++ b/app/controllers/state.js @@ -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); } diff --git a/app/models/state.js b/app/models/state.js index f951ca1d..138bfe9c 100644 --- a/app/models/state.js +++ b/app/models/state.js @@ -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', @@ -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] = [ @@ -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 diff --git a/app/services/state.js b/app/services/state.js index 366e8a62..b23e8256 100644 --- a/app/services/state.js +++ b/app/services/state.js @@ -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)); } @@ -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; diff --git a/app/services/system.js b/app/services/system.js index 8e040f1e..b62f7709 100644 --- a/app/services/system.js +++ b/app/services/system.js @@ -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() { }; @@ -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 }; diff --git a/package-lock.json b/package-lock.json index e58b625d..a47f67b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2780,14 +2780,14 @@ "dev": true }, "fsevents": { - "version": "1.2.7", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.7.tgz", - "integrity": "sha512-Pxm6sI2MeBD7RdD12RYsqaP0nMiwx8eZBXCa6z2L+mRHm2DYrOYwihmhjpkdjUHwQhslWQjRpEgNq4XvBmaAuw==", + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.9.tgz", + "integrity": "sha512-oeyj2H3EjjonWcFjD5NvZNE9Rqe4UW+nQBU2HNeKw0koVLEFIhtyETyAakeAM3de7Z/SW5kcA+fZUait9EApnw==", "dev": true, "optional": true, "requires": { - "nan": "^2.9.2", - "node-pre-gyp": "^0.10.0" + "nan": "^2.12.1", + "node-pre-gyp": "^0.12.0" }, "dependencies": { "abbrev": { @@ -2859,12 +2859,12 @@ "optional": true }, "debug": { - "version": "2.6.9", + "version": "4.1.1", "bundled": true, "dev": true, "optional": true, "requires": { - "ms": "2.0.0" + "ms": "^2.1.1" } }, "deep-extend": { @@ -3029,24 +3029,24 @@ } }, "ms": { - "version": "2.0.0", + "version": "2.1.1", "bundled": true, "dev": true, "optional": true }, "needle": { - "version": "2.2.4", + "version": "2.3.0", "bundled": true, "dev": true, "optional": true, "requires": { - "debug": "^2.1.2", + "debug": "^4.1.0", "iconv-lite": "^0.4.4", "sax": "^1.2.4" } }, "node-pre-gyp": { - "version": "0.10.3", + "version": "0.12.0", "bundled": true, "dev": true, "optional": true, @@ -3074,13 +3074,13 @@ } }, "npm-bundled": { - "version": "1.0.5", + "version": "1.0.6", "bundled": true, "dev": true, "optional": true }, "npm-packlist": { - "version": "1.2.0", + "version": "1.4.1", "bundled": true, "dev": true, "optional": true, @@ -3216,7 +3216,7 @@ "optional": true }, "semver": { - "version": "5.6.0", + "version": "5.7.0", "bundled": true, "dev": true, "optional": true