| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import contextlib |
| 21 |
import logging |
| 22 |
import math |
| 23 |
|
| 24 |
from s_tui.sturwid.complex_bar_graph import LabeledBarGraphVector, ScalableBarGraph |
| 25 |
|
| 26 |
logger = logging.getLogger(__name__) |
| 27 |
|
| 28 |
|
| 29 |
class BarGraphVector(LabeledBarGraphVector): |
| 30 |
@staticmethod |
| 31 |
def append_latest_value(values, new_val): |
| 32 |
values.append(new_val) |
| 33 |
return values[1:] |
| 34 |
|
| 35 |
MAX_SAMPLES = 1000 |
| 36 |
SCALE_DENSITY = 5 |
| 37 |
|
| 38 |
def __init__( |
| 39 |
self, |
| 40 |
source, |
| 41 |
regular_colors, |
| 42 |
graph_count, |
| 43 |
visible_graph_list, |
| 44 |
alert_colors=None, |
| 45 |
bar_width=1, |
| 46 |
): |
| 47 |
self.source = source |
| 48 |
self.graph_count = graph_count |
| 49 |
self.graph_name = self.source.get_source_name() |
| 50 |
self.measurement_unit = self.source.get_measurement_unit() |
| 51 |
|
| 52 |
self.num_samples = self.MAX_SAMPLES |
| 53 |
|
| 54 |
self.graph_data = [] |
| 55 |
|
| 56 |
for _ in range(graph_count): |
| 57 |
self.graph_data.append([0] * self.num_samples) |
| 58 |
|
| 59 |
|
| 60 |
self.graph_max = 1 |
| 61 |
|
| 62 |
self.color_a = regular_colors[0] |
| 63 |
self.color_b = regular_colors[1] |
| 64 |
self.smooth_a = regular_colors[2] |
| 65 |
self.smooth_b = regular_colors[3] |
| 66 |
|
| 67 |
if alert_colors: |
| 68 |
self.alert_colors = alert_colors |
| 69 |
else: |
| 70 |
self.alert_colors = regular_colors |
| 71 |
self.regular_colors = regular_colors |
| 72 |
|
| 73 |
self.satt = None |
| 74 |
|
| 75 |
y_label = [] |
| 76 |
|
| 77 |
graph_title = self.graph_name + " [" + self.measurement_unit + "]" |
| 78 |
sub_title_list = self.source.get_sensor_list() |
| 79 |
|
| 80 |
|
| 81 |
w = [] |
| 82 |
for _ in range(graph_count): |
| 83 |
graph = ScalableBarGraph(["bg background", self.color_a, self.color_b]) |
| 84 |
w.append(graph) |
| 85 |
super().__init__(graph_title, sub_title_list, y_label, w, visible_graph_list) |
| 86 |
|
| 87 |
for graph in self.bar_graph_vector: |
| 88 |
graph.set_bar_width(bar_width) |
| 89 |
|
| 90 |
self.color_counter_vector = [0] * graph_count |
| 91 |
|
| 92 |
def _set_colors(self, graph, colors): |
| 93 |
self.color_a = colors[0] |
| 94 |
self.color_b = colors[1] |
| 95 |
self.smooth_a = colors[2] |
| 96 |
self.smooth_b = colors[3] |
| 97 |
if self.satt: |
| 98 |
self.satt = {(1, 0): self.smooth_a, (2, 0): self.smooth_b} |
| 99 |
|
| 100 |
graph.set_segment_attributes( |
| 101 |
["bg background", self.color_a, self.color_b], satt=self.satt |
| 102 |
) |
| 103 |
|
| 104 |
def get_graph_name(self): |
| 105 |
return self.graph_name |
| 106 |
|
| 107 |
def get_measurement_unit(self): |
| 108 |
return self.measurement_unit |
| 109 |
|
| 110 |
def get_is_available(self): |
| 111 |
return self.source.get_is_available() |
| 112 |
|
| 113 |
def get_label_scale(self, min_val, max_val, size): |
| 114 |
"""Dynamically change the scale of the graph (y label)""" |
| 115 |
label_cnt = 1 if size < self.SCALE_DENSITY else int(size / self.SCALE_DENSITY) |
| 116 |
try: |
| 117 |
if max_val >= 100: |
| 118 |
label = [ |
| 119 |
int(min_val + i * (max_val - min_val) / label_cnt) |
| 120 |
for i in range(label_cnt + 1) |
| 121 |
] |
| 122 |
else: |
| 123 |
label = [ |
| 124 |
round((min_val + i * (max_val - min_val) / label_cnt), 1) |
| 125 |
for i in range(label_cnt + 1) |
| 126 |
] |
| 127 |
return label |
| 128 |
except ZeroDivisionError: |
| 129 |
logging.debug("Side label creation divided by 0") |
| 130 |
return "" |
| 131 |
|
| 132 |
def set_smooth_colors(self, smooth): |
| 133 |
if smooth: |
| 134 |
self.satt = {(1, 0): self.smooth_a, (2, 0): self.smooth_b} |
| 135 |
else: |
| 136 |
self.satt = None |
| 137 |
|
| 138 |
for graph in self.bar_graph_vector: |
| 139 |
graph.set_segment_attributes( |
| 140 |
["bg background", self.color_a, self.color_b], satt=self.satt |
| 141 |
) |
| 142 |
|
| 143 |
def update(self): |
| 144 |
if not self.get_is_available(): |
| 145 |
return |
| 146 |
|
| 147 |
|
| 148 |
triggered = False |
| 149 |
with contextlib.suppress(NotImplementedError): |
| 150 |
triggered = self.source.get_edge_triggered() |
| 151 |
|
| 152 |
current_reading = self.source.get_reading_list() |
| 153 |
current_thresholds = self.source.get_threshold_list() |
| 154 |
logging.info("Reading %s", current_reading) |
| 155 |
|
| 156 |
y_label_size_max = 0 |
| 157 |
local_top_value = [] |
| 158 |
|
| 159 |
graph_display_data = [] |
| 160 |
|
| 161 |
|
| 162 |
for graph_idx, graph in enumerate(self.bar_graph_vector): |
| 163 |
|
| 164 |
has_threshold = ( |
| 165 |
graph_idx < len(current_thresholds) |
| 166 |
and current_thresholds[graph_idx] is not None |
| 167 |
) |
| 168 |
if ( |
| 169 |
|
| 170 |
(not has_threshold and triggered) |
| 171 |
|
| 172 |
|
| 173 |
or ( |
| 174 |
has_threshold |
| 175 |
and current_reading[graph_idx] > current_thresholds[graph_idx] |
| 176 |
) |
| 177 |
): |
| 178 |
self._set_colors(graph, self.alert_colors) |
| 179 |
else: |
| 180 |
self._set_colors(graph, self.regular_colors) |
| 181 |
try: |
| 182 |
_ = self.visible_graph_list[graph_idx] |
| 183 |
except IndexError: |
| 184 |
|
| 185 |
self.visible_graph_list.append(True) |
| 186 |
|
| 187 |
if self.visible_graph_list[graph_idx]: |
| 188 |
self.graph_data[graph_idx] = self.append_latest_value( |
| 189 |
self.graph_data[graph_idx], current_reading[graph_idx] |
| 190 |
) |
| 191 |
|
| 192 |
|
| 193 |
num_displayed_bars = graph.get_size()[1] |
| 194 |
start_idx = self.MAX_SAMPLES - num_displayed_bars |
| 195 |
|
| 196 |
visible_graph_data = self.graph_data[graph_idx][start_idx - 1 :] |
| 197 |
local_top_value.append(max(visible_graph_data)) |
| 198 |
graph_display_data.append( |
| 199 |
(graph_idx, graph, start_idx, num_displayed_bars) |
| 200 |
) |
| 201 |
else: |
| 202 |
graph_display_data.append(None) |
| 203 |
|
| 204 |
update_max = False |
| 205 |
if self.graph_max == 1: |
| 206 |
self.graph_max = self.source.get_top() |
| 207 |
update_max = True |
| 208 |
|
| 209 |
local_max = math.ceil(max(local_top_value)) |
| 210 |
if local_max > self.graph_max: |
| 211 |
update_max = True |
| 212 |
self.graph_max = local_max |
| 213 |
|
| 214 |
|
| 215 |
for entry in graph_display_data: |
| 216 |
if entry is None: |
| 217 |
continue |
| 218 |
graph_idx, graph, start_idx, num_displayed_bars = entry |
| 219 |
bars = [] |
| 220 |
|
| 221 |
swap = self.color_counter_vector[graph_idx] % 2 == 1 |
| 222 |
for n in range(start_idx, self.MAX_SAMPLES): |
| 223 |
value = round(self.graph_data[graph_idx][n], 1) |
| 224 |
|
| 225 |
first = (n & 1) ^ swap |
| 226 |
if first: |
| 227 |
bars.append([0, value]) |
| 228 |
else: |
| 229 |
bars.append([value, 0]) |
| 230 |
self.color_counter_vector[graph_idx] += 1 |
| 231 |
|
| 232 |
graph.set_data(bars, float(self.graph_max)) |
| 233 |
y_label_size_max = max(y_label_size_max, graph.get_size()[0]) |
| 234 |
|
| 235 |
self.set_y_label( |
| 236 |
self.get_label_scale(0, self.graph_max, float(y_label_size_max)) |
| 237 |
) |
| 238 |
|
| 239 |
|
| 240 |
need_rebuild = update_max |
| 241 |
source_available = self.source.sensor_available |
| 242 |
if source_available: |
| 243 |
new_available = source_available[: len(self.sensor_available)] |
| 244 |
if new_available != self.sensor_available[: len(new_available)]: |
| 245 |
self.sensor_available[: len(new_available)] = new_available |
| 246 |
need_rebuild = True |
| 247 |
|
| 248 |
if need_rebuild: |
| 249 |
self.set_visible_graphs() |
| 250 |
|
| 251 |
def reset(self): |
| 252 |
self.graph_data = [] |
| 253 |
|
| 254 |
for _ in range(self.graph_count): |
| 255 |
self.graph_data.append([0] * self.num_samples) |
| 256 |
self.graph_max = 1 |