Skip to content

Commit

Permalink
Handle no battery data without crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Feb 21, 2024
1 parent f427fa2 commit 58137dd
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/isar_exr/api/energy_robotics_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import datetime
from logging import Logger
from time import sleep
from typing import Any, Dict
from typing import Any, Dict, Optional

from gql.dsl import DSLMutation, DSLQuery, DSLSchema, DSLVariableDefinitions, dsl_gql
from robot_interface.models.exceptions.robot_exceptions import (
Expand Down Expand Up @@ -506,7 +506,7 @@ def is_robot_awake(self, exr_robot_id: str) -> bool:
success: bool = status in [AwakeStatus.Awake]
return success

def get_battery_level(self, exr_robot_id: str) -> float:
def get_battery_level(self, exr_robot_id: str) -> Optional[float]:
params: dict = {"robotID": exr_robot_id}

variable_definitions_graphql: DSLVariableDefinitions = DSLVariableDefinitions()
Expand All @@ -528,17 +528,20 @@ def get_battery_level(self, exr_robot_id: str) -> float:
result: Dict[str, Any] = self.client.query(
dsl_gql(check_battery_query), params
)
except TimeoutError as e:
self.logger.error(
"Could not check robot battery level due to request timeout"
)
return None
except Exception as e:
message: str = "Could not check robot battery level"
self.logger.error(message)
raise RobotMissionStatusException( # TODO: occurs after obstacle
raise RobotMissionStatusException(
error_description=message,
)

if not result["currentRobotStatus"]["isConnected"]:
raise RobotMissionStatusException(
error_description="Robot is not connected",
)
return None

battery_level: float = result["currentRobotStatus"]["batteryStatus"][
"percentage"
Expand Down

0 comments on commit 58137dd

Please sign in to comment.