from google.cloud import storage import time import firebase_admin from firebase_admin import credentials from firebase_admin import db import traceback import sys import json import requests import time from firebase_admin import firestore from send_moni_noti import send_moni_noti from create_interactive_html_report import make_interactive_html_report session_type = 0 try: session_type = sys.argv[1] except: print(traceback.format_exc()) cred = credentials.Certificate("servicekey.json") # os.system("rm -rf AwsData") try: firebase_admin.initialize_app( cred, {"databaseURL": "https://farmbase-b2f7e-31c0c.firebaseio.com/"} ) except: qysy = 1 dbF = firestore.client() storage_client = storage.Client(project='farmbase-b2f7e') bucket_name = "farmbase-b2f7e.appspot.com" db_firestore = firestore.client() disabledUIDs = [ "snQYQZqQx3SmVbRztmEqYn5Mkcz2", "KZQ7TZIYXnXN0b07OtrL1hlyYij1", "CeMGYvLXrGR5ZThxZ46iV7vY8sa2", "TCXcp5VIsfhHZrh0nm2VsgBtcGy2", "mFFHQdEtiSbn2hbYQAwwoIdYVi02" ] def upload_report(uid): bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(("PaidMonitoredFields/" + uid + "/interactive_report.html")) blob.upload_from_filename((uid + "/interactive_report.html")) req_obj = { "UID": uid, "notification_body": "New Satellite Results For Your Field " + field_name + " Are Ready! Open the App Now!", "notification_title": "New Report Is Available!" } headers = {'Content-type': 'application/json'} url = 'https://us-central1-farmbase-b2f7e.cloudfunctions.net/send_fcm_notification' response = requests.post(url, data=json.dumps(req_obj), headers=headers) def instant_session(): #todo a = 1 #print(a) while a ==1: #print(a) report_requests_obj = db_firestore.collection(u'InteractiveReportRequests').where('Processed','==',0).get() #print(report_requests_obj) try: for temp_d in report_requests_obj: uid = temp_d.id uid = str(uid) print(uid) fields_arr = [] field_list_obj = db.reference('PaidMonitoredFields').child('PMF').child(uid).get(False,False) for field_id, val in field_list_obj.items(): fields_arr.append(int(field_id)) latest_field = str(max(fields_arr)) sensed_days_obj = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(latest_field).child('SensedDays').get() if sensed_days_obj is not None: print(temp_d.id) print(temp_d.to_dict()) temp_main_obj = temp_d.to_dict() #uid = temp_main_obj.get("UID", None) make_interactive_html_report(uid) temp_main_obj["Processed"] = 1 db_firestore.collection(u'InteractiveReportRequests').document(uid).set(temp_main_obj) upload_report(uid) except: print(traceback.format_exc()) time.sleep(60) def daily_session(): while True: uid_list = db.reference('PaidMonitoredFields').child('PMF').get(False, True) for uid, val in uid_list.items(): if uid not in disabledUIDs: try: make_interactive_html_report(uid) upload_report(uid) except: print(traceback.format_exc()) time.sleep((5*86400)) print(session_type) if session_type == '0': #print('ee') instant_session() elif session_type == '1': daily_session() else: a = 1