#!/usr/bin/env python ''' Created on Oct 1, 2014 @author: PJMcdona ''' # import csv from datetime import datetime import time import sqlite3 as lite from pycomm.ab_comm.clx import Driver as ClxDriver # import logging import traceback con = lite.connect("/mnt/usb/data.db") PLC_IP_ADDRESS = "192.168.1.10" PLC_TYPE = "VFD" def readConfig(): global PLC_IP_ADDRESS, PLC_TYPE with con: cur = con.cursor() query = "SELECT * FROM config ORDER BY dateChanged DESC LIMIT 1;" cur.execute(query) setup = cur.fetchall() PLC_IP_ADDRESS = setup[0][2] PLC_TYPE = setup[0][1] def readTag(addr, tag): # logging.basicConfig( # filename="clx.log", # format="%(levelname)-10s %(asctime)s %(message)s", # level=logging.DEBUG # ) c = ClxDriver() if c.open(addr): try: v = c.read_tag(tag) # print(v) return v except Exception: print("ERROR RETRIEVING TAG: {}".format(tag)) err = c.get_status() c.close() print err pass c.close() def readArray(addr, arr, length): # logging.basicConfig(filename="clx.log", format="%(levelname)-10s %(asctime)s %(message)s", level=logging.DEBUG) c = ClxDriver() if c.open(addr): try: v = c.read_array(arr, length) # print(v) return map(lambda x: x[1], v) except Exception: print("ERROR RETRIEVING ARRAY: {}".format(arr)) err = c.get_status() c.close() print err pass c.close() def checkDateInDB(da): y = int(da[0:4]) m = int(da[4:6]) d = int(da[6:8]) dquery = "SELECT id FROM card_history_dates WHERE year = {0} AND month = {1} AND day = {2};".format(y, m, d) # dquery = "SELECT id FROM WellData.card_history_dates WHERE year = 2016 AND month = 1 AND day = 5;" with con: cur = con.cursor() cur.execute(dquery) dates = cur.fetchall() if len(dates) > 0: print("Date {0} already in db".format(da)) else: ins_query = "INSERT INTO card_history_dates (year, month, day, first_id) VALUES ({0}, {1}, {2}, (SELECT MAX(id) FROM card_history));".format(y, m, d) print(ins_query) with con: cur = con.cursor() cur.execute(ins_query) con.commit() class Tag(): global readTag, con def __init__(self, name, tag, data_type, change_threshold, guarantee_sec, 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 def read(self, forceSend): 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, name, val) VALUES ({}, '{}', {})".format(time.time(), self.name, 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 with con: cur = con.cursor() 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, 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))[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 # ---------- MAP FUNCTIONS ---------- # modeMap = { 0: "Error", 1: "Auto", 2: "POC", 3: "Timer", 4: "Manual", 5: "DH PID" } card_type_map = { 0: "Normal", 1: "Shutdown", 2: "Alarm", 3: "Startup", 4: "Low Fillage" } statusMap = { 0: 'Stopped', 1: 'Running', 2: 'Pumped Off', 3: 'Faulted', 4: 'Starting', 5: 'Recovering', 100: 'Read Error', 1000: 'PLC Error', 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), 'card_type': Tag('card_type', 'Card_Past[1].Card_Type', 'STRING', 0, 3600, mapFn=card_type_map) } status = Status('run_status', 'Pump.Run_Status', 'STRING', 0, 3600, mapFn=statusMap) history_tags = { 'torque_reference': Tag('torque_reference', 'PF755_Drive:O.TrqRefAStpt', 'REAL', 1.0, 3600), 'speed_reference': Tag('speed_reference', 'Pump_PF755.PSet_SpeedRef', 'REAL', 10.0, 3600), 'downhole_adjusted_gross_stroke': Tag('downhole_adjusted_gross_stroke', 'Card_Past[1].Downhole_AdjustedGrossStroke', 'REAL', 2.0, 3600), 'downhole_fluid_load': Tag('downhole_fluid_load', 'Card_Past[1].Downhole_FluidLoad', 'REAL', 400.0, 3600), 'downhole_gross_stroke': Tag('downhole_gross_stroke', 'Card_Past[1].Downhole_GrossStroke', 'REAL', 2.0, 3600), 'downhole_max_load': Tag('downhole_max_load', 'Card_Past[1].Downhole_Max_Load.Load', 'REAL', 400.0, 3600), 'downhole_max_position': Tag('downhole_max_position', 'Card_Past[1].Downhole_Max_Position.Position', 'REAL', 2.0, 3600), 'downhole_min_load': Tag('downhole_min_load', 'Card_Past[1].Downhole_Min_Load.Load', 'REAL', 400.0, 3600), 'downhole_min_position': Tag('downhole_min_position', 'Card_Past[1].Downhole_Min_Position.Position', 'REAL', 2.0, 3600), 'downhole_net_stroke': Tag('downhole_net_stroke', 'Card_Past[1].Downhole_NetStroke', 'REAL', 2.0, 3600), 'drive_torque_mode': Tag('drive_torque_mode', 'DriveTorqueMode', 'BOOL', 1.0, 3600), 'fillage_percent': Tag('fillage_percent', 'Card_Past[1].Fillage_Percent', 'REAL', 5.0, 3600), 'fluid_gradient': Tag('fluid_gradient', 'Card_Past[1].Params.Fluid_Gradient', 'REAL', 0.002, 3600), 'fluid_level': Tag('fluid_level', 'Card_Past[1].Fluid_Above_Pump', 'REAL', 100.0, 3600), 'polished_rod_hp': Tag('polished_rod_hp', 'Card_Past[1].Polished_Rod_HP', 'REAL', 1.0, 3600), 'pump_hp': Tag('pump_hp', 'Card_Past[1].Pump_HP', 'REAL', 1.0, 3600), 'pump_intake_pressure': Tag('pump_intake_pressure', 'Card_Past[1].Pump_Intake_Pressure', 'REAL', 200.0, 3600), 'spm': Tag('spm', 'Card_Past[1].SPM', 'REAL', 0.5, 3600), 'stroke_production': Tag('stroke_production', 'Stroke_Production', 'REAL', 0.005, 3600), 'stuffing_box_friction': Tag('stuffing_box_friction', 'Card_Past[1].Params.Stuffing_Box_Friction', 'REAL', 1.0, 3600), 'surface_max_load': Tag('surface_max_load', 'Card_Past[1].Surface_Max.Load', 'REAL', 400.0, 3600), 'surface_max_position': Tag('surface_max_position', 'Card_Past[1].Surface_Max.Position', 'REAL', 1.0, 3600), 'surface_min_load': Tag('surface_min_load', 'Card_Past[1].Surface_Min.Load', 'REAL', 400.0, 3600), 'surface_min_position': Tag('surface_min_position', 'Card_Past[1].Surface_Min.Position', 'REAL', 1.0, 3600), 'surface_stroke_length': Tag('surface_stroke_length', 'Card_Past[1].Surface_StrokeLength', 'REAL', 1.0, 3600), 'tubing_head_pressure': Tag('tubing_head_pressure', 'Card_Past[1].Params.Tubing_Head_Pressure', 'REAL', 25.0, 3600), 'tubing_movement': Tag('tubing_movement', 'Card_Past[1].Tubing_Movement', 'REAL', 1.0, 3600), 'dt': Tag('dt', 'Card_Past[1].Params.dt', 'REAL', 0.001, 3600), } gaugeoff_tags = { 'year': Tag('year', 'GAUGEOFF_DateTime.Year', 'DINT', 0, 0), 'month': Tag('month', 'GAUGEOFF_DateTime.Month', 'DINT', 0, 0), 'day': Tag('day', 'GAUGEOFF_DateTime.Day', 'DINT', 0, 0), 'hour': Tag('hour', 'GAUGEOFF_DateTime.Hour', 'DINT', 0, 0), 'min': Tag('min', 'GAUGEOFF_DateTime.Min', 'DINT', 0, 0), 'sec': Tag('sec', 'GAUGEOFF_DateTime.Sec', 'DINT', 0, 0), 'percent_run': Tag('percent_run', 'GAUGEOFF_Percent_Run', 'REAL', 0, 0), 'kwh': Tag('kwh', 'GAUGEOFF_kWh', 'REAL', 0, 0), 'electricity_cost': Tag('electricity_cost', 'GAUGEOFF_Electricity_Cost', 'REAL', 0, 0), 'max_load': Tag('max_load', 'GAUGEOFF_Max_Load', 'REAL', 0, 0), 'min_load': Tag('min_load', 'GAUGEOFF_Min_Load', 'REAL', 0, 0), 'average_spm': Tag('average_spm', 'GAUGEOFF_Average_SPM', 'REAL', 0, 0), 'production_calculated': Tag('production_calculated', 'GAUGEOFF_Production_Calculated', 'REAL', 0, 0), 'full_card_production': Tag('full_card_production', 'GAUGEOFF_Full_Card_Production', 'REAL', 0, 0), 'polished_rod_hp': Tag('polished_rod_hp', 'GAUGEOFF_Polished_Rod_HP', 'REAL', 0, 0), 'lifting_cost': Tag('lifting_cost', 'GAUGEOFF_Lifting_Cost', 'REAL', 0, 0), 'fluid_level': Tag('fluid_level', 'GAUGEOFF_Fluid_Above_Pump', 'REAL', 0, 0), 'pump_intake_pressure': Tag('pump_intake_pressure', 'GAUGEOFF_pump_intake_pressure', 'REAL', 0, 0), 'kwh_regen': Tag('kwh_regen', 'GAUGEOFF_kWh_regen', 'REAL', 0, 0), 'inflow_rate': Tag('inflow_rate', 'GAUGEOFF_Inflow_Rate', 'REAL', 0, 0) } welltest_tags = { 'year': Tag('year', "Well_Test.DateTime_Complete.Year", "INT", 0, 0), 'month': Tag('month', "Well_Test.DateTime_Complete.Month", "INT", 0, 0), 'day': Tag('day', "Well_Test.DateTime_Complete.Day", "INT", 0, 0), 'hour': Tag('hour', "Well_Test.DateTime_Complete.Hour", "INT", 0, 0), 'min': Tag('min', "Well_Test.DateTime_Complete.Min", "INT", 0, 0), 'sec': Tag('sec', "Well_Test.DateTime_Complete.Sec", "INT", 0, 0), 'test_duration': Tag('test_duration', "Well_Test.Test_Duration", "REAL", 0, 0), 'v_water': Tag('v_water', "Well_Test.Volume_Water", "REAL", 0, 0), 'v_oil': Tag('v_oil', "Well_Test.Volume_Oil", "REAL", 0, 0), 'v_gas': Tag('v_gas', "Well_Test.Volume_Gas", "REAL", 0, 0), 'p_v_water': Tag('p_v_water', "Well_Test.Projected_Volume_Water", "REAL", 0, 0), 'p_v_oil': Tag('p_v_oil', "Well_Test.Projected_Volume_Oil", "REAL", 0, 0), 'k_factor': Tag('k_factor', "Well_Test.k_Factor", "REAL", 0, 0), 'api_oil': Tag('api_oil', "Well_Test.API_Oil", "REAL", 0, 0), 'sg_water': Tag('sg_water', "Well_Test.SG_Water", "REAL", 0, 0) } # setpoint_tags = { # 'mode': {'Mode', "Pump.Mode", "INT", 0.5, 3600, mapFn=modeMap}, # 'speed_setpoint_spm': {'Speed_Setpoint_SPM', "Pump.Speed_Setpoint_SPM", "REAL", 0.5, 3600}, # 'speed_max': {'Speed_Max', "Pump.Speed_Max", "REAL", 0.5, 3600}, # 'speed_min': {'Speed_Min', "Pump.Speed_Min", "REAL", 0.5, 3600}, # 'auto_speed_startpoint_spm': {'Auto-Speed_Startpoint_SPM', "Pump.Speed_Startpoint_SPM_Auto", "REAL", 0.5, 3600}, # 'auto_percentage_ramp_down': {'Auto-Percentage_Ramp_Down', "Pump.Mode", "REAL", 1.0, 3600}, # 'auto_increment_ramp_down': {'Auto-Increment_Ramp_Down', "Pump.Mode", "REAL", 1.0, 3600}, # 'auto_percent_ramp_up': {'Auto-Percent_Ramp_Up', "Pump.Mode", "REAL", 1.0, 3600}, # 'auto_percent_ramp_down': {'Auto-Percent_Ramp_Down', "Pump.Mode", "REAL", 1.0, 3600}, # 'auto_min_speed_strokes': {'Auto-Min_Speed_Strokes', "Pump.Mode", "REAL", 1.0, 3600}, # 'auto_percent_ramp_up': {'Auto-Percent_Ramp_Up', "Pump.Mode", "REAL", 1.0, 3600}, # 'auto_poc_startup_ignore_cards': {'Auto-POC-Startup_Ignore_Cards', "Pump.Mode", "REAL", 1.0, 3600}, # 'auto_poc_card_quantity': {'Auto-POC-Card_Quantity', "Pump.Mode", "REAL", 1.0, 3600}, # '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 num_points = readTag(PLC_IP_ADDRESS, "Card_Past[1].Num_Points")[0] surf_pos = readArray(PLC_IP_ADDRESS, "Card_Past[1].Surface_Position", num_points + 1)[1:] surf_pos.append(surf_pos[0]) surf_lod = readArray(PLC_IP_ADDRESS, "Card_Past[1].Surface_Load", num_points + 1)[1:] surf_lod.append(surf_lod[0]) down_pos = readArray(PLC_IP_ADDRESS, "Card_Past[1].Downhole_Position", num_points + 1)[1:] down_pos.append(down_pos[0]) down_lod = readArray(PLC_IP_ADDRESS, "Card_Past[1].Downhole_Load", num_points + 1)[1:] down_lod.append(down_lod[0]) return([surf_pos, surf_lod, down_pos, down_lod]) def evalTapers(): ts = time.time() numTapers = int(readTag(PLC_IP_ADDRESS, 'Card_Current.Params.Num_Tapers')[0]) for t in range(1, numTapers + 1): taper_length = readTag(PLC_IP_ADDRESS, 'Taper.Taper[{}].Setup.Length'.format(t))[0] taper_diameter = readTag(PLC_IP_ADDRESS, 'Taper.Taper[{}].Setup.Diameter'.format(t))[0] taper_material = readTag(PLC_IP_ADDRESS, 'Taper.Taper[{}].Setup.Material'.format(t))[0] if (taper_material == 1): taper_material = "Steel" elif (taper_material == 2): taper_material = "Fiberglass" tStr = "{{'taper':{}, 'length': {}, 'diameter': {}, 'material':'{}'}}".format(t, taper_length, taper_diameter, taper_material) tQuery = 'INSERT INTO well_config (tstamp, type, val) VALUES ({}, "taper", "{}")'.format(ts, tStr) print tQuery with con: cur = con.cursor() cur.execute(tQuery) con.commit() pump_diameter = readTag(PLC_IP_ADDRESS, 'UnitConfig.Pump_Diameter')[0] cfgQuery = "INSERT INTO well_config (tstamp, type, val) VALUES ({}, 'pump_diameter', '{}')".format(ts, pump_diameter) with con: cur = con.cursor() cur.execute(cfgQuery) con.commit() print "TAPER DATA READ!" return True def main(): read_tapers = False already_gauged_off = False already_entered_well_test = False last_date = "" last_stroke = 0 while True: try: if status.read(False): status.sendToDB() ############# # CARD DATA # ############# EOS = readTag(PLC_IP_ADDRESS, "End_Of_Stroke")[0] stroke_tags['card_id'].read(False) if (EOS and not (last_stroke == stroke_tags['card_id'].value)): sData = {} last_stroke = stroke_tags['card_id'].value stroke_time = time.time() dt = datetime.fromtimestamp(stroke_time) sData['localtime'] = dt sData['stroke_time'] = dt sData['utctime'] = datetime.utcfromtimestamp(stroke_time) for t in stroke_tags: stroke_tags[t].read(True) [sData['Surface_Position'], sData['Surface_Load'], sData['Downhole_Position'], sData['Downhole_Load']] = readPoints() # st = datetime.strftime(dt, "%Y%m%d_%H%M%S") date = datetime.strftime(dt, "%Y%m%d") if not date == last_date: checkDateInDB(date) last_date = date sData["card_type"] = stroke_tags['card_type'].value sData["card_id"] = stroke_tags['card_id'].value sData['sp_string'] = ', '.join(map(str, sData['Surface_Position'])) sData['sl_string'] = ', '.join(map(str, sData['Surface_Load'])) sData['dp_string'] = ', '.join(map(str, sData['Downhole_Position'])) sData['dl_string'] = ', '.join(map(str, sData['Downhole_Load'])) insert_query = "INSERT INTO card_history (Card_ID, Card_Type, Stroke_Time, Surface_Position, Surface_Load, Downhole_Position, Downhole_Load) VALUES (:card_id, :card_type, :stroke_time, :sp_string, :sl_string, :dp_string, :dl_string)" with con: cur = con.cursor() cur.execute(insert_query, sData) con.commit() print "CARD NUMBER " + str(sData["card_id"]) + " READ!" ################### # HISTORICAL DATA # ################### for hist in history_tags: h = history_tags[hist] if h.read(False): h.sendToDB() h.last_send_time = time.time() ############## # TAPER DATA # ############## update_taper = readTag(PLC_IP_ADDRESS, "Write_Tapers")[0] > 0 if (update_taper == 0): if read_tapers: read_tapers = False print "Update Tapers = False" if (update_taper and (not read_tapers)): print "reading taper file" read_tapers = evalTapers() ################## # GAUGE OFF DATA # ################## gauge_off = readTag(PLC_IP_ADDRESS, "Gauge_Off_Command")[0] if (gauge_off == 0): if already_gauged_off: already_gauged_off = False print "Already gauged off... Setting gauge_off to False" if (gauge_off and (not already_gauged_off)): print "Gauging off..." for goff in gaugeoff_tags: g = gaugeoff_tags[goff] g.read(True) gauge_date = datetime(year=gaugeoff_tags['year'].value, month=gaugeoff_tags['month'].value, day=gaugeoff_tags['day'].value, hour=gaugeoff_tags['hour'].value, minute=gaugeoff_tags['min'].value, second=gaugeoff_tags['sec'].value) with con: cur = con.cursor() con.execute("""INSERT INTO Hist_Day (gauge_date, percent_run, kWh, electricity_cost, peak_load, min_load, average_SPM, production_calculated, full_card_production, polished_rod_HP, lifting_cost, fluid_above_pump, pump_intake_pressure, kWh_regen, inflow_rate) VALUES ('%s', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f');""" % (gauge_date, gaugeoff_tags['percent_run'].value, gaugeoff_tags['kwh'].value, gaugeoff_tags['electricity_cost'].value, gaugeoff_tags['max_load'].value, gaugeoff_tags['min_load'].value, gaugeoff_tags['average_spm'].value, gaugeoff_tags['production_calculated'].value, gaugeoff_tags['full_card_production'].value, gaugeoff_tags['polished_rod_hp'].value, gaugeoff_tags['lifting_cost'].value, gaugeoff_tags['fluid_level'].value, gaugeoff_tags['pump_intake_pressure'].value, gaugeoff_tags['kwh_regen'].value, gaugeoff_tags['inflow_rate'].value)) con.commit() already_gauged_off = True print "Gauged off!" ################## # WELL TEST DATA # ################## well_test_entered = readTag(PLC_IP_ADDRESS, "Well_Test.Test_Submit")[0] > 0 if (well_test_entered == 0): if already_entered_well_test: already_entered_well_test = False print "Already entered well Test... Setting well_test_entered to False" if (well_test_entered and (not already_entered_well_test)): for wtest in welltest_tags: w = welltest_tags[wtest] w.read(True) print "Well Test Entered" print('{}/{}/{} {}:{}:{}'.format(welltest_tags['year'].value, welltest_tags['month'].value, welltest_tags['day'].value, welltest_tags['hour'].value, welltest_tags['min'].value, welltest_tags['sec'].value)) test_date = datetime(year=welltest_tags['year'].value, month=welltest_tags['month'].value, day=welltest_tags['day'].value, hour=welltest_tags['hour'].value, minute=welltest_tags['min'].value, second=welltest_tags['sec'].value) with con: cur = con.cursor() test_query = "INSERT INTO Well_Test (test_date, test_volume_oil, test_volume_water, test_volume_gas, k_factor, projected_volume_oil, projected_volume_water, api_gravity_oil, sg_water, test_hours) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}');".format(test_date, welltest_tags['v_oil'].value, welltest_tags['v_water'].value, welltest_tags['v_gas'].value, welltest_tags['k_factor'].value, welltest_tags['p_v_oil'].value, welltest_tags['p_v_water'].value, welltest_tags['api_oil'].value, welltest_tags['sg_water'].value, welltest_tags['test_duration'].value) # print test_query con.execute(test_query) con.commit() 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: {}".format(e)) traceback.print_exc() if __name__ == '__main__': main()