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

Hammer/fix dangling delivery alerts #905

Merged
merged 8 commits into from
Feb 26, 2024
2 changes: 1 addition & 1 deletion packages/api-client/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ApiServerModelsTortoiseModelsAlertsAlertLeaf,
ApiServerModelsTortoiseModelsBeaconsBeaconStateLeaf as BeaconState,
BuildingMap,
ApiServerModelsTortoiseModelsDeliveryAlertsDeliveryAlertLeaf as DeliveryAlert,
DeliveryAlert,
DispenserHealth,
DispenserState,
DoorHealth,
Expand Down
733 changes: 195 additions & 538 deletions packages/api-client/lib/openapi/api.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/api-client/lib/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { version as rmfModelVer } from 'rmf-models';

export const version = {
rmfModels: rmfModelVer,
rmfServer: '4f6e8567a0a3a44896f60898e84130c2be8a94c7',
rmfServer: '1403b63032764b781ef1ba349e50a9d895204bbb',
openapiGenerator: '6.2.1',
};
289 changes: 71 additions & 218 deletions packages/api-client/schema/index.ts

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion packages/api-server/api_server/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,12 @@ def convert_delivery_alert(delivery_alert: RmfDeliveryAlert):
RmfDeliveryAlert,
"delivery_alert_request",
lambda msg: rmf_events.delivery_alerts.on_next(convert_delivery_alert(msg)),
10,
rclpy.qos.QoSProfile(
history=rclpy.qos.HistoryPolicy.KEEP_LAST,
depth=10,
reliability=rclpy.qos.ReliabilityPolicy.RELIABLE,
durability=rclpy.qos.DurabilityPolicy.TRANSIENT_LOCAL,
),
)
self._subscriptions.append(delivery_alert_request_sub)

Expand Down
108 changes: 49 additions & 59 deletions packages/api-server/api_server/models/delivery_alerts.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
# pyright: reportGeneralTypeIssues=false
from enum import Enum

from . import tortoise_models as ttm
from pydantic import BaseModel

# TODO(AC): These conversions need to exactly match the enum values defined
# NOTE: These conversions need to exactly match the enum values defined
# in rmf_fleet_msgs::msgs::DeliveryAlert* messages. Any changes to them will
# require these conversions to be modified.


def category_from_msg(category: int) -> str:
value = ttm.DeliveryAlert.Category.Wrong
value = DeliveryAlert.Category.Wrong
match (category):
case 0:
value = ttm.DeliveryAlert.Category.Missing
value = DeliveryAlert.Category.Missing
case 1:
value = ttm.DeliveryAlert.Category.Wrong
value = DeliveryAlert.Category.Wrong
case 2:
value = ttm.DeliveryAlert.Category.Obstructed
value = DeliveryAlert.Category.Obstructed
case 3:
value = ttm.DeliveryAlert.Category.Cancelled
value = DeliveryAlert.Category.Cancelled
case _:
pass
return value


def tier_from_msg(tier: int) -> str:
value = ttm.DeliveryAlert.Tier.Error
value = DeliveryAlert.Tier.Error
match (tier):
case 0:
value = ttm.DeliveryAlert.Tier.Warning
value = DeliveryAlert.Tier.Warning
case 1:
value = ttm.DeliveryAlert.Tier.Error
value = DeliveryAlert.Tier.Error
case _:
pass
return value


def action_from_msg(action: int) -> str:
value = ttm.DeliveryAlert.Action.Waiting
value = DeliveryAlert.Action.Waiting
match (action):
case 0:
value = ttm.DeliveryAlert.Action.Waiting
value = DeliveryAlert.Action.Waiting
case 1:
value = ttm.DeliveryAlert.Action.Cancel
value = DeliveryAlert.Action.Cancel
case 2:
value = ttm.DeliveryAlert.Action.Override
value = DeliveryAlert.Action.Override
case 3:
value = ttm.DeliveryAlert.Action.Resume
value = DeliveryAlert.Action.Resume
return value


def category_to_msg(category: str) -> int:
value = 1
match (category):
case ttm.DeliveryAlert.Category.Missing:
case DeliveryAlert.Category.Missing:
value = 0
case ttm.DeliveryAlert.Category.Wrong:
case DeliveryAlert.Category.Wrong:
value = 1
case ttm.DeliveryAlert.Category.Obstructed:
case DeliveryAlert.Category.Obstructed:
value = 2
case ttm.DeliveryAlert.Category.Cancelled:
case DeliveryAlert.Category.Cancelled:
value = 3
case _:
pass
Expand All @@ -68,9 +68,9 @@ def category_to_msg(category: str) -> int:
def tier_to_msg(tier: str) -> int:
value = 1
match (tier):
case ttm.DeliveryAlert.Tier.Warning:
case DeliveryAlert.Tier.Warning:
value = 0
case ttm.DeliveryAlert.Tier.Error:
case DeliveryAlert.Tier.Error:
value = 1
case _:
pass
Expand All @@ -80,49 +80,39 @@ def tier_to_msg(tier: str) -> int:
def action_to_msg(action: str) -> int:
value = 0
match (action):
case ttm.DeliveryAlert.Action.Waiting:
case DeliveryAlert.Action.Waiting:
value = 0
case ttm.DeliveryAlert.Action.Cancel:
case DeliveryAlert.Action.Cancel:
value = 1
case ttm.DeliveryAlert.Action.Override:
case DeliveryAlert.Action.Override:
value = 2
case ttm.DeliveryAlert.Action.Resume:
case DeliveryAlert.Action.Resume:
value = 3
case _:
pass
return value


class DeliveryAlert(ttm.DeliveryAlertPydantic):
@staticmethod
def from_tortoise(tortoise: ttm.DeliveryAlert) -> "DeliveryAlert":
return DeliveryAlert(
id=tortoise.id,
category=tortoise.category,
tier=tortoise.tier,
action=tortoise.action,
task_id=tortoise.task_id,
message=tortoise.message,
)

async def save(self) -> None:
# Get previous delivery alert for this task ID
prev_waiting_delivery_alert = await ttm.DeliveryAlert.get_or_none(
task_id=self.task_id, action="waiting"
)

await ttm.DeliveryAlert.update_or_create(
{
"category": self.category,
"tier": self.tier,
"task_id": self.task_id,
"action": self.action,
"message": self.message,
},
id=self.id,
)

# If there was a previous delivery alert for this task ID, we cancel it
if prev_waiting_delivery_alert is not None:
await prev_waiting_delivery_alert.update_from_dict({"action": "cancelled"})
await prev_waiting_delivery_alert.save()
class DeliveryAlert(BaseModel):
class Category(str, Enum):
Missing = "missing"
Wrong = "wrong"
Obstructed = "obstructed"
Cancelled = "cancelled"

class Tier(str, Enum):
Warning = "warning"
Error = "error"

class Action(str, Enum):
Waiting = "waiting"
Cancel = "cancelled"
Override = "override"
Resume = "resume"

id: str
category: Category
tier: Tier
action: Action
task_id: str
message: str
14 changes: 0 additions & 14 deletions packages/api-server/api_server/rmf_io/book_keeper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from api_server.models import (
BeaconState,
BuildingMap,
DeliveryAlert,
DispenserHealth,
DispenserState,
DoorHealth,
Expand All @@ -37,7 +36,6 @@ class RmfBookKeeper:
[
"beacon_state",
"building_map",
"delivery_alert",
"door_state",
"door_health",
"lift_state",
Expand Down Expand Up @@ -67,7 +65,6 @@ def __init__(
self._loggers = self._ChildLoggers(
self._main_logger.getChild("beacon_state"),
self._main_logger.getChild("building_map"),
self._main_logger.getChild("delivery_alert"),
self._main_logger.getChild("door_state"),
self._main_logger.getChild("door_health"),
self._main_logger.getChild("lift_state"),
Expand All @@ -82,7 +79,6 @@ def __init__(
)

self._loggers.beacon_state.parent = self._main_logger
self._loggers.delivery_alert.parent = self._main_logger
self._loggers.door_state.parent = self._main_logger
self._loggers.door_health.parent = self._main_logger
self._loggers.lift_state.parent = self._main_logger
Expand All @@ -100,7 +96,6 @@ def __init__(
async def start(self):
self._loop = asyncio.get_event_loop()
self._record_beacon_state()
self._record_delivery_alert()
self._record_building_map()
self._record_door_state()
self._record_door_health()
Expand Down Expand Up @@ -142,15 +137,6 @@ async def update(beacon_state: BeaconState):
self.rmf.beacons.subscribe(lambda x: self._create_task(update(x)))
)

def _record_delivery_alert(self):
async def update(delivery_alert: DeliveryAlert):
await delivery_alert.save()
self._loggers.delivery_alert.info(json.dumps(delivery_alert.dict()))

self._subscriptions.append(
self.rmf.delivery_alerts.subscribe(lambda x: self._create_task(update(x)))
)

def _record_building_map(self):
async def update(building_map: BuildingMap):
if not building_map:
Expand Down
Loading
Loading