From ca5fc9c743b99cbe2a5551d264daa96b83539670 Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Fri, 5 Feb 2016 14:58:51 -0600 Subject: [PATCH] Config Pages, functions and tables added --- init/website | 3 +- python/tagserver_SQLite.py | 38 +++++++- www/app.js | 27 +++--- www/dbcreate_SQLite.sql | 7 ++ www/functions_SQLite.js | 53 ++++++++++- www/package.json | 3 +- www/public/js/controller.js | 150 ++++++++++++++++++++++++++++++++ www/public/js/router.js | 3 + www/public/partials/config.html | 80 +++++++++++++++++ www/views/angularIndex.html | 6 +- 10 files changed, 348 insertions(+), 22 deletions(-) create mode 100644 www/public/partials/config.html diff --git a/init/website b/init/website index ab46259..55f8d21 100755 --- a/init/website +++ b/init/website @@ -37,5 +37,4 @@ case "$1" in ;; esac -exit 0 - +exit 0 diff --git a/python/tagserver_SQLite.py b/python/tagserver_SQLite.py index 313e463..8de0543 100644 --- a/python/tagserver_SQLite.py +++ b/python/tagserver_SQLite.py @@ -37,20 +37,50 @@ def readTag(addr, tag): c.close() def main(): + + with con: cur = con.cursor() query = "SELECT * FROM tags WHERE deleted = 0;" cur.execute(query) tags = cur.fetchall() - PLC_IP_ADDRESS = "10.10.10.3" # MAKE THIS A db VALUE - scan_rate = 10 + configObj = {} + + + with con: + cur = con.cursor() + query = "SELECT parameter, val FROM config GROUP BY parameter;" + cur.execute(query) + config = cur.fetchall() + for x in config: + configObj[x[0]] = x[1] + + try: + PLC_IP_ADDRESS = str(configObj['ip_address']) + print("FYI, using PLC IP Address from the database {0}".format(PLC_IP_ADDRESS)) + except KeyError: + print("FYI, there is no PLC IP Address stored in the database, defaulting to 192.168.1.10") + PLC_IP_ADDRESS = "192.168.1.10" + + try: + scan_rate = int(configObj['scan_rate']) + print("FYI, using Scan Rate from the database {0}".format(scan_rate)) + except KeyError: + print("FYI, there is no Scan Rate stored in the database, defaulting to 10 seconds") + scan_rate = 10 + + + + tagList = [] + print("\nScan List\n--------------") if len(tags) > 0: for t in tags: tagList.append({"id": int(t[0]), "name": t[1], "val": None, "lastVal": None}) - print(tagList) + print(t[1]) + print("--------------\n") while True: try: @@ -61,7 +91,7 @@ def main(): with con: cur = con.cursor() aQuery = """INSERT INTO vals (tagID, val) VALUES ('%d', '%f');""" % (r["id"], float(r["val"])) - print(aQuery) + # print(aQuery) cur.execute(aQuery) con.commit() r["lastVal"] = r["val"] diff --git a/www/app.js b/www/app.js index 83e17b0..23e77c2 100644 --- a/www/app.js +++ b/www/app.js @@ -65,17 +65,22 @@ var angular = function(req, res) { res.render('angularIndex'); }; -app.post('/json/tag/add', fns.createTag); // Adds a tag to the scan list -app.post('/json/tag/update/', fns.updateTag); // Updates tag data -app.get('/json/tag/delete/:tag', fns.deleteTag); // Removes a tag from the scan list -app.get('/json/tag/:id', fns.getTag); // Lists all tags in the scan list -app.get('/json/tag', fns.getAllTags); // Lists all tags in the scan list -app.get('/json/val/:tag', fns.latestValueSingleTag); // Gets the latest value of a single tag -app.get('/json/series/:tag/:hours', fns.seriesTagValues); // Gets all the values of a tag for the last X hours -app.get('/json/valBetween/:tag/:startDatetime/:endDatetime', fns.seriesTagValuesBetween); // Gets the latest value of a single tag -app.get('/json/CSV/:tag/:startDatetime/:endDatetime', fns.seriesCSV); // Gets all the values of a tag for the last X hours -app.get('/json/CSV/:tag/:hours', fns.seriesCSV); // Gets all the values of a tag for the last X hours -app.get('/json/all', fns.latestValueAllTags); // Gets the latest values of all tags in the scan list +app.post('/json/tag/add', fns.createTag); // Adds a tag to the scan list +app.post('/json/tag/update/', fns.updateTag); // Updates tag data +app.get('/json/tag/delete/:tag', fns.deleteTag); // Removes a tag from the scan list +app.get('/json/tag/:id', fns.getTag); // Gets a specific tag in the scan list +app.get('/json/tag', fns.getAllTags); // Lists all tags in the scan list +app.get('/json/val/:tag', fns.latestValueSingleTag); // Gets the latest value of a single tag +app.get('/json/series/:tag/:hours', fns.seriesTagValues); // Gets all the values of a tag for the last X hours +app.get('/json/valBetween/:tag/:startDatetime/:endDatetime', fns.seriesTagValuesBetween); // Gets the values of a tag between the start time and end time +app.get('/json/CSV/:tag/:startDatetime/:endDatetime', fns.seriesCSV); // Gets a CSV of the values of a tag between the start time and end time +app.get('/json/CSV/:tag/:hours', fns.seriesCSV); // Gets a CSV of the values of a tag for the last x hours +app.get('/json/all', fns.latestValueAllTags); // Gets the latest values of all tags in the scan list +app.get('/json/config', fns.getSetup); // Gets the contents of the config table +app.post('/json/config', fns.updateSetup); // Adds a new parameter to the config table +app.get('/json/logger/status', fns.checkLoggerStatus); // Gets the status of the data logger +app.get('/json/logger/restart', fns.restartLogger); // Restarts the data logger + app.get('*', angular); diff --git a/www/dbcreate_SQLite.sql b/www/dbcreate_SQLite.sql index dbcaefa..37c244e 100644 --- a/www/dbcreate_SQLite.sql +++ b/www/dbcreate_SQLite.sql @@ -16,3 +16,10 @@ CREATE TABLE IF NOT EXISTS vals ( val REAL, dateAdded TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); + +CREATE TABLE IF NOT EXISTS config ( + id INTEGER PRIMARY KEY, + parameter TEXT, + val TEXT, + dateAdded TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); diff --git a/www/functions_SQLite.js b/www/functions_SQLite.js index e470dfb..096424d 100644 --- a/www/functions_SQLite.js +++ b/www/functions_SQLite.js @@ -282,7 +282,7 @@ exports.checkLoggerStatus = function(req, res){ var fs = require('fs'); // var ps = require("ps-node"); var running = require("is-running"); - fs.readFile('/root/dataLogger.pid', function (derr,ddata) { + fs.readFile('/root/tagserver.pid', function (derr,ddata) { if (derr) { console.log("Problem getting PID of tagserver"); res.json({status:"error", message: "Problem getting PID of tagserver"}); @@ -302,3 +302,54 @@ exports.restartLogger = function(req, res){ } }); }; + +exports.getSetup = function(req, res){ + var sqlite3 = require('sqlite3').verbose(); + var db = new sqlite3.Database(dbFile); + + var query = "SELECT parameter, val, dateAdded FROM config GROUP BY parameter;"; + var prepQuery = db.prepare(query); + prepQuery.all(req.params.id, function(err, rows) { + prepQuery.finalize(); + db.close(); + if (err) { + res.json({status:"error", message:err}); + console.log(err); + } else { + res.json({status:"OK", config:rows}); + } + }); +}; + +exports.updateSetup = function(req, res){ + var exec = require('child_process').exec; + var sqlite3 = require('sqlite3').verbose(); + var db = new sqlite3.Database(dbFile); + + console.log(req.body.parameter, req.body.val); + db.serialize(function(){ + var query = db.prepare('INSERT INTO config (parameter, val) VALUES (?, ?)'); + query.run(req.body.parameter, req.body.val, function(err) { + query.finalize(); + db.close(); + if (err) { + console.log({runErr:err}); + res.json({status:"error", message:err, query:query}); + } else { + res.redirect("/#/setup"); + exec('/etc/init.d/loggers stop', function(error, stdout, stderr){ + if (error){ + console.log({status:"error", message:error, query:query}); + } + setTimeout(function(){ + exec('/etc/init.d/loggers start', function(error, stdout, stderr){ + if (error){ + console.log({status:"error", message:error, query:query}); + } + }); + },5000); + }); + } + }); + }); +}; diff --git a/www/package.json b/www/package.json index 1a29e40..d00fe55 100644 --- a/www/package.json +++ b/www/package.json @@ -13,7 +13,8 @@ "mysql": "*", "serve-favicon": "*", "sqlite3": "*", - "n3-charts": "*" + "n3-charts": "*", + "is-running": "*" }, "devDependencies": {}, "scripts": { diff --git a/www/public/js/controller.js b/www/public/js/controller.js index a4b45b7..4527577 100644 --- a/www/public/js/controller.js +++ b/www/public/js/controller.js @@ -246,6 +246,82 @@ tsCtrlrs.factory('tags',function($q, $http, $log){ }; }); +tsCtrlrs.factory('config',function($q, $http, $log){ + + var getConfig = function(){ + var deferred = $q.defer(); + $http.get('/json/config/').success(function(data) { + if(data.status == "OK"){ + deferred.resolve({ + config:data.config, + status: data.status + }); + } else { + deferred.resolve({ + status:data.status, + message: data.message + }); + } + }); + return deferred.promise; + }; + + var submitParameter = function(entry){ + $http.post('/json/config', { + parameter: entry.parameter, + val: entry.val + }).success(function(data){ + return data; + }); + }; + + var getLoggerStatus = function(){ + var deferred = $q.defer(); + $http.get('/json/logger/status').success(function(data) { + if(data.status == "OK"){ + deferred.resolve({ + running:data.running, + status: data.status + }); + } else { + deferred.resolve({ + status:data.status, + message: data.message, + + }); + } + }); + return deferred.promise; + }; + + var restartLogger = function(){ + var deferred = $q.defer(); + $http.get('/json/logger/restart').success(function(data) { + if(data.status == "OK"){ + deferred.resolve({ + status: data.status + }); + } else { + deferred.resolve({ + status:data.status, + message: data.message + }); + } + }); + return deferred.promise; + }; + + + return { + getConfig:getConfig, + submitParameter: submitParameter, + getLoggerStatus: getLoggerStatus, + restartLogger: restartLogger + + }; + +}); + tsCtrlrs.controller('mainCtrl', function($scope, Page, Alerts) { $scope.Page = Page; $scope.Alerts = Alerts; @@ -408,6 +484,80 @@ tsCtrlrs.controller('tagValsCtrl', function($scope, $route, $http, $routeParams, $scope.loadTagVals($scope.startDatetime, $scope.endDatetime); }); +tsCtrlrs.controller('configCtrl', function($scope, Page, Alerts, $log, config) { + Page.setTitle('Configuration'); + Page.setPage('configuration'); + + + + $scope.loadConfig = function(){ + $scope.loading = true; + var getConfig = config.getConfig(); + getConfig.then(function(data) { + $scope.loading = false; + if (data.status == "OK"){ + $scope.config = data.config; + $scope.error = false; + } else { + $scope.error = data.message; + } + }); + }; + $scope.loadConfig(); + $scope.paramError = null; + var selString = "Select a parameter..."; + $scope.newParam = { + pSelected: selString, + parameter: null, + val: null + }; + + $scope.addParameter = function(){ + if ($scope.newParam.pSelected == selString){ + $scope.paramError = "No parameter selected"; + } else { + if ($scope.newParam.pSelected == "other") { + $scope.newParam.parameter = $scope.newParam.pEntry; + } else { + $scope.newParam.parameter = $scope.newParam.pSelected; + } + config.submitParameter($scope.newParam); + $scope.loadConfig(); + } + }; + + $scope.checkLogger = function(){ + $scope.loggerLoading = true; + var checkLoggerStatus = config.getLoggerStatus(); + checkLoggerStatus.then(function(data){ + $scope.loggerLoading = false; + if (data.status == "OK"){ + $scope.loggerRunning = data.running; + $scope.error = false; + } else { + $scope.error = data.message; + } + }); + }; + $scope.checkLogger(); + + $scope.restartLogger = function(){ + var restartLogger = config.restartLogger(); + restartLogger.then(function(data){ + if (data.status == "OK"){ + $scope.error = false; + $scope.checkLogger(); + } else { + $scope.error = data.message; + } + }); + }; + + +}); + + +//*---- FILTERS -----*// tsCtrlrs.filter('dString', function myDateFormat($filter){ return function(text){ var tempdate= new Date(text); diff --git a/www/public/js/router.js b/www/public/js/router.js index cbb258a..a5783ac 100644 --- a/www/public/js/router.js +++ b/www/public/js/router.js @@ -11,6 +11,9 @@ tagserver.config([ }).when('/tag/:tagID', { templateUrl: '/partials/tagVals.html', controller: 'tagValsCtrl' + }).when('/config', { + templateUrl: '/partials/config.html', + controller: 'configCtrl' }).when('/', { templateUrl: '/partials/dashboard.html', controller: 'dashboardCtrl' diff --git a/www/public/partials/config.html b/www/public/partials/config.html new file mode 100644 index 0000000..9facfaa --- /dev/null +++ b/www/public/partials/config.html @@ -0,0 +1,80 @@ +
+
+
+

Loading Config...

+ +
+
+
+ +
+
+
+
+

Error Caught!

+
{{message}}
+ +
+
+
+ +
+
+
+

Logger Status: + Running + Not Running +

+

+ Checking Logger Status... +

+ +
+
+
+

Configuration Parameters

+ + + + + + + + + + + + + + + +
ParameterValue
{{param.parameter}}{{param.val}}
+
+
+

New Parameter

+
+
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+ +
+
+
diff --git a/www/views/angularIndex.html b/www/views/angularIndex.html index 96c4277..cf28640 100644 --- a/www/views/angularIndex.html +++ b/www/views/angularIndex.html @@ -51,12 +51,12 @@