Skip to content

Commit

Permalink
added cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
hatomist committed Oct 17, 2024
1 parent c08250b commit b613353
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 6 deletions.
2 changes: 1 addition & 1 deletion openlch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "0.2.1"
__version__ = "0.3.0"

from .grpc import Servo
115 changes: 110 additions & 5 deletions openlch/cli.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
"""Defines the CLI for the OpenLCH project."""

import subprocess

import click
from .grpc import Servo

DEFAULT_IP = "192.168.42.1"

@click.group()
def cli() -> None:
"""OpenLCH CLI tool for interacting with MilkV boards."""
raise NotImplementedError("The CLI is not yet implemented")
"""OpenLCH CLI tool for interacting with MilkV boards.
Available commands:
- ping: Ping the MilkV board
- get-positions: Get current positions of all servos
- set-position: Set position for a specific servo
- set-wifi: Set WiFi credentials for the MilkV board
- get-servo-info: Get information about a specific servo
- scan-servos: Scan for connected servos
- change-servo-id: Change the ID of a servo
Use 'openlch COMMAND --help' for more information on a specific command.
"""
pass

@cli.command()
@click.option("--ip", required=True, help="IP address of the MilkV board")
@click.argument("ip", default=DEFAULT_IP)
def ping(ip: str) -> None:
"""Ping the MilkV board at the specified IP address."""
try:
result = subprocess.run(["ping", "-c", "4", ip], capture_output=True, text=True, check=False)
result = subprocess.run(["ping", "-c", "1", ip], capture_output=True, text=True, check=False)
if result.returncode == 0:
click.echo(f"Successfully pinged {ip}")
click.echo(result.stdout)
Expand All @@ -26,6 +38,99 @@ def ping(ip: str) -> None:
except Exception as e:
click.echo(f"An error occurred: {str(e)}")

@cli.command()
@click.argument("ip", default=DEFAULT_IP)
def get_positions(ip: str) -> None:
"""Get current positions of all servos."""
client = Servo(ip)
try:
positions = client.get_positions()
click.echo("Current positions:")
for id, position in positions:
click.echo(f"Servo {id}: {position}")
except Exception as e:
click.echo(f"An error occurred: {str(e)}")
finally:
client.close()

@cli.command()
@click.argument("id", type=int)
@click.argument("position", type=float)
@click.argument("ip", default=DEFAULT_IP)
def set_position(id: int, position: float, ip: str) -> None:
"""Set position for a specific servo."""
client = Servo(ip)
try:
client.set_positions([(id, position)])
click.echo(f"Position set for servo {id} to {position}")
except Exception as e:
click.echo(f"An error occurred: {str(e)}")
finally:
client.close()

@cli.command()
@click.argument("ssid")
@click.argument("password")
@click.argument("ip", default=DEFAULT_IP)
def set_wifi(ssid: str, password: str, ip: str) -> None:
"""Set WiFi credentials for the MilkV board."""
client = Servo(ip)
try:
client.set_wifi_info(ssid, password)
click.echo("WiFi credentials set successfully")
except Exception as e:
click.echo(f"An error occurred: {str(e)}")
finally:
client.close()

@cli.command()
@click.argument("id", type=int)
@click.argument("ip", default=DEFAULT_IP)
def get_servo_info(id: int, ip: str) -> None:
"""Get information about a specific servo."""
client = Servo(ip)
try:
info = client.get_servo_info(id)
click.echo(f"Servo {id} info:")
for key, value in info.items():
click.echo(f"{key}: {value}")
except Exception as e:
click.echo(f"An error occurred: {str(e)}")
finally:
client.close()

@cli.command()
@click.argument("ip", default=DEFAULT_IP)
def scan_servos(ip: str) -> None:
"""Scan for connected servos."""
client = Servo(ip)
try:
servo_ids = client.scan()
click.echo("Found servo IDs:")
for id in servo_ids:
click.echo(id)
except Exception as e:
click.echo(f"An error occurred: {str(e)}")
finally:
client.close()

@cli.command()
@click.argument("old_id", type=int)
@click.argument("new_id", type=int)
@click.argument("ip", default=DEFAULT_IP)
def change_servo_id(old_id: int, new_id: int, ip: str) -> None:
"""Change the ID of a servo."""
client = Servo(ip)
try:
success = client.change_id(old_id, new_id)
if success:
click.echo(f"Successfully changed servo ID from {old_id} to {new_id}")
else:
click.echo("Failed to change servo ID")
except Exception as e:
click.echo(f"An error occurred: {str(e)}")
finally:
client.close()

if __name__ == "__main__":
# python -m openlch.cli
Expand Down

0 comments on commit b613353

Please sign in to comment.