Files
control/touch_panel/panel.py
T

361 lines
13 KiB
Python
Executable File

#!/usr/bin/env python3
import json
import subprocess
import sys
from pathlib import Path
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QColor, QFont, QPainter
from PyQt5.QtWidgets import (
QApplication,
QFrame,
QGridLayout,
QHBoxLayout,
QLabel,
QMainWindow,
QPushButton,
QSlider,
QVBoxLayout,
QWidget,
)
ROOT = Path(__file__).resolve().parents[1]
BRIDGE = ROOT / "touch_panel" / "control_touch_bridge.php"
PHP = "/usr/bin/php"
def run_bridge(*args):
proc = subprocess.run(
[PHP, str(BRIDGE), *map(str, args)],
cwd=str(ROOT),
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
timeout=8,
check=False,
)
if proc.returncode != 0:
raise RuntimeError((proc.stderr or proc.stdout or "bridge failed").strip())
payload = json.loads(proc.stdout)
if not payload.get("ok"):
raise RuntimeError(payload.get("message") or payload.get("error") or "bridge error")
return payload["data"]
class StatusDot(QWidget):
def __init__(self):
super().__init__()
self.active = False
self.warning = False
self.pulse = 0
self.setFixedSize(34, 34)
def set_state(self, active, warning=False):
changed = self.active != active or self.warning != warning
self.active = active
self.warning = warning
if active and changed:
self.pulse = 6
self.update()
def tick(self):
if self.active:
self.pulse = 6 if self.pulse <= 0 else self.pulse - 1
self.update()
def paintEvent(self, event):
del event
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
base = QColor("#22c55e") if self.active else QColor("#64748b")
if self.warning:
base = QColor("#f59e0b")
if self.pulse > 0 and self.active and not self.warning:
base = QColor("#86efac") if self.pulse % 2 == 0 else QColor("#16a34a")
painter.setBrush(base)
painter.setPen(Qt.NoPen)
painter.drawEllipse(4, 4, 26, 26)
painter.setBrush(QColor(255, 255, 255, 55))
painter.drawEllipse(10, 8, 8, 8)
class StatusRow(QFrame):
def __init__(self, title):
super().__init__()
self.dot = StatusDot()
self.title = QLabel(title)
self.title.setObjectName("statusTitle")
self.detail = QLabel("-")
self.detail.setObjectName("statusDetail")
self.detail.setWordWrap(True)
texts = QVBoxLayout()
texts.setContentsMargins(0, 0, 0, 0)
texts.setSpacing(2)
texts.addWidget(self.title)
texts.addWidget(self.detail)
layout = QHBoxLayout(self)
layout.setContentsMargins(12, 9, 12, 9)
layout.setSpacing(10)
layout.addWidget(self.dot)
layout.addLayout(texts, 1)
self.setObjectName("statusRow")
def update_state(self, active, detail, warning=False):
self.dot.set_state(active, warning)
self.detail.setText(detail)
def tick(self):
self.dot.tick()
class FanButton(QPushButton):
def __init__(self, text):
super().__init__(text)
self.setCheckable(True)
self.setMinimumHeight(44)
class TouchPanel(QMainWindow):
def __init__(self):
super().__init__()
self.snapshot = {}
self.rows = {}
self.mode_buttons = {}
self.current_mode = "auto"
self.current_pwm = 120
self.in_flight = False
self.setWindowTitle("Control Touch Panel")
self.setMinimumSize(800, 480)
self._build_ui()
self.refresh_timer = QTimer(self)
self.refresh_timer.timeout.connect(self.refresh)
self.refresh_timer.start(1200)
self.pulse_timer = QTimer(self)
self.pulse_timer.timeout.connect(self.tick_pulses)
self.pulse_timer.start(180)
self.refresh()
def _build_ui(self):
root = QWidget()
grid = QGridLayout(root)
grid.setContentsMargins(16, 14, 16, 14)
grid.setHorizontalSpacing(14)
grid.setVerticalSpacing(10)
left = QFrame()
left.setObjectName("panel")
left_layout = QVBoxLayout(left)
left_layout.setContentsMargins(14, 14, 14, 14)
left_layout.setSpacing(10)
title = QLabel("상태")
title.setObjectName("sectionTitle")
left_layout.addWidget(title)
for key, label in [
("io", "IO"),
("wan", "WAN"),
("lan", "LAN"),
("wifi24", "2.4G"),
("wifi5", "5G"),
]:
row = StatusRow(label)
self.rows[key] = row
left_layout.addWidget(row)
left_layout.addStretch(1)
right = QFrame()
right.setObjectName("panel")
right_layout = QVBoxLayout(right)
right_layout.setContentsMargins(16, 14, 16, 14)
right_layout.setSpacing(12)
header = QHBoxLayout()
fan_title = QLabel("팬")
fan_title.setObjectName("sectionTitle")
self.clock = QLabel("--:--:--")
self.clock.setObjectName("clock")
header.addWidget(fan_title)
header.addStretch(1)
header.addWidget(self.clock)
right_layout.addLayout(header)
self.fan_rpm = QLabel("- RPM")
self.fan_rpm.setObjectName("bigValue")
self.fan_temp = QLabel("- C")
self.fan_temp.setObjectName("subValue")
self.fan_pwm = QLabel("- PWM")
self.fan_pwm.setObjectName("subValue")
right_layout.addWidget(self.fan_rpm)
right_layout.addWidget(self.fan_temp)
right_layout.addWidget(self.fan_pwm)
mode_layout = QHBoxLayout()
for mode in ["auto", "manual", "off"]:
btn = FanButton(mode)
btn.clicked.connect(lambda checked=False, m=mode: self.set_fan(m, self.current_pwm))
self.mode_buttons[mode] = btn
mode_layout.addWidget(btn)
right_layout.addLayout(mode_layout)
slider_wrap = QHBoxLayout()
minus = QPushButton("-")
minus.setObjectName("stepButton")
minus.clicked.connect(lambda: self.step_pwm(-10))
plus = QPushButton("+")
plus.setObjectName("stepButton")
plus.clicked.connect(lambda: self.step_pwm(10))
self.slider = QSlider(Qt.Horizontal)
self.slider.setRange(0, 255)
self.slider.setValue(120)
self.slider.sliderReleased.connect(lambda: self.set_fan("manual", self.slider.value()))
self.slider.valueChanged.connect(self.slider_changed)
slider_wrap.addWidget(minus)
slider_wrap.addWidget(self.slider, 1)
slider_wrap.addWidget(plus)
right_layout.addLayout(slider_wrap)
self.notice = QLabel("프로토타입")
self.notice.setObjectName("notice")
self.notice.setWordWrap(True)
right_layout.addWidget(self.notice)
right_layout.addStretch(1)
grid.addWidget(left, 0, 0)
grid.addWidget(right, 0, 1)
grid.setColumnStretch(0, 3)
grid.setColumnStretch(1, 4)
self.setCentralWidget(root)
def tick_pulses(self):
for row in self.rows.values():
row.tick()
def slider_changed(self, value):
self.current_pwm = value
self.fan_pwm.setText(f"{value} PWM / {value / 255 * 100:.1f}%")
def set_fan(self, mode, pwm):
if self.in_flight:
return
self.in_flight = True
self.notice.setText("팬 변경 중")
try:
self.snapshot = run_bridge("fan", mode, pwm)
self.notice.setText("팬 상태를 바꿨어")
self.render()
except Exception as exc:
self.notice.setText(str(exc))
finally:
self.in_flight = False
def step_pwm(self, delta):
value = max(0, min(255, self.slider.value() + delta))
self.slider.setValue(value)
self.set_fan("manual", value)
def refresh(self):
if self.in_flight:
return
try:
self.snapshot = run_bridge("status")
self.render()
except Exception as exc:
self.notice.setText(str(exc))
self.rows["io"].update_state(False, "control snapshot 실패", True)
def render(self):
data = self.snapshot
fan = data.get("fan", {})
system = data.get("system", {})
network = data.get("network", {})
wifi = data.get("wifi", {})
touch = data.get("touch_display", {})
interfaces = {row.get("name"): row for row in network.get("interfaces", [])}
clients = wifi.get("clients", [])
wifi24 = [c for c in clients if c.get("band") == "2.4G"]
wifi5 = [c for c in clients if c.get("band") == "5G"]
lan_clients = [c for c in clients if c.get("band") == "LAN"]
temp = float(system.get("temp_c") or 0)
rpm = int(fan.get("rpm") or 0)
pwm = int(fan.get("pwm") or 0)
mode = str(fan.get("mode") or "auto")
wan = interfaces.get(network.get("default_interface") or "eth0", {})
lan = interfaces.get("br0") or interfaces.get("eth1") or {}
w24 = interfaces.get("wlan0") or {}
w5 = interfaces.get("wlan1") or {}
io_active = bool(touch.get("connected")) and bool(fan.get("policy_ok", True))
io_detail = f"{system.get('hostname', '-')} / {temp:.1f} C"
self.rows["io"].update_state(io_active, io_detail, temp >= 75)
self.rows["wan"].update_state(self.iface_up(wan), self.iface_detail(wan), self.iface_warn(wan))
self.rows["lan"].update_state(self.iface_up(lan), f"{len(lan_clients)}대 / {self.iface_detail(lan)}", self.iface_warn(lan))
self.rows["wifi24"].update_state(self.iface_up(w24), f"{len(wifi24)}대 / {self.iface_detail(w24)}", self.iface_warn(w24))
self.rows["wifi5"].update_state(self.iface_up(w5), f"{len(wifi5)}대 / {self.iface_detail(w5)}", self.iface_warn(w5))
self.current_mode = mode
self.current_pwm = pwm
self.fan_rpm.setText(f"{rpm:,} RPM")
self.fan_temp.setText(f"판단 온도 {float(fan.get('control_temp_c') or temp):.1f} C")
self.fan_pwm.setText(f"{pwm} PWM / {float(fan.get('percent') or 0):.1f}%")
self.slider.blockSignals(True)
self.slider.setValue(pwm)
self.slider.blockSignals(False)
for key, btn in self.mode_buttons.items():
btn.setChecked(key == mode)
self.clock.setText(str(data.get("generated_at", "--:--:--")).split(" ")[-1])
@staticmethod
def iface_up(row):
return row.get("state") == "up" and row.get("carrier") == "up"
@staticmethod
def iface_warn(row):
return int(row.get("rx_errors") or 0) > 0 or int(row.get("tx_errors") or 0) > 0
@staticmethod
def iface_detail(row):
if not row:
return "없음"
rx_drop = int(row.get("rx_dropped") or 0)
tx_drop = int(row.get("tx_dropped") or 0)
return f"{row.get('name', '-')} {row.get('state', '-')} / drop {rx_drop + tx_drop}"
def main():
app = QApplication(sys.argv)
app.setStyleSheet(
"""
QWidget { background: #0b1220; color: #e5edf7; font-family: "Noto Sans CJK KR", "Noto Sans KR", sans-serif; }
#panel { background: #111a2e; border: 1px solid #27364f; border-radius: 18px; }
#sectionTitle { font-size: 24px; font-weight: 700; }
#statusRow { background: #18243a; border: 1px solid #263650; border-radius: 16px; }
#statusTitle { font-size: 18px; font-weight: 700; color: #f8fafc; }
#statusDetail { font-size: 13px; color: #9fb0c8; }
#bigValue { font-size: 56px; font-weight: 800; color: #f8fafc; }
#subValue { font-size: 22px; color: #b8c5d8; }
#clock { font-size: 18px; color: #8fb3ff; font-weight: 700; }
#notice { padding: 10px; background: #17233a; border: 1px solid #2b3c59; border-radius: 12px; color: #a8b8d0; }
QPushButton { min-height: 42px; border: 1px solid #33445f; border-radius: 14px; background: #1a2942; color: #dce8f8; font-size: 18px; font-weight: 700; }
QPushButton:checked { background: #16a34a; color: white; border-color: #22c55e; }
QPushButton#stepButton { min-width: 56px; font-size: 28px; }
QSlider::groove:horizontal { height: 10px; background: #263650; border-radius: 5px; }
QSlider::sub-page:horizontal { background: #22c55e; border-radius: 5px; }
QSlider::handle:horizontal { width: 28px; margin: -10px 0; border-radius: 14px; background: #f8fafc; }
"""
)
win = TouchPanel()
win.showFullScreen()
sys.exit(app.exec_())
if __name__ == "__main__":
main()