Skip to content

Commit

Permalink
Publish API connected value
Browse files Browse the repository at this point in the history
  • Loading branch information
b-Tomas committed Feb 3, 2025
1 parent 4a0e54a commit 5e99725
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mir_connector/inorbit_mir_connector/src/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ def _execution_loop(self):
"mode_text": mode_text,
"robot_model": self.status["robot_model"],
"waiting_for": self.mission_tracking.waiting_for_text,
# The API connected value tells wether or not the last API call was successful
# It is recommended to create a status based on this value and use it for incidents
"api_connected": self.mir_api.get_last_api_call_successful(),
}
self._logger.debug(f"Publishing key values: {key_values}")
self._robot_session.publish_key_values(key_values)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ class MirApiBaseClass(ABC):
def __init__(self, loglevel):
self.logger = logging.getLogger(name=self.__class__.__name__)
self.logger.setLevel(loglevel)
self.last_api_call_successful = None

def _handle_status(self, res, request_args):
"""Log and raise an exception if the request failed."""
try:
res.raise_for_status()
self.set_last_api_call_successful(True)
except HTTPError as e:
self.logger.error(f"Error making request: {e}\nArguments: {request_args}")
self.set_last_api_call_successful(False)
raise e

def _get(self, url: str, session: Session, **kwargs) -> Response:
Expand Down Expand Up @@ -52,6 +55,12 @@ def _put(self, url: str, session: Session, **kwargs) -> Response:
self._handle_status(res, kwargs)
return res

def set_last_api_call_successful(self, success: bool) -> None:
self.last_api_call_successful = success

def get_last_api_call_successful(self) -> bool:
return self.last_api_call_successful

@abstractmethod
def _create_api_session(self) -> Session:
"""Configures a session object to interact with the MiR API."""
Expand Down

0 comments on commit 5e99725

Please sign in to comment.