diff --git a/SQLite/create_SQLite_db.sql b/SQLite/create_SQLite_db.sql index e2e1f46..1104756 100644 --- a/SQLite/create_SQLite_db.sql +++ b/SQLite/create_SQLite_db.sql @@ -43,11 +43,18 @@ CREATE TABLE IF NOT EXISTS Well_Test ( deleted INTEGER DEFAULT 0 ); +-- CREATE TABLE IF NOT EXISTS config ( +-- id INTEGER PRIMARY KEY, +-- device_type TEXT, +-- ip_address TEXT, +-- dateChanged TIMESTAMP DEFAULT CURRENT_TIMESTAMP +-- ); + CREATE TABLE IF NOT EXISTS config ( id INTEGER PRIMARY KEY, - device_type TEXT, - ip_address TEXT, - dateChanged TIMESTAMP DEFAULT CURRENT_TIMESTAMP + parameter TEXT, + val TEXT, + dateAdded TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS Notes ( diff --git a/create_MySQL_db.sql b/create_MySQL_db.sql index ad90619..c966be2 100644 --- a/create_MySQL_db.sql +++ b/create_MySQL_db.sql @@ -48,12 +48,20 @@ CREATE TABLE IF NOT EXISTS poconsole.Well_Test ( PRIMARY KEY (id) ); +-- CREATE TABLE IF NOT EXISTS poconsole.config ( +-- id int(11) NOT NULL AUTO_INCREMENT, +-- device_type varchar(64), +-- ip_address varchar(64), +-- dateChanged datetime DEFAULT NOW(), +-- PRIMARY KEY (id) +-- ); + CREATE TABLE IF NOT EXISTS poconsole.config ( - id int(11) NOT NULL AUTO_INCREMENT, - device_type varchar(64), - ip_address varchar(64), - dateChanged datetime DEFAULT NOW(), - PRIMARY KEY (id) + id INT NOT NULL AUTO_INCREMENT, + parameter varchar(128), + val varchar(128), + dateAdded TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (id) ); CREATE TABLE IF NOT EXISTS poconsole.Notes ( diff --git a/dataLogger_SQLite.py b/dataLogger_SQLite.py index cdec16a..e0c97e8 100644 --- a/dataLogger_SQLite.py +++ b/dataLogger_SQLite.py @@ -13,24 +13,64 @@ import sqlite3 as lite from pycomm.ab_comm.clx import Driver as ClxDriver # import logging from tag.tag_sqlite import Tag +from tag.tag_sqlite import AnalogAlarm +from tag.tag_sqlite import bitAlarm import traceback con = lite.connect("/mnt/usb/data.db") # con = lite.connect("/Users/patrickjmcd/Desktop/data.db") -PLC_IP_ADDRESS = "192.168.1.10" -PLC_TYPE = "VFD" +configProperties = {} def readConfig(): - global PLC_IP_ADDRESS, PLC_TYPE + global configProperties + configObj = {} with con: cur = con.cursor() - query = "SELECT * FROM config ORDER BY dateChanged DESC LIMIT 1;" + query = "SELECT parameter, val FROM config GROUP BY parameter;" cur.execute(query) - setup = cur.fetchall() - PLC_IP_ADDRESS = setup[0][2] - PLC_TYPE = setup[0][1] + config = cur.fetchall() + for x in config: + configObj[x[0]] = x[1] + try: + configProperties['PLC_IP_ADDRESS'] = str(configObj['ip_address']) + print("FYI, using PLC IP Address from the database {0}".format(configProperties['PLC_IP_ADDRESS'])) + except KeyError: + print("FYI, there is no PLC IP Address stored in the database, defaulting to 192.168.1.10") + configProperties['PLC_IP_ADDRESS'] = "192.168.1.10" + + try: + configProperties['plc_type'] = str(configObj['plc_type']) + print("FYI, using PLC Type from the database {0}".format(configProperties['plc_type'])) + except KeyError: + print("FYI, there is no PLC Type stored in the database, defaulting to CLX") + configProperties['plc_type'] = "CLX" + + try: + configProperties['scan_rate'] = int(configObj['scan_rate']) + print("FYI, using Scan Rate from the database {0}".format(configProperties['scan_rate'])) + except KeyError: + print("FYI, there is no Scan Rate stored in the database, defaulting to 10 seconds") + configProperties['scan_rate'] = 10 + + try: + sa_test = str(configObj['save_all']) + if sa_test.lower() == "true": + configProperties['save_all'] = True + elif sa_test.lower() == "false": + configProperties['save_all'] = False + else: + configProperties['save_all'] = "test" + print("FYI, value for save_all is {0}".format(configProperties['save_all'])) + except KeyError: + print("FYI, there is no save_all value stored in the database, using 'test'") + configProperties['save_all'] = 'test' + +try: + readConfig() +except: + traceback.print_exc() def readTag(addr, tag): time.sleep(0.01) @@ -42,7 +82,6 @@ def readTag(addr, tag): return v except Exception: print("ERROR RETRIEVING TAG: {}".format(tag)) - err = c.get_status() c.close() print traceback.print_exc() pass @@ -96,14 +135,10 @@ class Status(Tag): cur.execute(query) con.commit() self.last_send_time = time.time() - if statusChanged: - self.lastStatusCheckVal = self.status - - # ---------- MAP FUNCTIONS ---------- # maps = { - 'modeMap':{ + 'modeMap': { 0: "Error", 1: "Auto", 2: "POC", @@ -148,7 +183,9 @@ gaugeoff_tags = {} # Tags stored at gauge off welltest_tags = {} # Tags stored at well test submit bit_tags = {} safety_tags = {} -status = Status('run_status', 'Pump.Run_Status', 'STRING', 0, 3600, 0, mapFn=maps['statusMap']) +custom_tags = {} +status = Status('run_status', 'Pump.Run_Status', 0, 'STRING', 0, 3600, mapFn=maps['statusMap']) + def setupTags(): with con: @@ -156,41 +193,47 @@ def setupTags(): query = "SELECT t.name as name, c.tag_class as class, t.tag as tag, t.data_type as data_type, t.change_threshold as change_threshold, t.guarantee_sec as guarantee_sec, t.id as id, t.map_function as map_function FROM tags t JOIN tag_classes c ON c.id = t.class;" cur.execute(query) tags = cur.fetchall() + # (u'downhole_gross_stroke', u'history', u'Card_Past[1].Downhole_GrossStroke', u'REAL', 2.0, 3600, 6, None) # 0: name, 1: class, 2: tag, 3: data_type, 4: change_threshold, 5: guarantee_sec, 6: db id, 7: map_function for x in tags: print(x) if str(x[1]) == 'stroke': if x[7]: - stroke_tags[x[0]] = Tag(str(x[0]), str(x[2]), str(x[3]), x[4], x[5], x[6], mapFn=maps[str(x[7])]) + stroke_tags[x[0]] = Tag(str(x[0]), str(x[2]), x[6], str(x[3]), x[4], x[5], mapFn=maps[str(x[7])]) else: - stroke_tags[x[0]] = Tag(str(x[0]), str(x[2]), str(x[3]), x[4], x[5], x[6]) + stroke_tags[x[0]] = Tag(str(x[0]), str(x[2]), x[6], str(x[3]), x[4], x[5]) elif str(x[1]) == 'history': if x[7]: - history_tags[x[0]] = Tag(str(x[0]), str(x[2]), str(x[3]), x[4], x[5], x[6], mapFn=maps[str(x[7])]) + history_tags[x[0]] = Tag(str(x[0]), str(x[2]), x[6], str(x[3]), x[4], x[5], mapFn=maps[str(x[7])]) else: - history_tags[x[0]] = Tag(str(x[0]), str(x[2]), str(x[3]), x[4], x[5], x[6]) + history_tags[x[0]] = Tag(str(x[0]), str(x[2]), x[6], str(x[3]), x[4], x[5]) elif str(x[1]) == 'gaugeoff': if x[7]: - gaugeoff_tags[x[0]] = Tag(str(x[0]), str(x[2]), str(x[3]), x[4], x[5], x[6], mapFn=maps[str(x[7])]) + gaugeoff_tags[x[0]] = Tag(str(x[0]), str(x[2]), x[6], str(x[3]), x[4], x[5], mapFn=maps[str(x[7])]) else: - gaugeoff_tags[x[0]] = Tag(str(x[0]), str(x[2]), str(x[3]), x[4], x[5], x[6]) + gaugeoff_tags[x[0]] = Tag(str(x[0]), str(x[2]), x[6], str(x[3]), x[4], x[5]) elif str(x[1]) == 'welltest': if x[7]: - welltest_tags[x[0]] = Tag(str(x[0]), str(x[2]), str(x[3]), x[4], x[5], x[6], mapFn=maps[str(x[7])]) + welltest_tags[x[0]] = Tag(str(x[0]), str(x[2]), x[6], str(x[3]), x[4], x[5], mapFn=maps[str(x[7])]) else: - welltest_tags[x[0]] = Tag(str(x[0]), str(x[2]), str(x[3]), x[4], x[5], x[6]) + welltest_tags[x[0]] = Tag(str(x[0]), str(x[2]), x[6], str(x[3]), x[4], x[5]) + elif str(x[1]) == 'custom': + if x[7]: + custom_tags[x[0]] = Tag(str(x[0]), str(x[2]), x[6], str(x[3]), x[4], x[5], mapFn=maps[str(x[7])]) + else: + custom_tags[x[0]] = Tag(str(x[0]), str(x[2]), x[6], str(x[3]), x[4], x[5]) with con: cur = con.cursor() - 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;" + query = "SELECT c.alarm_class as class, a.name as name, a.tag as tag, a.condition as condition, a.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]), int(x[4]), device_type="CLX", ip_address=PLC_IP_ADDRESS) + safety_tags[x[1]] = AnalogAlarm(str(x[1]), str(x[2]), int(x[4]), device_type="CLX", ip_address=configProperties['PLC_IP_ADDRESS']) elif str(x[0]) == 'bit': - 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) + bit_tags[x[1]] = bitAlarm(str(x[1]), str(x[2]), str(x[3]), int(x[4]), device_type="CLX", ip_address=configProperties['PLC_IP_ADDRESS']) print('===== STROKE TAGS =====') for t in stroke_tags: @@ -219,27 +262,29 @@ def setupTags(): setupTags() + 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:] + global configProperties + num_points = readTag(configProperties['PLC_IP_ADDRESS'], "Card_Past[1].Num_Points")[0] + surf_pos = readArray(configProperties['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 = readArray(configProperties['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 = readArray(configProperties['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 = readArray(configProperties['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(): + global configProperties ts = time.time() - numTapers = int(readTag(PLC_IP_ADDRESS, 'Card_Current.Params.Num_Tapers')[0]) + numTapers = int(readTag(configProperties['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] + taper_length = readTag(configProperties['PLC_IP_ADDRESS'], 'Taper.Taper[{}].Setup.Length'.format(t))[0] + taper_diameter = readTag(configProperties['PLC_IP_ADDRESS'], 'Taper.Taper[{}].Setup.Diameter'.format(t))[0] + taper_material = readTag(configProperties['PLC_IP_ADDRESS'], 'Taper.Taper[{}].Setup.Material'.format(t))[0] if (taper_material == 1): taper_material = "Steel" elif (taper_material == 2): @@ -253,7 +298,7 @@ def evalTapers(): cur.execute(tQuery) con.commit() - pump_diameter = readTag(PLC_IP_ADDRESS, 'UnitConfig.Pump_Diameter')[0] + pump_diameter = readTag(configProperties['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() @@ -264,21 +309,27 @@ def evalTapers(): def main(): - readConfig() + global configProperties + read_tapers = False already_gauged_off = False already_entered_well_test = False last_date = "" last_stroke = 0 + last_status = "" + statusChanged = False while True: try: - status.read("test") + current_status = status.read("test") + statusChanged = not (current_status == last_status) + if statusChanged: + last_status = current_status ############# # CARD DATA # ############# - EOS = readTag(PLC_IP_ADDRESS, "End_Of_Stroke")[0] + EOS = readTag(configProperties['PLC_IP_ADDRESS'], "End_Of_Stroke")[0] stroke_tags['card_id'].read('test') if (EOS and not (last_stroke == stroke_tags['card_id'].value)): sData = {} @@ -321,11 +372,15 @@ def main(): h = history_tags[hist] h.read("test") + for cust in custom_tags: + t = custom_tags[hist] + t.read("test") + ############## # TAPER DATA # ############## - update_taper = readTag(PLC_IP_ADDRESS, "Write_Tapers")[0] > 0 + update_taper = readTag(configProperties['PLC_IP_ADDRESS'], "Write_Tapers")[0] > 0 if (update_taper == 0): if read_tapers: read_tapers = False @@ -338,7 +393,7 @@ def main(): ################## # GAUGE OFF DATA # ################## - gauge_off = readTag(PLC_IP_ADDRESS, "Gauge_Off_Command")[0] + gauge_off = readTag(configProperties['PLC_IP_ADDRESS'], "Gauge_Off_Command")[0] if (gauge_off == 0): if already_gauged_off: already_gauged_off = False @@ -364,7 +419,7 @@ def main(): # WELL TEST DATA # ################## - well_test_entered = readTag(PLC_IP_ADDRESS, "Well_Test.Test_Submit")[0] > 0 + well_test_entered = readTag(configProperties['PLC_IP_ADDRESS'], "Well_Test.Test_Submit")[0] > 0 if (well_test_entered == 0): if already_entered_well_test: already_entered_well_test = False diff --git a/mysql_cfg.pickle b/mysql_cfg.pickle new file mode 100644 index 0000000..fdde65b --- /dev/null +++ b/mysql_cfg.pickle @@ -0,0 +1,18 @@ +(dp0 +S'host' +p1 +S'127.0.0.1' +p2 +sS'password' +p3 +S'henrypump' +p4 +sS'user' +p5 +S'website' +p6 +sS'database' +p7 +S'poconsole' +p8 +s. \ No newline at end of file