Files
octo-funnel/octoprint_tailscale_funnel/status_monitor.py

74 lines
2.6 KiB
Python

# coding=utf-8
from __future__ import absolute_import
import threading
import time
class StatusMonitor:
def __init__(self, plugin, interval=30):
self.plugin = plugin
self.interval = interval
self._logger = plugin._logger
self._stop_event = threading.Event()
self._thread = None
def start(self):
"""
Start the status monitoring thread
"""
if self._thread is not None and self._thread.is_alive():
self._logger.warning("Status monitor is already running")
return
self._stop_event.clear()
self._thread = threading.Thread(target=self._monitor_loop)
self._thread.daemon = True
self._thread.start()
self._logger.info("Status monitor started")
def stop(self):
"""
Stop the status monitoring thread
"""
if self._thread is None:
return
self._stop_event.set()
self._thread.join()
self._thread = None
self._logger.info("Status monitor stopped")
def _monitor_loop(self):
"""
Main monitoring loop
"""
while not self._stop_event.is_set():
try:
# Check status if tailscale interface is available
if self.plugin.tailscale_interface:
# Get current funnel status
funnel_enabled = self.plugin.tailscale_interface.is_funnel_enabled()
# Update settings if needed
current_setting = self.plugin._settings.get_boolean(["enabled"])
if funnel_enabled != current_setting:
self.plugin._settings.set_boolean(["enabled"], funnel_enabled)
self.plugin._settings.save()
self._logger.info("Funnel status updated in settings: {}".format(funnel_enabled))
# Send a notification to the frontend
self.plugin._plugin_manager.send_plugin_message(
self.plugin._identifier,
{
"type": "funnel_status_change",
"enabled": funnel_enabled
}
)
except Exception as e:
self._logger.error("Error in status monitoring loop: {}".format(str(e)))
# Wait for the next interval or until stopped
if self._stop_event.wait(self.interval):
break