diff --git a/alarmLogger.py b/alarmLogger.py index 1bcdce3..518ca56 100644 --- a/alarmLogger.py +++ b/alarmLogger.py @@ -1,211 +1,211 @@ -#!/usr/bin/env python - -''' -Created on Oct 1, 2014 - -@author: PJMcdona -''' - -from datetime import datetime -from random import randint -from time import sleep -import sys - -# MYSQL Config -import MySQLdb - -db = MySQLdb.connect(host="127.0.0.1", user="website", passwd="henrypump", db="WellData") -cur = db.cursor() -query = "SELECT * FROM WellData.config ORDER BY dateChanged DESC LIMIT 1;" -cur.execute(query) -setup = cur.fetchall() -db.commit() -db.close() - -PLC_IP_ADDRESS = setup[0][2] -PLC_TYPE = setup[0][1] - -sys.path.append("../") - - -path_to_CSV = "/mnt/usb/" - -# TUXEIP Connection to PLC -# TUXEIP Connection to PLC -from tuxeip import TuxEIP, LGX, LGX_REAL - -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') - - - -safetySetup = [["Safety_Casing_Pressure", "Casing Pressure"], - ["Safety_Flow_Line_Pressure", "Flow Line Pressure"], - ["Safety_Flowmeter", "Flowmeter"], - ["Safety_Fluid_Load", "Fluid Load"], - ["Safety_Inclinometer", "Inclinometer"], - ["Safety_Load_HiHi", "Load HiHi"], - ["Safety_Load_Hi", "Load Hi"], - ["Safety_Load_Lo", "Load Lo"], - ["Safety_Load_LoLo", "Load LoLo"], - ["Safety_Speed", "Speed"], - ["Safety_Tubing_Pressure", "Tubing Pressure"] - ] - -condition = { - 20: "Low", - 21: "High", - 24: "LoLo", - 25: "HiHi", - 32: "Input Failure", - 34: "Configuration Error", - 16: "Failure to Stop", - 17: "Failure to Start", - 18: "Drive Fault" -} - -bitsSetup = [ - ["Pump.Auto_Stop", "Pump Off (Auto Mode)", "Unit Stop"], - ["Pump.POC_Stop", "Pump Off (POC Mode)", "Unit Stop"], - ["Pump.Timed_Stop", "Pump Off (Timer Mode)", "Unit Stop"], - ["Pump.Stop", "User Initiated Stop", "Unit Stop"], - ["PeakEnergy.Stop", "Peak Energy Stop", "Unit Stop"], - ["Pump.Start", "User Initiated Start", "Unit Start"], - ["Pump.POC_Restart", "Restart (POC Mode)", "Unit Start"], - ["Pump.Timed_Restart", "Restart (Timer Mode)", "Unit Start"], - ["Pump.Auto_Restart", "Restart (Auto Mode)", "Unit Start"], - ["PeakEnergy.Restart", "Peak Energy Restart", "Unit Start"], - ["Pump.Jog", "Unit Jogged", "Unit Jog"], -] - -safetyList = [] -bitList = [] - - -class SafetyTag: - """Safety structure""" - - def __init__(self, tag, name): - self.name = name - self.tag = tag - self.alarm = False - self.warning = False - self.condition = "" - self.type = "" - self.value = 0.0 - self.stored = False - - def writeToDB(self): - global tux - global sess - global conn - global condition - stroke_num = int(tux.ReadLGXDataAsInteger(sess, conn, "Stroke_Count_Today", 1)[0]) - db = MySQLdb.connect(host="127.0.0.1", user="website", passwd="henrypump", db="WellData") - cur = db.cursor() - event_query = "INSERT INTO WellData.Event_List (device_name, type, cond, value, datetime, tag, stroke_number) VALUES ('{0}', '{1}', '{2}', {3}, '{4}', '{5}', {6});".format( - self.name, self.type, self.condition, self.value, datetime.now(), self.name, stroke_num) - print event_query - store_event = cur.execute(event_query) - db.commit() - db.close() - self.stored = True - - def ReadStatus(self): - global tux - global sess - global conn - global condition - try: - a = tux.ReadLGXDataAsInteger(sess, conn, self.tag + ".Alarm", 1) - w = tux.ReadLGXDataAsInteger(sess, conn, self.tag + ".Warning", 1) - aInt = int(a[0]) - wInt = int(w[0]) - if aInt == 1: - self.alarm = True - self.condition = condition[tux.ReadLGXDataAsInteger(sess, conn, self.tag + ".Alarm_Code", 1)] - self.value = round(tux.ReadLGXDataAsInteger(sess, conn, self.tag + ".Alarm_Value", 1), 3) - self.type = "Alarm" - if (self.stored == False): - self.writeToDB() - elif wInt == 1: - self.warning = True - self.condition = condition[tux.ReadLGXDataAsInteger(sess, conn, self.tag + ".Alarm_Code", 1)] - self.value = round(tux.ReadLGXDataAsInteger(sess, conn, self.tag + ".Alarm_Value", 1), 3) - self.type = "Warning" - if (self.stored == False): - self.writeToDB() - else: - self.alarm = False - self.warning = False - self.condition = "" - self.type = "" - self.stored = False - except: - print "Unable to read " + self.tag - - -class BitTag: - """Safety structure""" - - def __init__(self, tag, name, condition): - self.name = name - self.tag = tag - self.condition = condition - self.type = "Info" - self.value = 0.0 - self.stored = False - - def writeToDB(self): - global tux - global sess - global conn - global condition - stroke_num = int(tux.ReadLGXDataAsInteger(sess, conn, "Stroke_Count_Today", 1)[0]) - db = MySQLdb.connect(host="127.0.0.1", user="website", passwd="henrypump", db="WellData") - cur = db.cursor() - event_query = "INSERT INTO WellData.Event_List (device_name, type, cond, value, datetime, tag, stroke_number) VALUES ('{0}', '{1}', '{2}', {3}, '{4}', '{5}', {6});".format( - self.name, self.type, self.condition, self.value, datetime.now(), self.name, stroke_num) - print event_query - store_event = cur.execute(event_query) - db.commit() - db.close() - self.stored = True - - def ReadStatus(self): - global tux - global sess - global conn - global condition - try: - a = tux.ReadLGXDataAsInteger(sess, conn, self.tag, 1) - aInt = int(a[0]) - # print self.tag + ": " + str(aInt) - if aInt <> 0: - if (self.stored == False): - self.writeToDB() - else: - self.stored = False - except: - print "Unable to read " + self.tag - - -for saf in safetySetup: - safetyList.append(SafetyTag(saf[0], saf[1])) - -for bit in bitsSetup: - bitList.append(BitTag(bit[0], bit[1], bit[2])) - -if (PLC_TYPE == "VFD"): - safetyList.append(SafetyTag("Safety_Pump_PF755", "Drive")) -if (PLC_TYPE == "E300"): - safetyList.append(SafetyTag("Safety_E300", "Relay Overload")) - -while True: - for s in safetyList: - s.ReadStatus() - for b in bitList: - b.ReadStatus() - sleep(.50) +#!/usr/bin/env python + +''' +Created on Oct 1, 2014 + +@author: PJMcdona +''' + +from datetime import datetime +from random import randint +from time import sleep +import sys + +# MYSQL Config +import MySQLdb + +db = MySQLdb.connect(host="127.0.0.1", user="website", passwd="henrypump", db="WellData") +cur = db.cursor() +query = "SELECT * FROM WellData.config ORDER BY dateChanged DESC LIMIT 1;" +cur.execute(query) +setup = cur.fetchall() +db.commit() +db.close() + +PLC_IP_ADDRESS = setup[0][2] +PLC_TYPE = setup[0][1] + +sys.path.append("../") + + +path_to_CSV = "/mnt/usb/" + +# TUXEIP Connection to PLC +# TUXEIP Connection to PLC +from tuxeip import TuxEIP, LGX, LGX_REAL + +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') + + + +safetySetup = [["Safety_Casing_Pressure", "Casing Pressure"], + ["Safety_Flow_Line_Pressure", "Flow Line Pressure"], + ["Safety_Flowmeter", "Flowmeter"], + ["Safety_Fluid_Load", "Fluid Load"], + ["Safety_Inclinometer", "Inclinometer"], + ["Safety_Load_HiHi", "Load HiHi"], + ["Safety_Load_Hi", "Load Hi"], + ["Safety_Load_Lo", "Load Lo"], + ["Safety_Load_LoLo", "Load LoLo"], + ["Safety_Speed", "Speed"], + ["Safety_Tubing_Pressure", "Tubing Pressure"] + ] + +condition = { + 20: "Low", + 21: "High", + 24: "LoLo", + 25: "HiHi", + 32: "Input Failure", + 34: "Configuration Error", + 16: "Failure to Stop", + 17: "Failure to Start", + 18: "Drive Fault" +} + +bitsSetup = [ + ["Pump.Auto_Stop", "Pump Off (Auto Mode)", "Unit Stop"], + ["Pump.POC_Stop", "Pump Off (POC Mode)", "Unit Stop"], + ["Pump.Timed_Stop", "Pump Off (Timer Mode)", "Unit Stop"], + ["Pump.Stop", "User Initiated Stop", "Unit Stop"], + ["PeakEnergy.Stop", "Peak Energy Stop", "Unit Stop"], + ["Pump.Start", "User Initiated Start", "Unit Start"], + ["Pump.POC_Restart", "Restart (POC Mode)", "Unit Start"], + ["Pump.Timed_Restart", "Restart (Timer Mode)", "Unit Start"], + ["Pump.Auto_Restart", "Restart (Auto Mode)", "Unit Start"], + ["PeakEnergy.Restart", "Peak Energy Restart", "Unit Start"], + ["Pump.Jog", "Unit Jogged", "Unit Jog"], +] + +safetyList = [] +bitList = [] + + +class SafetyTag: + """Safety structure""" + + def __init__(self, tag, name): + self.name = name + self.tag = tag + self.alarm = False + self.warning = False + self.condition = "" + self.type = "" + self.value = 0.0 + self.stored = False + + def writeToDB(self): + global tux + global sess + global conn + global condition + stroke_num = int(tux.ReadLGXDataAsInteger(sess, conn, "Stroke_Count_Today", 1)[0]) + db = MySQLdb.connect(host="127.0.0.1", user="website", passwd="henrypump", db="WellData") + cur = db.cursor() + event_query = "INSERT INTO WellData.Event_List (device_name, type, cond, value, datetime, tag, stroke_number) VALUES ('{0}', '{1}', '{2}', {3}, '{4}', '{5}', {6});".format( + self.name, self.type, self.condition, self.value, datetime.now(), self.name, stroke_num) + print event_query + store_event = cur.execute(event_query) + db.commit() + db.close() + self.stored = True + + def ReadStatus(self): + global tux + global sess + global conn + global condition + try: + a = tux.ReadLGXDataAsInteger(sess, conn, self.tag + ".Alarm", 1) + w = tux.ReadLGXDataAsInteger(sess, conn, self.tag + ".Warning", 1) + aInt = int(a[0]) + wInt = int(w[0]) + if aInt == 1: + self.alarm = True + self.condition = condition[tux.ReadLGXDataAsInteger(sess, conn, self.tag + ".Alarm_Code", 1)] + self.value = round(tux.ReadLGXDataAsInteger(sess, conn, self.tag + ".Alarm_Value", 1), 3) + self.type = "Alarm" + if (self.stored == False): + self.writeToDB() + elif wInt == 1: + self.warning = True + self.condition = condition[tux.ReadLGXDataAsInteger(sess, conn, self.tag + ".Alarm_Code", 1)] + self.value = round(tux.ReadLGXDataAsInteger(sess, conn, self.tag + ".Alarm_Value", 1), 3) + self.type = "Warning" + if (self.stored == False): + self.writeToDB() + else: + self.alarm = False + self.warning = False + self.condition = "" + self.type = "" + self.stored = False + except: + print "Unable to read " + self.tag + + +class BitTag: + """Safety structure""" + + def __init__(self, tag, name, condition): + self.name = name + self.tag = tag + self.condition = condition + self.type = "Info" + self.value = 0.0 + self.stored = False + + def writeToDB(self): + global tux + global sess + global conn + global condition + stroke_num = int(tux.ReadLGXDataAsInteger(sess, conn, "Stroke_Count_Today", 1)[0]) + db = MySQLdb.connect(host="127.0.0.1", user="website", passwd="henrypump", db="WellData") + cur = db.cursor() + event_query = "INSERT INTO WellData.Event_List (device_name, type, cond, value, datetime, tag, stroke_number) VALUES ('{0}', '{1}', '{2}', {3}, '{4}', '{5}', {6});".format( + self.name, self.type, self.condition, self.value, datetime.now(), self.name, stroke_num) + print event_query + store_event = cur.execute(event_query) + db.commit() + db.close() + self.stored = True + + def ReadStatus(self): + global tux + global sess + global conn + global condition + try: + a = tux.ReadLGXDataAsInteger(sess, conn, self.tag, 1) + aInt = int(a[0]) + # print self.tag + ": " + str(aInt) + if aInt <> 0: + if (self.stored == False): + self.writeToDB() + else: + self.stored = False + except: + print "Unable to read " + self.tag + + +for saf in safetySetup: + safetyList.append(SafetyTag(saf[0], saf[1])) + +for bit in bitsSetup: + bitList.append(BitTag(bit[0], bit[1], bit[2])) + +if (PLC_TYPE == "VFD"): + safetyList.append(SafetyTag("Safety_Pump_PF755", "Drive")) +if (PLC_TYPE == "E300"): + safetyList.append(SafetyTag("Safety_E300", "Relay Overload")) + +while True: + for s in safetyList: + s.ReadStatus() + for b in bitList: + b.ReadStatus() + sleep(.50) diff --git a/alarmLogger_pycomm.py b/alarmLogger_pycomm.py index 1ecacbc..becddb6 100644 --- a/alarmLogger_pycomm.py +++ b/alarmLogger_pycomm.py @@ -1,143 +1,143 @@ -#!/usr/bin/python - -from datetime import datetime -from time import sleep -import sys - -# MYSQL Config -import MySQLdb - -db = MySQLdb.connect(host="127.0.0.1", user="website", passwd="henrypump", db="WellData") -cur = db.cursor() -query = "SELECT * FROM WellData.config ORDER BY dateChanged DESC LIMIT 1;" -cur.execute(query) -setup = cur.fetchall() -db.commit() -db.close() - -try: - PLC_IP_ADDRESS = setup[0][2] - PLC_TYPE = setup[0][1] -except: - print("PLC Address not set in database... using default of 192.168.1.10") - PLC_IP_ADDRESS = "192.168.1.10" - PLC_TYPE = "VFD" - -#PYCOMM Connection to PLC -from pycomm.ab_comm.clx import Driver as ClxDriver -from pycomm.ab_comm.clx import CommError as ClxCommError -from pycomm.ab_comm.clx import DataError as ClxDataError -c = ClxDriver(True, 'ClxDriver.log') - -safetySetup = [{'tag':"Safety_Casing_Pressure", 'name':"Casing Pressure", "active":False}, - {'tag':"Safety_Flow_Line_Pressure", 'name':"Flow Line Pressure", "active":False}, - {'tag':"Safety_Flowmeter", 'name':"Flowmeter", "active":False}, - {'tag':"Safety_Fluid_Load", 'name':"Fluid Load", "active":False}, - {'tag':"Safety_Inclinometer", 'name':"Inclinometer", "active":False}, - {'tag':"Safety_Load_HiHi", 'name':"Load HiHi", "active":False}, - {'tag':"Safety_Load_Hi", 'name':"Load Hi", "active":False}, - {'tag':"Safety_Load_Lo", 'name':"Load Lo", "active":False}, - {'tag':"Safety_Load_LoLo", 'name':"Load LoLo", "active":False}, - {'tag':"Safety_Speed", 'name':"Speed", "active":False}, - {'tag':"Safety_Tubing_Pressure", 'name':"Tubing Pressure", "active":False} -] - -condition = { - 20: "Low", - 21: "High", - 24: "LoLo", - 25: "HiHi", - 32: "Input Failure", - 34: "Configuration Error", - 16: "Failure to Stop", - 17: "Failure to Start", - 18: "Drive Fault" -} - -bitsSetup = [ - {'tag':"Pump.Auto_Stop", 'name':"Pump Off (Auto Mode)", 'condition':"Unit Stop", 'active':False}, - {'tag':"Pump.POC_Stop", 'name':"Pump Off (POC Mode)", 'condition':"Unit Stop", 'active':False}, - {'tag':"Pump.Timed_Stop", 'name':"Pump Off (Timer Mode)", 'condition':"Unit Stop", 'active':False}, - {'tag':"Pump.Stop", 'name':"User Initiated Stop", 'condition':"Unit Stop", 'active':False}, - {'tag':"PeakEnergy.Stop", 'name':"Peak Energy Stop", 'condition':"Unit Stop", 'active':False}, - {'tag':"Pump.Start", 'name':"User Initiated Start", 'condition':"Unit Start", 'active':False}, - {'tag':"Pump.POC_Restart", 'name':"Restart (POC Mode)", 'condition':"Unit Start", 'active':False}, - {'tag':"Pump.Timed_Restart", 'name':"Restart (Timer Mode)", 'condition':"Unit Start", 'active':False}, - {'tag':"Pump.Auto_Restart", 'name':"Restart (Auto Mode)", 'condition':"Unit Start", 'active':False}, - {'tag':"PeakEnergy.Restart", 'name':"Peak Energy Restart", 'condition':"Unit Start", 'active':False}, - {'tag':"Pump.Jog", 'name':"Unit Jogged", 'condition':"Unit Jog", 'active':False}, -] - -def write_alarm(name, type, condition, value, tag): - """writes the specified alarm to the MySQL database""" - global c - - stroke_num = int(c.read_tag(["Stroke_Count_Today"])[0][1]) - db = MySQLdb.connect(host="127.0.0.1", user="website", passwd="henrypump", db="WellData") - cur = db.cursor() - event_query = "INSERT INTO WellData.Event_List (device_name, type, cond, value, datetime, tag, stroke_number) VALUES ('{0}', '{1}', '{2}', {3}, '{4}', '{5}', {6});".format( - name, type, condition, value, datetime.now(), tag, stroke_num) - print event_query - store_event = cur.execute(event_query) - db.commit() - db.close() - -c.open(PLC_IP_ADDRESS) -retry = False -number_of_retry = 0 -while True: - try: - if retry: - retry = False - print "retrying" - sleep(0.5) - c.forward_close() - if number_of_retry >= 5: - c.close() - c.open(PLC_IP_ADDRESS) - number_of_retry = 0 - - for safety in safetySetup: - try: - alarm_sts = int(c.read_tag(["{0}.Alarm".format(safety['tag'])])[0][1]) - warning_sts = int(c.read_tag([safety['tag'] + ".Warning"])[0][1]) - print("Tag: {0}, Alarm: {1}, Warning: {2}".format(safety['tag'], alarm_sts, warning_sts)) - safety_type = "" - if alarm_sts == 1: - safety_type = "Alarm" - if warning_sts == 1: - safety_type = "Warning" - if (alarm_sts == 1 or warning_sts == 1): - safety_condition = condition[c.read_tag(["{0}.Alarm_Code".format(safety['tag'])])[0][1]] - safety_value = round(c.read_tag(["{0}.Alarm_Value".format(safety['tag'])])[0][1], 3) - if not safety['active']: - write_alarm(safety['name'], safety_type, safety_condition, safety_value, safety['tag']) - safety['active'] = True - else: - safety['active'] = False - except Exception, e: - print("[ERROR] Error reading safety {0}: {1}".format(safety['tag'], e)) - for bit in bitsSetup: - try: - bit_sts = int(c.read_tag([bit['tag']])[0][1]) - print("Tag: {0}, Status: {1}".format(bit['tag'], bit_sts)) - bit_type = "Info" - if bit_sts == 1: - bit_condition = bit['condition'] - bit_value = 0.0 - if not bit['active']: - write_alarm(bit['name'], bit_type, bit_condition, bit_value, bit['tag']) - bit['active'] = True - else: - bit['active'] = False - except Exception, e: - print("[ERROR] Error reading bit {0}: {1}".format(bit['tag'], e)) - - sleep(0.5) - except ClxCommError as e: - print(e) - retry = True - - except ClxDataError as e: - print (e) -c.close() +#!/usr/bin/python + +from datetime import datetime +from time import sleep +import sys + +# MYSQL Config +import MySQLdb + +db = MySQLdb.connect(host="127.0.0.1", user="website", passwd="henrypump", db="WellData") +cur = db.cursor() +query = "SELECT * FROM WellData.config ORDER BY dateChanged DESC LIMIT 1;" +cur.execute(query) +setup = cur.fetchall() +db.commit() +db.close() + +try: + PLC_IP_ADDRESS = setup[0][2] + PLC_TYPE = setup[0][1] +except: + print("PLC Address not set in database... using default of 192.168.1.10") + PLC_IP_ADDRESS = "192.168.1.10" + PLC_TYPE = "VFD" + +#PYCOMM Connection to PLC +from pycomm.ab_comm.clx import Driver as ClxDriver +from pycomm.ab_comm.clx import CommError as ClxCommError +from pycomm.ab_comm.clx import DataError as ClxDataError +c = ClxDriver(True, 'ClxDriver.log') + +safetySetup = [{'tag':"Safety_Casing_Pressure", 'name':"Casing Pressure", "active":False}, + {'tag':"Safety_Flow_Line_Pressure", 'name':"Flow Line Pressure", "active":False}, + {'tag':"Safety_Flowmeter", 'name':"Flowmeter", "active":False}, + {'tag':"Safety_Fluid_Load", 'name':"Fluid Load", "active":False}, + {'tag':"Safety_Inclinometer", 'name':"Inclinometer", "active":False}, + {'tag':"Safety_Load_HiHi", 'name':"Load HiHi", "active":False}, + {'tag':"Safety_Load_Hi", 'name':"Load Hi", "active":False}, + {'tag':"Safety_Load_Lo", 'name':"Load Lo", "active":False}, + {'tag':"Safety_Load_LoLo", 'name':"Load LoLo", "active":False}, + {'tag':"Safety_Speed", 'name':"Speed", "active":False}, + {'tag':"Safety_Tubing_Pressure", 'name':"Tubing Pressure", "active":False} +] + +condition = { + 20: "Low", + 21: "High", + 24: "LoLo", + 25: "HiHi", + 32: "Input Failure", + 34: "Configuration Error", + 16: "Failure to Stop", + 17: "Failure to Start", + 18: "Drive Fault" +} + +bitsSetup = [ + {'tag':"Pump.Auto_Stop", 'name':"Pump Off (Auto Mode)", 'condition':"Unit Stop", 'active':False}, + {'tag':"Pump.POC_Stop", 'name':"Pump Off (POC Mode)", 'condition':"Unit Stop", 'active':False}, + {'tag':"Pump.Timed_Stop", 'name':"Pump Off (Timer Mode)", 'condition':"Unit Stop", 'active':False}, + {'tag':"Pump.Stop", 'name':"User Initiated Stop", 'condition':"Unit Stop", 'active':False}, + {'tag':"PeakEnergy.Stop", 'name':"Peak Energy Stop", 'condition':"Unit Stop", 'active':False}, + {'tag':"Pump.Start", 'name':"User Initiated Start", 'condition':"Unit Start", 'active':False}, + {'tag':"Pump.POC_Restart", 'name':"Restart (POC Mode)", 'condition':"Unit Start", 'active':False}, + {'tag':"Pump.Timed_Restart", 'name':"Restart (Timer Mode)", 'condition':"Unit Start", 'active':False}, + {'tag':"Pump.Auto_Restart", 'name':"Restart (Auto Mode)", 'condition':"Unit Start", 'active':False}, + {'tag':"PeakEnergy.Restart", 'name':"Peak Energy Restart", 'condition':"Unit Start", 'active':False}, + {'tag':"Pump.Jog", 'name':"Unit Jogged", 'condition':"Unit Jog", 'active':False}, +] + +def write_alarm(name, type, condition, value, tag): + """writes the specified alarm to the MySQL database""" + global c + + stroke_num = int(c.read_tag(["Stroke_Count_Today"])[0][1]) + db = MySQLdb.connect(host="127.0.0.1", user="website", passwd="henrypump", db="WellData") + cur = db.cursor() + event_query = "INSERT INTO WellData.Event_List (device_name, type, cond, value, datetime, tag, stroke_number) VALUES ('{0}', '{1}', '{2}', {3}, '{4}', '{5}', {6});".format( + name, type, condition, value, datetime.now(), tag, stroke_num) + print event_query + store_event = cur.execute(event_query) + db.commit() + db.close() + +c.open(PLC_IP_ADDRESS) +retry = False +number_of_retry = 0 +while True: + try: + if retry: + retry = False + print "retrying" + sleep(0.5) + c.forward_close() + if number_of_retry >= 5: + c.close() + c.open(PLC_IP_ADDRESS) + number_of_retry = 0 + + for safety in safetySetup: + try: + alarm_sts = int(c.read_tag(["{0}.Alarm".format(safety['tag'])])[0][1]) + warning_sts = int(c.read_tag([safety['tag'] + ".Warning"])[0][1]) + print("Tag: {0}, Alarm: {1}, Warning: {2}".format(safety['tag'], alarm_sts, warning_sts)) + safety_type = "" + if alarm_sts == 1: + safety_type = "Alarm" + if warning_sts == 1: + safety_type = "Warning" + if (alarm_sts == 1 or warning_sts == 1): + safety_condition = condition[c.read_tag(["{0}.Alarm_Code".format(safety['tag'])])[0][1]] + safety_value = round(c.read_tag(["{0}.Alarm_Value".format(safety['tag'])])[0][1], 3) + if not safety['active']: + write_alarm(safety['name'], safety_type, safety_condition, safety_value, safety['tag']) + safety['active'] = True + else: + safety['active'] = False + except Exception, e: + print("[ERROR] Error reading safety {0}: {1}".format(safety['tag'], e)) + for bit in bitsSetup: + try: + bit_sts = int(c.read_tag([bit['tag']])[0][1]) + print("Tag: {0}, Status: {1}".format(bit['tag'], bit_sts)) + bit_type = "Info" + if bit_sts == 1: + bit_condition = bit['condition'] + bit_value = 0.0 + if not bit['active']: + write_alarm(bit['name'], bit_type, bit_condition, bit_value, bit['tag']) + bit['active'] = True + else: + bit['active'] = False + except Exception, e: + print("[ERROR] Error reading bit {0}: {1}".format(bit['tag'], e)) + + sleep(0.5) + except ClxCommError as e: + print(e) + retry = True + + except ClxDataError as e: + print (e) +c.close()