From f79de8e2cf46920f5fd82207755402a73a021f1b Mon Sep 17 00:00:00 2001 From: Wilfred Tyler Gee Date: Mon, 2 Mar 2026 11:32:04 -1000 Subject: [PATCH] Trying to fix the path. --- resources/arduino/install-arduino-cli.sh | 11 +--- resources/arduino/install-power-board.sh | 8 ++- src/panoptes/pocs/utils/cli/power.py | 79 +++++++++++++++--------- 3 files changed, 60 insertions(+), 38 deletions(-) diff --git a/resources/arduino/install-arduino-cli.sh b/resources/arduino/install-arduino-cli.sh index 9d630b931..f1440d98b 100755 --- a/resources/arduino/install-arduino-cli.sh +++ b/resources/arduino/install-arduino-cli.sh @@ -6,8 +6,8 @@ if command -v arduino-cli &> /dev/null; then exit 0 fi -# Make sure we are at home. -cd +# Make sure we are at home or give a warning and exit. +cd "$HOME" || { echo "Could not change to home directory."; exit 1; } # Get the arduino-cli tool and install. curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh @@ -16,10 +16,3 @@ curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install. "${HOME}/bin/arduino-cli" core update-index "${HOME}/bin/arduino-cli" core install arduino:avr "${HOME}/bin/arduino-cli" lib install ArduinoJson - -# Ask if the user wants to install the power board software. -read -p "Do you want to install the power board software? [y/N] " -n 1 -r -echo -if [[ $REPLY =~ ^[Yy]$ ]]; then - source ./install-power-board.sh -fi diff --git a/resources/arduino/install-power-board.sh b/resources/arduino/install-power-board.sh index d300d2de7..c9c7ca053 100755 --- a/resources/arduino/install-power-board.sh +++ b/resources/arduino/install-power-board.sh @@ -3,6 +3,12 @@ FQBN="${FQBN:-arduino:avr:uno}" SKETCH_LOCATION="${SKETCH_LOCATION:-PowerBoard}" ARDUINO_PORT="${ARDUINO_PORT:-/dev/ttyACM0}" +DO_UPLOAD="${DO_UPLOAD:-true}" + +# Change to the directory of this script. +cd "$(dirname "${BASH_SOURCE[0]}")" || { echo "Could not change to script directory."; exit 1; } arduino-cli compile -b "${FQBN}" "${SKETCH_LOCATION}" -arduino-cli upload -p "${ARDUINO_PORT}" -b "${FQBN}" "${SKETCH_LOCATION}" +if [ "${DO_UPLOAD}" = "true" ]; then + arduino-cli upload -p "${ARDUINO_PORT}" -b "${FQBN}" "${SKETCH_LOCATION}" +fi diff --git a/src/panoptes/pocs/utils/cli/power.py b/src/panoptes/pocs/utils/cli/power.py index 570be4d0d..ef65bf05d 100644 --- a/src/panoptes/pocs/utils/cli/power.py +++ b/src/panoptes/pocs/utils/cli/power.py @@ -14,10 +14,13 @@ import requests import typer from rich import print +from rich.console import Console from sparklines import sparklines from panoptes.pocs.utils.service.power import RelayCommand +console = Console() + @dataclass class HostInfo: @@ -200,25 +203,13 @@ def setup_power( help="Confirm power board setup.", ), ] = False, - do_install: Annotated[ - bool, - typer.Option( - ..., - "--install", - prompt="Would you like to install the arduino script?", - help="Install the arduino script.", - ), - ] = False, - install_script: Path = typer.Option( - "resources/arduino/install-power-board.sh", help="Path to the power monitor script." - ), + arduino_device: str = typer.Option("/dev/ttyACM0", help="Path to the Arduino device."), ): """Set up the power board port and labels; optionally install Arduino sketch. Args: confirm: Confirmation flag to proceed with setup. - do_install: If True, run the Arduino install script before setup. - install_script: Path to the Arduino install script. + arduino_device: Path to the Arduino device for uploading the sketch. Returns: None @@ -227,20 +218,52 @@ def setup_power( print("[red]Cancelled.[/red]") return typer.Abort() - if do_install: - if not install_script.exists(): - print(f"[red]Cannot find install script at {install_script}[/red]") - return typer.Abort() - # Change directory to the arduino script. - os.chdir(install_script.parent) - cmd = f"bash {install_script.name}" - print(f"Running: {cmd}") - try: - subprocess.run(cmd, shell=True) - except subprocess.CalledProcessError as e: - print(f"[red]Error running install script: {e}[/red]") - else: - print("[green]Arduino script installed.[/green]") + arduino_cli_script = Path("~/resources/arduino/install-arduino-cli.sh").expanduser() + power_board_script = Path("~/resources/arduino/install-power-board.sh").expanduser() + + if not arduino_cli_script.exists() and not power_board_script.exists(): + print( + f"[red]Error: Neither Arduino CLI setup script nor power board setup script found at " + f"{arduino_cli_script} and {power_board_script}. Cannot proceed with setup.[/red]" + ) + return None + + with console.status(f"Running Arduino CLI setup script: {arduino_cli_script}", spinner="dots"): + result = subprocess.run(f"bash {arduino_cli_script}", shell=True, capture_output=True, text=True) + if result.returncode != 0: + print(f"[red]{result.stdout}\n{result.stderr}[/red]") + return None + else: + print("[green]✅ Arduino CLI setup complete.[/green]") + + # Check if the Arduino device is available + arduino_device_path = Path(arduino_device) + do_upload = "true" if arduino_device_path.exists() else "false" + + env = os.environ.copy() + env["DO_UPLOAD"] = do_upload + + upload_msg = "with upload" if do_upload == "true" else "without upload (device not found)" + with console.status( + f"Running power board setup script: {power_board_script} ({upload_msg})", spinner="dots" + ): + result = subprocess.run( + f"bash {power_board_script}", shell=True, capture_output=True, text=True, env=env + ) + if result.returncode != 0: + print(f"[red]{result.stdout}\n{result.stderr}[/red]") + return None + else: + print("[green]✅ Power board script complete.[/green]") + if do_upload == "0": + print( + f"[yellow]⚠️ Sketch was compiled but not uploaded because " + f"device {arduino_device} was not found. Please connect the " + f"device and run this command again to upload the sketch." + f"[/yellow]" + ) + + return None if __name__ == "__main__":