Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use payloads for mqtt messages #681

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -34,6 +34,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 @@ -482,24 +487,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 @@ -514,46 +517,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,12 +1,13 @@
from dataclasses import dataclass
from datetime import datetime
from typing import List, Optional
from typing import List, Optional, Union

from alitra import Pose
from transitions import State

from robot_interface.models.mission.status import RobotStatus
from robot_interface.models.robots.battery_state import BatteryState
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 @@ -54,12 +55,7 @@ class DocumentInfo:
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 @@ -82,3 +78,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: Union[str, dict]
installation_code: str
analysis_to_be_run: Optional[str]
Eddasol marked this conversation as resolved.
Show resolved Hide resolved
timestamp: datetime
Loading