Moved files around, started using Tag submodule

This commit is contained in:
Patrick McDonagh
2016-04-19 17:25:16 -05:00
parent cddab5d10a
commit 921fed5c08
4 changed files with 11 additions and 138 deletions

View File

@@ -1,11 +1,10 @@
CREATE TABLE IF NOT EXISTS Event_List (
id INTEGER PRIMARY KEY,
device_name TEXT,
alarmID INTEGER,
type TEXT,
cond TEXT,
value REAL,
datetime TIMESTAMP,
tag TEXT,
stroke_number INTEGER
);

View File

@@ -2,12 +2,11 @@ CREATE DATABASE IF NOT EXISTS poconsole;
CREATE TABLE IF NOT EXISTS poconsole.Event_List (
id int(11) NOT NULL AUTO_INCREMENT,
device_name varchar(64),
alarmID int(11),
type varchar(64),
cond varchar(64),
value float,
datetime datetime,
tag varchar(64),
stroke_number int(11),
PRIMARY KEY (id)
);

View File

@@ -3,7 +3,7 @@
'''
Created on Oct 1, 2014
@author: PJMcdona
@author: Patrick McDonagh
'''
# import csv
@@ -12,6 +12,7 @@ import time
import sqlite3 as lite
from pycomm.ab_comm.clx import Driver as ClxDriver
# import logging
from tag.tag_sqlite import Tag
import traceback
con = lite.connect("/mnt/usb/data.db")
@@ -32,11 +33,6 @@ def readConfig():
def readTag(addr, tag):
# logging.basicConfig(
# filename="clx.log",
# format="%(levelname)-10s %(asctime)s %(message)s",
# level=logging.DEBUG
# )
time.sleep(0.01)
c = ClxDriver()
if c.open(addr):
@@ -48,7 +44,7 @@ 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()
@@ -91,58 +87,7 @@ def checkDateInDB(da):
con.commit()
class Tag():
global readTag, con
def __init__(self, name, tag, data_type, change_threshold, guarantee_sec, db_id, mapFn=None):
self.name = name
self.tag = tag
self.data_type = data_type
self.value = None
self.last_value = None
self.guarantee_sec = guarantee_sec
self.chg_threshold = change_threshold
self.last_send_time = 0
self.mapFn = mapFn
self.db_id = db_id
def read(self, forceSend):
time.sleep(0.01)
if self.tag:
v = readTag(PLC_IP_ADDRESS, self.tag)
if v:
if self.data_type == 'BOOL' or self.data_type == 'STRING':
val = v[0]
if self.mapFn:
val = self.mapFn[val]
if (self.last_send_time == 0) or (self.value is None) or not (self.value == val) or ((time.time() - self.last_send_time) > self.guarantee_sec) or (forceSend):
self.last_value = self.value
self.value = val
return True
else:
return False
else:
if (self.last_send_time == 0) or (self.value is None) or (abs(self.value - v[0]) > self.chg_threshold) or ((time.time() - self.last_send_time) > self.guarantee_sec) or (forceSend):
self.last_value = self.value
self.value = v[0]
return True
else:
return False
else:
return False
return False
def sendToDB(self):
query = "INSERT INTO tag_vals (dtime, tagID, val) VALUES ({}, {}, {})".format(time.time(), self.db_id, self.value)
print query
with con:
cur = con.cursor()
cur.execute(query)
con.commit()
class Status(Tag):
def sendToDB(self):
query = "INSERT INTO run_status (dtime, status) VALUES ({}, '{}')".format(time.time(), self.value)
print query
@@ -151,72 +96,6 @@ class Status(Tag):
cur.execute(query)
con.commit()
self.last_send_time = time.time()
class AnalogAlarm():
def __init__(self, name, tag):
self.name = name
self.tag = tag
self.alarm = False
self.warning = False
self.lastAlarmCheckVal = False
self.lastWarningCheckVal = False
def checkStatus(self, stroke_number):
global readTag, PLC_IP_ADDRESS, maps, con
condition = ''
self.alarm = readTag(PLC_IP_ADDRESS, '{}.Alarm'.format(self.tag))[0] > 0
alarmChanged = not (self.alarm == self.lastAlarmCheckVal)
self.warning = readTag(PLC_IP_ADDRESS, '{}.Warning'.format(self.tag))[0] > 0
warningChanged = not (self.warning == self.lastWarningCheckVal)
if (alarmChanged and self.alarm) or (warningChanged and self.warning):
condition = maps['conditionMap'][readTag(PLC_IP_ADDRESS, '{}.Alarm_Code'.format(self.tag))[0]]
value = readTag(PLC_IP_ADDRESS, '{}.Alarm_Value'.format(self.tag))[0]
triggerType = "Alarm"
if warningChanged:
triggerType = 'Warning'
iQuery = "INSERT INTO Event_List (device_name, type, cond, value, datetime, tag, stroke_number) VALUES ('{0}', '{1}', '{2}', {3}, '{4}', '{5}', {6});".format(
self.name, triggerType, condition, value, time.time(), self.tag, stroke_number)
print iQuery
with con:
cur = con.cursor()
cur.execute(iQuery)
con.commit()
if warningChanged:
self.lastWarningCheckVal = self.warning
if alarmChanged:
self.lastAlarmCheckVal = self.alarm
class bitAlarm():
def __init__(self, name, tag, condition):
self.name = name
self.tag = tag
self.condition = condition
self.status = False
self.lastStatusCheckVal = False
def checkStatus(self, stroke_number):
global readTag, PLC_IP_ADDRESS, con
self.status = readTag(PLC_IP_ADDRESS, self.tag)[0] > 0
statusChanged = not (self.status == self.lastStatusCheckVal)
if statusChanged and self.status:
value = readTag(PLC_IP_ADDRESS, '{}.Alarm_Value'.format(self.tag))[0]
iQuery = "INSERT INTO Event_List (device_name, type, cond, value, datetime, tag, stroke_number) VALUES ('{0}', '{1}', '{2}', {3}, '{4}', '{5}', {6});".format(
self.name, 'Info', self.condition, 0.0, time.time(), self.tag, stroke_number)
print iQuery
with con:
cur = con.cursor()
cur.execute(iQuery)
con.commit()
if statusChanged:
self.lastStatusCheckVal = self.status
@@ -303,15 +182,15 @@ def setupTags():
with con:
cur = con.cursor()
query = "SELECT c.alarm_class as class, a.name as name, a.tag as tag, a.condition as condition FROM alarms a JOIN alarm_classes c ON a.class = c.id;"
query = "SELECT c.alarm_class as class, a.name as name, a.tag as tag, a.condition as condition, t.id as id FROM alarms a JOIN alarm_classes c ON a.class = c.id;"
cur.execute(query)
alarms = cur.fetchall()
for x in alarms:
# 0: class, 1: name, 2: tag, 3: condition
if str(x[0]) == 'analog':
safety_tags[x[1]] = AnalogAlarm(str(x[1]), str(x[2]))
safety_tags[x[1]] = AnalogAlarm(str(x[1]), str(x[2]), int(x[4]), device_type="CLX", ip_address=PLC_IP_ADDRESS)
elif str(x[0]) == 'bit':
bit_tags[x[1]] = bitAlarm(str(x[1]), str(x[2]), str(x[3]))
bit_tags[x[1]] = bitAlarm(str(x[1]), str(x[2]), str(x[3]), int(x[4]), device_type="CLX", ip_address=PLC_IP_ADDRESS)
print('===== STROKE TAGS =====')
for t in stroke_tags:
@@ -394,15 +273,13 @@ def main():
while True:
try:
if status.read(False):
status.sendToDB()
status.read("test")
#############
# CARD DATA #
#############
EOS = readTag(PLC_IP_ADDRESS, "End_Of_Stroke")[0]
stroke_tags['card_id'].read(False)
stroke_tags['card_id'].read('test')
if (EOS and not (last_stroke == stroke_tags['card_id'].value)):
sData = {}
last_stroke = stroke_tags['card_id'].value
@@ -442,9 +319,7 @@ def main():
for hist in history_tags:
h = history_tags[hist]
if h.read(False):
h.sendToDB()
h.last_send_time = time.time()
h.read("test")
##############
# TAPER DATA #