Skip to content

Commit

Permalink
Refactor robot status
Browse files Browse the repository at this point in the history
  • Loading branch information
oysand committed May 24, 2024
1 parent 0c53b5f commit 95210fc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 146 deletions.
12 changes: 0 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
from isar.services.service_connections.mqtt.robot_info_publisher import (
RobotInfoPublisher,
)
from isar.services.service_connections.mqtt.robot_status_publisher import (
RobotStatusPublisher,
)
from isar.state_machine.state_machine import StateMachine, main
from isar.storage.uploader import Uploader
from robot_interface.robot_interface import RobotInterface
Expand Down Expand Up @@ -56,15 +53,6 @@
target=mqtt_client.run, name="ISAR MQTT Client", daemon=True
)
threads.append(mqtt_thread)
robot_status_publisher: RobotStatusPublisher = RobotStatusPublisher(
mqtt_queue=queues.mqtt_queue, robot=robot, state_machine=state_machine
)
robot_status_thread: Thread = Thread(
target=robot_status_publisher.run,
name="ISAR Robot Status Publisher",
daemon=True,
)
threads.append(robot_status_thread)

robot_info_publisher: RobotInfoPublisher = RobotInfoPublisher(
mqtt_queue=queues.mqtt_queue
Expand Down
6 changes: 2 additions & 4 deletions src/isar/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,13 @@ def __init__(self) -> None:
DATA_CLASSIFICATION: str = Field(default="internal")

# List of MQTT Topics
TOPIC_ISAR_STATE: str = Field(default="state", validate_default=True)
TOPIC_ISAR_STATUS: str = Field(default="status", validate_default=True)
TOPIC_ISAR_MISSION: str = Field(default="mission", validate_default=True)
TOPIC_ISAR_TASK: str = Field(default="task", validate_default=True)
TOPIC_ISAR_STEP: str = Field(default="step", validate_default=True)
TOPIC_ISAR_INSPECTION_RESULT: str = Field(
default="inspection_result", validate_default=True
)
TOPIC_ISAR_ROBOT_STATUS: str = Field(default="robot_status", validate_default=True)
TOPIC_ISAR_ROBOT_INFO: str = Field(default="robot_info", validate_default=True)
TOPIC_ISAR_ROBOT_HEARTBEAT: str = Field(
default="robot_heartbeat", validate_default=True
Expand Down Expand Up @@ -284,11 +283,10 @@ def set_log_levels(cls, v: Any, info: ValidationInfo) -> dict:
}

@field_validator(
"TOPIC_ISAR_STATE",
"TOPIC_ISAR_STATUS",
"TOPIC_ISAR_MISSION",
"TOPIC_ISAR_TASK",
"TOPIC_ISAR_STEP",
"TOPIC_ISAR_ROBOT_STATUS",
"TOPIC_ISAR_ROBOT_INFO",
"TOPIC_ISAR_ROBOT_HEARTBEAT",
"TOPIC_ISAR_INSPECTION_RESULT",
Expand Down
125 changes: 0 additions & 125 deletions src/isar/services/service_connections/mqtt/robot_status_publisher.py

This file was deleted.

23 changes: 18 additions & 5 deletions src/isar/state_machine/state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
from robot_interface.models.exceptions.robot_exceptions import ErrorMessage
from robot_interface.models.initialize.initialize_params import InitializeParams
from robot_interface.models.mission.mission import Mission
from robot_interface.models.mission.status import MissionStatus, StepStatus, TaskStatus
from robot_interface.models.mission.status import (
MissionStatus,
RobotStatus,
StepStatus,
TaskStatus,
)
from robot_interface.models.mission.step import Step
from robot_interface.models.mission.task import Task
from robot_interface.robot_interface import RobotInterface
Expand Down Expand Up @@ -438,7 +443,7 @@ def update_state(self):
self.send_state_status()
self._log_state_transition(self.current_state)
self.logger.info(f"State: {self.current_state}")
self.publish_state()
self.publish_status()

def reset_state_machine(self) -> None:
self.logger.info("Resetting state machine")
Expand Down Expand Up @@ -580,25 +585,33 @@ def publish_step_status(self, step: Step) -> None:
retain=False,
)

def publish_state(self) -> None:
def publish_status(self) -> None:
if not self.mqtt_publisher:
return
payload: str = json.dumps(
{
"isar_id": settings.ISAR_ID,
"robot_name": settings.ROBOT_NAME,
"state": self.current_state,
"status": self._current_status(),
"timestamp": datetime.utcnow(),
},
cls=EnhancedJSONEncoder,
)

self.mqtt_publisher.publish(
topic=settings.TOPIC_ISAR_STATE,
topic=settings.TOPIC_ISAR_STATUS,
payload=payload,
retain=False,
)

def _current_status(self) -> RobotStatus:
if self.current_state == States.Idle:
return RobotStatus.Available
elif self.current_state == States.Offline:
return RobotStatus.Offline
else:
return RobotStatus.Busy

def _log_state_transition(self, next_state):
"""Logs all state transitions that are not self-transitions."""
self.transitions_list.append(next_state)
Expand Down

0 comments on commit 95210fc

Please sign in to comment.