def delete(self, path, key): try: ref = self.db_firestore.reference(path) ref.child(key).delete() except Exception as e: save_error_log(e, "db.py", "delete(self, " + path + ", " + key + ")")
def get(self, collection, document): try: doc_ref = self.db_firestore.collection(collection).document( document) return doc_ref.get().to_dict() except Exception as e: save_error_log(e, "db.py", "get(self, " + collection + "/" + document + ")")
def get_interval(context): from logger import save_error_log try: result = context.get('rpi-setting', "healthCheckPeriod")['value'] return int(result) except Exception as e: save_error_log(e, "health.py", "get_interval") return 60
def __init__(self): try: cred = credentials.Certificate('me-arash-firebase-adminsdk.json') firebase_admin.initialize_app(cred, { 'projectId': "me-arash", }) self.db_firestore = firestore.client() except Exception as e: save_error_log(e, "db.py", "__init__(self)")
def add(self, path, data): try: id = str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")) doc_ref = self.db_firestore.collection(path).document(id) doc_ref.set(data) self.update_catalog(path, 'add', id) return doc_ref except Exception as e: save_error_log(e, "db.py", "add(self, " + path + ", " + str(data) + ")", "", True)
def update_catalog(self, path, operator, id): try: db_name = 'db-catalogs' data = self.get(db_name, path) if data: data_ref = self.db_firestore.collection(db_name).document(path) data_to_update = { "count": int(data['count'] + 1), "last_update": id } data_ref.update(data_to_update) else: data = {"count": 1, "last_update": id} self.db_firestore.collection(db_name).document(path).set(data) except Exception as e: save_error_log( e, "db.py", "update_catalog(self, " + path + ", " + str(data) + ")", "", True)
def main(context): from grove.helper import SlotHelper interval = 1 try: sh = SlotHelper(SlotHelper.ADC) pin = sh.argv2pin() sensor = GroveMoistureSensor(pin) print('Detecting moisture...') while True: m = sensor.moisture if m >= 0 and m < 300: result = 'Dry' elif m >= 300 and m < 600: result = 'Moist' else: result = 'Wet' #print('Moisture value: {0}, {1}'.format(m, result)) data = {"moisture": m, "date": datetime.datetime.now()} write_result = context.add("RPI-moisture", data) if write_result != None: print(str(data)) else: pass interval = int( context.get('rpi-setting', "moisturePeriod")['value']) time.sleep(interval) except Exception as e: print(str(e)) error_id = save_error_log(e, "Health/moisture.py", "...") data = {"success": False, "error": error_id} context.add("RPI-moisture", data) time.sleep(interval)
def execute_healt(context): import time import sys import os from health_information import provide_info from logger import save_error_log sys.path.append( "/home/arash/work/IOT/Health/venv/lib/python3.7/site-packages/") os.system('cls' if os.name == 'nt' else 'echo -e \\\\033c') count = 0 while True: count += 1 if count >= 10: os.system('cls' if os.name == 'nt' else 'echo -e \\\\033c') count = 0 try: interval = get_interval(context) data = provide_info() write_result = context.add("RPI-health", data) if write_result != None: print(str(data)) else: pass if interval == None: interval = 2 time.sleep(interval) except Exception as e: print(str(e)) error_id = save_error_log(e, "Health/main.py", "...") data = {"success": False, "error": error_id} context.add("RPI-health", data) time.sleep(interval)