-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Anders Chirico Indrebø <[email protected]>
- Loading branch information
Showing
8 changed files
with
160 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import logging | ||
import time | ||
from typing import TYPE_CHECKING, Optional | ||
|
||
from transitions import State | ||
|
||
from isar.config.settings import settings | ||
from isar.services.utilities.threaded_request import ( | ||
ThreadedRequest, | ||
ThreadedRequestNotFinishedError, | ||
) | ||
from robot_interface.models.exceptions.robot_exceptions import RobotException | ||
from robot_interface.models.mission.status import RobotStatus | ||
|
||
if TYPE_CHECKING: | ||
from isar.state_machine.state_machine import StateMachine | ||
|
||
|
||
class Offline(State): | ||
def __init__(self, state_machine: "StateMachine") -> None: | ||
super().__init__(name="offline", on_enter=self.start, on_exit=self.stop) | ||
self.state_machine: "StateMachine" = state_machine | ||
self.logger = logging.getLogger("state_machine") | ||
self.robot_status_thread: Optional[ThreadedRequest] = None | ||
|
||
def start(self) -> None: | ||
self.state_machine.update_state() | ||
self._run() | ||
|
||
def stop(self) -> None: | ||
if self.robot_status_thread: | ||
self.robot_status_thread.wait_for_thread() | ||
self.robot_status_thread = None | ||
|
||
def _run(self) -> None: | ||
while True: | ||
if not self.robot_status_thread: | ||
self.robot_status_thread = ThreadedRequest( | ||
request_func=self.state_machine.robot.robot_status | ||
) | ||
self.robot_status_thread.start_thread( | ||
name="State Machine Offline Get Robot Status" | ||
) | ||
|
||
try: | ||
robot_status: RobotStatus = self.robot_status_thread.get_output() | ||
except ThreadedRequestNotFinishedError: | ||
time.sleep(self.state_machine.sleep_time) | ||
continue | ||
|
||
except (RobotException,) as e: | ||
self.logger.error( | ||
f"Failed to get robot status because: {e.error_description}" | ||
) | ||
|
||
if robot_status != RobotStatus.Offline: | ||
transition = self.state_machine.robot_turned_online # type: ignore | ||
break | ||
|
||
time.sleep(settings.ROBOT_API_STATUS_POLL_INTERVAL) | ||
|
||
transition() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters