diff --git a/dataLoggerMySQL.py b/dataLoggerMySQL.py index f06d9c2..c21a97c 100644 --- a/dataLoggerMySQL.py +++ b/dataLoggerMySQL.py @@ -17,6 +17,30 @@ import time import MySQLdb +def checkDateInDB(da): + y = int(da[0:4]) + m = int(da[4:6]) + d = int(da[6:8]) + dquery = "SELECT id FROM WellData.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;" + db = MySQLdb.connect(host="127.0.0.1",user="website",passwd="henrypump",db="WellData") + cur = db.cursor() + cur.execute(dquery) + dates = cur.fetchall() + # db.commit() + db.close() + if len(dates) > 0: + print("Date {0} already in db".format(da)) + else: + ins_query = "INSERT INTO WellData.card_history_dates (year, month, day, first_id) VALUES ({0}, {1}, {2}, (SELECT MAX(id) FROM WellData.card_history));".format(y, m, d) + print(ins_query) + db = MySQLdb.connect(host="127.0.0.1",user="website",passwd="henrypump",db="WellData") + cur = db.cursor() + cur.execute(ins_query) + db.commit() + db.close() + + db = MySQLdb.connect(host="127.0.0.1",user="website",passwd="henrypump",db="WellData") cur = db.cursor() @@ -178,6 +202,7 @@ cards_read = 1 read_tapers = False already_gauged_off = False already_entered_well_test = False +last_date = "" while True: now = datetime.now() with open('/mnt/usb/status.txt', 'w') as status_file: @@ -192,7 +217,6 @@ while True: Already_Read = ReadFromPLC("Card_Past[1].Data_Read", "INT") > 0 num_points = ReadFromPLC("Card_Past[1].Num_Points", "INT") - if (EOS and not Already_Read and (EOS != 911)): stroke_time = time.time() dt = datetime.fromtimestamp(stroke_time) @@ -215,6 +239,9 @@ while True: 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 if (data["Card_Type"] == 0): data["Card_Type"] = "Normal"