-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathframes.py
More file actions
159 lines (142 loc) · 6.06 KB
/
frames.py
File metadata and controls
159 lines (142 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import re
import json
from PyQt5.QtGui import QTextCursor
# ── Colour helpers ────────────────────────────────────────────────────────────
serverFormat = '<span style="color:#29F500;">{}</span>'
allFormat = '<span style="color:gray;">{}</span>'
uiFormat = '<span style="color:yellow;">{}</span>'
discFormat = '<span style="color:red;">{}</span>'
warnFormat = '<span style="color:orange;">{}</span>'
messageFormat = '<span style="color:white;">{}</span>'
# Regex patterns for firmware text lines
_RE_CHAT = re.compile(r'^\[CHAT\]\[([0-9A-Fa-f]{8})\]:\s*(.*)')
_RE_TX_CHAT = re.compile(r'^TX CHAT:\s*(.*)')
_RE_DROP = re.compile(r'^@DROP\s+(.*)')
_RE_MET = re.compile(r'^@MET\s+(\{.*\})')
_RE_SUPPORT = re.compile(r'^@SUPPORTPACK\s+(\{.*\})')
_RE_POLICY = re.compile(r'^@POLICY\s+(\{.*\})')
_RE_RST = re.compile(r'^@RST\s+(\{.*\})')
_RE_CRASH = re.compile(r'^@CRASH\s+(\{.*\})')
_RE_OK = re.compile(r'^OK\s+(.*)')
_RE_ERR = re.compile(r'^ERR:\s*(.*)')
_RE_WARN = re.compile(r'^WARN:\s*(.*)')
# ESP-IDF log: I (12345) TAG: message or W/E/D/V
_RE_ESP_LOG = re.compile(r'^([IWEDV])\s+\((\d+)\)\s+(\w+):\s*(.*)')
class RivrLineHandler:
"""Parse and display a single text line from Rivr firmware serial output."""
def received_line(self, line: str, ui) -> None:
line = line.strip()
if not line or line == '>':
return
m = _RE_CHAT.match(line)
if m:
node_id, msg = m.group(1), m.group(2)
ui.tEMonitor.append(
allFormat.format(f'[CHAT from 0x{node_id}]')
)
ui.tEMonitor.append(messageFormat.format(f' {msg}'))
ui.tEMonitor.moveCursor(QTextCursor.End)
# also push to chat-only view if available
if hasattr(ui, 'tEChat'):
ui.tEChat.append(
allFormat.format(f'[0x{node_id}]')
)
ui.tEChat.append(messageFormat.format(f' {msg}'))
ui.tEChat.moveCursor(QTextCursor.End)
return
m = _RE_TX_CHAT.match(line)
if m:
ui.tEMonitor.append(
uiFormat.format(f'[TX] {m.group(1)}')
)
ui.tEMonitor.moveCursor(QTextCursor.End)
if hasattr(ui, 'tEChat'):
ui.tEChat.append(uiFormat.format(f'[TX] {m.group(1)}'))
ui.tEChat.moveCursor(QTextCursor.End)
return
m = _RE_DROP.match(line)
if m:
ui.tEMonitor.append(discFormat.format(f'[@DROP] {m.group(1)}'))
ui.tEMonitor.moveCursor(QTextCursor.End)
return
m = _RE_MET.match(line)
if m:
try:
d = json.loads(m.group(1))
# Header line
ui.tEMonitor.append(allFormat.format(
f'── @MET node=0x{d.get("node_id", 0):08X} ──'
))
# Print every key=value pair, 4 per line
items = [f'{k}={v}' for k, v in d.items() if k != 'node_id']
row = []
for item in items:
row.append(item)
if len(row) == 4:
ui.tEMonitor.append(allFormat.format(' ' + ' '.join(row)))
row = []
if row:
ui.tEMonitor.append(allFormat.format(' ' + ' '.join(row)))
except (ValueError, KeyError):
ui.tEMonitor.append(allFormat.format(f'[@MET] {m.group(1)[:80]}'))
ui.tEMonitor.moveCursor(QTextCursor.End)
return
m = _RE_SUPPORT.match(line)
if m:
try:
d = json.loads(m.group(1))
summary = (
f'[@BOOT] env={d.get("env", "?")} '
f'sha={d.get("sha", "?")} '
f'role={d.get("role", "?")} '
f'radio={d.get("radio", "?")} '
f'freq={d.get("freq", "?")}'
)
ui.tEMonitor.append(serverFormat.format(summary))
except (ValueError, KeyError):
ui.tEMonitor.append(
serverFormat.format(f'[@SUPPORTPACK] {m.group(1)[:80]}')
)
ui.tEMonitor.moveCursor(QTextCursor.End)
return
m = _RE_CRASH.match(line)
if m:
ui.tEMonitor.append(discFormat.format(f'[@CRASH] {m.group(1)}'))
ui.tEMonitor.moveCursor(QTextCursor.End)
return
for pat in (_RE_POLICY, _RE_RST):
m = pat.match(line)
if m:
ui.tEMonitor.append(allFormat.format(f'{line[:100]}'))
ui.tEMonitor.moveCursor(QTextCursor.End)
return
m = _RE_ESP_LOG.match(line)
if m:
level, ts, tag, msg = m.group(1), m.group(2), m.group(3), m.group(4)
if level == 'E':
fmt = discFormat
elif level == 'W':
fmt = warnFormat
else:
fmt = allFormat
ui.tEMonitor.append(fmt.format(f'[{level}][{tag}] {msg}'))
ui.tEMonitor.moveCursor(QTextCursor.End)
return
m = _RE_ERR.match(line)
if m:
ui.tEMonitor.append(discFormat.format(f'[ERR] {m.group(1)}'))
ui.tEMonitor.moveCursor(QTextCursor.End)
return
m = _RE_WARN.match(line)
if m:
ui.tEMonitor.append(warnFormat.format(f'[WARN] {m.group(1)}'))
ui.tEMonitor.moveCursor(QTextCursor.End)
return
m = _RE_OK.match(line)
if m:
ui.tEMonitor.append(serverFormat.format(f'[OK] {m.group(1)}'))
ui.tEMonitor.moveCursor(QTextCursor.End)
return
# ── Fallback: show line as-is ─────────────────────────────────────
ui.tEMonitor.append(allFormat.format(line[:200]))
ui.tEMonitor.moveCursor(QTextCursor.End)