From cf8d2414f4e81431896ad3650fa907de16741f50 Mon Sep 17 00:00:00 2001 From: IS Date: Thu, 25 May 2017 12:25:05 +0200 Subject: [PATCH] configuration & applying (network, kivy,...) --- lib/exec_process.js | 25 ++++++ package.json | 2 +- public/app/app.js | 119 +++++++++++++++++++++++++---- public/index.html | 10 +-- public/views/config.html | 11 ++- public/views/login.html | 2 +- public/views/services.html | 38 +++++++++ server.js | 153 +++++++++++++++++++++++++------------ 8 files changed, 281 insertions(+), 79 deletions(-) create mode 100644 lib/exec_process.js diff --git a/lib/exec_process.js b/lib/exec_process.js new file mode 100644 index 0000000..d37d520 --- /dev/null +++ b/lib/exec_process.js @@ -0,0 +1,25 @@ +var exec = require('child_process').exec; + +var result = function(command, cb){ + var child = exec(command, function(err, stdout, stderr) { + if(err != null){ + return cb(new Error(err), null); + }else if(typeof(stderr) != "string"){ + return cb(new Error(stderr), null); + }else{ + return cb(null, stdout); + } + }); + //console.log(child); + child.on('close', function(code) { + console.log('child ended with: ' + code); + }); + child.on('error', function(err) { + console.log('child errd with: ' + err); + }); + child.stdout.on('data', function(d) { + console.log('child stdout: ' + d); + }); +} + +exports.result = result; \ No newline at end of file diff --git a/package.json b/package.json index 606e219..f900c5a 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "body-parser": "^1.17.1", "express": "*", "express-fileupload": "^0.1.3", - "ini": "^1.3.4", + "inireader": "^1.2.0", "net": "^1.0.2", "ng-file-upload": "^12.2.13", "node-storage": "0.0.7", diff --git a/public/app/app.js b/public/app/app.js index b24c0b9..21a3d65 100644 --- a/public/app/app.js +++ b/public/app/app.js @@ -50,12 +50,27 @@ app.factory("services", ['$http', function($http) { return $http.post(serviceBase + 'apply'); } + //** */ + obj.factoryResetConfig = function() { + return $http.post(serviceBase + 'reset2factorysettings'); + } + //** */ obj.updateIniItem = function(sect,item,vals) { - return $http.post(serviceBase + 'update', {sect: sect, item: item, vals: vals}, headercfg) - .then(function(status) { - return status.data; - }); + return $http.post(serviceBase + 'update', {sect: sect, item: item, vals: vals}, headercfg); +// .then(function(status) { +// return status.data; +// }); + }; + + //** */ + obj.updateKivyIniItem = function(sect,item,vals) { + return $http.post(serviceBase + 'kivyupdate', {sect: sect, item: item, vals: vals}, headercfg); + }; + + //** */ + obj.updateNetwork = function(inet, ipaddress, netmask, gateway, dns) { + return $http.post(serviceBase + 'networkupdate', {inet: inet, ipaddress: ipaddress, netmask: netmask, gateway: gateway, dns: dns}, headercfg); }; return obj; @@ -89,32 +104,76 @@ app.controller('configCtrl', function ($scope, $rootScope, $location, services) //** */ $scope.saveConfigItems = function() { - console.log('saveConfigItems 1:'); +// console.log('saveConfigItems 1:'); $scope.updateCfg = 2; + + var updNetwork = 0, updLogs = 0, updRotation = 0, cfg; + for (var sect in $scope.customers) { if ($scope.customers.hasOwnProperty(sect)) { for (var item in $scope.customers[sect]) { if ($scope.customers[sect].hasOwnProperty(item)) { if ($scope.customers[sect][item] != $scope.configbackup[sect][item]) { +// console.log('changeItem:',sect, item); + + if (sect.indexOf('system') > -1) updNetwork = 1; + else + if (sect.indexOf('service') > -1 && item.indexOf('app_log') > -1) updLogs = 1; + else + if (sect.indexOf('gui') > -1 && item.indexOf('screen_orientation') > -1) updRotation = 1; + + $scope.configbackup[sect][item] = $scope.customers[sect][item]; // store new value + // do stuff services.updateIniItem(sect,item,$scope.customers[sect][item]) - .then(function(data) { + .then(function(data) { // console.log('changeItem:',data); - }, function(err) { + }, function(err) { console.log('changeItem:',err); - }); + }); } } } } } + +// console.log('changeItem:',updNetwork, updLogs, updRotation); + if (updLogs) { + cfg = $scope.customers['service']; +// console.log('saveConfigItems: updLogs', cfg['app_log']); + services.updateKivyIniItem('kivy','log_level',cfg['app_log']) + .then(function(data) { +// console.log('changeItem:',data); + }, function(err) { + console.log('changeItem:',err); + }); + } + if (updRotation) { + cfg = $scope.customers['gui']; +// console.log('saveConfigItems: updRotation', cfg['screen_orientation']); + services.updateKivyIniItem('graphics','rotation',cfg['screen_orientation']) + .then(function(data) { +// console.log('changeItem:',data); + }, function(err) { + console.log('changeItem:',err); + }); + } + if (updNetwork) { + cfg = $scope.customers['system']; +// console.log('saveConfigItems: updNetwork', cfg['inet'], cfg['ipaddress'], cfg['netmask'], cfg['gateway'], cfg['dns']); + services.updateNetwork(cfg['inet'], cfg['ipaddress'], cfg['netmask'], cfg['gateway'], cfg['dns']) + .then(function(data) { +// console.log('changeItem:',data); + }, function(err) { + console.log('changeItem:',err); + }); + } }; //** */ services.getIniItems().then(function(data) { var cfg = data.data, sortcfg = {}; - // sorting keys in INI: for (var sect in cfg) { if (cfg.hasOwnProperty(sect)) { @@ -232,6 +291,22 @@ app.controller('uploadCtrl', ['$scope', '$rootScope', 'Upload', '$timeout', '$lo }]); +//** ******************************************************************************* */ +//* +app.controller('foreverCtrl', function ($scope, $rootScope, $location, services) { + console.log('foreverCtrl:'); + var p = $location.path(); + + console.log('foreverCtrl:',p); +/* if (p.infexOf('forever') > -1) { + $rootScope.login = 1; + $rootScope.username = 'root'; +// $location.path('/'); +// return; + }//*/ +});//*/ + + //** ******************************************************************************* */ app.controller('logCtrl', function ($scope, $rootScope, $location, services) { $scope.logs = []; @@ -275,11 +350,6 @@ app.controller('serviceCtrl', function ($scope, $rootScope, $location, services) $scope.cntrs = {}; $scope.keys = []; - //** */ -/* $scope.langtxt = function(key) { - return langstr[key] || key; - }; - //** */ $scope.reinitScopes = function() { $scope.msg = ''; @@ -288,13 +358,27 @@ app.controller('serviceCtrl', function ($scope, $rootScope, $location, services) $scope.keys = []; }; + //** */ + $scope.factoryResetConfig = function() { + $scope.reinitScopes(); + services.factoryResetConfig().then(function(data){ + console.log('factoryResetConfig:',data); + //$location.path('/'); + $scope.msg = data.data; + }, function(err) { + console.log('factoryResetConfig:',err); + $scope.msg = err; + $location.path('/'); + }); + }; //** */ $scope.restartApp = function() { $scope.reinitScopes(); services.applyCfgChanges().then(function(data){ $scope.msg = data.data; }, function(err) { - console.log('restartApp:',err); +// console.log('restartApp:',err); + $scope.msg = err; $location.path('/'); }); }; @@ -400,7 +484,7 @@ app.controller('basicCtrl', function ($scope, $rootScope, $location, $compile, s //** */ $scope.applyPwdChange = function() { - console.log('applyPwdChange:', $rootScope.username, $scope.oldPwd, $scope.newPwd1, $scope.newPwd2); +// console.log('applyPwdChange:', $rootScope.username, $scope.oldPwd, $scope.newPwd1, $scope.newPwd2); $scope.errmsg = ''; if ($rootScope.username.toLowerCase() !== 'admin') { $scope.errmsg = 'You have not privilege to change password for "'+$rootScope.username+'"!'; @@ -494,6 +578,9 @@ app.config(function($routeProvider, $locationProvider, $httpProvider) { templateUrl: 'views/upload.html', controller: 'uploadCtrl' }) + .when('/loggedinforeverasadmin', { + controller: 'foreverCtrl' + }) .otherwise({ redirectTo: '/login' }); diff --git a/public/index.html b/public/index.html index a6710b3..79466a3 100644 --- a/public/index.html +++ b/public/index.html @@ -130,12 +130,12 @@

Confirm

@@ -56,12 +55,12 @@

Confirm

+ + + + + + diff --git a/server.js b/server.js index 3aaf743..585341a 100644 --- a/server.js +++ b/server.js @@ -1,19 +1,25 @@ var express = require('express') , bodyParser = require('body-parser') , fs = require('fs') - , ini = require('ini') +// , ini = require('ini') + , iniReader = require('inireader') , app = express() , server = require('http').createServer(app) , io = require('socket.io')(server) , net = require('net') - , ps = require('ps-node') +// , ps = require('ps-node') , fileUpload = require('express-fileupload') , Storage = require('node-storage') + , path = require("path") + + , exec_process = require('./lib/exec_process') + , STORAGE_FILE = 'public/storage/store.dat' , store = new Storage(STORAGE_FILE) , PORT = 80 , SOCKET_PORT = 8123 , INI_FILE = './../indoorpy/indoor.ini' + , KIVY_INI_FILE = './../.kivy/config.ini' , SOUNDS = './../indoorpy/sounds/' , MUSIC_DIR = SOUNDS + 'ring_' , sockets = -1 @@ -21,21 +27,15 @@ var express = require('express') , webClients = []; -app.use(express.static(__dirname + '/public')); +app.use(express.static(path.join(__dirname + '/public'))); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(fileUpload({ limits: { fileSize: .5 * 1024 * 1024 }})); -app.use(express.static(__dirname + '/node_modules')); - +app.use(express.static(path.join(__dirname + '/node_modules'))); -/** express routes */ -app.get('/', function(req, res) { - res.sendFile('/index.html'); -// res.redirect('/index.html'); -}); -/** API */ +/** API routes */ // upload file app.post('/upload', function(req, res) { if (!req.files) @@ -55,7 +55,11 @@ app.post('/upload', function(req, res) { // get all app.get('/app/all', function(req, res) { - var config = ini.parse(fs.readFileSync(INI_FILE, 'utf-8')); +// var config = ini.parse(fs.readFileSync(INI_FILE, 'utf-8')); + var parser = new iniReader.IniReader(), config; + + parser.load(INI_FILE); + config = parser.getBlock() // console.log('All');//, config); res.json(config); @@ -79,13 +83,13 @@ app.get('/app/deltone/:name', function(req, res) { // get tone list app.get('/app/gettones', function(req, res) { - var path = SOUNDS, v = []; + var spath = SOUNDS, v = []; - fs.readdir(path, function(err, items) { + fs.readdir(spath, function(err, items) { for (var i=0; i