Able to switch back and forth between MySQL and SQLite
This commit is contained in:
@@ -8,7 +8,7 @@ import micro800 as u800
|
||||
import traceback
|
||||
import pickle
|
||||
|
||||
with open('mysql_cfg.pickle', 'rb') as cfgFile:
|
||||
with open('/root/tagserver/python/mysql_cfg.pickle', 'rb') as cfgFile:
|
||||
mysql_cfg = pickle.load(cfgFile)
|
||||
con = mysqlcon.connect(**mysql_cfg)
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ def readTag(addr, tag):
|
||||
v = c.read_tag(tag)
|
||||
return v
|
||||
except Exception:
|
||||
print("ERROR RETRIEVING TAG: {}".format(tag))
|
||||
print("ERROR RETRIEVING TAG: {} at {}".format(tag, addr))
|
||||
err = c.get_status()
|
||||
c.close()
|
||||
print err
|
||||
@@ -49,7 +49,7 @@ class Tag():
|
||||
def read(self, forceSend):
|
||||
writeToDB = False
|
||||
if self.tag:
|
||||
v = readFn(self.plc_ip_address, self.tag)
|
||||
v = self.readFn(str(self.plc_ip_address), str(self.tag))
|
||||
if v:
|
||||
if self.data_type == 'BOOL' or self.data_type == 'STRING':
|
||||
val = v[0]
|
||||
@@ -73,9 +73,10 @@ class Tag():
|
||||
|
||||
def sendToDB(self):
|
||||
query = "INSERT INTO tag_vals (dtime, tagID, val) VALUES ({}, '{}', {})".format(time.time(), self.db_id, self.value)
|
||||
self.last_send_time = time.time()
|
||||
print query
|
||||
# TODO: CHECK ON THIS LOGIC -- with con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query)
|
||||
con.commit()
|
||||
cur.close()
|
||||
with con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query)
|
||||
con.commit()
|
||||
cur.close()
|
||||
|
||||
@@ -12,7 +12,7 @@ from tag_mysql import Tag
|
||||
import traceback
|
||||
import time
|
||||
|
||||
with open('mysql_cfg.pickle', 'rb') as pickleconfig:
|
||||
with open('/root/tagserver/python/mysql_cfg.pickle', 'rb') as pickleconfig:
|
||||
mysql_cfg = pickle.load(pickleconfig)
|
||||
|
||||
if mysql_cfg:
|
||||
|
||||
@@ -8,125 +8,87 @@ Created on Dec 8, 2015
|
||||
|
||||
import time
|
||||
import sqlite3 as lite
|
||||
from pycomm.ab_comm.clx import Driver as ClxDriver
|
||||
import micro800 as u800
|
||||
import logging
|
||||
from tag_sqlite import Tag
|
||||
import traceback
|
||||
|
||||
# con = lite.connect("/usr/db/data.db")
|
||||
con = lite.connect('/mnt/usb/data.db')
|
||||
|
||||
configProperties = {}
|
||||
|
||||
def readTag(addr, tag):
|
||||
# logging.basicConfig(
|
||||
# filename="ClxDriver.log",
|
||||
# format="%(levelname)-10s %(asctime)s %(message)s",
|
||||
# level=logging.DEBUG
|
||||
# )
|
||||
c = ClxDriver()
|
||||
|
||||
if c.open(addr):
|
||||
try:
|
||||
v = c.read_tag(tag)
|
||||
# print(v)
|
||||
return v
|
||||
except Exception as e:
|
||||
err = c.get_status()
|
||||
c.close()
|
||||
print err
|
||||
print e
|
||||
pass
|
||||
c.close()
|
||||
|
||||
def main():
|
||||
|
||||
|
||||
with con:
|
||||
cur = con.cursor()
|
||||
query = "SELECT * FROM tags WHERE deleted = 0;"
|
||||
cur.execute(query)
|
||||
tags = cur.fetchall()
|
||||
with con:
|
||||
cur = con.cursor()
|
||||
query = "SELECT * FROM tags WHERE deleted = 0;"
|
||||
cur.execute(query)
|
||||
tags = cur.fetchall()
|
||||
|
||||
configObj = {}
|
||||
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]
|
||||
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:
|
||||
configProperties['PLC_IP_ADDRESS'] = str(configObj['ip_address'])
|
||||
print("FYI, using PLC IP Address from the database {0}".format(configProperties['PLC_IP_ADDRESS']))
|
||||
except KeyError:
|
||||
print("FYI, there is no PLC IP Address stored in the database, defaulting to 192.168.1.10")
|
||||
configProperties['PLC_IP_ADDRESS'] = "192.168.1.10"
|
||||
try:
|
||||
configProperties['PLC_IP_ADDRESS'] = str(configObj['ip_address'])
|
||||
print("FYI, using PLC IP Address from the database {0}".format(configProperties['PLC_IP_ADDRESS']))
|
||||
except KeyError:
|
||||
print("FYI, there is no PLC IP Address stored in the database, defaulting to 192.168.1.10")
|
||||
configProperties['PLC_IP_ADDRESS'] = "192.168.1.10"
|
||||
|
||||
try:
|
||||
configProperties['plc_type'] = str(configObj['plc_type'])
|
||||
print("FYI, using PLC Type from the database {0}".format(configProperties['plc_type']))
|
||||
except KeyError:
|
||||
print("FYI, there is no PLC Type stored in the database, defaulting to CLX")
|
||||
configProperties['plc_type'] = "CLX"
|
||||
try:
|
||||
configProperties['plc_type'] = str(configObj['plc_type'])
|
||||
print("FYI, using PLC Type from the database {0}".format(configProperties['plc_type']))
|
||||
except KeyError:
|
||||
print("FYI, there is no PLC Type stored in the database, defaulting to CLX")
|
||||
configProperties['plc_type'] = "CLX"
|
||||
|
||||
try:
|
||||
configProperties['scan_rate'] = int(configObj['scan_rate'])
|
||||
print("FYI, using Scan Rate from the database {0}".format(configProperties['scan_rate']))
|
||||
except KeyError:
|
||||
print("FYI, there is no Scan Rate stored in the database, defaulting to 10 seconds")
|
||||
configProperties['scan_rate'] = 10
|
||||
try:
|
||||
configProperties['scan_rate'] = int(configObj['scan_rate'])
|
||||
print("FYI, using Scan Rate from the database {0}".format(configProperties['scan_rate']))
|
||||
except KeyError:
|
||||
print("FYI, there is no Scan Rate stored in the database, defaulting to 10 seconds")
|
||||
configProperties['scan_rate'] = 10
|
||||
|
||||
try:
|
||||
sa_test = str(configObj['save_all'])
|
||||
if sa_test == "true":
|
||||
configProperties['save_all'] = True
|
||||
else:
|
||||
configProperties['save_all'] = False
|
||||
print("FYI, value for save_all is {0}".format(configProperties['save_all']))
|
||||
except KeyError:
|
||||
print("FYI, there is no save_all value stored in the database, using False")
|
||||
configProperties['save_all'] = False
|
||||
try:
|
||||
sa_test = str(configObj['save_all'])
|
||||
if sa_test == "true":
|
||||
configProperties['save_all'] = True
|
||||
else:
|
||||
configProperties['save_all'] = False
|
||||
print("FYI, value for save_all is {0}".format(configProperties['save_all']))
|
||||
except KeyError:
|
||||
print("FYI, there is no save_all value stored in the database, using False")
|
||||
configProperties['save_all'] = False
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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(t[1])
|
||||
print("--------------\n")
|
||||
tag_store = {}
|
||||
|
||||
while True:
|
||||
try:
|
||||
for r in tagList:
|
||||
r['val'] = 0
|
||||
if configProperties['plc_type'] == "u800":
|
||||
r["val"] = u800.readMicroTag(configProperties['PLC_IP_ADDRESS'], str(r['name']))[0]
|
||||
else:
|
||||
r["val"] = readTag(configProperties['PLC_IP_ADDRESS'], str(r['name']))[0]
|
||||
if len(tags) > 0:
|
||||
for t in tags:
|
||||
# (1, u'Pump Intake Pressure', u'5', u'Pump_Intake_Pressure', u'Pressure at the Intake of the Pump', None, 100.0, 3600, u'PSI', 0.0, 3000.0, u'2016-04-13 21:27:01', 0)
|
||||
tag_store[t[1]] = Tag(t[1], t[3], t[0], t[5], t[6], t[7], mapFn=t[8], plc_type=configProperties['plc_type'], plc_ip_address=configProperties['PLC_IP_ADDRESS'])
|
||||
|
||||
print("{0} - {1}".format(r["name"], r["val"]))
|
||||
if (not configProperties['save_all'] and not r["val"] == r["lastVal"]) or configProperties['save_all']:
|
||||
with con:
|
||||
cur = con.cursor()
|
||||
aQuery = """INSERT INTO vals (tagID, val) VALUES ('%d', '%f');""" % (r["id"], float(r["val"]))
|
||||
# print(aQuery)
|
||||
cur.execute(aQuery)
|
||||
con.commit()
|
||||
print("<saved>")
|
||||
r["lastVal"] = r["val"]
|
||||
print("-----------")
|
||||
time.sleep(configProperties['scan_rate'])
|
||||
|
||||
except Exception as err:
|
||||
print err
|
||||
main()
|
||||
while True:
|
||||
for tag in tag_store:
|
||||
try:
|
||||
tag_store[tag].read(configProperties['save_all'])
|
||||
except:
|
||||
print("ERROR EVALUATING {}".format(tag))
|
||||
traceback.print_exc()
|
||||
time.sleep(configProperties['scan_rate'])
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
||||
@@ -83,6 +83,8 @@ 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 '/json/clearDatabase/all', fns.clearValues # Removes all tag values from the database
|
||||
app.get '/json/clearDatabase/:id', fns.clearValues # Removes tag values from the database
|
||||
app.get '*', angular
|
||||
|
||||
###*
|
||||
|
||||
@@ -66,8 +66,8 @@ exports.getAllTags = (req, res) ->
|
||||
|
||||
exports.createTag = (req, res) ->
|
||||
req.app.locals.pool.getConnection (err, db) ->
|
||||
query = 'INSERT INTO tags (tag, units, minExpected, maxExpected, name, description, class) VALUES (?, ?, ?, ?, ?, ?, 5)'
|
||||
db.query query, [req.body.tag, req.body.units, req.body.minExpected, req.body.maxExpected, req.body.name, req.body.description], (err, results) ->
|
||||
query = 'INSERT INTO tags (tag, units, minExpected, maxExpected, name, description, class, guarantee_sec, change_threshold) VALUES (?, ?, ?, ?, ?, ?, 5, ?, ?)'
|
||||
db.query query, [req.body.tag, req.body.units, req.body.minExpected, req.body.maxExpected, req.body.name, req.body.description, req.body.guarantee_sec, req.body.change_threshold], (err, results) ->
|
||||
if err
|
||||
res.json
|
||||
status: 'error'
|
||||
@@ -96,9 +96,10 @@ exports.getTag = (req, res) ->
|
||||
|
||||
|
||||
exports.updateTag = (req, res) ->
|
||||
console.log(req.body)
|
||||
req.app.locals.pool.getConnection (err, db) ->
|
||||
query = 'UPDATE tags set tag = ?, units = ?, minExpected = ?, maxExpected = ?, name = ?, description = ? WHERE id = ?'
|
||||
db.query query, [req.body.tag, req.body.units, req.body.minExpected, req.body.maxExpected, req.body.name, req.body.description, req.body.id], (err, results) ->
|
||||
query = 'UPDATE tags set tag = ?, units = ?, minExpected = ?, maxExpected = ?, name = ?, description = ?, guarantee_sec = ?, change_threshold = ? WHERE id = ?'
|
||||
db.query query, [req.body.tag, req.body.units, req.body.minExpected, req.body.maxExpected, req.body.name, req.body.description, req.body.guarantee_sec, req.body.change_threshold, req.body.id], (err, results) ->
|
||||
db.release()
|
||||
if err
|
||||
res.json
|
||||
@@ -386,3 +387,32 @@ exports.updateSetup = (req, res) ->
|
||||
query: query
|
||||
), 5000
|
||||
undefined
|
||||
|
||||
exports.clearValues = (req, res) ->
|
||||
if req.params.id
|
||||
req.app.locals.pool.getConnection (err, db) ->
|
||||
query = 'DELETE FROM tag_vals WHERE tagID = ?;';
|
||||
db.query query, [req.params.id], (err) ->
|
||||
db.release()
|
||||
if err
|
||||
res.json
|
||||
status: 'error'
|
||||
message: err
|
||||
console.log err
|
||||
else
|
||||
res.json
|
||||
status: 'OK'
|
||||
else
|
||||
req.app.locals.pool.getConnection (err, db) ->
|
||||
query = 'DELETE FROM tag_vals WHERE id >= 0;';
|
||||
db.query query, (err) ->
|
||||
db.release()
|
||||
if err
|
||||
res.json
|
||||
status: 'error'
|
||||
message: err
|
||||
console.log err
|
||||
else
|
||||
res.json
|
||||
status: 'OK'
|
||||
undefined
|
||||
|
||||
@@ -81,9 +81,9 @@ exports.createTag = (req, res) ->
|
||||
sqlite3 = require('sqlite3').verbose()
|
||||
db = new (sqlite3.Database)(dbFile)
|
||||
db.serialize ->
|
||||
query = 'INSERT INTO tags (tagName, units, minExpected, maxExpected, vanityName, description) VALUES (?, ?, ?, ?, ?, ?)'
|
||||
query = 'INSERT INTO tags (tag, units, minExpected, maxExpected, name, description, class, guarantee_sec, change_threshold) VALUES (?, ?, ?, ?, ?, ?, 5, ?, ?)'
|
||||
prepQuery = db.prepare(query)
|
||||
prepQuery.run req.body.tagName, req.body.units, req.body.minExpected, req.body.maxExpected, req.body.vanityName, req.body.description, (err) ->
|
||||
prepQuery.run req.body.tag, req.body.units, req.body.minExpected, req.body.maxExpected, req.body.name, req.body.description, req.body.guarantee_sec, req.body.change_threshold, (err) ->
|
||||
prepQuery.finalize()
|
||||
db.close()
|
||||
if err
|
||||
@@ -125,9 +125,9 @@ exports.updateTag = (req, res) ->
|
||||
sqlite3 = require('sqlite3').verbose()
|
||||
db = new (sqlite3.Database)(dbFile)
|
||||
db.serialize ->
|
||||
query = 'UPDATE tags set tagName = ?, units = ?, minExpected = ?, maxExpected = ?, vanityName = ?, description = ? WHERE id = ?'
|
||||
query = 'UPDATE tags set tag = ?, units = ?, minExpected = ?, maxExpected = ?, name = ?, description = ?, guarantee_sec = ?, change_threshold = ? WHERE id = ?'
|
||||
prepQuery = db.prepare(query)
|
||||
prepQuery.run req.body.tagName, req.body.units, req.body.minExpected, req.body.maxExpected, req.body.vanityName, req.body.description, req.body.id, (err) ->
|
||||
prepQuery.run req.body.tag, req.body.units, req.body.minExpected, req.body.maxExpected, req.body.name, req.body.description, req.body.guarantee_sec, req.body.change_threshold, req.body.id, (err) ->
|
||||
prepQuery.finalize()
|
||||
db.close()
|
||||
if err
|
||||
@@ -167,7 +167,7 @@ exports.seriesTagValues = (req, res) ->
|
||||
sqlite3 = require('sqlite3').verbose()
|
||||
db = new (sqlite3.Database)(dbFile)
|
||||
db.serialize ->
|
||||
query = 'SELECT * FROM vals WHERE tagID = ? AND dateAdded > DATETIME(\'now\', \'-1 HOUR\')'
|
||||
query = 'SELECT * FROM tag_vals WHERE tagID = ? AND dtime > DATETIME(\'now\', \'-1 HOUR\')'
|
||||
prepQuery = db.prepare(query)
|
||||
prepQuery.all parseInt(req.params.tag), (err, rows) ->
|
||||
prepQuery.finalize()
|
||||
@@ -191,7 +191,7 @@ exports.seriesTagValuesBetween = (req, res) ->
|
||||
sqlite3 = require('sqlite3').verbose()
|
||||
db = new (sqlite3.Database)(dbFile)
|
||||
db.serialize ->
|
||||
query = 'SELECT * FROM vals WHERE tagID = ? AND dateAdded >= DATETIME(?) AND dateAdded <= DATETIME(?)'
|
||||
query = 'SELECT * FROM tag_vals WHERE tagID = ? AND dtime >= DATETIME(?) AND dtime <= DATETIME(?)'
|
||||
prepQuery = db.prepare(query)
|
||||
prepQuery.all parseInt(req.params.tag), dString_to_sqlite(req.params.startDatetime), dString_to_sqlite(req.params.endDatetime), (err, rows) ->
|
||||
prepQuery.finalize()
|
||||
@@ -214,8 +214,8 @@ exports.seriesTagValuesBetween = (req, res) ->
|
||||
return
|
||||
|
||||
createCSVrow = (header, dataRow) ->
|
||||
i = header.indexOf(dataRow.vanityName)
|
||||
csvRow = dataRow.id.toString() + ',' + dataRow.dateAdded + ','
|
||||
i = header.indexOf(dataRow.name)
|
||||
csvRow = dataRow.id.toString() + ',' + dataRow.dtime + ','
|
||||
if i >= 0
|
||||
j = 2
|
||||
while j < header.length
|
||||
@@ -232,7 +232,7 @@ exports.allDataCSV = (req, res) ->
|
||||
sqlite3 = require('sqlite3').verbose()
|
||||
db = new (sqlite3.Database)(dbFile)
|
||||
db.serialize ->
|
||||
query = 'SELECT v.id, t.vanityName, v.val, v.dateAdded FROM tags t JOIN vals v ON t.id = v.tagID'
|
||||
query = 'SELECT v.id, t.name, v.val, v.dtime FROM tags t JOIN tag_vals v ON t.id = v.tagID'
|
||||
prepQuery = db.prepare(query)
|
||||
prepQuery.all (err, rows) ->
|
||||
prepQuery.finalize()
|
||||
@@ -251,12 +251,12 @@ exports.allDataCSV = (req, res) ->
|
||||
csvString = ''
|
||||
da = [
|
||||
'id'
|
||||
'DateAdded'
|
||||
'dtime'
|
||||
]
|
||||
tagVanityNames = tags.map((t) ->
|
||||
t.vanityName
|
||||
tagnames = tags.map((t) ->
|
||||
t.name
|
||||
)
|
||||
h = da.concat(tagVanityNames)
|
||||
h = da.concat(tagnames)
|
||||
console.log h
|
||||
csvString = csvString + h.join(',') + '\u000d'
|
||||
i = 0
|
||||
@@ -275,7 +275,7 @@ exports.seriesCSV = (req, res) ->
|
||||
sqlite3 = require('sqlite3').verbose()
|
||||
db = new (sqlite3.Database)(dbFile)
|
||||
db.serialize ->
|
||||
query = 'SELECT v.id, t.vanityName, v.val, v.dateAdded FROM tags t JOIN vals v ON t.id = v.tagID WHERE tagID = ? AND v.dateAdded > DATETIME(\'now\', \'-1 HOUR\')'
|
||||
query = 'SELECT v.id, t.name, v.val, v.dtime FROM tags t JOIN tag_vals v ON t.id = v.tagID WHERE tagID = ? AND v.dtime > DATETIME(\'now\', \'-1 HOUR\')'
|
||||
prepQuery = db.prepare(query)
|
||||
prepQuery.all parseInt(req.params.tag), (err, rows) ->
|
||||
prepQuery.finalize()
|
||||
@@ -290,15 +290,15 @@ exports.seriesCSV = (req, res) ->
|
||||
csvString = ''
|
||||
h = [
|
||||
'id'
|
||||
'DateAdded'
|
||||
rows[0].vanityName
|
||||
'dtime'
|
||||
rows[0].name
|
||||
]
|
||||
csvString = csvString + h.join(',') + '\u000d'
|
||||
i = 0
|
||||
while i < rows.length
|
||||
csvString = csvString + [
|
||||
rows[i].id
|
||||
rows[i].dateAdded
|
||||
rows[i].dtime
|
||||
rows[i].val
|
||||
].join(',') + '\u000d'
|
||||
i++
|
||||
@@ -313,7 +313,7 @@ exports.seriesCSVBetween = (req, res) ->
|
||||
sqlite3 = require('sqlite3').verbose()
|
||||
db = new (sqlite3.Database)(dbFile)
|
||||
db.serialize ->
|
||||
query = 'SELECT v.id, t.vanityName, v.val, v.dateAdded FROM tags t JOIN vals v ON t.id = v.tagID WHERE tagID = ? AND dateAdded >= DATETIME(?) AND dateAdded <= DATETIME(?)'
|
||||
query = 'SELECT v.id, t.name, v.val, v.dtime FROM tags t JOIN tag_vals v ON t.id = v.tagID WHERE tagID = ? AND dtime >= DATETIME(?) AND dtime <= DATETIME(?)'
|
||||
prepQuery = db.prepare(query)
|
||||
prepQuery.all parseInt(req.params.tag), dString_to_sqlite(req.params.startDatetime), dString_to_sqlite(req.params.endDatetime), (err, rows) ->
|
||||
prepQuery.finalize()
|
||||
@@ -328,15 +328,15 @@ exports.seriesCSVBetween = (req, res) ->
|
||||
csvString = ''
|
||||
h = [
|
||||
'id'
|
||||
'DateAdded'
|
||||
rows[0].vanityName
|
||||
'dtime'
|
||||
rows[0].name
|
||||
]
|
||||
csvString = csvString + h.join(',') + '\u000d'
|
||||
i = 0
|
||||
while i < rows.length
|
||||
csvString = csvString + [
|
||||
rows[i].id
|
||||
rows[i].dateAdded
|
||||
rows[i].dtime
|
||||
rows[i].val
|
||||
].join(',') + '\u000d'
|
||||
i++
|
||||
@@ -351,7 +351,7 @@ exports.latestValueSingleTag = (req, res) ->
|
||||
sqlite3 = require('sqlite3').verbose()
|
||||
db = new (sqlite3.Database)(dbFile)
|
||||
db.serialize ->
|
||||
query = 'SELECT * FROM vals WHERE id = (SELECT MAX(id) FROM vals WHERE tagID = ?)'
|
||||
query = 'SELECT * FROM tag_vals WHERE id = (SELECT MAX(id) FROM tag_vals WHERE tagID = ?)'
|
||||
prepQuery = db.prepare(query)
|
||||
prepQuery.all req.params.tag, (err, rows) ->
|
||||
console.log rows
|
||||
@@ -374,7 +374,7 @@ exports.latestValueAllTags = (req, res) ->
|
||||
sqlite3 = require('sqlite3').verbose()
|
||||
db = new (sqlite3.Database)(dbFile)
|
||||
db.serialize ->
|
||||
query = 'SELECT t.tagName as tagName, t.vanityName as vanityName, t.description as description, t.units as units, t.id as t_id, t.minExpected as min, t.maxExpected as max, MAX(v.id) as v_id, v.val as val, v.dateAdded as dtime FROM vals v JOIN tags t ON v.tagID = t.id WHERE t.deleted = 0 GROUP BY v.tagID'
|
||||
query = 'SELECT t.tag as tag, t.name as name, t.description as description, t.units as units, t.id as t_id, t.minExpected as min, t.maxExpected as max, MAX(v.id) as v_id, v.val as val, v.dtime as dtime FROM tag_vals v JOIN tags t ON v.tagID = t.id WHERE t.deleted = 0 GROUP BY v.tagID'
|
||||
prepQuery = db.prepare(query)
|
||||
prepQuery.all req.params.id, (err, rows) ->
|
||||
prepQuery.finalize()
|
||||
@@ -480,3 +480,38 @@ exports.updateSetup = (req, res) ->
|
||||
return
|
||||
return
|
||||
return
|
||||
|
||||
exports.clearValues = (req, res) ->
|
||||
sqlite3 = require('sqlite3').verbose()
|
||||
db = new (sqlite3.Database)(dbFile)
|
||||
if req.params.id
|
||||
db.serialize () ->
|
||||
query = 'DELETE FROM tag_vals WHERE tagID = ?;';
|
||||
prepQuery = db.prepare(query)
|
||||
prepQuery.run req.params.id, (err) ->
|
||||
prepQuery.finalize()
|
||||
db.close()
|
||||
if err
|
||||
res.json
|
||||
status: 'error'
|
||||
message: err
|
||||
console.log err
|
||||
else
|
||||
res.json
|
||||
status: 'OK'
|
||||
else
|
||||
db.serialize () ->
|
||||
query = 'DELETE FROM tag_vals WHERE id >= 0;';
|
||||
prepQuery = db.prepare(query)
|
||||
prepQuery.run req.params.id, (err) ->
|
||||
prepQuery.finalize()
|
||||
db.close()
|
||||
if err
|
||||
res.json
|
||||
status: 'error'
|
||||
message: err
|
||||
console.log err
|
||||
else
|
||||
res.json
|
||||
status: 'OK'
|
||||
undefined
|
||||
|
||||
@@ -1 +1,15 @@
|
||||
.topMargin40 {
|
||||
margin-top: 40px;
|
||||
}
|
||||
|
||||
.row-flex {
|
||||
display: -webkit-box;
|
||||
display: -webkit-flex;
|
||||
display: -ms-flexbox;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.row-flex > [class*='col-'] {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
@@ -200,6 +200,8 @@ tsCtrlrs.factory('tags',function($q, $http, $log){
|
||||
units: tag.units,
|
||||
minExpected: tag.minExpected,
|
||||
maxExpected: tag.maxExpected,
|
||||
guarantee_sec: tag.guarantee_sec,
|
||||
change_threshold: tag.change_threshold,
|
||||
description: tag.description
|
||||
}).success(function(data){
|
||||
return data;
|
||||
@@ -215,6 +217,8 @@ tsCtrlrs.factory('tags',function($q, $http, $log){
|
||||
units: tag.units,
|
||||
minExpected: tag.minExpected,
|
||||
maxExpected: tag.maxExpected,
|
||||
guarantee_sec: tag.guarantee_sec,
|
||||
change_threshold: tag.change_threshold,
|
||||
description: tag.description
|
||||
}).success(function(data){
|
||||
return data;
|
||||
@@ -234,6 +238,28 @@ tsCtrlrs.factory('tags',function($q, $http, $log){
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
var clearSingleTagData = function(id){
|
||||
var deferred = $q.defer();
|
||||
var url = '/json/clearDatabase/' + id;
|
||||
$http.get(url).success(function(data) {
|
||||
deferred.resolve({
|
||||
status: data.status
|
||||
});
|
||||
});
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
var clearAllTagData = function(){
|
||||
var deferred = $q.defer();
|
||||
var url = '/json/clearDatabase/all';
|
||||
$http.get(url).success(function(data) {
|
||||
deferred.resolve({
|
||||
status: data.status
|
||||
});
|
||||
});
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
return {
|
||||
getTag: getTag,
|
||||
getTagList: getTagList,
|
||||
@@ -243,6 +269,8 @@ tsCtrlrs.factory('tags',function($q, $http, $log){
|
||||
createTag: createTag,
|
||||
updateTag: updateTag,
|
||||
deleteTag: deleteTag,
|
||||
clearSingleTagData: clearSingleTagData,
|
||||
clearAllTagData: clearAllTagData
|
||||
};
|
||||
});
|
||||
|
||||
@@ -396,6 +424,32 @@ tsCtrlrs.controller('tagsCtrl', function($scope, $route, $http, $routeParams, Pa
|
||||
});
|
||||
};
|
||||
|
||||
$scope.openClearTagData = function(id){
|
||||
var getTag = tags.getTag(id);
|
||||
getTag.then(function(data){
|
||||
if (data.status == "OK"){
|
||||
$scope.error = false;
|
||||
$scope.dTagValues = data.tag;
|
||||
$log.info("Thinking about deleting tag data with parameters: "+ JSON.stringify($scope.dTagValues));
|
||||
} else {
|
||||
$scope.error = data.message;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
$scope.deleteTagValues = function(id){
|
||||
var clearSingleTagData = tags.clearSingleTagData(id);
|
||||
clearSingleTagData.then(function(data){
|
||||
$log.info("deleting tag "+ id + " status: " + data.status);
|
||||
if (data.status == "OK"){
|
||||
$scope.error = false;
|
||||
$scope.loadTagList();
|
||||
} else {
|
||||
$scope.error = data.message;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
$scope.openEditTag = function(id){
|
||||
var getTag = tags.getTag(id);
|
||||
getTag.then(function(data){
|
||||
|
||||
@@ -32,19 +32,31 @@
|
||||
<div ng-if="!error" class="container">
|
||||
<div class="row" style="margin-bottom:20px;">
|
||||
<div class="col-md-12">
|
||||
<h1>{{tag.vanityName}}</h1>
|
||||
<span class="timeLabel">From: </span><quick-datepicker ng-model='startDatetime'></quick-datepicker> <span class="timeLabel">To: </span><quick-datepicker ng-model='endDatetime'></quick-datepicker> <button ng-click="loadTagVals(startDatetime, endDatetime)" class="btn btn-large btn-success padMe"><i class="fa fa-refresh"></i> Reload Values</button>
|
||||
<h1>{{tag.name}}</h1>
|
||||
<div class="row row-flex">
|
||||
<div class="col-md-4">
|
||||
<span class="timeLabel">From: </span><quick-datepicker ng-model='startDatetime'></quick-datepicker>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<span class="timeLabel">To: </span><quick-datepicker ng-model='endDatetime'></quick-datepicker>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<button ng-click="loadTagVals(startDatetime, endDatetime)" class="btn btn-large btn-success padMe"><i class="fa fa-refresh"></i> Reload Values</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
|
||||
<a href="/json/CSV/{{tag.id}}/{{startDatetime | dString}}/{{endDatetime | dString }}" class="btn btn-large btn-primary padMe"><i class="fa fa-download"></i> Download Data</a>
|
||||
<div class="col-md-8">
|
||||
<div class="col-md-12">
|
||||
<div class="tagChart" style="height:400px;">
|
||||
<linechart data="data" options="options"></linechart>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
</div>
|
||||
|
||||
<div class="row topMargin40">
|
||||
<div class="col-md-6">
|
||||
<a href="/json/CSV/{{tag.id}}/{{startDatetime | dString}}/{{endDatetime | dString }}" class="btn btn-large btn-primary padMe"><i class="fa fa-download"></i> Download Data</a>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
|
||||
@@ -1,109 +1,166 @@
|
||||
<div class="modal fade" id="addModal">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<h4 class="modal-title">Add a New Tag...</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="well" ng-if="message"><h3 class="text-danger">{{message}}</h3></div>
|
||||
<form>
|
||||
<div class="form-group">
|
||||
<label for="tag">Tag</label>
|
||||
<input type="text" ng-model="newTag.tag" class="form-control" id="tag" placeholder="Tag Name Here">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="name">Name</label>
|
||||
<input type="text" ng-model="newTag.name" class="form-control" id="name" placeholder="Vanity Name Here">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="description">Description</label>
|
||||
<input type="textarea" ng-model="newTag.description" class="form-control" id="description" placeholder="Tag Description Here">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="minExpected">Minimum Value Expected</label>
|
||||
<input type="number" ng-model="newTag.minExpected" class="form-control" id="minExpected" placeholder="0">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="maxExpected">Maximum Value Expected</label>
|
||||
<input type="number" ng-model="newTag.maxExpected" class="form-control" id="maxExpected" placeholder="100">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="units">Units</label>
|
||||
<input type="text" ng-model="newTag.units" class="form-control" id="units" placeholder="lbs, PSI, in, etc.">
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
|
||||
<button type="button" class="btn btn-primary" ng-click="submitAddTag();" data-dismiss="modal">Add Tag</button>
|
||||
</div>
|
||||
</div><!-- /.modal-content -->
|
||||
</div><!-- /.modal-dialog -->
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<h4 class="modal-title">Add a New Tag...</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="well" ng-if="message"><h3 class="text-danger">{{message}}</h3></div>
|
||||
<form>
|
||||
<div class="form-group">
|
||||
<label for="tag">Tag</label>
|
||||
<input type="text" ng-model="newTag.tag" class="form-control" id="tag" placeholder="Tag Name Here">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="name">Name</label>
|
||||
<input type="text" ng-model="newTag.name" class="form-control" id="name" placeholder="Vanity Name Here">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="description">Description</label>
|
||||
<input type="textarea" ng-model="newTag.description" class="form-control" id="description" placeholder="Tag Description Here">
|
||||
</div>
|
||||
<div class="col-md-5">
|
||||
<div class="form-group">
|
||||
<label for="minExpected">Minimum Value Expected</label>
|
||||
<input type="number" ng-model="newTag.minExpected" class="form-control" id="minExpected" placeholder="0">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-5">
|
||||
<div class="form-group">
|
||||
<label for="maxExpected">Maximum Value Expected</label>
|
||||
<input type="number" ng-model="newTag.maxExpected" class="form-control" id="maxExpected" placeholder="100">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-2">
|
||||
<div class="form-group">
|
||||
<label for="units">Units</label>
|
||||
<input type="text" ng-model="newTag.units" class="form-control" id="units" placeholder="lbs, PSI, in, etc.">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="form-group">
|
||||
<label for="change_threshold">Change Threshold</label>
|
||||
<input type="number" ng-model="newTag.change_threshold" class="form-control" id="change_threshold" placeholder="0">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="form-group">
|
||||
<label for="guarantee_sec">Guarantee Sec.</label>
|
||||
<input type="number" ng-model="newTag.guarantee_sec" class="form-control" id="guarantee_sec" placeholder="3600">
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
|
||||
<button type="button" class="btn btn-primary" ng-click="submitAddTag();" data-dismiss="modal">Add Tag</button>
|
||||
</div>
|
||||
</div><!-- /.modal-content -->
|
||||
</div><!-- /.modal-dialog -->
|
||||
</div><!-- /.modal -->
|
||||
|
||||
|
||||
<div class="modal fade" id="editModal">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<h4 class="modal-title">Edit a Tag...</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="well" ng-if="message"><h3 class="text-danger">{{message}}</h3></div>
|
||||
<form>
|
||||
<div class="form-group">
|
||||
<label for="tag">Tag Name</label>
|
||||
<input type="text" ng-model="editTag.tag" class="form-control" id="tag" placeholder="Tag Name Here">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="name">Vanity Name</label>
|
||||
<input type="text" ng-model="editTag.name" class="form-control" id="name" placeholder="Vanity Name Here">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="description">Description</label>
|
||||
<input type="textarea" ng-model="editTag.description" class="form-control" id="description" placeholder="Tag Description Here">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="minExpected">Minimum Value Expected</label>
|
||||
<input type="number" ng-model="editTag.minExpected" class="form-control" id="minExpected" placeholder="0">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="maxExpected">Maximum Value Expected</label>
|
||||
<input type="number" ng-model="editTag.maxExpected" class="form-control" id="maxExpected" placeholder="100">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="units">Units</label>
|
||||
<input type="text" ng-model="editTag.units" class="form-control" id="units" placeholder="lbs, PSI, in, etc.">
|
||||
</div>
|
||||
</form>
|
||||
<pre>{{editTag}}</pre>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
|
||||
<button type="button" class="btn btn-primary" ng-click="submitEditTag();" data-dismiss="modal">Submit Tag Edits</button>
|
||||
</div>
|
||||
</div><!-- /.modal-content -->
|
||||
</div><!-- /.modal-dialog -->
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<h4 class="modal-title">Edit a Tag...</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="well" ng-if="message"><h3 class="text-danger">{{message}}</h3></div>
|
||||
<form>
|
||||
<div class="form-group">
|
||||
<label for="tag">Tag Name</label>
|
||||
<input type="text" ng-model="editTag.tag" class="form-control" id="tag" placeholder="Tag Name Here">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="name">Vanity Name</label>
|
||||
<input type="text" ng-model="editTag.name" class="form-control" id="name" placeholder="Vanity Name Here">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="description">Description</label>
|
||||
<input type="textarea" ng-model="editTag.description" class="form-control" id="description" placeholder="Tag Description Here">
|
||||
</div>
|
||||
<div class="col-md-5">
|
||||
<div class="form-group">
|
||||
<label for="minExpected">Minimum Value Expected</label>
|
||||
<input type="number" ng-model="editTag.minExpected" class="form-control" id="minExpected" placeholder="0">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-5">
|
||||
<div class="form-group">
|
||||
<label for="maxExpected">Maximum Value Expected</label>
|
||||
<input type="number" ng-model="editTag.maxExpected" class="form-control" id="maxExpected" placeholder="100">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-2">
|
||||
<div class="form-group">
|
||||
<label for="units">Units</label>
|
||||
<input type="text" ng-model="editTag.units" class="form-control" id="units" placeholder="lbs, PSI, in, etc.">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="form-group">
|
||||
<label for="change_threshold">Change Threshold</label>
|
||||
<input type="number" ng-model="editTag.change_threshold" class="form-control" id="change_threshold" placeholder="0">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="form-group">
|
||||
<label for="guarantee_sec">Guarantee Sec.</label>
|
||||
<input type="number" ng-model="editTag.guarantee_sec" class="form-control" id="guarantee_sec" placeholder="3600">
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<pre>{{editTag}}</pre>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
|
||||
<button type="button" class="btn btn-primary" ng-click="submitEditTag();" data-dismiss="modal">Submit Tag Edits</button>
|
||||
</div>
|
||||
</div><!-- /.modal-content -->
|
||||
</div><!-- /.modal-dialog -->
|
||||
</div><!-- /.modal -->
|
||||
|
||||
|
||||
<div class="modal fade" id="deleteModal">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<h4 class="modal-title">Are you sure?</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="well" ng-if="message"><h3 class="text-danger">{{message}}</h3></div>
|
||||
<h3>Are you sure you want to delete the tag {{dTag.name}} ({{dTag.tag}})?</h3>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-primary" data-dismiss="modal">NO!!!!!!</button>
|
||||
<button type="button" class="btn btn-danger" ng-click="deleteTag(dTag.id);" data-dismiss="modal">Heck yes, delete it!</button>
|
||||
</div>
|
||||
</div><!-- /.modal-content -->
|
||||
</div><!-- /.modal-dialog -->
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<h4 class="modal-title">Are you sure?</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="well" ng-if="message"><h3 class="text-danger">{{message}}</h3></div>
|
||||
<h3>Are you sure you want to delete the tag {{dTag.name}} ({{dTag.tag}})?</h3>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-primary" data-dismiss="modal">NO!!!!!!</button>
|
||||
<button type="button" class="btn btn-danger" ng-click="deleteTag(dTag.id);" data-dismiss="modal">Heck yes, delete it!</button>
|
||||
</div>
|
||||
</div><!-- /.modal-content -->
|
||||
</div><!-- /.modal-dialog -->
|
||||
</div><!-- /.modal -->
|
||||
|
||||
<div class="modal fade" id="clearTagDataModal">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<h4 class="modal-title">Are you sure?</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="well" ng-if="message"><h3 class="text-danger">{{message}}</h3></div>
|
||||
<h3>Are you sure you want to delete the data for tag {{dTagValues.name}} ({{dTagValues.tag}})?</h3>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-primary" data-dismiss="modal">NO!!!!!!</button>
|
||||
<button type="button" class="btn btn-danger" ng-click="deleteTagValues(dTagValues.id);" data-dismiss="modal">Heck yes, delete it!</button>
|
||||
</div>
|
||||
</div><!-- /.modal-content -->
|
||||
</div><!-- /.modal-dialog -->
|
||||
</div><!-- /.modal -->
|
||||
|
||||
|
||||
@@ -140,6 +197,7 @@
|
||||
<td>Max Expected Value</td>
|
||||
<td>Units</td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
</thead>
|
||||
@@ -151,6 +209,7 @@
|
||||
<td>{{tag.maxExpected}}</td>
|
||||
<td>{{tag.units}}</td>
|
||||
<td><button data-toggle="modal" data-target="#editModal" ng-click="openEditTag(tag.id)" class="btn btn-primary">Edit</button></td>
|
||||
<td><button data-toggle="modal" data-target="#clearTagDataModal" ng-click="openClearTagData(tag.id)" class="btn btn-primary">Clear Data</button></td>
|
||||
<td><button data-toggle="modal" data-target="#deleteModal" ng-click="openDeleteTag(tag.id)" class="btn btn-danger">Delete</button></td>
|
||||
<script>
|
||||
$(document).ready(function(){
|
||||
|
||||
Reference in New Issue
Block a user