diff --git a/dataLogger_SQLite_pycomm.py b/dataLoggerSQLite.py similarity index 81% rename from dataLogger_SQLite_pycomm.py rename to dataLoggerSQLite.py index 738a0f4..7a136d7 100644 --- a/dataLogger_SQLite_pycomm.py +++ b/dataLoggerSQLite.py @@ -145,6 +145,76 @@ class Status(Tag): 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, conditionMap, 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 = conditionMap[readTag(PLC_IP_ADDRESS, '{}.Alarm_Code'.format(self.tag))] + value = readTag(PLC_IP_ADDRESS, '{}.Alarm_Value'.format(self.tag)) + 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, self.condition, self.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)) + 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 + + + # ---------- MAP FUNCTIONS ---------- # modeMap = { 0: "Error", @@ -175,6 +245,18 @@ statusMap = { 9999: 'No Response' } +conditionMap = { + 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" +} + # ---------- TAGS ---------- # stroke_tags = { 'card_id': Tag('card_id', 'Card_Past[1].ID', 'DINT', 25, 3600), @@ -273,6 +355,36 @@ welltest_tags = { # 'poc_percent_pumpoff': {'POC-Percent_Pumpoff', "Pump.Mode", "REAL", 1.0, 3600} # } +bit_tags = { + 'Pump Off (Auto Mode)': bitAlarm('Pump Off (Auto Mode)', 'Pump.Auto_Stop', 'Unit Stop'), + 'Pump Off (POC Mode)': bitAlarm('Pump Off (POC Mode)', 'Pump.POC_Stop', 'Unit Stop'), + 'Pump Off (Timer Mode)': bitAlarm('Pump Off (Timer Mode)', 'Pump.Timed_Stop', 'Unit Stop'), + 'User Initiated Stop': bitAlarm('User Initiated Stop', 'Pump.Stop', 'Unit Stop'), + 'Peak Energy Stop': bitAlarm('Peak Energy Stop', 'PeakEnergy.Stop', 'Unit Stop'), + 'User Initiated Start': bitAlarm('User Initiated Start', 'Pump.Start', 'Unit Start'), + 'Restart (POC Mode)': bitAlarm('Restart (POC Mode)', 'Pump.POC_Restart', 'Unit Start'), + 'Restart (Timer Mode)': bitAlarm('Restart (Timer Mode)', 'Pump.Timed_Restart', 'Unit Start'), + 'Restart (Auto Mode)': bitAlarm('Restart (Auto Mode)', 'Pump.Auto_Restart', 'Unit Start'), + 'Peak Energy Restart': bitAlarm('Peak Energy Restart', 'PeakEnergy.Restart', 'Unit Start'), + 'Unit Jogged': bitAlarm('Unit Jogged', 'Pump.Jog', 'Unit Jog') + +} + + +safety_tags = { + 'Casing Pressure': AnalogAlarm('Casing Pressure', 'Safety_Casing_Pressure'), + 'Flow Line Pressure': AnalogAlarm('Flow Line Pressure', 'Safety_Flow_Line_Pressure'), + 'Flowmeter': AnalogAlarm('Flowmeter', 'Safety_Flowmeter'), + 'Fluid Load': AnalogAlarm('Fluid Load', 'Safety_Fluid_Load'), + 'Inclinometer': AnalogAlarm('Inclinometer', 'Safety_Inclinometer'), + 'Load HiHi': AnalogAlarm('Load HiHi', 'Safety_Load_HiHi'), + 'Load Hi': AnalogAlarm('Load Hi', 'Safety_Load_Hi'), + 'Load Lo': AnalogAlarm('Load Lo', 'Safety_Load_Lo'), + 'Load LoLo': AnalogAlarm('Load LoLo', 'Safety_Load_LoLo'), + 'Speed': AnalogAlarm('Speed', 'Safety_Speed'), + 'Tubing Pressure': AnalogAlarm('Tubing Pressure', 'Safety_Tubing_Pressure') +} + def readPoints(): global PLC_IP_ADDRESS @@ -445,6 +557,15 @@ def main(): already_entered_well_test = True print "Well Test Stored!" + ################### + # ALARMS & EVENTS # + ################### + for t in safety_tags: + safety_tags[t].checkStatus(stroke_tags['card_id'].value) + + for b in bit_tags: + bit_tags[b].checkStatus(stroke_tags['card_id'].value) + time.sleep(.20) except Exception, e: print("Error during loop: {}", e)