터치 패널 WAN 속도 자동 단위 변환

This commit is contained in:
seo committed 2026-07-30 21:54:20 +09:00
1 parent a3e6ea1c89
commit a2a45a0118
2 files changed
+56 -16

No files matched your search

+55 -15
View File
@@ -31,6 +31,32 @@ PC_IP = "192.168.50.73"
PC_MAC = "74:56:3C:CD:CD:79"
def byte_rate_unit(values):
max_value = max([abs(float(value)) for value in values] + [0.0])
units = [
(1024 ** 3, "GB/s"),
(1024 ** 2, "MB/s"),
(1024, "KB/s"),
]
for factor, label in units:
if max_value >= factor:
return factor, label
return 1, "B/s"
def format_byte_rate(value, factor=None, unit=None):
if factor is None or unit is None:
factor, unit = byte_rate_unit([value])
scaled = float(value) / factor
if abs(scaled) >= 100:
text = f"{scaled:.0f}"
elif abs(scaled) >= 10:
text = f"{scaled:.1f}"
else:
text = f"{scaled:.2f}"
return f"{text} {unit}"
def read_text(path, default=""):
try:
return Path(path).read_text(encoding="utf-8").strip()
@@ -322,13 +348,14 @@ class MetricCard(QFrame):
class Sparkline(QFrame):
def __init__(self, title, color="#38bdf8", second_color=None, y_unit="", right_unit=""):
def __init__(self, title, color="#38bdf8", second_color=None, y_unit="", right_unit="", auto_byte_unit=False):
super().__init__()
self.setObjectName("chartCard")
self.color = QColor(color)
self.second_color = QColor(second_color) if second_color else None
self.y_unit = y_unit
self.right_unit = right_unit
self.auto_byte_unit = auto_byte_unit
self.values = []
self.second_values = []
self.title = QLabel(title)
@@ -366,17 +393,25 @@ class Sparkline(QFrame):
painter.drawLine(rect.left(), rect.bottom(), rect.right(), rect.bottom())
if len(self.values) < 2:
return
low = min(self.values)
high = max(self.values)
values = list(self.values)
second_values = list(self.second_values)
unit_factor = 1
unit_label = self.y_unit
if self.auto_byte_unit:
unit_factor, unit_label = byte_rate_unit(values + second_values)
values = [value / unit_factor for value in values]
second_values = [value / unit_factor for value in second_values]
low = min(values)
high = max(values)
if high <= low:
high = low + 1
painter.setPen(QColor("#93a4bd"))
painter.drawText(1, rect.top() + 10, self.axis_text(high))
painter.drawText(1, rect.bottom(), self.axis_text(low))
self.draw_series(painter, rect, self.values, low, high, self.color)
if self.second_color and self.second_values:
second_low = min(self.second_values)
second_high = max(self.second_values)
painter.drawText(1, rect.top() + 10, self.axis_text(high, unit_label))
painter.drawText(1, rect.bottom(), self.axis_text(low, unit_label))
self.draw_series(painter, rect, values, low, high, self.color)
if self.second_color and second_values:
second_low = min(second_values)
second_high = max(second_values)
if second_high <= second_low:
second_high = second_low + 1
if self.right_unit:
@@ -384,7 +419,9 @@ class Sparkline(QFrame):
right_x = self.canvas.rect().right() - 31
painter.drawText(right_x, rect.top() + 10, self.axis_text(second_high, self.right_unit))
painter.drawText(right_x, rect.bottom(), self.axis_text(second_low, self.right_unit))
self.draw_series(painter, rect, self.second_values, second_low, second_high, self.second_color)
self.draw_series(painter, rect, second_values, second_low, second_high, self.second_color)
else:
self.draw_series(painter, rect, second_values, low, high, self.second_color)
def axis_text(self, value, unit=None):
if abs(value) >= 100:
@@ -524,7 +561,7 @@ class TouchPanel(QMainWindow):
chart_grid.setVerticalSpacing(8)
for index, (key, chart) in enumerate([
("temp", Sparkline("온도 / RPM", "#f97316", "#38bdf8", "C", "rpm")),
("wan", Sparkline("WAN 트래픽", "#38bdf8", "#22c55e", "M")),
("wan", Sparkline("WAN 트래픽", "#38bdf8", "#22c55e", auto_byte_unit=True)),
]):
self.charts[key] = chart
chart_grid.addWidget(chart, index, 0)
@@ -572,8 +609,8 @@ class TouchPanel(QMainWindow):
}
if previous:
rates[key] = {
"rx": max(0.0, (current[0] - previous[0]) * 8 / dt / 1_000_000),
"tx": max(0.0, (current[1] - previous[1]) * 8 / dt / 1_000_000),
"rx": max(0.0, (current[0] - previous[0]) / dt),
"tx": max(0.0, (current[1] - previous[1]) / dt),
}
else:
rates[key] = {"rx": 0.0, "tx": 0.0}
@@ -593,7 +630,10 @@ class TouchPanel(QMainWindow):
self.metrics["rpm"].set_value(f"{rpm:,}")
self.metrics["temp"].set_value(f"{temp:.1f} C")
self.metrics["pwm"].set_value(f"{pwm} / {percent:.1f}%")
self.metrics["wan"].set_value(f"{rates['wan']['rx']:.2f}/{rates['wan']['tx']:.2f} Mbps")
wan_factor, wan_unit = byte_rate_unit([rates["wan"]["rx"], rates["wan"]["tx"]])
rx_text = format_byte_rate(rates["wan"]["rx"], wan_factor, wan_unit)
tx_text = format_byte_rate(rates["wan"]["tx"], wan_factor, wan_unit)
self.metrics["wan"].set_value(f"{rx_text}/{tx_text}")
self.slider.blockSignals(True)
self.slider.setValue(pwm)
@@ -641,7 +681,7 @@ class TouchPanel(QMainWindow):
self.net_history["wan"]["tx"].append(wan["tx"])
self.charts["wan"].set_values(
self.net_history["wan"]["rx"],
f"{wan['rx']:.2f}/{wan['tx']:.2f} Mbps",
f"{format_byte_rate(wan['rx'])}/{format_byte_rate(wan['tx'])}",
self.net_history["wan"]["tx"],
)