diff --git a/run.py b/run.py index 0705f65..f098d46 100755 --- a/run.py +++ b/run.py @@ -2,7 +2,7 @@ import get_specs import traceback -import logging +#import logging import yaml from multiprocessing import Process, Manager, Pool, TimeoutError, active_children, log_to_stderr from multiprocessing.pool import Pool @@ -14,6 +14,9 @@ import sys import ur5_control import os import signal +import socket +from flask import Flask, render_template, request +import requests @@ -55,6 +58,46 @@ def wait_for(val, name): while val is False: sleep(0.1) +def start_server_socket(): + app = Flask(__name__) + + @app.route('/report_ip', methods=['POST']) + def report_ip(): + client_ip = request.json.get('ip') + fprint(f"Received IP: {client_ip}") + # You can store or process the IP address as needed + return "IP Received", 200 + + app.run(host='0.0.0.0', port=5000) + +def start_client_socket(server_ip): + 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(): + # Handle message from server + data = request.json + 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 setup_server(pool): # linux server setup global config @@ -69,6 +112,10 @@ def setup_server(pool): #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() + + # wait for user to confirm init on webpage wait_for(led_ready, "LED controller initialization") wait_for(sensor_ready, "Sensor Initialization") @@ -92,13 +139,13 @@ def mainloop_server(pool): counter = counter + 1 def run_loading_app(): - from flask import Flask, render_template + app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') - app.run(debug=True, use_reloader=False) + app.run(debug=True, use_reloader=False, port=7000) def setup_client(pool): # Windows client setup @@ -110,14 +157,14 @@ def setup_client(pool): firefox = webdriver.Firefox() firefox.fullscreen_window() - firefox.get('http://localhost:5000') + firefox.get('http://localhost:7000') # start Linux server VM if config["core"]["server"] == "Hyper-V": 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(vm_control.init, callback=vm_start_callback) + pool.apply_async(start_client_socket) wait_for(vm_ready, "VM Startup") p.terminate()