Repository navigation
Expand file tree
/
Copy pathstatus.py
More file actions
334 lines (281 loc) · 11.4 KB
/
Copy pathstatus.py
File metadata and controls
334 lines (281 loc) · 11.4 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
#!/usr/bin/env python3
# =============================================================
# DeepStream 即時管理面板(systemd timer 版本 / thiedge01)
# 一執行就同時顯示:服務狀態 + 辨識 log + 雲端上傳 log
# 按鍵即時生效(免按 Enter):
# 1 開啟 = 啟動 deepstream.service + 啟動雲端 timer
# 2 關閉 = 停止 deepstream.service + 停止雲端 timer
# q 離開
#
# 與舊版差異:
# - 上傳排程由「crontab 單隻 PutAPI」改為「systemd timer」
# (send-api-cloud.timer)
# - 上傳 log 改讀各自的 .log 檔
# - 兩個日誌窗口改為等高顯示
# =============================================================
import subprocess
import sys
import threading
import time
import select
import termios
import tty
from collections import deque
from pathlib import Path
import os
# -------------------- 檢查 Rich 套件 --------------------
try:
from rich.live import Live
from rich.layout import Layout
from rich.panel import Panel
from rich.text import Text
from rich.console import Console
except ImportError:
raise SystemExit(
"缺少 rich 套件,請先在 tracking 環境安裝:\n"
" conda activate tracking && pip install rich"
)
# =============================================================
# 設定區(本機 thiedge01)
# =============================================================
# systemd unit 名稱
DEEPSTREAM_SERVICE = "deepstream.service"
CLOUD_TIMER = "send-api-cloud.timer"
# 專案目錄(自動偵測,找不到用預設)
def auto_detect_project_dir() -> Path:
env_dir = os.environ.get("DS_PROJECT_DIR")
if env_dir and Path(env_dir).exists():
return Path(env_dir)
script_dir = Path(__file__).resolve().parent
for parent in [script_dir] + list(script_dir.parents):
if (parent / "main.py").exists():
return parent
return Path("/home/nvidia/THI/DeepStream-Multi-Model")
PROJECT_DIR = auto_detect_project_dir()
# 雲端 API 的 log 檔(由 send_interval_api*.py 產生)
CLOUD_LOG = str(PROJECT_DIR / "send_interval_api.live.log")
# 固定設定
MAX_KEEP = 500 # 日誌保留行數
# =============================================================
# 顯示偵測結果
# =============================================================
console = Console()
console.print("\n[bold cyan]╔════════════════════════════════════════════════╗[/]")
console.print("[bold cyan]║ DeepStream 即時管理面板 - systemd timer 版 ║[/]")
console.print("[bold cyan]╚════════════════════════════════════════════════╝[/]\n")
console.print(f"[green]✓[/] 專案目錄 : [yellow]{PROJECT_DIR}[/]")
console.print(f"[green]✓[/] 主服務 : [yellow]{DEEPSTREAM_SERVICE}[/]")
console.print(f"[green]✓[/] 雲端 timer: [yellow]{CLOUD_TIMER}[/] log: {CLOUD_LOG}")
console.print("")
# =============================================================
# 全域變數與緩衝區
# =============================================================
recog_buffer = deque(maxlen=MAX_KEEP) # 辨識服務日誌
cloud_buffer = deque(maxlen=MAX_KEEP) # 雲端上傳日誌
stop_event = threading.Event()
# 狀態快取
state = {"cloud_timer": None}
# =============================================================
# 背景日誌讀取執行緒
# =============================================================
def recog_reader():
"""持續讀取 DeepStream 服務日誌(journalctl -f)。"""
try:
proc = subprocess.Popen(
["journalctl", "-u", DEEPSTREAM_SERVICE, "-o", "cat",
"-n", "80", "-f", "--no-pager"],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, bufsize=1,
)
except FileNotFoundError:
recog_buffer.append("[錯誤] 找不到 journalctl 指令")
return
for line in proc.stdout:
if stop_event.is_set():
break
recog_buffer.append(line.rstrip("\n"))
try:
proc.terminate()
except Exception:
pass
def make_tail_reader(logpath, buf):
"""回傳一個 tail -F 讀某個 log 檔的執行緒函數。"""
def _reader():
try:
proc = subprocess.Popen(
["tail", "-n", "40", "-F", logpath],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, bufsize=1,
)
except FileNotFoundError:
buf.append("[錯誤] 找不到 tail 指令")
return
except Exception as e:
buf.append(f"[錯誤] 無法讀取日誌: {e}")
return
for line in proc.stdout:
if stop_event.is_set():
break
buf.append(line.rstrip("\n"))
try:
proc.terminate()
except Exception:
pass
return _reader
# =============================================================
# 服務 / timer 狀態與操作
# =============================================================
def is_active(unit: str) -> str:
"""回傳 unit 的 is-active 狀態字串(active/inactive/failed...)。"""
result = subprocess.run(
["systemctl", "is-active", unit],
capture_output=True, text=True, check=False,
)
return result.stdout.strip()
def timer_is_enabled(unit: str) -> bool:
"""timer 是否正在運作(active = 有在計時)。"""
return is_active(unit) == "active"
def start_all():
"""開啟:啟動主服務 + 雲端 timer。"""
subprocess.run(["sudo", "systemctl", "start", DEEPSTREAM_SERVICE], check=False)
subprocess.run(["sudo", "systemctl", "start", CLOUD_TIMER], check=False)
state["cloud_timer"] = True
def stop_all():
"""關閉:停止主服務 + 雲端 timer。"""
subprocess.run(["sudo", "systemctl", "stop", DEEPSTREAM_SERVICE], check=False)
subprocess.run(["sudo", "systemctl", "stop", CLOUD_TIMER], check=False)
state["cloud_timer"] = False
# =============================================================
# 畫面渲染
# =============================================================
def colorize_log_line(line: str) -> str:
safe = line.replace("[", "\\[")
low = line.lower()
if "error" in low or "traceback" in low or "fail" in low or "失敗" in line:
return f"[red]{safe}[/]"
if "warn" in low or "逾時" in line:
return f"[yellow]{safe}[/]"
if "info" in low or "成功上傳" in line or "success" in low:
return f"[green]{safe}[/]"
return safe
def make_log_panel(buf, lines_n: int, title: str, color: str) -> Panel:
rows = list(buf)[-lines_n:]
if rows:
text = Text.from_markup("\n".join(colorize_log_line(x) for x in rows))
else:
text = Text("(等待 log 輸出中...)", style="dim")
return Panel(text, title=f"[{color}]{title}[/]", border_style=color)
def render_dashboard(term_height: int) -> Layout:
# ----- 主服務狀態 -----
status = is_active(DEEPSTREAM_SERVICE)
if status == "active":
service_text = "[bold green]● 運作中 (active)[/]"
elif status == "failed":
service_text = "[bold red]✕ 失敗 (failed)[/]"
else:
service_text = f"[bold red]○ 已停止 ({status or '未知'})[/]"
# ----- 雲端 timer 狀態 -----
def timer_label(on):
if on is True:
return "[bold green]● 啟用[/]"
if on is False:
return "[bold red]○ 暫停[/]"
return "[dim]未知[/]"
cloud_text = timer_label(state["cloud_timer"])
# ----- 標題面板 -----
header = Panel(
f"辨識服務:{service_text} 雲端上傳:{cloud_text}",
title="[cyan]DeepStream 即時管理面板[/]",
border_style="cyan",
)
# ----- 底部提示 -----
footer = Panel(
"[green]1[/] 開啟(辨識+雲端) "
"[red]2[/] 關閉(辨識+雲端) [dim]q[/] 離開",
border_style="dim",
)
# ----- 面板高度計算(兩個日誌面板等高)-----
# 可用高度 = 總高 - header(3) - footer(3),剩下的由兩個面板平分
avail = max(6, term_height - 6)
each_panel_height = avail // 2
log_lines = max(3, each_panel_height - 2) # 扣掉面板本身上下邊框各 1 行
recog_panel = make_log_panel(recog_buffer, log_lines,
"📡 辨識即時日誌", "bright_blue")
cloud_panel = make_log_panel(cloud_buffer, log_lines,
"☁️ 雲端上傳日誌 (send_interval_api)", "magenta")
layout = Layout()
layout.split_column(
Layout(header, size=3),
Layout(recog_panel), # 不給 size → 與下方平分
Layout(cloud_panel), # 不給 size → 與上方平分
Layout(footer, size=3),
)
return layout
# =============================================================
# 鍵盤輸入與控制
# =============================================================
def read_key(timeout: float = 0.3):
ready, _, _ = select.select([sys.stdin], [], [], timeout)
if ready:
return sys.stdin.read(1)
return None
def suspend_run_and_execute(live, old_termios, func):
"""暫停 Live 畫面、還原終端機(可輸入 sudo 密碼)、執行 func、再恢復。"""
live.stop()
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_termios)
try:
func()
finally:
time.sleep(1.0)
tty.setcbreak(sys.stdin.fileno())
live.start()
def action_open():
console.print("\n[green]🔓 開啟:啟動辨識服務 + 雲端 timer...[/]")
start_all()
console.print("[green]✅ 完成。[/]")
def action_close():
console.print("\n[yellow]🔒 關閉:停止辨識服務 + 雲端 timer...[/]")
stop_all()
console.print("[yellow]✅ 完成。[/]")
# =============================================================
# 主程式
# =============================================================
def main():
# 啟動兩個背景 log 讀取執行緒(辨識 + 雲端)
threading.Thread(target=recog_reader, daemon=True).start()
threading.Thread(target=make_tail_reader(CLOUD_LOG, cloud_buffer), daemon=True).start()
# 初始化讀取 timer 狀態
console.print("[dim]讀取目前狀態中...[/]")
try:
state["cloud_timer"] = timer_is_enabled(CLOUD_TIMER)
except Exception:
state["cloud_timer"] = None
fd = sys.stdin.fileno()
old_termios = termios.tcgetattr(fd)
try:
tty.setcbreak(fd)
with Live(console=console, screen=True,
auto_refresh=False, refresh_per_second=4) as live:
while True:
live.update(render_dashboard(console.size.height), refresh=True)
key = read_key(0.3)
if not key:
continue
key = key.lower()
if key == "1":
suspend_run_and_execute(live, old_termios, action_open)
elif key == "2":
suspend_run_and_execute(live, old_termios, action_close)
elif key == "q":
break
finally:
stop_event.set()
termios.tcsetattr(fd, termios.TCSADRAIN, old_termios)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n使用者中斷")
except Exception as e:
print(f"\n錯誤: {e}")
sys.exit(1)