| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
"""This module implements a Temperature source""" |
| 19 |
|
| 20 |
from __future__ import annotations |
| 21 |
|
| 22 |
import logging |
| 23 |
import warnings |
| 24 |
from collections import OrderedDict |
| 25 |
|
| 26 |
import psutil |
| 27 |
|
| 28 |
from s_tui.sources.source import Source |
| 29 |
|
| 30 |
|
| 31 |
class TempSource(Source): |
| 32 |
"""This class inherits a source and implements a temperature source""" |
| 33 |
|
| 34 |
THRESHOLD_TEMP = 80 |
| 35 |
|
| 36 |
def __init__(self, temp_thresh: int | str | None = None) -> None: |
| 37 |
warnings.filterwarnings( |
| 38 |
"ignore", |
| 39 |
".*FileNotFound.*", |
| 40 |
) |
| 41 |
try: |
| 42 |
sensors_data = psutil.sensors_temperatures() |
| 43 |
if not sensors_data: |
| 44 |
self.is_available = False |
| 45 |
logging.debug("sensors_temperatures() returned empty/None") |
| 46 |
return |
| 47 |
self.is_available = True |
| 48 |
except (AttributeError, OSError): |
| 49 |
self.is_available = False |
| 50 |
logging.debug("cpu temperature is not available from psutil") |
| 51 |
return |
| 52 |
|
| 53 |
Source.__init__(self) |
| 54 |
|
| 55 |
self.name = "Temp" |
| 56 |
self.measurement_unit = "C" |
| 57 |
self.max_last_temp = 0 |
| 58 |
self.temp_thresh_is_set = False |
| 59 |
self.pallet = ( |
| 60 |
"temp light", |
| 61 |
"temp dark", |
| 62 |
"temp light smooth", |
| 63 |
"temp dark smooth", |
| 64 |
) |
| 65 |
self.alert_pallet = ( |
| 66 |
"high temp light", |
| 67 |
"high temp dark", |
| 68 |
"high temp light smooth", |
| 69 |
"high temp dark smooth", |
| 70 |
) |
| 71 |
|
| 72 |
self.max_temp = 10 |
| 73 |
sensors_dict = None |
| 74 |
try: |
| 75 |
sensors_dict = OrderedDict(sorted(psutil.sensors_temperatures().items())) |
| 76 |
except OSError: |
| 77 |
logging.debug("Unable to create sensors dict") |
| 78 |
self.is_available = False |
| 79 |
return |
| 80 |
self._sensor_lookup = {} |
| 81 |
multi_sensors = [] |
| 82 |
for key, value in sensors_dict.items(): |
| 83 |
sensor_name = "".join(key.title().split(" ")) |
| 84 |
for sensor_idx, sensor in enumerate(value): |
| 85 |
sensor_label = sensor.label |
| 86 |
if sensor.current <= 1.0 or sensor.current >= 127.0: |
| 87 |
continue |
| 88 |
|
| 89 |
full_name = "" |
| 90 |
if not sensor_label: |
| 91 |
full_name = sensor_name + "," + str(sensor_idx) |
| 92 |
else: |
| 93 |
full_name = "".join(sensor_label.title().split(" ")) |
| 94 |
sensor_count = multi_sensors.count(full_name) |
| 95 |
multi_sensors.append(full_name) |
| 96 |
full_name += "," + str(sensor_count) |
| 97 |
|
| 98 |
logging.debug("Temp sensor name %s", full_name) |
| 99 |
self.available_sensors.append(full_name) |
| 100 |
self._sensor_lookup[(key, sensor_idx)] = len(self.available_sensors) - 1 |
| 101 |
|
| 102 |
self.sensor_available = [True] * len(self.available_sensors) |
| 103 |
self.last_measurement = [0.0] * len(self.available_sensors) |
| 104 |
|
| 105 |
|
| 106 |
self.temp_thresh = self.THRESHOLD_TEMP |
| 107 |
if temp_thresh is not None: |
| 108 |
self.temp_thresh_is_set = True |
| 109 |
if int(temp_thresh) > 0: |
| 110 |
self.temp_thresh = int(temp_thresh) |
| 111 |
logging.debug("Updated custom threshold to %s", self.temp_thresh) |
| 112 |
|
| 113 |
|
| 114 |
self.last_thresholds = [self.temp_thresh] * len(self.available_sensors) |
| 115 |
|
| 116 |
def update(self) -> None: |
| 117 |
try: |
| 118 |
sensors_data = psutil.sensors_temperatures() |
| 119 |
except OSError as e: |
| 120 |
logging.debug("sensors_temperatures() raised %s, keeping stale data", e) |
| 121 |
return |
| 122 |
|
| 123 |
if sensors_data is None: |
| 124 |
return |
| 125 |
|
| 126 |
try: |
| 127 |
sample = OrderedDict(sorted(sensors_data.items())) |
| 128 |
except OSError: |
| 129 |
return |
| 130 |
|
| 131 |
updated = set() |
| 132 |
for key, sensors in sample.items(): |
| 133 |
for sensor_idx, minor_sensor in enumerate(sensors): |
| 134 |
idx = self._sensor_lookup.get((key, sensor_idx)) |
| 135 |
if idx is None: |
| 136 |
continue |
| 137 |
if minor_sensor.current <= 1.0 or minor_sensor.current >= 127.0: |
| 138 |
self.sensor_available[idx] = False |
| 139 |
continue |
| 140 |
self.last_measurement[idx] = minor_sensor.current |
| 141 |
if ( |
| 142 |
minor_sensor.high is not None |
| 143 |
and minor_sensor.high |
| 144 |
and minor_sensor.high < 127.0 |
| 145 |
and self.temp_thresh_is_set is False |
| 146 |
): |
| 147 |
self.last_thresholds[idx] = minor_sensor.high |
| 148 |
else: |
| 149 |
self.last_thresholds[idx] = self.temp_thresh |
| 150 |
self.sensor_available[idx] = True |
| 151 |
updated.add(idx) |
| 152 |
|
| 153 |
|
| 154 |
for idx in range(len(self.available_sensors)): |
| 155 |
if idx not in updated: |
| 156 |
self.sensor_available[idx] = False |
| 157 |
|
| 158 |
available_temps = [ |
| 159 |
self.last_measurement[i] |
| 160 |
for i in range(len(self.available_sensors)) |
| 161 |
if self.sensor_available[i] |
| 162 |
] |
| 163 |
if available_temps: |
| 164 |
self.max_last_temp = max(available_temps) |
| 165 |
|
| 166 |
Source.update(self) |
| 167 |
|
| 168 |
def get_edge_triggered(self) -> bool: |
| 169 |
return self.max_last_temp > self.temp_thresh |
| 170 |
|
| 171 |
def get_sensor_alerts(self) -> list[str | None]: |
| 172 |
"""Return per-sensor alert attributes matching graph bar coloring.""" |
| 173 |
triggered = self.max_last_temp > self.temp_thresh |
| 174 |
alerts: list[str | None] = [None] * len(self.available_sensors) |
| 175 |
for idx in range(len(self.available_sensors)): |
| 176 |
if not self.sensor_available[idx]: |
| 177 |
continue |
| 178 |
thresh = self.last_thresholds[idx] |
| 179 |
if (thresh is None and triggered) or ( |
| 180 |
thresh is not None and self.last_measurement[idx] > thresh |
| 181 |
): |
| 182 |
alerts[idx] = "high temp txt" |
| 183 |
return alerts |
| 184 |
|
| 185 |
def get_max_triggered(self) -> bool: |
| 186 |
"""Returns whether the current temperature threshold is exceeded""" |
| 187 |
return self.max_temp > self.temp_thresh |
| 188 |
|
| 189 |
def reset(self) -> None: |
| 190 |
self.max_temp = 10 |
| 191 |
|
| 192 |
def get_maximum(self) -> float: |
| 193 |
raise NotImplementedError("Get maximum is not implemented") |
| 194 |
|
| 195 |
def get_top(self) -> int: |
| 196 |
|
| 197 |
if hasattr(self, "_cached_top_temp"): |
| 198 |
return self._cached_top_temp |
| 199 |
|
| 200 |
top_temp = 10 |
| 201 |
available_temps = psutil.sensors_temperatures().values() |
| 202 |
for temp in available_temps: |
| 203 |
for temp_minor in temp: |
| 204 |
if ( |
| 205 |
temp_minor.high is not None |
| 206 |
and temp_minor.high > top_temp |
| 207 |
and temp_minor.critical is not None |
| 208 |
): |
| 209 |
top_temp = temp_minor.critical |
| 210 |
self._cached_top_temp = int(min(top_temp, 99)) |
| 211 |
return self._cached_top_temp |