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