134 lines
2.8 KiB
Python
Executable File
134 lines
2.8 KiB
Python
Executable File
#!/usr/bin/python3
|
|
import bottle
|
|
import multiprocessing
|
|
import time
|
|
import os
|
|
import json
|
|
import utils_cache
|
|
from agent_config import AgentConfig
|
|
|
|
class AccessPoint:
|
|
active: bool = False
|
|
|
|
def __init__(self) -> None:
|
|
pass
|
|
|
|
|
|
def start(self):
|
|
pass
|
|
|
|
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...')
|
|
|
|
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('/halt')
|
|
def web_reboot():
|
|
os.system('sudo halt')
|
|
return RESPONSE_OK
|
|
|
|
@bottle.route('/cache')
|
|
def web_list_cache():
|
|
result = []
|
|
if CACHE is None:
|
|
return result
|
|
|
|
# Iterate cache and return available files list
|
|
for f in os.listdir(CACHE.dir):
|
|
result.append(f)
|
|
|
|
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
|
|
|
|
print(f'Run web process...')
|
|
bottle.run(host='0.0.0.0', port=8080)
|
|
|
|
def start():
|
|
global ACCESS_POINT, SERVER_PROCESS
|
|
|
|
ACCESS_POINT.start()
|
|
SERVER_PROCESS = multiprocessing.Process(target=web_process, args=(None,))
|
|
SERVER_PROCESS.start()
|
|
|
|
def stop():
|
|
global ACCESS_POINT, SERVER_PROCESS
|
|
|
|
ACCESS_POINT.stop()
|
|
SERVER_PROCESS.kill()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# Start test stuff
|
|
start()
|
|
|
|
# Wait 120 seconds for tests
|
|
time.sleep(120.0)
|
|
|
|
# Stop test
|
|
stop()
|