Skip to content

Commit 41e6c2f

Browse files
committed
Remove queue_utils
1 parent 2ad0e7d commit 41e6c2f

22 files changed

Lines changed: 104 additions & 185 deletions

File tree

src/isar/models/communication/queues/events.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections import deque
22
from queue import Empty, Queue
3-
from typing import TypeVar
3+
from typing import Optional, TypeVar
44

55
from transitions import State
66

@@ -17,9 +17,23 @@ class Event(Queue[T]):
1717
def __init__(self) -> None:
1818
super().__init__(maxsize=1)
1919

20-
def check(self) -> T:
20+
def trigger_event(self, data: T) -> None:
21+
self.put(data)
22+
23+
def consume_event(self) -> Optional[T]:
24+
try:
25+
return self.get(block=False)
26+
except Empty:
27+
return None
28+
29+
def has_event(self) -> bool:
30+
return (
31+
self.qsize() != 0
32+
) # Queue size is not reliable, but should be sufficient for this case
33+
34+
def check(self) -> Optional[T]:
2135
if not self._qsize():
22-
raise Empty
36+
return None
2337
with self.mutex:
2438
queueList = list(self.queue)
2539
return queueList.pop()
@@ -65,7 +79,7 @@ def __init__(self) -> None:
6579
class StateMachineEvents:
6680
def __init__(self) -> None:
6781
self.start_mission: Event[Mission] = Event()
68-
self.stop_mission: Event[str] = Event()
82+
self.stop_mission: Event[bool] = Event()
6983
self.pause_mission: Event[bool] = Event()
7084
self.task_status_request: Event[str] = Event()
7185

src/isar/models/communication/queues/queue_utils.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/isar/robot/robot.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
SharedState,
1010
StateMachineEvents,
1111
)
12-
from isar.models.communication.queues.queue_utils import check_for_event, trigger_event
1312
from isar.robot.robot_start_mission import RobotStartMissionThread
1413
from isar.robot.robot_status import RobotStatusThread
1514
from isar.robot.robot_stop_mission import RobotStopMissionThread
@@ -55,7 +54,7 @@ def stop(self) -> None:
5554
self.start_mission_thread = None
5655

5756
def _start_mission_event_handler(self, event: Event[Mission]) -> None:
58-
start_mission = check_for_event(event)
57+
start_mission = event.consume_event()
5958
if start_mission is not None:
6059
if (
6160
self.start_mission_thread is not None
@@ -74,7 +73,7 @@ def _start_mission_event_handler(self, event: Event[Mission]) -> None:
7473
self.start_mission_thread.start()
7574

7675
def _task_status_request_handler(self, event: Event[str]) -> None:
77-
task_id: str = check_for_event(event)
76+
task_id: str = event.consume_event()
7877
if task_id:
7978
self.robot_task_status_thread = RobotTaskStatusThread(
8079
self.robot_service_events,
@@ -84,8 +83,8 @@ def _task_status_request_handler(self, event: Event[str]) -> None:
8483
)
8584
self.robot_task_status_thread.start()
8685

87-
def _stop_mission_request_handler(self, event: Event[str]) -> None:
88-
if check_for_event(event):
86+
def _stop_mission_request_handler(self, event: Event[bool]) -> None:
87+
if event.consume_event():
8988
if (
9089
self.stop_mission_thread is not None
9190
and self.stop_mission_thread.is_alive()
@@ -103,8 +102,8 @@ def _stop_mission_request_handler(self, event: Event[str]) -> None:
103102
error_reason=ErrorReason.RobotStillStartingMissionException,
104103
error_description=error_description,
105104
)
106-
trigger_event(
107-
self.robot_service_events.mission_failed_to_stop, error_message
105+
self.robot_service_events.mission_failed_to_stop.trigger_event(
106+
error_message
108107
)
109108
return
110109
self.stop_mission_thread = RobotStopMissionThread(

src/isar/robot/robot_start_mission.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33

44
from isar.config.settings import settings
55
from isar.models.communication.queues.events import RobotServiceEvents
6-
from isar.models.communication.queues.queue_utils import (
7-
trigger_event,
8-
trigger_event_without_data,
9-
)
106
from robot_interface.models.exceptions.robot_exceptions import (
117
ErrorMessage,
128
RobotException,
@@ -44,13 +40,13 @@ def run(self):
4440
self.logger.error(
4541
f"Mission is infeasible and cannot be scheduled because: {e.error_description}"
4642
)
47-
trigger_event(
48-
self.robot_service_events.mission_failed,
43+
self.robot_service_events.mission_failed.trigger_event(
4944
ErrorMessage(
5045
error_reason=e.error_reason,
5146
error_description=e.error_description,
52-
),
47+
)
5348
)
49+
5450
break
5551
except RobotException as e:
5652
retries += 1
@@ -66,25 +62,23 @@ def run(self):
6662
f"{e.error_description}"
6763
)
6864

69-
trigger_event(
70-
self.robot_service_events.mission_failed,
65+
self.robot_service_events.mission_failed.trigger_event(
7166
ErrorMessage(
7267
error_reason=e.error_reason,
7368
error_description=e.error_description,
74-
),
69+
)
7570
)
7671
break
7772

7873
continue
7974

8075
started_mission = True
8176
except RobotInfeasibleMissionException as e:
82-
trigger_event(
83-
self.robot_service_events.mission_failed,
77+
self.robot_service_events.mission_failed.trigger_event(
8478
ErrorMessage(
8579
error_reason=e.error_reason, error_description=e.error_description
8680
),
8781
)
8882

8983
if started_mission:
90-
trigger_event_without_data(self.robot_service_events.mission_started)
84+
self.robot_service_events.mission_started.trigger_event(True)

src/isar/robot/robot_status.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from isar.config.settings import settings
66
from isar.models.communication.queues.events import SharedState
7-
from isar.models.communication.queues.queue_utils import update_shared_state
87
from robot_interface.models.exceptions.robot_exceptions import RobotException
98
from robot_interface.robot_interface import RobotInterface
109

@@ -49,7 +48,7 @@ def run(self):
4948
self.last_robot_status_poll_time = time.time()
5049

5150
robot_status = self.robot.robot_status()
52-
update_shared_state(self.shared_state.robot_status, robot_status)
51+
self.shared_state.robot_status.update(robot_status)
5352
except RobotException as e:
5453
self.logger.error(f"Failed to retrieve robot status: {e}")
5554
continue

src/isar/robot/robot_stop_mission.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55

66
from isar.config.settings import settings
77
from isar.models.communication.queues.events import RobotServiceEvents
8-
from isar.models.communication.queues.queue_utils import (
9-
trigger_event,
10-
trigger_event_without_data,
11-
)
128
from robot_interface.models.exceptions.robot_exceptions import (
139
ErrorMessage,
1410
RobotActionException,
@@ -51,9 +47,7 @@ def run(self) -> None:
5147
time.sleep(settings.FSM_SLEEP_TIME)
5248
continue
5349

54-
trigger_event_without_data(
55-
self.robot_service_events.mission_successfully_stopped
56-
)
50+
self.robot_service_events.mission_successfully_stopped.trigger_event(True)
5751
return
5852

5953
error_description = (
@@ -68,4 +62,4 @@ def run(self) -> None:
6862
error_description=error_description,
6963
)
7064

71-
trigger_event(self.robot_service_events.mission_failed_to_stop, error_message)
65+
self.robot_service_events.mission_failed_to_stop.trigger_event(error_message)

src/isar/robot/robot_task_status.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from isar.config.settings import settings
77
from isar.models.communication.queues.events import RobotServiceEvents
8-
from isar.models.communication.queues.queue_utils import trigger_event
98
from isar.services.utilities.threaded_request import ThreadedRequest
109
from robot_interface.models.exceptions.robot_exceptions import (
1110
ErrorMessage,
@@ -82,10 +81,7 @@ def run(self) -> None:
8281
)
8382
break
8483

85-
trigger_event(self.robot_service_events.task_status_updated, task_status)
84+
self.robot_service_events.task_status_updated.trigger_event(task_status)
8685
return
8786

88-
trigger_event(
89-
self.robot_service_events.task_status_failed,
90-
failed_task_error,
91-
)
87+
self.robot_service_events.task_status_failed.trigger_event(failed_task_error)

src/isar/services/utilities/scheduling_utilities.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22
from copy import deepcopy
33
from http import HTTPStatus
4-
from queue import Empty
54
from typing import Any, List
65

76
from fastapi import HTTPException
@@ -55,16 +54,16 @@ def get_state(self) -> States:
5554
HTTPException 500 Internal Server Error
5655
If the current state is not available on the queue
5756
"""
58-
try:
59-
return self.shared_state.state.check()
60-
except Empty:
57+
current_state = self.shared_state.state.check()
58+
if current_state is None:
6159
error_message: str = (
6260
"Internal Server Error - Current state of the state machine is unknown"
6361
)
6462
self.logger.error(error_message)
6563
raise HTTPException(
6664
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=error_message
6765
)
66+
return current_state
6867

6968
def get_mission(self, mission_id: str) -> Mission:
7069
"""Get the mission with mission_id from the current mission planner

src/isar/state_machine/state_machine.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
TaskSelectorStop,
1616
)
1717
from isar.models.communication.queues.events import Events, SharedState
18-
from isar.models.communication.queues.queue_utils import update_shared_state
1918
from isar.state_machine.states.await_next_mission import AwaitNextMission
2019
from isar.state_machine.states.blocked_protective_stop import BlockedProtectiveStop
2120
from isar.state_machine.states.home import Home
@@ -189,7 +188,7 @@ def iterate_current_task(self):
189188
def update_state(self):
190189
"""Updates the current state of the state machine."""
191190
self.current_state = States(self.state) # type: ignore
192-
update_shared_state(self.shared_state.state, self.current_state)
191+
self.shared_state.state.update(self.current_state)
193192
self._log_state_transition(self.current_state)
194193
self.logger.info("State: %s", self.current_state)
195194
self.publish_status()
@@ -208,9 +207,7 @@ def start_mission(self, mission: Mission):
208207
self.task_selector.initialize(tasks=self.current_mission.tasks)
209208

210209
def send_task_status(self):
211-
update_shared_state(
212-
self.shared_state.state_machine_current_task, self.current_task
213-
)
210+
self.shared_state.state_machine_current_task.update(self.current_task)
214211

215212
def report_task_status(self, task: Task) -> None:
216213
if task.status == TaskStatus.Failed:

src/isar/state_machine/states/blocked_protective_stop.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from isar.eventhandlers.eventhandler import EventHandlerBase, EventHandlerMapping
44
from isar.models.communication.queues.events import Event
5-
from isar.models.communication.queues.queue_utils import check_shared_state
65
from robot_interface.models.mission.status import RobotStatus
76

87
if TYPE_CHECKING:
@@ -15,7 +14,7 @@ def __init__(self, state_machine: "StateMachine"):
1514
shared_state = state_machine.shared_state
1615

1716
def _robot_status_event_handler(event: Event[RobotStatus]):
18-
robot_status: RobotStatus = check_shared_state(event)
17+
robot_status: RobotStatus = event.check()
1918
if robot_status != RobotStatus.BlockedProtectiveStop:
2019
return state_machine.robot_status_changed # type: ignore
2120
return None

0 commit comments

Comments
 (0)