Skip to content

Commit

Permalink
Attempt at fixing no sensors OHM init error.
Browse files Browse the repository at this point in the history
Also added custom exceptions for hardware_monitor.py
  • Loading branch information
andreabak committed Nov 9, 2020
1 parent 567acc0 commit a1fa5f0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
20 changes: 19 additions & 1 deletion RGBHardwareMonitor/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import configparser
import traceback
from time import sleep
from typing import Optional

from RGBHardwareMonitor.hardware_monitor import HMNoSensorsError
from . import runtime
from . import rgb_serial
from . import hardware_monitor
Expand Down Expand Up @@ -103,7 +105,23 @@ def real_main():
rgb_serial.arduino_id = runtime.config['RGBHardwareMonitor']['arduino_serial_id']

with RGBHardwareMonitorSysTray(animation_cls=WaitIconAnimation, start_animation=True) as systray:
rgb_serial.rings = ring_lights_from_cfg(runtime.config) # TODO: Handle sensor doesn't exist exception
init_exc: Optional[Exception] = None
for _ in range(3):
try:
rgb_serial.rings = ring_lights_from_cfg(runtime.config)
except HMNoSensorsError as exc:
init_exc = exc
sleeptime: float = 5.0
logger.debug(f'Got no sensors found error. OpenHardwareMonitor initializing? Retrying in {sleeptime}')
sleep(sleeptime)
except Exception as exc:
init_exc = exc
raise
else:
init_exc = None
break
else:
raise RuntimeError(f'Initialization failed: {init_exc}') from init_exc
while not quit_event.is_set():
if not pause_event.is_set():
rgb_serial.update_loop(systray=systray)
Expand Down
30 changes: 27 additions & 3 deletions RGBHardwareMonitor/hardware_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@
openhardwaremonitor_exe_path: Optional[str] = None


class HardwareMonitorError(Exception):
"""Base class for hardware monitor exceptions"""


class HMExecError(HardwareMonitorError):
"""Exception class for process execution errors"""


class HMWMIError(HardwareMonitorError):
"""Exception class for WMI related errors"""


class HMWMINamespaceError(HMWMIError):
"""Exception class for WMI namespace not found errors"""


class HMNoSensorsError(HardwareMonitorError):
"""Exception class for no sensors returned from WMI query"""


class HMSensorNotFound(HardwareMonitorError):
"""Exception class for sensor not found from WMI query"""


# TODO: Catch WMI errors (like disconnection), check wmi lib


Expand All @@ -26,7 +50,7 @@ def is_openhardwaremonitor_running():

def openhardwaremonitor_start():
if openhardwaremonitor_exe_path is None:
raise ValueError('Cannot run OpenHardwareMonitor: executable path not specified')
raise HMExecError('Cannot run OpenHardwareMonitor: executable path not specified')
run_dir = Path(openhardwaremonitor_exe_path).parent
logger.debug('Starting OpenHardwareMonitor...')
run_as_admin(openhardwaremonitor_exe_path, run_dir=str(run_dir))
Expand All @@ -38,7 +62,7 @@ def openhardwaremonitor_start():
break
retry -= 1
else:
raise RuntimeError('Failed starting OpenHardwareMonitor: WMI timed out')
raise HMWMIError('Failed starting OpenHardwareMonitor: WMI timed out')


@dataclass
Expand Down Expand Up @@ -213,7 +237,7 @@ def __post_init__(self, start_ohm):
wmi_ohm = _wmi_get_ohm()
if not is_openhardwaremonitor_running():
if not start_ohm:
raise RuntimeError('Failed while querying OpenHardwareMonitor WMI namespace. OHM not running?')
raise HMWMINamespaceError('Failed while querying OpenHardwareMonitor WMI namespace. OHM not running?')
openhardwaremonitor_start()
wmi_ohm = _wmi_get_ohm()
self.name = WMI().Win32_ComputerSystem()[0].Name
Expand Down
9 changes: 6 additions & 3 deletions RGBHardwareMonitor/rgb_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .log import logger
from .runtime import quit_event, pause_event
from .hardware_monitor import SystemInfo, Sensor
from .hardware_monitor import SystemInfo, Sensor, HMNoSensorsError, HMSensorNotFound
from .systray import WaitIconAnimation, RunningIconAnimation


Expand All @@ -32,15 +32,18 @@ class SensorSpec:
def __post_init__(self):
if self.__class__.system_info is None:
self.__class__.system_info = SystemInfo(start_ohm=True)
for sensor in getattr(self.__class__.system_info, self.device).sensors:
sensors = getattr(self.__class__.system_info, self.device).sensors
if not sensors:
raise HMNoSensorsError('No sensors available from hardware monitor')
for sensor in sensors:
for f_attr, f_val in self.filters.items():
if getattr(sensor, f_attr) != f_val: # if any filter fails, break loop
break
else: # didn't break loop, so all filters matched for sensor
self.sensor = sensor
break
else:
raise OSError(f'Sensor not found (device: {self.device}, filters: {str(self.filters)})')
raise HMSensorNotFound(f'Sensor not found (device: {self.device}, filters: {str(self.filters)})')

@property
def value(self):
Expand Down

0 comments on commit a1fa5f0

Please sign in to comment.