rastabrew.py (2161B)
1 #!/bin/env python3 2 3 import os 4 import time 5 import RPi.GPIO as GPIO 6 from blessed import Terminal 7 import datetime 8 import requests 9 import web 10 import config 11 import socket 12 13 basedir = os.path.dirname(os.path.realpath(__file__)) + '/' 14 db_local = basedir+'rastabrew.db' 15 #db = web.database(dbn='sqlite', db=basedir + 'rastabrew.db') 16 db = web.database(dbn='sqlite', db=db_local) 17 db2 = web.database(dbn=config.dbn, host=config.host, user=config.user, port=config.port, db=config.db, pw=config.pw) 18 19 term = Terminal() 20 pin = 22 21 GPIO.setmode(GPIO.BCM) 22 GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # RASTABREW 23 24 bubbles = 0 25 bubble1time = False 26 brewerishere = False 27 haz_been_offline = False 28 29 def is_webz_on(): 30 try: 31 socket.create_connection(("google.com",80)) 32 return True 33 except: 34 pass 35 print('no internet connection!') 36 return False 37 38 def send_offline_data(tablename): 39 print('sending offline data!') 40 #send offline data once online again 41 od = db.select(tablename) 42 for i in od: 43 db2.insert('bubblecounter', tid=i.tid) 44 #db.delete('bubblecounter', where="'id="+str(i.id)+"'") 45 db.query("delete from bubblecounter where id="+str(i.id)+";") 46 print('shit! not offline again!') 47 48 start_time = time.time() 49 get_code_time = time.time() 50 while True: 51 run_time = time.time() - start_time 52 update = time.time() - get_code_time 53 if GPIO.input(pin): 54 bubble1time = True 55 else: 56 if bubble1time == True: 57 bubbles = bubbles + 1 58 bubble1time = False 59 if is_webz_on() == True: 60 if haz_been_offline == True: 61 send_offline_data('bubblecounter') 62 haz_been_offline = False 63 try: 64 db2.insert('bubblecounter', tid=datetime.datetime.now()) 65 except Exception as e: print(e) 66 else: 67 haz_been_offline = True 68 db.insert('bubblecounter', tid=datetime.datetime.now()) 69 time.sleep(0.01) 70 print(term.clear) 71 print('Blub blub *.0 o ' + str(bubbles) + ' bubble counter') 72 if brewerishere: 73 print('Brewer is here') 74 #code.interact(local=locals()) 75