refactor: Make /cast API accept IP and use a single cast function
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
82
main.py
82
main.py
@@ -14,15 +14,18 @@ auto_stop_timer = None # Timer for automatic VNC stop
|
|||||||
|
|
||||||
@app.route('/cast', methods=['POST'])
|
@app.route('/cast', methods=['POST'])
|
||||||
def cast_spell():
|
def cast_spell():
|
||||||
machine = request.form.get('machine')
|
ip_address = request.form.get('ip_address')
|
||||||
logging.info(f"Received POST request on /cast. Requested machine: {machine}")
|
logging.info(f"Received POST request on /cast. Requested ip_address: {ip_address}")
|
||||||
|
|
||||||
|
if not ip_address:
|
||||||
|
logging.warning(f"Invalid or missing ip_address parameter: {ip_address}")
|
||||||
|
return f"Invalid or missing 'ip_address' parameter.", 400
|
||||||
|
|
||||||
global auto_stop_timer
|
global auto_stop_timer
|
||||||
|
|
||||||
if machine == "trotec":
|
logging.info(f"Casting to {ip_address}.")
|
||||||
logging.info("Casting to Trotec.")
|
|
||||||
kill_vnc() # Kill any existing VNC session first
|
kill_vnc() # Kill any existing VNC session first
|
||||||
cast_trotec()
|
cast_machine(ip_address)
|
||||||
# Restart auto-stop timer
|
# Restart auto-stop timer
|
||||||
if auto_stop_timer is not None and auto_stop_timer.is_alive():
|
if auto_stop_timer is not None and auto_stop_timer.is_alive():
|
||||||
auto_stop_timer.cancel()
|
auto_stop_timer.cancel()
|
||||||
@@ -30,34 +33,7 @@ def cast_spell():
|
|||||||
auto_stop_timer = threading.Timer(AUTO_STOP_TIMEOUT_SECONDS, _auto_stop_vnc)
|
auto_stop_timer = threading.Timer(AUTO_STOP_TIMEOUT_SECONDS, _auto_stop_vnc)
|
||||||
auto_stop_timer.start()
|
auto_stop_timer.start()
|
||||||
logging.info(f"Scheduled auto-stop for VNC in {AUTO_STOP_TIMEOUT_SECONDS} seconds.")
|
logging.info(f"Scheduled auto-stop for VNC in {AUTO_STOP_TIMEOUT_SECONDS} seconds.")
|
||||||
return f"Successfully cast to Trotec.", 200
|
return f"Successfully cast to {ip_address}.", 200
|
||||||
elif machine == "thunder":
|
|
||||||
logging.info("Casting to Thunder.")
|
|
||||||
kill_vnc() # Kill any existing VNC session first
|
|
||||||
cast_thunder()
|
|
||||||
# Restart auto-stop timer
|
|
||||||
if auto_stop_timer is not None and auto_stop_timer.is_alive():
|
|
||||||
auto_stop_timer.cancel()
|
|
||||||
logging.info("Cancelled previous auto-stop timer.")
|
|
||||||
auto_stop_timer = threading.Timer(AUTO_STOP_TIMEOUT_SECONDS, _auto_stop_vnc)
|
|
||||||
auto_stop_timer.start()
|
|
||||||
logging.info(f"Scheduled auto-stop for VNC in {AUTO_STOP_TIMEOUT_SECONDS} seconds.")
|
|
||||||
return f"Successfully cast to Thunder.", 200
|
|
||||||
elif machine == "xtool":
|
|
||||||
logging.info("Casting to XTool.")
|
|
||||||
kill_vnc() # Kill any existing VNC session first
|
|
||||||
cast_xtool()
|
|
||||||
# Restart auto-stop timer
|
|
||||||
if auto_stop_timer is not None and auto_stop_timer.is_alive():
|
|
||||||
auto_stop_timer.cancel()
|
|
||||||
logging.info("Cancelled previous auto-stop timer.")
|
|
||||||
auto_stop_timer = threading.Timer(AUTO_STOP_TIMEOUT_SECONDS, _auto_stop_vnc)
|
|
||||||
auto_stop_timer.start()
|
|
||||||
logging.info(f"Scheduled auto-stop for VNC in {AUTO_STOP_TIMEOUT_SECONDS} seconds.")
|
|
||||||
return f"Successfully cast to XTool.", 200
|
|
||||||
else:
|
|
||||||
logging.warning(f"Invalid or missing machine parameter: {machine}")
|
|
||||||
return f"Invalid or missing 'machine' parameter. Use 'trotec' or 'thunder'.", 400
|
|
||||||
|
|
||||||
@app.route('/stop', methods=['POST'])
|
@app.route('/stop', methods=['POST'])
|
||||||
def stop_cast():
|
def stop_cast():
|
||||||
@@ -79,9 +55,9 @@ def _auto_stop_vnc():
|
|||||||
kill_vnc()
|
kill_vnc()
|
||||||
auto_stop_timer = None # Clear the timer reference
|
auto_stop_timer = None # Clear the timer reference
|
||||||
|
|
||||||
def cast_trotec():
|
def cast_machine(ip_address):
|
||||||
"""Executes the xtightvncviewer command."""
|
"""Executes the xtightvncviewer command for a given IP address."""
|
||||||
command = "DISPLAY=:1 xtightvncviewer -viewonly -fullscreen 172.17.17.214"
|
command = f"DISPLAY=:1 xtightvncviewer -viewonly -fullscreen {ip_address}"
|
||||||
try:
|
try:
|
||||||
logging.info(f"Launching command: {command}")
|
logging.info(f"Launching command: {command}")
|
||||||
# Use Popen to run the command in the background (non-blocking)
|
# Use Popen to run the command in the background (non-blocking)
|
||||||
@@ -90,39 +66,9 @@ def cast_trotec():
|
|||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
# This error is more likely if the shell itself (e.g., /bin/sh) is not found,
|
# This error is more likely if the shell itself (e.g., /bin/sh) is not found,
|
||||||
# or if 'xtightvncviewer' is not in PATH and the shell fails to find it.
|
# or if 'xtightvncviewer' is not in PATH and the shell fails to find it.
|
||||||
logging.error(f"Failed to launch VNC for Trotec: Shell or VNC command might not be found. Ensure xtightvncviewer is in PATH and shell is available.")
|
logging.error(f"Failed to launch VNC for {ip_address}: Shell or VNC command might not be found. Ensure xtightvncviewer is in PATH and shell is available.")
|
||||||
except Exception as e: # Catch other potential errors during Popen
|
except Exception as e: # Catch other potential errors during Popen
|
||||||
logging.error(f"An error occurred while launching VNC for Trotec with Popen: {e}")
|
logging.error(f"An error occurred while launching VNC for {ip_address} with Popen: {e}")
|
||||||
|
|
||||||
def cast_thunder():
|
|
||||||
"""Executes the xtightvncviewer command for Thunder."""
|
|
||||||
command = "DISPLAY=:1 xtightvncviewer -viewonly -fullscreen 172.17.17.215"
|
|
||||||
try:
|
|
||||||
logging.info(f"Launching command: {command}")
|
|
||||||
# Use Popen to run the command in the background (non-blocking)
|
|
||||||
subprocess.Popen(command, shell=True)
|
|
||||||
logging.info(f"Command '{command}' launched successfully.")
|
|
||||||
except FileNotFoundError:
|
|
||||||
# This error is more likely if the shell itself (e.g., /bin/sh) is not found,
|
|
||||||
# or if 'xtightvncviewer' is not in PATH and the shell fails to find it.
|
|
||||||
logging.error(f"Failed to launch VNC for Thunder: Shell or VNC command might not be found. Ensure xtightvncviewer is in PATH and shell is available.")
|
|
||||||
except Exception as e: # Catch other potential errors during Popen
|
|
||||||
logging.error(f"An error occurred while launching VNC for Thunder with Popen: {e}")
|
|
||||||
|
|
||||||
def cast_xtool():
|
|
||||||
"""Executes the xtightvncviewer command for XTool."""
|
|
||||||
command = "DISPLAY=:1 xtightvncviewer -viewonly -fullscreen 172.17.17.221"
|
|
||||||
try:
|
|
||||||
logging.info(f"Launching command: {command}")
|
|
||||||
# Use Popen to run the command in the background (non-blocking)
|
|
||||||
subprocess.Popen(command, shell=True)
|
|
||||||
logging.info(f"Command '{command}' launched successfully.")
|
|
||||||
except FileNotFoundError:
|
|
||||||
# This error is more likely if the shell itself (e.g., /bin/sh) is not found,
|
|
||||||
# or if 'xtightvncviewer' is not in PATH and the shell fails to find it.
|
|
||||||
logging.error(f"Failed to launch VNC for XTool: Shell or VNC command might not be found. Ensure xtightvncviewer is in PATH and shell is available.")
|
|
||||||
except Exception as e: # Catch other potential errors during Popen
|
|
||||||
logging.error(f"An error occurred while launching VNC for XTool with Popen: {e}")
|
|
||||||
|
|
||||||
def kill_vnc():
|
def kill_vnc():
|
||||||
"""Executes the killall command for xtightvncviewer."""
|
"""Executes the killall command for xtightvncviewer."""
|
||||||
|
|||||||
Reference in New Issue
Block a user