✨ Autorefresh port list while not connected

Implements #4473
This commit is contained in:
Gina Häußge committed 2022-11-17 15:30:02 +01:00
1 parent 4636c14dc3
commit cb63baa1e2
4 files changed
+65

No files matched your search

+1
View File
@@ -36,6 +36,7 @@ class Events:
CONNECTED = "Connected"
DISCONNECTING = "Disconnecting"
DISCONNECTED = "Disconnected"
CONNECTIONS_AUTOREFRESHED = "ConnectionsAutorefreshed"
# State changes
PRINTER_STATE_CHANGED = "PrinterStateChanged"
+6
View File
@@ -105,6 +105,12 @@ class SerialConfig(BaseModel):
autoconnect: bool = False
"""Whether to try to automatically connect to the printer on startup or not"""
autorefresh: bool = True
"""Whether to automatically refresh the port list while no connection is established"""
autorefreshInterval: int = 1
"""Interval in seconds at which to refresh the port list while no connection is established"""
log: bool = False
"""Whether to log whole communication to serial.log (warning: might decrease performance)"""
+54
View File
@@ -40,6 +40,7 @@ from watchdog.observers import Observer
from watchdog.observers.polling import PollingObserver
from werkzeug.exceptions import HTTPException
import octoprint.events
import octoprint.filemanager
import octoprint.util
import octoprint.util.net
@@ -1148,6 +1149,59 @@ class Server:
"Something went wrong while attempting to automatically connect to the printer"
)
# auto refresh serial ports while not connected
if self._settings.getBoolean(["serial", "autorefresh"]):
from octoprint.util.comm import serialList
last_ports = None
autorefresh = None
def refresh_serial_list():
nonlocal last_ports
new_ports = sorted(serialList())
if new_ports != last_ports:
self._logger.info(
"Serial port list was updated, refreshing the port list in the frontend"
)
eventManager.fire(
events.Events.CONNECTIONS_AUTOREFRESHED,
payload={"ports": new_ports},
)
last_ports = new_ports
def autorefresh_active():
return printer.is_closed_or_error()
def autorefresh_stopped():
nonlocal autorefresh
self._logger.info("Autorefresh of serial port list stopped")
autorefresh = None
def run_autorefresh():
nonlocal autorefresh
if autorefresh is not None:
autorefresh.stop()
autorefresh = None
autorefresh = octoprint.util.RepeatedTimer(
self._settings.getInt(["serial", "autorefreshInterval"]),
refresh_serial_list,
run_first=True,
condition=autorefresh_active,
on_finish=autorefresh_stopped,
)
self._logger.info("Starting autorefresh of serial port list")
autorefresh.start()
run_autorefresh()
eventManager.subscribe(
octoprint.events.Events.DISCONNECTED, lambda e, p: run_autorefresh()
)
# start up watchdogs
try:
watched = self._settings.getBaseFolder("watched")
@@ -220,6 +220,10 @@ $(function () {
self.requestData();
};
self.onEventConnectionsAutorefreshed = function () {
self.requestData();
};
self.onStartup = function () {
var connectionTab = $("#connection");
connectionTab.on("show", function () {