Working network test

This commit is contained in:
2024-01-18 16:35:02 -06:00
parent 58605dbe85
commit d2a4d93590
4 changed files with 180 additions and 31 deletions

86
run.py
View File

@@ -30,7 +30,7 @@ sensor_ready = False
vm_ready = False
killme = None
#pool = None
serverproc = None
def arm_start_callback(res):
global arm_ready
@@ -53,6 +53,7 @@ def vm_start_callback(res):
vm_ready = True
def wait_for(val, name):
#global val
if val is False:
fprint("waiting for " + name + " to complete...")
while val is False:
@@ -70,19 +71,8 @@ def start_server_socket():
app.run(host='0.0.0.0', port=5000)
def start_client_socket(server_ip):
def start_client_socket():
app = Flask(__name__)
server_url = 'http://' + server_ip + ':5000/report_ip'
def send_ip_to_server(server_url, client_ip):
try:
response = requests.post(server_url, json={'ip': client_ip}, timeout=1)
fprint(f"Server response: {response.text}")
return True
except requests.exceptions.RequestException as e:
fprint(f"Error sending IP to server: {e}")
return False
@app.route('/control_client', methods=['POST'])
def message_from_server():
@@ -91,13 +81,27 @@ def start_client_socket(server_ip):
fprint(f"Message from server: {data.get('message')}")
return "Message received", 200
while not send_ip_to_server(server_url, config["core"]["clientip"]):
sleep(1)
global vm_ready
vm_ready = True
app.run(host='0.0.0.0', port=6000)
def check_server_online(serverip, clientip):
def send_ip_to_server(server_url, client_ip):
try:
response = requests.post(server_url, json={'ip': client_ip}, timeout=1)
fprint(f"Server response: {response.text}")
return True
except requests.exceptions.RequestException as e:
fprint(f"Error sending IP to server: {e}")
return False
server_url = 'http://' + serverip + ':5000/report_ip'
while not send_ip_to_server(server_url, clientip):
sleep(1)
fprint("Successfully connected to server.")
return True
def setup_server(pool):
# linux server setup
global config
@@ -106,21 +110,37 @@ def setup_server(pool):
global camera_ready
global led_ready
global arm_ready
global serverproc
fprint("Starting Jukebox control system...")
pool.apply_async(ur5_control.init, (config["arm"]["ip"],), callback=arm_start_callback)
#pool.apply_async(led_control.init, callback=led_start_callback)
#pool.apply_async(camera_control.init, callback=camera_start_callback)
#pool.apply_async(sensor_control.init, callback=sensor_start_callback)
p = Process(target=start_server_socket)
p.start()
serverproc = Process(target=start_server_socket)
serverproc.start()
# wait for user to confirm init on webpage
wait_for(led_ready, "LED controller initialization")
wait_for(sensor_ready, "Sensor Initialization")
wait_for(camera_ready, "Camera initilization")
wait_for(arm_ready, "UR5 initilization")
if led_ready is False:
fprint("waiting for " + "LED controller initialization" + " to complete...")
while led_ready is False:
sleep(0.1)
if sensor_ready is False:
fprint("waiting for " + "Sensor Initialization" + " to complete...")
while sensor_ready is False:
sleep(0.1)
if camera_ready is False:
fprint("waiting for " + "Camera initilization" + " to complete...")
while camera_ready is False:
sleep(0.1)
if arm_ready is False:
fprint("waiting for " + "UR5 initilization" + " to complete...")
while arm_ready is False:
sleep(0.1)
@@ -150,7 +170,8 @@ def run_loading_app():
def setup_client(pool):
# Windows client setup
global config
global vm_ready
global serverproc
# Open loading wepage
p = Process(target=run_loading_app)
p.start()
@@ -164,11 +185,20 @@ def setup_client(pool):
run_cmd("Start-VM -Name Jukebox*") # any and all VMs starting with "Jukebox"
# Wait for VM to start and be reachable over the network
pool.apply_async(start_client_socket)
serverproc = Process(target=start_client_socket)
serverproc.start()
wait_for(vm_ready, "VM Startup")
pool.apply_async(check_server_online, (config["core"]["serverip"],config["core"]["clientip"]), callback=vm_start_callback)
#wait_for(vm_ready, "VM Startup")
#global vm_ready
if vm_ready is False:
fprint("waiting for " + "VM Startup" + " to complete...")
while vm_ready is False:
sleep(0.1)
p.terminate()
firefox.get(config["core"]["serverip"])
firefox.get("http://" + config["core"]["serverip"] + ":5000")
return True
@@ -253,11 +283,13 @@ if __name__ == "__main__":
if config["core"]["mode"] == "winclient":
from selenium import webdriver
if setup_client(pool):
fprint("Entering main loop...")
while(keeprunning):
mainloop_client(pool)
elif config["core"]["mode"] == "linuxserver":
if setup_server(pool):
fprint("Entering main loop...")
while(keeprunning):
mainloop_server(pool)