v1.4.2 #3
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Series 3 Watcher — System Tray Launcher v1.4.1
|
Series 3 Watcher — System Tray Launcher v1.4.2
|
||||||
Requires: pystray, Pillow, tkinter (stdlib)
|
Requires: pystray, Pillow, tkinter (stdlib)
|
||||||
|
|
||||||
Run with: pythonw series3_tray.py (no console window)
|
Run with: pythonw series3_tray.py (no console window)
|
||||||
@@ -277,46 +277,49 @@ class WatcherTray:
|
|||||||
status = self.state.get("status", "starting")
|
status = self.state.get("status", "starting")
|
||||||
last_err = self.state.get("last_error")
|
last_err = self.state.get("last_error")
|
||||||
last_scan = self.state.get("last_scan")
|
last_scan = self.state.get("last_scan")
|
||||||
|
api_status = self.state.get("api_status", "disabled")
|
||||||
|
unit_count = len(self.state.get("units", []))
|
||||||
|
|
||||||
if status == "error":
|
if status == "error":
|
||||||
return "Status: Error — {}".format(last_err or "unknown")
|
return "Error — {}".format(last_err or "unknown")
|
||||||
if status == "starting":
|
if status == "starting":
|
||||||
return "Status: Starting..."
|
return "Starting..."
|
||||||
|
|
||||||
|
# Scan age
|
||||||
if last_scan is not None:
|
if last_scan is not None:
|
||||||
age_secs = int((datetime.now() - last_scan).total_seconds())
|
age_secs = int((datetime.now() - last_scan).total_seconds())
|
||||||
if age_secs < 60:
|
age_str = "{}s ago".format(age_secs) if age_secs < 60 else "{}m ago".format(age_secs // 60)
|
||||||
age_str = "{}s ago".format(age_secs)
|
|
||||||
else:
|
else:
|
||||||
age_str = "{}m ago".format(age_secs // 60)
|
age_str = "never"
|
||||||
unit_count = len(self.state.get("units", []))
|
|
||||||
return "Status: {} | {} unit(s) | scan {}".format(
|
|
||||||
status.upper(), unit_count, age_str
|
|
||||||
)
|
|
||||||
return "Status: {}".format(status.upper())
|
|
||||||
|
|
||||||
def _build_units_submenu(self):
|
# API status label
|
||||||
units = self.state.get("units", [])
|
if api_status == "ok":
|
||||||
if not units:
|
api_str = "API OK"
|
||||||
items = [pystray.MenuItem("No units detected", None, enabled=False)]
|
elif api_status == "fail":
|
||||||
|
api_str = "API FAIL"
|
||||||
else:
|
else:
|
||||||
items = []
|
api_str = "API off"
|
||||||
for u in units:
|
|
||||||
label = "{uid} — {status} ({age:.1f}h ago)".format(
|
return "Running — {} | {} unit(s) | scan {}".format(api_str, unit_count, age_str)
|
||||||
uid=u["uid"],
|
|
||||||
status=u["status"],
|
def _tray_status(self):
|
||||||
age=u["age_hours"],
|
"""Return the icon status key based on watcher + API health."""
|
||||||
)
|
status = self.state.get("status", "starting")
|
||||||
items.append(pystray.MenuItem(label, None, enabled=False))
|
if status == "error":
|
||||||
return pystray.Menu(*items)
|
return "error"
|
||||||
|
if status == "starting":
|
||||||
|
return "starting"
|
||||||
|
api_status = self.state.get("api_status", "disabled")
|
||||||
|
if api_status == "fail":
|
||||||
|
return "missing" # red — API failing
|
||||||
|
if api_status == "disabled":
|
||||||
|
return "pending" # amber — running but not reporting
|
||||||
|
return "ok" # green — running and API good
|
||||||
|
|
||||||
def _build_menu(self):
|
def _build_menu(self):
|
||||||
# Use a callable for the status item so pystray re-evaluates it
|
|
||||||
# every time the menu is opened — keeps it in sync with the tooltip.
|
|
||||||
return pystray.Menu(
|
return pystray.Menu(
|
||||||
pystray.MenuItem(lambda item: self._status_text(), None, enabled=False),
|
pystray.MenuItem(lambda item: self._status_text(), None, enabled=False),
|
||||||
pystray.Menu.SEPARATOR,
|
pystray.Menu.SEPARATOR,
|
||||||
pystray.MenuItem("Units", lambda item: self._build_units_submenu()),
|
|
||||||
pystray.Menu.SEPARATOR,
|
|
||||||
pystray.MenuItem("Settings...", self._open_settings),
|
pystray.MenuItem("Settings...", self._open_settings),
|
||||||
pystray.MenuItem("Open Log Folder", self._open_logs),
|
pystray.MenuItem("Open Log Folder", self._open_logs),
|
||||||
pystray.Menu.SEPARATOR,
|
pystray.Menu.SEPARATOR,
|
||||||
@@ -331,17 +334,17 @@ class WatcherTray:
|
|||||||
update_check_counter = 0 # check for updates every ~5 min (30 * 10s ticks)
|
update_check_counter = 0 # check for updates every ~5 min (30 * 10s ticks)
|
||||||
|
|
||||||
while not self.stop_event.is_set():
|
while not self.stop_event.is_set():
|
||||||
status = self.state.get("status", "starting")
|
icon_status = self._tray_status()
|
||||||
|
|
||||||
if self._icon is not None:
|
if self._icon is not None:
|
||||||
# Always rebuild menu every cycle so unit list and scan age stay fresh
|
# Always rebuild menu every cycle so status text stays fresh
|
||||||
with self._menu_lock:
|
with self._menu_lock:
|
||||||
self._icon.menu = self._build_menu()
|
self._icon.menu = self._build_menu()
|
||||||
|
|
||||||
if status != last_status:
|
if icon_status != last_status:
|
||||||
self._icon.icon = make_icon(status)
|
self._icon.icon = make_icon(icon_status)
|
||||||
self._icon.title = "Series 3 Watcher — {}".format(status.upper())
|
self._icon.title = "Series 3 Watcher — {}".format(self._status_text())
|
||||||
last_status = status
|
last_status = icon_status
|
||||||
|
|
||||||
# Check if terra-view signalled an update via heartbeat response
|
# Check if terra-view signalled an update via heartbeat response
|
||||||
if self.state.get("update_available"):
|
if self.state.get("update_available"):
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Series 3 Watcher — v1.4.1
|
Series 3 Watcher — v1.4.2
|
||||||
|
|
||||||
Environment:
|
Environment:
|
||||||
- Python 3.8 (Windows 7 compatible)
|
- Python 3.8 (Windows 7 compatible)
|
||||||
@@ -220,7 +220,7 @@ def scan_latest(
|
|||||||
|
|
||||||
|
|
||||||
# --- API heartbeat / SFM telemetry helpers ---
|
# --- API heartbeat / SFM telemetry helpers ---
|
||||||
VERSION = "1.4.1"
|
VERSION = "1.4.2"
|
||||||
|
|
||||||
|
|
||||||
def _read_log_tail(log_file: str, n: int = 25) -> Optional[list]:
|
def _read_log_tail(log_file: str, n: int = 25) -> Optional[list]:
|
||||||
@@ -398,45 +398,25 @@ def run_watcher(state: Dict[str, Any], stop_event: threading.Event) -> None:
|
|||||||
)
|
)
|
||||||
now_epoch = time.time()
|
now_epoch = time.time()
|
||||||
|
|
||||||
# Build per-unit status list for the tray
|
# Log detected units to console and log file (info only, no status judgement)
|
||||||
unit_list = []
|
unit_list = []
|
||||||
worst = "ok" # tracks worst status across all units
|
|
||||||
|
|
||||||
if latest:
|
if latest:
|
||||||
print("\nDetected Units (within last {} days):".format(MAX_EVENT_AGE_DAYS))
|
print("\nDetected Units (within last {} days):".format(MAX_EVENT_AGE_DAYS))
|
||||||
for uid in sorted(latest.keys()):
|
for uid in sorted(latest.keys()):
|
||||||
info = latest[uid]
|
info = latest[uid]
|
||||||
age_hours = (now_epoch - info["mtime"]) / 3600.0
|
age_hours = (now_epoch - info["mtime"]) / 3600.0
|
||||||
if age_hours > MISSING_HOURS:
|
|
||||||
status, col = "missing", C_MIS
|
|
||||||
elif age_hours > OK_HOURS:
|
|
||||||
status, col = "pending", C_PEN
|
|
||||||
else:
|
|
||||||
status, col = "ok", C_OK
|
|
||||||
|
|
||||||
# escalate worst status
|
|
||||||
if status == "missing":
|
|
||||||
worst = "missing"
|
|
||||||
elif status == "pending" and worst != "missing":
|
|
||||||
worst = "pending"
|
|
||||||
|
|
||||||
unit_list.append({
|
unit_list.append({
|
||||||
"uid": uid,
|
"uid": uid,
|
||||||
"status": status,
|
|
||||||
"age_hours": age_hours,
|
"age_hours": age_hours,
|
||||||
"last": fmt_last(info["mtime"]),
|
"last": fmt_last(info["mtime"]),
|
||||||
"fname": info["fname"],
|
"fname": info["fname"],
|
||||||
})
|
})
|
||||||
|
|
||||||
line = (
|
line = (
|
||||||
"{col}{uid:<8} {status:<8} Age: {age:<7} Last: {last} (File: {fname}){rst}".format(
|
"{uid:<8} Age: {age:<7} Last: {last} (File: {fname})".format(
|
||||||
col=col,
|
|
||||||
uid=uid,
|
uid=uid,
|
||||||
status=status.capitalize(),
|
|
||||||
age=fmt_age(now_epoch, info["mtime"]),
|
age=fmt_age(now_epoch, info["mtime"]),
|
||||||
last=fmt_last(info["mtime"]),
|
last=fmt_last(info["mtime"]),
|
||||||
fname=info["fname"],
|
fname=info["fname"],
|
||||||
rst=C_RST,
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
print(line)
|
print(line)
|
||||||
@@ -448,10 +428,9 @@ def run_watcher(state: Dict[str, Any], stop_event: threading.Event) -> None:
|
|||||||
ENABLE_LOGGING,
|
ENABLE_LOGGING,
|
||||||
"[info] no recent MLG activity within {} days".format(MAX_EVENT_AGE_DAYS),
|
"[info] no recent MLG activity within {} days".format(MAX_EVENT_AGE_DAYS),
|
||||||
)
|
)
|
||||||
worst = "missing"
|
|
||||||
|
|
||||||
# Update shared state for tray
|
# Update shared state for tray — status reflects watcher health, not unit ages
|
||||||
state["status"] = worst
|
state["status"] = "running"
|
||||||
state["units"] = unit_list
|
state["units"] = unit_list
|
||||||
state["last_scan"] = datetime.now()
|
state["last_scan"] = datetime.now()
|
||||||
state["last_error"] = None
|
state["last_error"] = None
|
||||||
@@ -463,12 +442,19 @@ def run_watcher(state: Dict[str, Any], stop_event: threading.Event) -> None:
|
|||||||
if now_ts - last_api_ts >= interval:
|
if now_ts - last_api_ts >= interval:
|
||||||
hb_payload = build_sfm_payload(latest, cfg)
|
hb_payload = build_sfm_payload(latest, cfg)
|
||||||
hb_payload["version"] = VERSION
|
hb_payload["version"] = VERSION
|
||||||
|
hb_payload["watcher_status"] = state.get("status", "unknown")
|
||||||
hb_payload["log_tail"] = _read_log_tail(cfg.get("LOG_FILE", ""), 25)
|
hb_payload["log_tail"] = _read_log_tail(cfg.get("LOG_FILE", ""), 25)
|
||||||
response = send_api_payload(hb_payload, cfg.get("API_URL", ""))
|
response = send_api_payload(hb_payload, cfg.get("API_URL", ""))
|
||||||
last_api_ts = now_ts
|
last_api_ts = now_ts
|
||||||
# Surface update signal to tray
|
if response is not None:
|
||||||
if response and response.get("update_available"):
|
state["api_status"] = "ok"
|
||||||
|
state["last_api"] = datetime.now()
|
||||||
|
if response.get("update_available"):
|
||||||
state["update_available"] = True
|
state["update_available"] = True
|
||||||
|
else:
|
||||||
|
state["api_status"] = "fail"
|
||||||
|
else:
|
||||||
|
state["api_status"] = "disabled"
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
err = "[loop-error] {}".format(e)
|
err = "[loop-error] {}".format(e)
|
||||||
|
|||||||
Reference in New Issue
Block a user