reading tags from mysql and writing values to mysql

This commit is contained in:
Patrick McDonagh
2016-04-07 17:06:55 -05:00
parent f0f94aaa0f
commit 528b62bcbb
12 changed files with 64 additions and 28 deletions

View File

@@ -11,4 +11,4 @@ sS'user'
p5
S'website'
p6
s.
s.

View File

@@ -1 +1 @@
pycomm.ab_comm.clx WARNING 2016-01-25 14:45:25,488 (5, 'forward_close returned False')
pycomm.cip.cip_base WARNING 2016-04-07 16:14:59,861 (5, 'forward_close returned False')

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -13,6 +13,10 @@ with open('mysql_cfg.pickle', 'rb') as cfgFile:
con = mysqlcon.connect(**mysql_cfg)
PLC_IP_ADDRESS = "10.10.10.3"
def readTag(addr, tag):
time.sleep(0.01)
c = ClxDriver()
@@ -25,17 +29,17 @@ def readTag(addr, tag):
print("ERROR RETRIEVING TAG: {}".format(tag))
err = c.get_status()
c.close()
print err
print traceback.print_exc()
pass
c.close()
class Tag():
global readTag, con
global readTag, con, PLC_IP_ADDRESS
def __init__(self, name, tag, data_type, change_threshold, guarantee_sec, mapFn=None, plc_type='CLK'):
self.name = name
self.tag = tag
self.data_type = data_type
def __init__(self, name, tag, data_type, change_threshold, guarantee_sec, mapFn=None, plc_type='CLX'):
self.name = str(name)
self.tag = str(tag)
self.data_type = str(data_type)
self.value = None
self.last_value = None
self.guarantee_sec = guarantee_sec
@@ -50,7 +54,7 @@ class Tag():
def read(self, forceSend):
writeToDB = False
if self.tag:
v = readFn(PLC_IP_ADDRESS, self.tag)
v = self.readFn(PLC_IP_ADDRESS, self.tag)
if v:
if self.data_type == 'BOOL' or self.data_type == 'STRING':
val = v[0]
@@ -73,7 +77,9 @@ class Tag():
self.sendToDB()
def sendToDB(self):
query = "INSERT INTO tag_vals (dtime, name, val) VALUES ({}, '{}', {})".format(time.time(), self.name, self.value)
# TODO: Datetime
query = "INSERT INTO poconsole.tag_vals (dtime, name, val) VALUES ('{}', '{}', {})".format(time.strftime('%Y-%m-%d %H:%M:%S'), self.name, self.value)
self.last_send_time = time.time()
print query
# TODO: CHECK ON THIS LOGIC -- with con:
cur = con.cursor()

View File

@@ -10,6 +10,7 @@ import mysql.connector as mysqlcon
import pickle
from tag_mysql import Tag
import traceback
import time
with open('mysql_cfg.pickle', 'rb') as pickleconfig:
mysql_cfg = pickle.load(pickleconfig)
@@ -22,14 +23,15 @@ tag_store = {}
def main():
db.connect()
cur = db.cursor()
query = "SELECT * FROM poconsole.tags"
query = "SELECT * FROM poconsole.tags WHERE class = 5 AND deleted = 0"
cur.execute(query)
tags = cur.fetchall()
#(1, u'card_type', 1, u'Card_Past[1].Card_Type', u'STRING', 0.0, 3600, u'card_type_map')
print tags
# [(1, u'Century Counter Up', 5, u'Century_Counter_Up', u'REAL', 10.0, 3600, None, 0)]
db.disconnect()
for t in tags:
tag_store[t[1]] = Tag(t[1], t[2], t[3], t[4], t[5], mapFn=t[6])
tag_store[t[1]] = Tag(t[1], t[3], t[4], t[5], t[6], mapFn=t[7])
PLC_IP_ADDRESS = "10.10.10.3" # MAKE THIS A db VALUE

View File

@@ -1,18 +1,46 @@
CREATE DATABASE TagData;
CREATE TABLE `TagData`.`tags` (
`id` INT NOT NULL AUTO_INCREMENT,
`tagName` VARCHAR(128) NULL,
`dateAdded` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`units` VARCHAR(16) NULL,
`deleted` INT NULL DEFAULT 0,
PRIMARY KEY (`id`));
CREATE DATABASE poconsole;
CREATE TABLE IF NOT EXISTS poconsole.tag_classes(
id int(11) NOT NULL AUTO_INCREMENT,
tag_class varchar(64),
description varchar(64),
PRIMARY KEY (id)
);
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`));
CREATE TABLE IF NOT EXISTS poconsole.tags(
id int(11) NOT NULL AUTO_INCREMENT,
name TEXT,
class int(11),
tag varchar(128),
data_type varchar(32),
change_threshold float,
guarantee_sec integer(11),
map_function varchar(64),
deleted INT NULL DEFAULT 0,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS poconsole.tag_vals(
id int(11) NOT NULL AUTO_INCREMENT,
dtime datetime,
name varchar(128),
val float,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS poconsole.config (
id INT NOT NULL AUTO_INCREMENT,
parameter varchar(128),
val varchar(128),
dateAdded TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
INSERT INTO poconsole.tag_classes (id, tag_class, description) VALUES (1, 'stroke', 'Stroke Information');
INSERT INTO poconsole.tag_classes (id, tag_class, description) VALUES (2, 'history', 'Historical Data');
INSERT INTO poconsole.tag_classes (id, tag_class, description) VALUES (3, 'gaugeoff', 'Gauge Off Data');
INSERT INTO poconsole.tag_classes (id, tag_class, description) VALUES (4, 'welltest', 'Well Test Data');
INSERT INTO poconsole.tag_classes (id, tag_class, description) VALUES (5, 'custom', 'Custom tags');
CREATE USER 'website'@'localhost' IDENTIFIED BY 'henrypump';
GRANT ALL ON *.* TO 'website'@'localhost';
@@ -20,4 +48,4 @@ 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;
FLUSH PRIVILEGES;