Skip to content

Commit

Permalink
Use payloads for mqtt messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddasol committed Dec 12, 2024
1 parent c8ff71c commit 6b38e7d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 60 deletions.
76 changes: 37 additions & 39 deletions src/isar/state_machine/state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
from robot_interface.models.mission.task import TASKS
from robot_interface.robot_interface import RobotInterface
from robot_interface.telemetry.mqtt_client import MqttClientInterface
from robot_interface.telemetry.payloads import (
RobotStatusPayload,
MissionPayload,
TaskPayload,
)
from robot_interface.utilities.json_service import EnhancedJSONEncoder


Expand Down Expand Up @@ -484,24 +489,22 @@ def publish_mission_status(self) -> None:
if self.current_mission:
if self.current_mission.error_message:
error_message = self.current_mission.error_message
payload: str = json.dumps(
{
"isar_id": settings.ISAR_ID,
"robot_name": settings.ROBOT_NAME,
"mission_id": self.current_mission.id if self.current_mission else None,
"status": self.current_mission.status if self.current_mission else None,
"error_reason": error_message.error_reason if error_message else None,
"error_description": (
error_message.error_description if error_message else None
),
"timestamp": datetime.now(timezone.utc),
},
cls=EnhancedJSONEncoder,

payload: MissionPayload = MissionPayload(
isar_id=settings.ISAR_ID,
robot_name=settings.ROBOT_NAME,
mission_id=self.current_mission.id if self.current_mission else None,
status=self.current_mission.status if self.current_mission else None,
error_reason=error_message.error_reason if error_message else None,
error_description=(
error_message.error_description if error_message else None
),
timestamp=datetime.now(timezone.utc),
)

self.mqtt_publisher.publish(
topic=settings.TOPIC_ISAR_MISSION,
payload=payload,
payload=json.dumps(payload, cls=EnhancedJSONEncoder),
qos=1,
retain=True,
)
Expand All @@ -516,46 +519,41 @@ def publish_task_status(self, task: TASKS) -> None:
if task.error_message:
error_message = task.error_message

payload: str = json.dumps(
{
"isar_id": settings.ISAR_ID,
"robot_name": settings.ROBOT_NAME,
"mission_id": self.current_mission.id if self.current_mission else None,
"task_id": task.id if task else None,
"status": task.status if task else None,
"task_type": task.type,
"error_reason": error_message.error_reason if error_message else None,
"error_description": (
error_message.error_description if error_message else None
),
"timestamp": datetime.now(timezone.utc),
},
cls=EnhancedJSONEncoder,
payload: TaskPayload = TaskPayload(
isar_id=settings.ISAR_ID,
robot_name=settings.ROBOT_NAME,
mission_id=self.current_mission.id if self.current_mission else None,
task_id=task.id if task else None,
status=task.status if task else None,
task_type=task.type if task else None,
error_reason=error_message.error_reason if error_message else None,
error_description=(
error_message.error_description if error_message else None
),
timestamp=datetime.now(timezone.utc),
)

self.mqtt_publisher.publish(
topic=settings.TOPIC_ISAR_TASK,
payload=payload,
payload=json.dumps(payload, cls=EnhancedJSONEncoder),
qos=1,
retain=True,
)

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,
"status": self._current_status(),
"timestamp": datetime.now(timezone.utc),
},
cls=EnhancedJSONEncoder,

payload: RobotStatusPayload = RobotStatusPayload(
isar_id=settings.ISAR_ID,
robot_name=settings.ROBOT_NAME,
status=self._current_status(),
timestamp=datetime.now(timezone.utc),
)

self.mqtt_publisher.publish(
topic=settings.TOPIC_ISAR_STATUS,
payload=payload,
payload=json.dumps(payload, cls=EnhancedJSONEncoder),
qos=1,
retain=True,
)
Expand Down
23 changes: 11 additions & 12 deletions src/isar/storage/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from robot_interface.models.inspection.inspection import Inspection
from robot_interface.models.mission.mission import Mission
from robot_interface.telemetry.mqtt_client import MqttClientInterface
from robot_interface.telemetry.payloads import InspectionResultPayload
from robot_interface.utilities.json_service import EnhancedJSONEncoder


Expand Down Expand Up @@ -149,21 +150,19 @@ def _publish_inspection_result(
"""
if not self.mqtt_publisher:
return
payload: str = json.dumps(
{
"isar_id": settings.ISAR_ID,
"robot_name": settings.ROBOT_NAME,
"inspection_id": inspection.id,
"inspection_path": inspection_path,
"installation_code": settings.PLANT_SHORT_NAME,
"analysis_to_be_run": inspection.metadata.analysis_type,
"timestamp": datetime.now(timezone.utc),
},
cls=EnhancedJSONEncoder,

payload: InspectionResultPayload = InspectionResultPayload(
isar_id=settings.ISAR_ID,
robot_name=settings.ROBOT_NAME,
inspection_id=inspection.id,
inspection_path=inspection_path,
installation_code=settings.PLANT_SHORT_NAME,
analysis_to_be_run=inspection.metadata.analysis_type,
timestamp=datetime.now(timezone.utc),
)
self.mqtt_publisher.publish(
topic=settings.TOPIC_ISAR_INSPECTION_RESULT,
payload=payload,
payload=json.dumps(payload, cls=EnhancedJSONEncoder),
qos=1,
retain=True,
)
49 changes: 40 additions & 9 deletions src/robot_interface/telemetry/payloads.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from dataclasses import dataclass
from datetime import datetime
from typing import List
from typing import List, Optional

from alitra import Pose
from transitions import State

from robot_interface.models.mission.status import RobotStatus
from robot_interface.models.mission.status import RobotStatus, MissionStatus, TaskStatus
from robot_interface.models.mission.task import TaskTypes
from robot_interface.models.exceptions.robot_exceptions import ErrorReason


@dataclass
Expand Down Expand Up @@ -59,12 +60,7 @@ class VideoStream:
class RobotStatusPayload:
isar_id: str
robot_name: str
robot_status: RobotStatus
previous_robot_status: RobotStatus
current_isar_state: State
current_mission_id: str
current_task_id: str
current_step_id: str
status: RobotStatus
timestamp: datetime


Expand All @@ -88,3 +84,38 @@ class RobotHeartbeatPayload:
isar_id: str
robot_name: str
timestamp: datetime


@dataclass
class MissionPayload:
isar_id: str
robot_name: str
mission_id: Optional[str]
status: Optional[MissionStatus]
error_reason: Optional[ErrorReason]
error_description: Optional[str]
timestamp: datetime


@dataclass
class TaskPayload:
isar_id: str
robot_name: str
mission_id: Optional[str]
task_id: Optional[str]
status: Optional[TaskStatus]
task_type: Optional[TaskTypes]
error_reason: Optional[ErrorReason]
error_description: Optional[str]
timestamp: datetime


@dataclass
class InspectionResultPayload:
isar_id: str
robot_name: str
inspection_id: str
inspection_path: str | dict
installation_code: str
analysis_to_be_run: Optional[str]
timestamp: datetime

0 comments on commit 6b38e7d

Please sign in to comment.