|
| 1 | +# |
| 2 | +# Copyright (C) 2026 DroneCAN Development Team <dronecan.org> |
| 3 | +# |
| 4 | +# This software is distributed under the terms of the MIT License. |
| 5 | +# |
| 6 | + |
| 7 | +import dronecan |
| 8 | +from functools import partial |
| 9 | +from PyQt6.QtWidgets import QVBoxLayout, QLabel, QDialog, QGroupBox, QTableWidget, QTableWidgetItem, QHeaderView |
| 10 | +from PyQt6.QtCore import Qt, QTimer |
| 11 | +from PyQt6.QtGui import QBrush, QColor |
| 12 | +from logging import getLogger |
| 13 | +from ..widgets import get_icon, get_monospace_font |
| 14 | + |
| 15 | +__all__ = 'PANEL_NAME', 'spawn', 'get_icon' |
| 16 | + |
| 17 | +PANEL_NAME = 'CircuitStatus' |
| 18 | + |
| 19 | +logger = getLogger(__name__) |
| 20 | + |
| 21 | +_singleton = None |
| 22 | + |
| 23 | + |
| 24 | +class CircuitStatusPanel(QDialog): |
| 25 | + def __init__(self, parent, node): |
| 26 | + super(CircuitStatusPanel, self).__init__(parent) |
| 27 | + self.setWindowTitle('CircuitStatus Monitor') |
| 28 | + self.setAttribute(Qt.WA_DeleteOnClose) |
| 29 | + |
| 30 | + self._node = node |
| 31 | + |
| 32 | + # Main Layout |
| 33 | + layout = QVBoxLayout(self) |
| 34 | + |
| 35 | + # --------------------------------------------------------- |
| 36 | + # Monitoring Group Box (CircuitStatus updates) |
| 37 | + # --------------------------------------------------------- |
| 38 | + monitor_group = QGroupBox('Circuit Status Monitor', self) |
| 39 | + monitor_layout = QVBoxLayout() |
| 40 | + |
| 41 | + self._table = QTableWidget(self) |
| 42 | + self._table.setColumnCount(5) |
| 43 | + self._table.setHorizontalHeaderLabels(['Circuit', 'Voltage', 'Current', 'Power', 'Status / Errors']) |
| 44 | + self._table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch) |
| 45 | + self._table.verticalHeader().setVisible(False) |
| 46 | + |
| 47 | + monitor_layout.addWidget(self._table) |
| 48 | + monitor_group.setLayout(monitor_layout) |
| 49 | + layout.addWidget(monitor_group) |
| 50 | + |
| 51 | + self._circuit_rows = {} |
| 52 | + |
| 53 | + self.setLayout(layout) |
| 54 | + self.resize(550, 300) |
| 55 | + |
| 56 | + # Register DroneCAN handler for CircuitStatus |
| 57 | + self._handlers = [ |
| 58 | + self._node.add_handler(dronecan.uavcan.equipment.power.CircuitStatus, self._on_circuit_status) |
| 59 | + ] |
| 60 | + |
| 61 | + # Timer to check for offline/stale status |
| 62 | + self._stale_timer = QTimer(self) |
| 63 | + self._stale_timer.timeout.connect(self._check_stale_circuits) |
| 64 | + self._stale_timer.start(1000) |
| 65 | + |
| 66 | + def _on_circuit_status(self, event): |
| 67 | + import time |
| 68 | + msg = event.message |
| 69 | + cid = msg.circuit_id |
| 70 | + |
| 71 | + if cid not in self._circuit_rows: |
| 72 | + # Insert row at sorted position |
| 73 | + row_idx = sum(1 for existing_cid in self._circuit_rows if existing_cid < cid) |
| 74 | + self._table.insertRow(row_idx) |
| 75 | + |
| 76 | + item_name = QTableWidgetItem(f'Circuit {cid}') |
| 77 | + item_volt = QTableWidgetItem('NC') |
| 78 | + item_curr = QTableWidgetItem('NC') |
| 79 | + item_pwr = QTableWidgetItem('NC') |
| 80 | + item_err = QTableWidgetItem('NC') |
| 81 | + |
| 82 | + font = get_monospace_font() |
| 83 | + for item in (item_name, item_volt, item_curr, item_pwr, item_err): |
| 84 | + item.setFont(font) |
| 85 | + item.setFlags(item.flags() & ~Qt.ItemFlag.ItemIsEditable) |
| 86 | + |
| 87 | + self._table.setItem(row_idx, 0, item_name) |
| 88 | + self._table.setItem(row_idx, 1, item_volt) |
| 89 | + self._table.setItem(row_idx, 2, item_curr) |
| 90 | + self._table.setItem(row_idx, 3, item_pwr) |
| 91 | + self._table.setItem(row_idx, 4, item_err) |
| 92 | + |
| 93 | + self._circuit_rows[cid] = { |
| 94 | + 'voltage': item_volt, |
| 95 | + 'current': item_curr, |
| 96 | + 'power': item_pwr, |
| 97 | + 'error': item_err, |
| 98 | + 'last_update': 0 |
| 99 | + } |
| 100 | + |
| 101 | + row = self._circuit_rows[cid] |
| 102 | + row['last_update'] = time.time() |
| 103 | + |
| 104 | + # Format voltage, current, power |
| 105 | + v = msg.voltage |
| 106 | + i = msg.current |
| 107 | + p = v * i |
| 108 | + |
| 109 | + row['voltage'].setText(f'{v:6.2f} V') |
| 110 | + row['current'].setText(f'{i:6.2f} A') |
| 111 | + row['power'].setText(f'{p:6.2f} W') |
| 112 | + |
| 113 | + # Parse error flags |
| 114 | + errs = [] |
| 115 | + flags = msg.error_flags |
| 116 | + if flags & msg.ERROR_FLAG_OVERVOLTAGE: |
| 117 | + errs.append('OVER_V') |
| 118 | + if flags & msg.ERROR_FLAG_UNDERVOLTAGE: |
| 119 | + errs.append('UNDER_V') |
| 120 | + if flags & msg.ERROR_FLAG_OVERCURRENT: |
| 121 | + errs.append('OVER_C') |
| 122 | + if flags & msg.ERROR_FLAG_UNDERCURRENT: |
| 123 | + errs.append('UNDER_C') |
| 124 | + |
| 125 | + if errs: |
| 126 | + row['error'].setText(', '.join(errs)) |
| 127 | + row['error'].setForeground(QBrush(QColor('red'))) |
| 128 | + font = row['error'].font() |
| 129 | + font.setBold(True) |
| 130 | + row['error'].setFont(font) |
| 131 | + else: |
| 132 | + row['error'].setText('OK') |
| 133 | + row['error'].setForeground(QBrush(QColor('green'))) |
| 134 | + font = row['error'].font() |
| 135 | + font.setBold(False) |
| 136 | + row['error'].setFont(font) |
| 137 | + |
| 138 | + def _check_stale_circuits(self): |
| 139 | + import time |
| 140 | + now = time.time() |
| 141 | + for cid, row in self._circuit_rows.items(): |
| 142 | + if row['last_update'] == 0: |
| 143 | + continue |
| 144 | + if now - row['last_update'] > 3.0: |
| 145 | + row['voltage'].setText('STALE') |
| 146 | + row['current'].setText('STALE') |
| 147 | + row['power'].setText('STALE') |
| 148 | + row['error'].setText('OFFLINE') |
| 149 | + row['error'].setForeground(QBrush(QColor('gray'))) |
| 150 | + font = row['error'].font() |
| 151 | + font.setBold(False) |
| 152 | + row['error'].setFont(font) |
| 153 | + |
| 154 | + def __del__(self): |
| 155 | + global _singleton |
| 156 | + _singleton = None |
| 157 | + for h in self._handlers: |
| 158 | + try: |
| 159 | + h.remove() |
| 160 | + except Exception: |
| 161 | + pass |
| 162 | + |
| 163 | + def closeEvent(self, event): |
| 164 | + global _singleton |
| 165 | + _singleton = None |
| 166 | + for h in self._handlers: |
| 167 | + try: |
| 168 | + h.remove() |
| 169 | + except Exception: |
| 170 | + pass |
| 171 | + super(CircuitStatusPanel, self).closeEvent(event) |
| 172 | + |
| 173 | + |
| 174 | +def spawn(parent, node): |
| 175 | + global _singleton |
| 176 | + if _singleton is None: |
| 177 | + _singleton = CircuitStatusPanel(parent, node) |
| 178 | + |
| 179 | + _singleton.show() |
| 180 | + _singleton.raise_() |
| 181 | + _singleton.activateWindow() |
| 182 | + |
| 183 | + return _singleton |
| 184 | + |
| 185 | + |
| 186 | +get_icon = partial(get_icon, 'fa6s.asterisk') |
0 commit comments