Skip to content

Commit

Permalink
MiR100 Connector: Allow disabling the websockets interface (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-Tomas authored Oct 29, 2024
1 parent 4e794d8 commit 4873493
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 17 deletions.
2 changes: 1 addition & 1 deletion mir_connector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ flake8 --max-line-length=100 --exclude venv
```bash
# Create the virtualenv if not active already
virtualenv venv/
pip install -e '.[dev]'
. venv/bin/activate
pip install -e '.[dev]'
pytest -v
```

Expand Down
1 change: 1 addition & 0 deletions mir_connector/config/my_fleet.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
connector_config:
mir_host_address: localhost
mir_host_port: 80
mir_enable_ws: True
mir_ws_port: 9090
mir_use_ssl: False
mir_username: username
Expand Down
2 changes: 2 additions & 0 deletions mir_connector/inorbit_mir_connector/config/mir100_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"mir_host_port": 80,
"mir_ws_port": 9090,
"mir_use_ssl": False,
"mir_enable_ws": True,
"mir_username": "",
"mir_password": "",
"enable_mission_tracking": True,
Expand All @@ -40,6 +41,7 @@ class MiR100ConfigModel(BaseModel):

mir_host_address: str
mir_host_port: int
mir_enable_ws: bool
mir_ws_port: int
mir_use_ssl: bool
mir_username: str
Expand Down
35 changes: 20 additions & 15 deletions mir_connector/inorbit_mir_connector/src/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ def __init__(self, robot_id: str, config: MiR100Config) -> None:
)

# Configure the ws connection to the robot
self.mir_ws = MirWebSocketV2(
mir_host_address=config.connector_config.mir_host_address,
mir_ws_port=config.connector_config.mir_ws_port,
mir_use_ssl=config.connector_config.mir_use_ssl,
loglevel=log_level,
)
self.ws_enabled = config.connector_config.mir_enable_ws
if self.ws_enabled:
self.mir_ws = MirWebSocketV2(
mir_host_address=config.connector_config.mir_host_address,
mir_ws_port=config.connector_config.mir_ws_port,
mir_use_ssl=config.connector_config.mir_use_ssl,
loglevel=log_level,
)

# Configure the timezone
self.robot_tz_info = pytz.timezone("UTC")
Expand All @@ -78,6 +80,7 @@ def __init__(self, robot_id: str, config: MiR100Config) -> None:
"robot_name": robot_id,
"api_key": os.environ["INORBIT_KEY"],
"robot_key": config.inorbit_robot_key,
"use_ssl": os.environ.get("INORBIT_USE_SSL", "true").lower() == "true",
}
if "INORBIT_URL" in os.environ:
robot_session_params["endpoint"] = os.environ["INORBIT_URL"]
Expand Down Expand Up @@ -264,14 +267,15 @@ def start(self):
# Reporting system stats
# TODO(b-Tomas): Report more system stats

cpu_usage = self.mir_ws.get_cpu_usage()
disk_usage = self.mir_ws.get_disk_usage()
memory_usage = self.mir_ws.get_memory_usage()
self.inorbit_sess.publish_system_stats(
cpu_load_percentage=cpu_usage,
hdd_usage_percentage=disk_usage,
ram_usage_percentage=memory_usage,
)
if self.ws_enabled:
cpu_usage = self.mir_ws.get_cpu_usage()
disk_usage = self.mir_ws.get_disk_usage()
memory_usage = self.mir_ws.get_memory_usage()
self.inorbit_sess.publish_system_stats(
cpu_load_percentage=cpu_usage,
hdd_usage_percentage=disk_usage,
ram_usage_percentage=memory_usage,
)

# publish mission data
try:
Expand All @@ -286,5 +290,6 @@ def inorbit_connected(self):
def stop(self):
"""Exit the Connector cleanly."""
self._should_run = False
self.mir_ws.disconnect()
self.inorbit_sess.disconnect()
if self.ws_enabled:
self.mir_ws.disconnect()
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def example_mir100_configuration_dict(example_configuration_dict):
"connector_config": {
"mir_host_address": "localhost",
"mir_host_port": 80,
"mir_enable_ws": True,
"mir_ws_port": 9090,
"mir_use_ssl": False,
"mir_username": "admin",
Expand Down
62 changes: 61 additions & 1 deletion mir_connector/inorbit_mir_connector/tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from inorbit_mir_connector.config.mir100_model import MiR100Config


# NOTE(b-Tomas): Added some example data below to help creating rea
@pytest.fixture
def connector(monkeypatch):
monkeypatch.setenv("INORBIT_KEY", "abc123")
Expand All @@ -37,6 +36,7 @@ def connector(monkeypatch):
connector_config={
"mir_host_address": "example.com",
"mir_host_port": 80,
"mir_enable_ws": True,
"mir_ws_port": 9090,
"mir_use_ssl": False,
"mir_username": "user",
Expand Down Expand Up @@ -146,6 +146,7 @@ def create_connector(user_scripts):
"mir_password": "pass",
"mir_host_address": "example.com",
"mir_host_port": 80,
"mir_enable_ws": True,
"mir_ws_port": 9090,
"mir_use_ssl": False,
"mir_api_version": "v2.0",
Expand Down Expand Up @@ -184,6 +185,65 @@ def create_connector(user_scripts):
os.environ[k] = v


def test_enable_ws_flag(monkeypatch):
monkeypatch.setenv("INORBIT_KEY", "abc123")
monkeypatch.setattr(MirApiV2, "_create_api_session", MagicMock())
monkeypatch.setattr(MirApiV2, "_create_web_session", MagicMock())
monkeypatch.setattr(websocket, "WebSocketApp", MagicMock())
monkeypatch.setattr(RobotSession, "connect", MagicMock())
monkeypatch.setattr(RobotSession, "register_commands_path", MagicMock())
monkeypatch.setattr(time, "sleep", Mock())
monkeypatch.setattr(inorbit_mir_connector.src.connector.os, "makedirs", Mock())

config = MiR100Config(
inorbit_robot_key="robot_key",
location_tz="UTC",
log_level="INFO",
connector_type="mir100",
connector_version="0.1.0",
connector_config={
"mir_username": "user",
"mir_password": "pass",
"mir_host_address": "example.com",
"mir_host_port": 80,
"mir_enable_ws": False,
"mir_ws_port": 9090,
"mir_use_ssl": False,
"mir_api_version": "v2.0",
"enable_mission_tracking": False,
},
user_scripts={},
cameras=[],
)
connector = Mir100Connector("mir100-1", config)
assert connector.ws_enabled is False
assert not hasattr(connector, "mir_ws")

config = MiR100Config(
inorbit_robot_key="robot_key",
location_tz="UTC",
log_level="INFO",
connector_type="mir100",
connector_version="0.1.0",
connector_config={
"mir_username": "user",
"mir_password": "pass",
"mir_host_address": "example.com",
"mir_host_port": 80,
"mir_enable_ws": True,
"mir_ws_port": 9090,
"mir_use_ssl": False,
"mir_api_version": "v2.0",
"enable_mission_tracking": False,
},
user_scripts={},
cameras=[],
)
connector = Mir100Connector("mir100-1", config)
assert connector.ws_enabled is True
assert hasattr(connector, "mir_ws")


def test_command_callback_state(connector, callback_kwargs):
MIR_STATE = {3: "READY", 4: "PAUSE", 11: "MANUALCONTROL"}

Expand Down

0 comments on commit 4873493

Please sign in to comment.