Changed from solar to generic tag server
This commit is contained in:
67
python/tagserver.py
Normal file
67
python/tagserver.py
Normal file
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
'''
|
||||
Created on Dec 8, 2015
|
||||
|
||||
@author: Patrick McDonagh
|
||||
'''
|
||||
|
||||
|
||||
from datetime import datetime
|
||||
import sys
|
||||
from random import randint
|
||||
import time
|
||||
import MySQLdb
|
||||
import tuxeip
|
||||
|
||||
|
||||
#TUXEIP Connection to PLC
|
||||
from tuxeip import TuxEIP, LGX, LGX_REAL
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
db = MySQLdb.connect(host="127.0.0.1",user="website",passwd="henrypump",db="SolarData")
|
||||
cur = db.cursor()
|
||||
query = "SELECT * FROM SolarData.tags WHERE deleted = 0;"
|
||||
cur.execute(query)
|
||||
tags = cur.fetchall()
|
||||
# ((1L, 'DC_Bus_Voltage', datetime.datetime(2015, 12, 8, 16, 2, 32), 'V', 0L), (2L, 'Output_Frequency', datetime.datetime(2015, 12, 8, 16, 31, 12), 'Hz', 0L))
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
PLC_IP_ADDRESS = "192.168.1.10" # MAKE THIS A db VALUE
|
||||
|
||||
tagList = [];
|
||||
if len(tags) > 0:
|
||||
for t in tags:
|
||||
tagList.append({"id":int(t[0]), "name":t[1], "val":None, "lastVal":None});
|
||||
|
||||
try:
|
||||
tux = TuxEIP(libpath="/usr/lib/libtuxeip.so")
|
||||
sess = tux.OpenSession(PLC_IP_ADDRESS)
|
||||
reg = tux.RegisterSession(sess)
|
||||
conn = tux.ConnectPLCOverCNET(sess, LGX, 1, 100, 123, randint(0,9999), 123, 321, 100, 5000, 1, '01')
|
||||
|
||||
while True:
|
||||
for r in tagList:
|
||||
r["val"] = tux.ReadLGXDataAsFloat(sess, conn, r['name'], 1)[0]
|
||||
print("{0} - {1}".format(r["name"], r["val"]))
|
||||
if not r["val"] == r["lastVal"]:
|
||||
db = MySQLdb.connect(host="127.0.0.1",user="website",passwd="henrypump",db="SolarData")
|
||||
cur = db.cursor()
|
||||
aQuery = """INSERT INTO SolarData.values (tagID, val) VALUES ('%d', '%f');"""%(r["id"], float(r["val"]))
|
||||
print(aQuery)
|
||||
storeVal = cur.execute(aQuery)
|
||||
db.commit()
|
||||
db.close()
|
||||
r["lastVal"] = r["val"]
|
||||
|
||||
time.sleep(10)
|
||||
except Exception as err:
|
||||
print err
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -1,5 +1,5 @@
|
||||
CREATE DATABASE SolarData;
|
||||
CREATE TABLE `SolarData`.`tags` (
|
||||
CREATE DATABASE TagData;
|
||||
CREATE TABLE `TagData`.`tags` (
|
||||
`id` INT NOT NULL AUTO_INCREMENT,
|
||||
`tagName` VARCHAR(128) NULL,
|
||||
`dateAdded` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
@@ -7,9 +7,17 @@ CREATE TABLE `SolarData`.`tags` (
|
||||
`deleted` INT NULL DEFAULT 0,
|
||||
PRIMARY KEY (`id`));
|
||||
|
||||
CREATE TABLE `SolarData`.`values` (
|
||||
CREATE TABLE `TagData`.`values` (
|
||||
`id` INT NOT NULL AUTO_INCREMENT,
|
||||
`tagID` INT NULL,
|
||||
`val` FLOAT NULL,
|
||||
`dateAdded` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`));
|
||||
PRIMARY KEY (`id`));
|
||||
|
||||
CREATE USER 'website'@'localhost' IDENTIFIED BY 'henrypump';
|
||||
GRANT ALL ON *.* TO 'website'@'localhost';
|
||||
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'henrypump';
|
||||
GRANT ALL ON *.* to 'admin'@'localhost';
|
||||
CREATE USER 'admin'@'%' IDENTIFIED BY 'henrypump';
|
||||
GRANT ALL ON *.* to 'admin'@'%';
|
||||
FLUSH PRIVILEGES;
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
|
||||
var getScanList = function(sl){
|
||||
var query = "SELECT * FROM SolarData.tags WHERE deleted = 0;";
|
||||
var query = "SELECT * FROM TagData.tags WHERE deleted = 0;";
|
||||
|
||||
};
|
||||
|
||||
@@ -16,10 +16,11 @@ exports.addTag = function(req, res){
|
||||
tagName: req.params.tagName,
|
||||
units: req.params.units,
|
||||
};
|
||||
var query = "INSERT INTO SolarData.tags SET ?";
|
||||
var query = "INSERT INTO TagData.tags SET ?";
|
||||
req.app.locals.db.query(query, vals, function(err, rows, fields) {
|
||||
if (err) {
|
||||
res.json({status:"error", message:err});
|
||||
console.log(err);
|
||||
} else {
|
||||
res.json({status:"success"});
|
||||
}
|
||||
@@ -27,10 +28,11 @@ exports.addTag = function(req, res){
|
||||
};
|
||||
|
||||
exports.removeTag = function(req, res){
|
||||
var query = "UPDATE SolarData.tags SET deleted = 1 WHERE id = " + parseInt(req.params.tag) + ";";
|
||||
var query = "UPDATE TagData.tags SET deleted = 1 WHERE id = " + parseInt(req.params.tag) + ";";
|
||||
req.app.locals.db.query(query, function(err, rows, fields) {
|
||||
if (err) {
|
||||
res.json({status:"error", message:err});
|
||||
console.log(err);
|
||||
} else {
|
||||
res.json({status:"success"});
|
||||
}
|
||||
@@ -38,12 +40,13 @@ exports.removeTag = function(req, res){
|
||||
};
|
||||
|
||||
exports.latestTagValue = function(req, res){
|
||||
var query = "SELECT * FROM SolarData.values WHERE id = (SELECT MAX(id) FROM SolarData.values WHERE tagID = (SELECT id FROM SolarData.tags WHERE tagName = '" + req.params.tag + "'));";
|
||||
var query = "SELECT * FROM TagData.values WHERE id = (SELECT MAX(id) FROM TagData.values WHERE tagID = (SELECT id FROM TagData.tags WHERE tagName = '" + req.params.tag + "'));";
|
||||
req.app.locals.db.query(query, function(err, rows, fields) {
|
||||
if (err) {
|
||||
res.json({status:"error", message:err});
|
||||
console.log(err);
|
||||
} else {
|
||||
res.json({tag_val:rows[0]});
|
||||
res.json({status:"success", tag_val:rows[0]});
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -53,12 +56,13 @@ exports.seriesTagValues = function(req, res){
|
||||
};
|
||||
|
||||
exports.allTags = function(req, res){
|
||||
var query = "SELECT * FROM SolarData.tags WHERE deleted = 0;";
|
||||
var query = "SELECT * FROM TagData.tags WHERE deleted = 0;";
|
||||
req.app.locals.db.query(query, function(err, rows, fields) {
|
||||
if (err) {
|
||||
res.json({status:"error", message:err});
|
||||
console.log(err);
|
||||
} else {
|
||||
res.json({tags:rows});
|
||||
res.json({status:"success", tags:rows});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user