from server2023 import server2023 import firebase_admin from firebase_admin import credentials from firebase_admin import db from firebase_admin import messaging import time import threading import os import sys import json import traceback from numba import jit, cuda import sys import os import traceback import json import pandas as pd try: session_type = sys.argv[1] except: print(traceback.format_exc()) cred = credentials.Certificate('servicekey.json') try: firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) except: print('fire running') uid = 'snQYQZqQx3SmVbRztmEqYn5Mkcz2' import traceback def bulk_api_thread(ww): i = 1 iters =0 while i == 1: try: pending_list = db.reference('BulkPendingData').get() for (p,q) in pending_list.items(): uid = p for (x,y) in q.items(): fieldid = x print(fieldid) server2023(uid, fieldid) hist_obj = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('PreviousDataRequests').get() try: for(a,b) in hist_obj.item(): abb = 1 except: print('all requests processed') db.reference('BulkPendingData').child(uid).child(fieldid).delete() except: print(traceback.format_exc()) pending_list = {} print('sleeping for 1 mins.') time.sleep(60) def api_thread(ww): i = 1 iters =0 while i == 1: try: pending_list = db.reference('PendingData').get() for (p,q) in pending_list.items(): uid = p if uid != 'ipRHhCOFIDV2pxgg7Nfz1ufZBmV2': for (x,y) in q.items(): fieldid = x server2023(uid, fieldid) hist_obj = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('PreviousDataRequests').get() try: for(a,b) in hist_obj.item(): abb = 1 except: print('all requests processed') print(traceback.format_exc()) db.reference('PendingData').child(uid).child(fieldid).delete() except: print(traceback.format_exc()) pending_list = {} print('sleeping for 1 mins.') time.sleep(60) def tmux_thread(ww): while ww == '1': tmux_init() time.sleep(172800) # def main_b2b_bulk_thread(uid, n_tmux): # # while ww == '1': # tmux_init(uid,n_tmux) # #os.system("rm -rf AwsData") # time.sleep(86400) # #print(numm) def send_keys_tmux_session(session_num, fields_arr): fields_obj = {} for single_field in fields_arr: fields_obj[single_field[1]] = single_field[0] # if single_field[1] == '1680249793393': # print(single_field[1]) session_char = 'p' file_name = session_char + str(session_num) + ".json" with open(file_name, 'w') as json_file: json.dump(fields_obj, json_file) # with open(file_name, "w") as outfile: # outfile.write(fields_obj) print(len(fields_arr)) tmux_cmd = 'tmux send-keys -t ' + session_char + str(session_num) + ' "python3.7 ' + ' init_sat_tmux.py \'' + file_name + '\'" Enter' #tmux_cmd = 'tmux send-keys -t s' + str(session_num) + ' "python3.7 ' + ' init_sat_tmux.py ' + str(fields_obj) + '" Enter' print(tmux_cmd) os.system(tmux_cmd) def tmux_init(): all_fields = [] field_num = 0 n_tmux = 21 priority_proportion = 0.8 temp_list = db.reference("PaidMonitoredFields").child("PMF").get(False, True) temp_list = {} temp_list["ipRHhCOFIDV2pxgg7Nfz1ufZBmV2"] = True #temp_list["F34CLcmyzzczz7gt764WRQKXda72"] = True #temp_list["HF9dSIoDEYcTwebrRWVzb08SQln2"] = True clip_interval_num = 0 priority_uids = ['M53J9SImW9My4vghlCaaWLYxsc22','sAiJIWRbNOR5grDmRabVxW5BXp03'] disabledUIDs = [ "snQYQZqQx3SmVbRztmEqYn5Mkcz2", "KZQ7TZIYXnXN0b07OtrL1hlyYij1", "CeMGYvLXrGR5ZThxZ46iV7vY8sa2", "TCXcp5VIsfhHZrh0nm2VsgBtcGy2", "mFFHQdEtiSbn2hbYQAwwoIdYVi02", "sAiJIWRbNOR5grDmRabVxW5BXp03", "D4xU2QGhooXfK6qiEeHdAlp0wk53", "HxugQlTZOYXSHIAv5Qh63iBlMof2" ] filename = "UUID_FieldID_coordinates.xlsx" df3 = pd.read_excel(filename) field_ids = [] api_key = "dd6e11f5d20e4694bed72932241007" file_count = 0 for index, row in df3.iterrows(): # print(row["Field ID"]) field_id = str(row["Field ID"]) field_ids.append(str(field_id)) for uid, tt in temp_list.items(): uid_obj = db.reference("PaidMonitoredFields").child("PMF").child(uid).get(False, True) for fieldid, uu in uid_obj.items(): if (uid not in disabledUIDs) and (fieldid in field_ids): all_fields.append((uid, fieldid)) field_num = field_num + 1 interval = round(field_num/(n_tmux-1)) print([field_num, interval]) clipped_fields_arr = [] i = 0 priority_fields_num = 0 clip_interval_num = 0 for single_field in all_fields: uid, fieldid = single_field[0], single_field[1] if clip_interval_num < interval: clipped_fields_arr.append(single_field) priority_fields_num = priority_fields_num + 1 clip_interval_num = clip_interval_num+1 else: # print('priority field') send_keys_tmux_session((i+1),clipped_fields_arr) clipped_fields_arr = [] clip_interval_num = 0 i = i+1 time.sleep(10) send_keys_tmux_session((i+1),clipped_fields_arr) #server2023('f9A8DwHK73hLgLY5x3tXh9bfCBh2', '1708580450420') print(session_type) if int(session_type) == 0: tmux_thread('1') print('tmux') elif int(session_type) == 1: api_thread('1') print('api') elif int(session_type) == 2: bulk_api_thread('1') print('bulk api')