diff --git a/src/octoprint/events.py b/src/octoprint/events.py index 010c20604..00d164fa0 100644 --- a/src/octoprint/events.py +++ b/src/octoprint/events.py @@ -36,6 +36,7 @@ class Events: CONNECTED = "Connected" DISCONNECTING = "Disconnecting" DISCONNECTED = "Disconnected" + CONNECTIONS_AUTOREFRESHED = "ConnectionsAutorefreshed" # State changes PRINTER_STATE_CHANGED = "PrinterStateChanged" diff --git a/src/octoprint/schema/config/serial.py b/src/octoprint/schema/config/serial.py index 6da0e2f5c..5d3130921 100644 --- a/src/octoprint/schema/config/serial.py +++ b/src/octoprint/schema/config/serial.py @@ -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)""" diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index c8a77dbbc..9bd7f6583 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -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") diff --git a/src/octoprint/static/js/app/viewmodels/connection.js b/src/octoprint/static/js/app/viewmodels/connection.js index 664c30918..60b7a2258 100644 --- a/src/octoprint/static/js/app/viewmodels/connection.js +++ b/src/octoprint/static/js/app/viewmodels/connection.js @@ -220,6 +220,10 @@ $(function () { self.requestData(); }; + self.onEventConnectionsAutorefreshed = function () { + self.requestData(); + }; + self.onStartup = function () { var connectionTab = $("#connection"); connectionTab.on("show", function () {