diff --git a/src/agent_gsm.py b/src/agent_gsm.py index e731699..548c791 100644 --- a/src/agent_gsm.py +++ b/src/agent_gsm.py @@ -353,7 +353,6 @@ def run_caller_task(t): # Start call. It will analyse audio as well and upload results make_call(target_addr) - # Runs caller probe - load task list and perform calls @@ -387,7 +386,7 @@ def run_probe(): if TASK_LIST.tasks is not None: utils.log_verbose(f"Resulting task list: {TASK_LIST.tasks}") - + # Run test immediately if specified if CONFIG.ForceRun and len(TASK_LIST.tasks) > 0: run_caller_task(TASK_LIST.tasks[0]) break @@ -414,15 +413,32 @@ def run_probe(): except Exception as err: utils.log_error(message="Unexpected error.", err=err) - spent_time = utils.get_monotonic_time() - start_time + # Sleep for + spent_time = utils.get_monotonic_time() - start_time - # Wait 1 minute - if spent_time < 60: - time.sleep(60 - spent_time) + # Wait 1 minute + if spent_time < 60: + timeout_time = 60 - spent_time + else: + timeout_time = 0 + + # Try to get next task + try: + if agent_point.WEB_QUEUE is None: + utils.log('Web task queue is None') + + task = agent_point.WEB_QUEUE.get(block=True, timeout=timeout_time) + + if task is not None: + run_caller_task(task) + except: + # Do nothing here, it is normal to get exception + + pass # In case of empty task list wait 1 minute before refresh - if len(TASK_LIST.tasks) == 0: - time.sleep(60) + # if len(TASK_LIST.tasks) == 0: + # time.sleep(60) def remove_pid_on_exit(): @@ -474,6 +490,14 @@ if __name__ == '__main__': signal.signal(signal.SIGINT, receive_signal) signal.signal(signal.SIGQUIT, receive_signal) + if CONFIG.CacheDir: + CACHE = utils_cache.InfoCache(dir=CONFIG.CacheDir) + + # Start own hotspot and API server + agent_point.CONFIG = CONFIG + agent_point.CACHE = CACHE + agent_point.start() + # Preconnect the phone if CONFIG.BT_MAC: # Connect to phone before @@ -485,7 +509,7 @@ if __name__ == '__main__': utils.log_error(f'No BT MAC specified, cannot connect. Exiting.') raise SystemExit(EXIT_ERROR) - # Init BT modem + # Init BT modem - here we wait for it bt_call_controller.init() # Logging settings @@ -494,18 +518,12 @@ if __name__ == '__main__': if CONFIG.LogPath: utils.open_log_file(CONFIG.LogPath, 'at') - - if CONFIG.CacheDir: - CACHE = utils_cache.InfoCache(dir=CONFIG.CacheDir) - - # Update path to pvqa/aqua-wb VOICE_QUALITY_AVAILABLE = utils_sevana.find_binaries(DIR_PROJECT / 'bin') # Load latest licenses & configs - this requires utils_sevana.find_binaries() to be called before # utils_sevana.load_config_and_licenses(config['backend']) - # Limit number of calls if CONFIG.TaskLimit: utils.log(f'Limiting number of calls to {CONFIG.TaskLimit}') @@ -533,12 +551,6 @@ if __name__ == '__main__': utils.log_error(f'Failed to obtain information about {BACKEND.instance}. Exiting.') exit(EXIT_ERROR) - # if not BACKEND.online: - - # Start own hotspot and API server - agent_point.CACHE = CACHE - agent_point.start() - # Cache phone information CACHE.put_phone(BACKEND.phone) diff --git a/src/agent_point.py b/src/agent_point.py index f0f211d..e77ed2b 100755 --- a/src/agent_point.py +++ b/src/agent_point.py @@ -3,7 +3,9 @@ import bottle import multiprocessing import time import os +import json import utils_cache +from agent_config import AgentConfig class AccessPoint: active: bool = False @@ -18,22 +20,54 @@ class AccessPoint: def stop(self): pass +# Just a stub for now ACCESS_POINT = AccessPoint() + +# Web server process SERVER_PROCESS = None + +# Good status response RESPONSE_OK = {'status': 'ok'} + +# Available information in cache CACHE : utils_cache.InfoCache = None +CONFIG: AgentConfig = None + +# Web queue +WEB_QUEUE = multiprocessing.Manager().Queue() @bottle.route('/status') def web_status(): print(f'Serving /status request...') - return RESPONSE_OK + + r = RESPONSE_OK + if CONFIG is not None: + r['name'] = CONFIG.Name + r['backend'] = CONFIG.Backend + r['bt_mac'] = CONFIG.BT_MAC + + if CACHE is not None: + print('Cache is found...') + # Phone information + phone = CACHE.get_phone(CONFIG.Name) + if phone is not None: + print('Phone information is found...') + r['phone'] = phone.to_dict() + + # Task list information + task_list = CACHE.get_tasks(CONFIG.Name) + if task_list is not None and task_list.tasks is not None: + r['task_list'] = task_list.tasks + else: + print('Cache not found.') + return r @bottle.route('/reboot') def web_reboot(): os.system('sudo reboot') return RESPONSE_OK -@bottle.route('/stop') +@bottle.route('/halt') def web_reboot(): os.system('sudo halt') return RESPONSE_OK @@ -50,8 +84,27 @@ def web_list_cache(): return result +@bottle.route('/call', method=['POST']) +def web_call(): + global WEB_QUEUE + try: + data = bottle.request.json + + # Send task definition + print('Sending data to ougoing queue...') + WEB_QUEUE.put_nowait(data) + + print('Returning OK response.') + return RESPONSE_OK + except Exception as e: + print(f'{str(e)}') + return RESPONSE_OK + + +def web_process(mp_queue: multiprocessing.Queue): + #global WEB_QUEUE + #WEB_QUEUE = mp_queue -def web_process(): print(f'Run web process...') bottle.run(host='0.0.0.0', port=8080) @@ -59,7 +112,7 @@ def start(): global ACCESS_POINT, SERVER_PROCESS ACCESS_POINT.start() - SERVER_PROCESS = multiprocessing.Process(target=web_process) + SERVER_PROCESS = multiprocessing.Process(target=web_process, args=(None,)) SERVER_PROCESS.start() def stop():