rastabrew

git clone https://git.tarina.org/rastabrew
Log | Files | Refs

commit 891202570745e0b02cc4882c59f36906fdb412fe
parent 3a725a96df6489c9ea5f1438fb4a52f6d8fa9a45
Author: rob <rob@tarina.org>
Date:   Wed, 24 Aug 2022 10:44:12 +0300

save offline then push online

Diffstat:
Mrastabrew.py | 33+++++++++++++++++++++++++++++++--
Mride_on.py | 36++++++++++++++++++++++++++++++++++--
2 files changed, 65 insertions(+), 4 deletions(-)

diff --git a/rastabrew.py b/rastabrew.py @@ -8,6 +8,7 @@ import datetime import requests import web import config +import socket basedir = os.path.dirname(os.path.realpath(__file__)) + '/' db_local = basedir+'rastabrew.db' @@ -23,6 +24,26 @@ GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # RASTABREW bubbles = 0 bubble1time = False brewerishere = False +haz_been_offline = False + +def is_webz_on(): + try: + socket.create_connection(("google.com",80)) + return True + except: + pass + print('no internet connection!') + return False + +def send_offline_data(tablename): + print('sending offline data!') + #send offline data once online again + od = db.select(tablename) + for i in od: + db2.insert('bubblecounter', tid=i.tid) + #db.delete('bubblecounter', where="'id="+str(i.id)+"'") + db.query("delete from bubblecounter where id="+str(i.id)+";") + print('shit! not offline again!') start_time = time.time() get_code_time = time.time() @@ -35,8 +56,16 @@ while True: if bubble1time == True: bubbles = bubbles + 1 bubble1time = False - db.insert('bubblecounter', tid=datetime.datetime.now()) - db2.insert('bubblecounter', tid=datetime.datetime.now()) + if is_webz_on() == True: + if haz_been_offline == True: + send_offline_data('bubblecounter') + haz_been_offline = False + try: + db2.insert('bubblecounter', tid=datetime.datetime.now()) + except Exception as e: print(e) + else: + haz_been_offline = True + db.insert('bubblecounter', tid=datetime.datetime.now()) time.sleep(0.01) print(term.clear) print('Blub blub *.0 o ' + str(bubbles) + ' bubble counter') diff --git a/ride_on.py b/ride_on.py @@ -8,6 +8,7 @@ from blessed import Terminal import datetime import web import config +import socket basedir = os.path.dirname(os.path.realpath(__file__)) + '/' @@ -26,10 +27,33 @@ with Onewire('u') as o: temp2 = o.sensor('28.2F1F7C060000').temperature print('Humidity: ' + humidity + ' Temp: ' + temp1 + ' Temp2: ' + temp2) +def is_webz_on(): + try: + socket.create_connection(("google.com",80)) + return True + except: + pass + print('no internet connection!') + return False + +def send_offline_data(tablename): + print('sending offline data!') + #send offline data once online again + od = db.select('sensors', what='id,tid,pir,humidity,temp1,temp2') + for i in od: + print(i) + #try: + db2.insert('sensors', tid=i.tid, pir=bool(i.pir), humidity=i.humidity, temp1=i.temp1, temp2=i.temp2) + db.query("delete from sensors where id="+str(i.id)+";") + #except: + # print('shit! not offline again!') + # return + GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) #LAMP RELAY GPIO.setup(4, GPIO.IN) # PIR1 +haz_been_offline = False brewerishere = False while True: @@ -45,8 +69,16 @@ while True: brewerishere = False print(term.clear) print('Humidity: ' + humidity + ' Temp: ' + temp1 + ' Temp2: ' + temp2) - db.insert('sensors', tid=datetime.datetime.now(), pir=brewerishere, humidity=humidity, temp1=temp1, temp2=temp2) - db2.insert('sensors', tid=datetime.datetime.now(), pir=brewerishere, humidity=humidity, temp1=temp1, temp2=temp2) + if is_webz_on() == True: + if haz_been_offline == True: + haz_been_offline = False + send_offline_data('bubblecounter') + try: + db2.insert('sensors', tid=datetime.datetime.now(), pir=brewerishere, humidity=humidity, temp1=temp1, temp2=temp2) + except Exception as e: print(e) + else: + haz_been_offline = True + db.insert('sensors', tid=datetime.datetime.now(), pir=brewerishere, humidity=humidity, temp1=temp1, temp2=temp2) time.sleep(10) if brewerishere: print('Brewer is here')