-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.py
More file actions
executable file
·497 lines (401 loc) · 17.7 KB
/
analyzer.py
File metadata and controls
executable file
·497 lines (401 loc) · 17.7 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#!/usr/bin/env python3
"""
Protocol Analyzer
Captures parsed frames with hex dumps, decoded field tables, inter-frame
timing, throughput stats, and a frame-rate graph. Features auto-scroll
toggle, copy-to-clipboard, packet limit control, and auto-reconnect.
Requirements: Serial Studio API server on port 7777, Python 3.6+, tkinter.
"""
import base64, json, signal, sys, threading, time
from collections import deque
from datetime import datetime
try:
import tkinter as tk
from tkinter import ttk
except ImportError:
sys.exit("[Analyzer] tkinter required")
# ── Theme ────────────────────────────────────────────────────────────────────
BG = "#0d1117"
SURFACE = "#161b22"
HEADER = "#1c2633"
BORDER = "#30363d"
TEXT = "#e6edf3"
DIM = "#8b949e"
ACCENT = "#58a6ff"
GREEN = "#3fb950"
ORANGE = "#d29922"
RED = "#f85149"
ROW_ALT = "#111820"
DET_BG = "#0a0e14"
HEX_CLR = "#7d8590"
FIELD_CLR= "#d2a8ff"
UNITS_CLR= "#7ee787"
TAG_CLR = "#79c0ff"
# Platform-aware font families
MONO_FONT = "Menlo" if sys.platform == "darwin" else "Consolas" if sys.platform == "win32" else "Monospace"
SANS_FONT = "Helvetica Neue" if sys.platform == "darwin" else "Segoe UI" if sys.platform == "win32" else "Sans"
MAX_FRAMES = 1000
RATE_W, RATE_H = 200, 40
from grpc_client import GRPCClient
# ── Data model ───────────────────────────────────────────────────────────────
class FrameRecord:
__slots__ = ("num", "timestamp", "raw", "fields", "size", "delta_ms")
def __init__(self, num, timestamp, raw, fields, size, delta_ms):
self.num = num
self.timestamp = timestamp
self.raw = raw
self.fields = fields
self.size = size
self.delta_ms = delta_ms
IDLE_SEC = 3
class DataStore:
def __init__(self):
self.lock = threading.Lock()
self.frames = deque(maxlen=MAX_FRAMES)
self.frame_count = 0
self.total_bytes = 0
self.start_time = None
self.last_time = None
self.deltas = deque(maxlen=200)
self.rate_history = deque(maxlen=RATE_W) # fps samples for graph
self._rate_frames = 0
self._rate_bytes = 0
self._rate_t = None
self.current_bps = 0.0
@property
def is_active(self):
return self.last_time is not None and (time.time() - self.last_time) < IDLE_SEC
@property
def duration(self):
if self.start_time is None or self.last_time is None:
return 0
return self.last_time - self.start_time
@property
def fps(self):
"""Current instantaneous frame rate."""
if self.rate_history:
return self.rate_history[-1]
return 0
def ingest(self, frame_data):
now = time.time()
with self.lock:
self.frame_count += 1
if self.start_time is None:
self.start_time = now
self._rate_t = now
delta = 0.0
if self.last_time is not None:
delta = (now - self.last_time) * 1000
self.deltas.append(delta)
self.last_time = now
# Rate sampling every 100ms, scaled to per-second
self._rate_frames += 1
elapsed = now - (self._rate_t or now)
if elapsed >= 0.1:
self.rate_history.append(self._rate_frames / elapsed)
self.current_bps = self._rate_bytes / elapsed
self._rate_frames = 0
self._rate_bytes = 0
self._rate_t = now
decoded = []
raw_vals = []
for g in frame_data.get("groups", []):
gt = g.get("title", "")
for ds in g.get("datasets", []):
title = ds.get("title", "?")
value = ds.get("value", "")
units = ds.get("units", "")
label = f"{gt}/{title}" if gt else title
decoded.append((label, value, units))
raw_vals.append(value)
raw_str = ",".join(raw_vals)
self.total_bytes += len(raw_str)
self._rate_bytes += len(raw_str)
ts = datetime.now().strftime("%H:%M:%S.%f")[:-3]
self.frames.append(FrameRecord(
self.frame_count, ts, raw_str, decoded, len(raw_str), delta))
def ingest_raw(self, b64_data):
"""Ingest raw device data (base64-encoded) when no dashboard is active."""
try:
raw_bytes = base64.b64decode(b64_data)
except Exception:
return
if not raw_bytes:
return
now = time.time()
with self.lock:
self.frame_count += 1
if self.start_time is None:
self.start_time = now
self._rate_t = now
delta = 0.0
if self.last_time is not None:
delta = (now - self.last_time) * 1000
self.deltas.append(delta)
self.last_time = now
self._rate_frames += 1
elapsed = now - (self._rate_t or now)
if elapsed >= 0.1:
self.rate_history.append(self._rate_frames / elapsed)
self.current_bps = self._rate_bytes / elapsed
self._rate_frames = 0
self._rate_bytes = 0
self._rate_t = now
# Build raw string and decoded fields from raw bytes
raw_str = raw_bytes.decode("utf-8", errors="replace").strip()
self.total_bytes += len(raw_bytes)
self._rate_bytes += len(raw_bytes)
# Try to split as CSV for field display
decoded = []
parts = raw_str.split(",")
for i, part in enumerate(parts):
part = part.strip()
decoded.append((f"[{i}]", part, ""))
ts = datetime.now().strftime("%H:%M:%S.%f")[:-3]
self.frames.append(FrameRecord(
self.frame_count, ts, raw_str, decoded, len(raw_bytes), delta))
# ── Helpers ──────────────────────────────────────────────────────────────────
def hex_dump(data, width=16):
raw = data.encode("utf-8", errors="replace")
lines = []
for off in range(0, len(raw), width):
c = raw[off:off + width]
hx = " ".join(f"{b:02X}" for b in c)
asc = "".join(chr(b) if 32 <= b < 127 else "." for b in c)
lines.append(f"{off:04X} {hx:<{width*3}} {asc}")
return "\n".join(lines)
# ── Rate graph ───────────────────────────────────────────────────────────────
class RateGraph(tk.Canvas):
def __init__(self, parent, **kw):
super().__init__(parent, width=RATE_W, height=RATE_H,
bg=DET_BG, highlightthickness=0, **kw)
def draw(self, values):
self.delete("all")
n = len(values)
if n < 2:
return
hi = max(values) or 1
w, h = RATE_W, RATE_H
pad = 2
# grid lines
for frac in (0.25, 0.5, 0.75):
y = h - pad - (h - 2*pad) * frac
self.create_line(pad, y, w - pad, y, fill=BORDER, dash=(2, 4))
# line
pts = []
for i, v in enumerate(values):
x = pad + (w - 2*pad) * i / (n - 1)
y = h - pad - (h - 2*pad) * min(v / hi, 1.0)
pts.extend((x, y))
self.create_line(pts, fill=GREEN, width=1.5, smooth=True)
# label
self.create_text(w - pad, pad, text=f"{values[-1]:.0f} fps",
fill=GREEN, anchor="ne", font=(MONO_FONT, 8))
# ── GUI ──────────────────────────────────────────────────────────────────────
class App:
def __init__(self, store, client):
self.store, self.client = store, client
self.auto_scroll = True
self.prev_count = 0
self.root = tk.Tk()
self.root.title("Protocol Analyzer")
self.root.geometry("960x680")
self.root.minsize(720, 450)
self.root.configure(bg=BG)
self.root.protocol("WM_DELETE_WINDOW", self._quit)
# header
hdr = tk.Frame(self.root, bg=HEADER, height=44)
hdr.pack(fill="x"); hdr.pack_propagate(False)
tk.Label(hdr, text="Protocol Analyzer", bg=HEADER, fg=TEXT,
font=(SANS_FONT, 13, "bold")).pack(side="left", padx=14)
self.stats_lbl = tk.Label(hdr, text="", bg=HEADER, fg=DIM,
font=(MONO_FONT, 10))
self.stats_lbl.pack(side="right", padx=14)
# auto-scroll toggle
self.scroll_btn = tk.Label(
hdr, text=" Auto-scroll ✓ ", bg=GREEN, fg=BG, cursor="hand2",
font=(SANS_FONT, 10), padx=6, pady=2)
self.scroll_btn.pack(side="right", padx=(0, 8))
self.scroll_btn.bind("<Button-1>", lambda _: self._toggle_scroll())
# rate graph
self.rate_graph = RateGraph(hdr)
self.rate_graph.pack(side="right", padx=(0, 8), pady=6)
tk.Frame(self.root, bg=BORDER, height=1).pack(fill="x")
# paned
pane = tk.PanedWindow(self.root, orient="vertical", bg=BG,
sashwidth=6, sashrelief="flat")
pane.pack(fill="both", expand=True, padx=12, pady=8)
# frame list
top = tk.Frame(pane, bg=BG)
pane.add(top, height=360)
style = ttk.Style()
style.theme_use("default")
style.configure("PA.Treeview",
background=SURFACE, foreground=TEXT,
fieldbackground=SURFACE, rowheight=24,
borderwidth=0, font=(MONO_FONT, 10))
style.configure("PA.Treeview.Heading",
background=HEADER, foreground=DIM,
borderwidth=0, relief="flat",
font=(SANS_FONT, 10, "bold"))
style.map("PA.Treeview",
background=[("selected", "#1f3a5f")],
foreground=[("selected", ACCENT)])
style.layout("PA.Treeview", [("Treeview.treearea", {"sticky": "nswe"})])
cols = ("#", "TIME", "SIZE", "DELTA", "FIELDS", "PREVIEW")
self.tree = ttk.Treeview(top, columns=cols, show="headings", style="PA.Treeview")
ws = (55, 95, 55, 75, 50, 540)
for col, w in zip(cols, ws):
self.tree.heading(col, text=col)
self.tree.column(col, width=w, anchor="w" if col == "PREVIEW" else "e", minwidth=30)
self.tree.pack(side="left", fill="both", expand=True)
self.tree.bind("<<TreeviewSelect>>", self._on_select)
self.tree.tag_configure("even", background=SURFACE)
self.tree.tag_configure("odd", background=ROW_ALT)
# detail view
bot_frame = tk.Frame(pane, bg=DET_BG)
pane.add(bot_frame)
# copy button
det_hdr = tk.Frame(bot_frame, bg=DET_BG, height=22)
det_hdr.pack(fill="x"); det_hdr.pack_propagate(False)
tk.Label(det_hdr, text="FRAME DETAIL", bg=DET_BG, fg=DIM,
font=(SANS_FONT, 9, "bold")).pack(side="left", padx=8)
copy_btn = tk.Label(det_hdr, text=" Copy ", bg=BORDER, fg=TEXT,
cursor="hand2", font=(SANS_FONT, 9), padx=4)
copy_btn.pack(side="right", padx=8)
copy_btn.bind("<Button-1>", lambda _: self._copy_detail())
self.detail = tk.Text(
bot_frame, bg=DET_BG, fg=DIM, font=(MONO_FONT, 10),
state="disabled", wrap="none", highlightthickness=0,
borderwidth=0, padx=8, pady=4)
self.detail.pack(fill="both", expand=True)
self.detail.tag_configure("hdr", foreground=ACCENT, font=(MONO_FONT, 10, "bold"))
self.detail.tag_configure("hex", foreground=HEX_CLR)
self.detail.tag_configure("fld", foreground=FIELD_CLR)
self.detail.tag_configure("val", foreground=TEXT)
self.detail.tag_configure("unt", foreground=UNITS_CLR)
self.detail.tag_configure("dim", foreground=DIM)
# footer
tk.Frame(self.root, bg=BORDER, height=1).pack(fill="x")
ftr = tk.Frame(self.root, bg=BG, height=28)
ftr.pack(fill="x"); ftr.pack_propagate(False)
self.lbl_total = tk.Label(ftr, text="", bg=BG, fg=DIM, font=(MONO_FONT, 10))
self.lbl_total.pack(side="left", padx=14)
self.lbl_avg = tk.Label(ftr, text="", bg=BG, fg=DIM, font=(MONO_FONT, 10))
self.lbl_avg.pack(side="right", padx=14)
self._tick()
def _toggle_scroll(self):
self.auto_scroll = not self.auto_scroll
if self.auto_scroll:
self.scroll_btn.config(text=" Auto-scroll ✓ ", bg=GREEN, fg=BG)
else:
self.scroll_btn.config(text=" Auto-scroll ✗ ", bg=BORDER, fg=TEXT)
def _copy_detail(self):
text = self.detail.get("1.0", "end").strip()
if text:
self.root.clipboard_clear()
self.root.clipboard_append(text)
def _on_select(self, _):
sel = self.tree.selection()
if not sel:
return
num = int(self.tree.item(sel[0])["values"][0])
with self.store.lock:
frames = list(self.store.frames)
fr = None
for f in frames:
if f.num == num:
fr = f
break
if not fr:
return
d = self.detail
d.config(state="normal")
d.delete("1.0", "end")
d.insert("end", f"Frame #{fr.num} ", "hdr")
d.insert("end", f"{fr.timestamp} {fr.size} bytes ", "dim")
d.insert("end", f"\u0394 {fr.delta_ms:.1f} ms\n\n", "dim")
d.insert("end", "HEX DUMP\n", "hdr")
d.insert("end", hex_dump(fr.raw) + "\n\n", "hex")
d.insert("end", "DECODED FIELDS\n", "hdr")
for i, (label, val, units) in enumerate(fr.fields):
d.insert("end", f" {i:>2d} ", "dim")
d.insert("end", f"{label} ", "fld")
d.insert("end", val, "val")
if units:
d.insert("end", f" {units}", "unt")
d.insert("end", "\n")
d.config(state="disabled")
def _tick(self):
if not self.client.running:
self.root.destroy(); return
with self.store.lock:
frames = list(self.store.frames)
count = self.store.frame_count
total = self.store.total_bytes
active = self.store.is_active
dur = self.store.duration
fps = self.store.fps
bps = self.store.current_bps
avg_d = (sum(self.store.deltas) / len(self.store.deltas)
if self.store.deltas else 0)
rates = list(self.store.rate_history)
mode = "Parsed" # gRPC StreamFrames only provides parsed frames
if active:
self.stats_lbl.config(
text=f"[{mode}] {count:,} frames • {fps:,.0f} fps • {bps/1024:.1f} KB/s")
elif count > 0:
self.stats_lbl.config(text=f"[{mode}] {count:,} frames • {dur:.0f}s • Idle")
else:
self.stats_lbl.config(text=f"[{mode}] Waiting for data…")
self.lbl_total.config(text=f"{total:,} bytes captured")
self.lbl_avg.config(text=f"avg \u0394 {avg_d:.1f} ms" if count > 0 else "")
# rate graph — use graph's latest value for stats label too
if rates:
fps = rates[-1]
self.rate_graph.draw(rates)
# new frames
if count > self.prev_count:
new = [f for f in frames if f.num > self.prev_count]
for f in new:
preview = f.raw[:55] + ("\u2026" if len(f.raw) > 55 else "")
tag = "even" if f.num % 2 == 0 else "odd"
self.tree.insert("", "end", values=(
f.num, f.timestamp, f.size,
f"{f.delta_ms:.1f}", len(f.fields), preview
), tags=(tag,))
if self.auto_scroll:
children = self.tree.get_children()
if children:
self.tree.see(children[-1])
# prune old rows to avoid memory bloat
children = self.tree.get_children()
if len(children) > MAX_FRAMES:
for iid in children[:len(children) - MAX_FRAMES]:
self.tree.delete(iid)
self.prev_count = count
self.root.after(100, self._tick)
def _quit(self):
self.client.running = False
self.root.destroy()
def run(self):
self.root.mainloop()
# ── Main ─────────────────────────────────────────────────────────────────────
def main():
store = DataStore()
client = GRPCClient()
client.on_frame = store.ingest
client.on_raw = lambda data, ts: store.ingest_raw(
base64.b64encode(data).decode("ascii"))
signal.signal(signal.SIGTERM, lambda *_: setattr(client, "running", False))
signal.signal(signal.SIGINT, lambda *_: setattr(client, "running", False))
if not client.connect():
print("[Analyzer] Waiting for API server…", file=sys.stderr)
threading.Thread(target=client.run_loop, daemon=True).start()
try:
App(store, client).run()
except Exception as e:
print(f"[Analyzer] {e}", file=sys.stderr)
client.stop()
if __name__ == "__main__":
main()