🐛 Implement auto connect on startup
If configured as such, OctoPrint will attempt to auto connect to the default profile on startup.
This commit is contained in:
1 parent
3196f0b445
commit
47e25d54cd
8 files changed
+59
-42
No files matched your search
@@ -1,6 +1,5 @@
|
||||
# General TODOs
|
||||
|
||||
* auto connect on start
|
||||
* Settings API for transport/protocol settings
|
||||
* UI for connection profiles
|
||||
* UI for transport/protocol settings
|
||||
|
||||
@@ -166,6 +166,11 @@ class Transport(ListenerAware):
|
||||
f"Transport state changed from '{old_state}' to '{value}'",
|
||||
)
|
||||
|
||||
def can_connect(self, **params):
|
||||
options = self.get_connection_options()
|
||||
param_dict = get_param_dict(params, options)
|
||||
return self.check_connection(**param_dict)
|
||||
|
||||
def connect(self, **params):
|
||||
if self.state == TransportState.CONNECTED:
|
||||
raise TransportAlreadyConnectedError("Already connected, disconnect first")
|
||||
@@ -199,6 +204,9 @@ class Transport(ListenerAware):
|
||||
def drop_connection(self, wait=True):
|
||||
return True
|
||||
|
||||
def check_connection(self, *args, **kwargs):
|
||||
return True
|
||||
|
||||
def read(self, size=None, timeout=None):
|
||||
data = self.do_read(size=size, timeout=timeout)
|
||||
self.notify_listeners("on_transport_log_received_data", self, data)
|
||||
|
||||
@@ -301,6 +301,13 @@ class SerialTransport(Transport):
|
||||
|
||||
return not error
|
||||
|
||||
def check_connection(self, *args, **kwargs):
|
||||
try:
|
||||
self._create_serial(**kwargs)
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
def do_read(self, size=None, timeout=None):
|
||||
return self._serial.read(size=size)
|
||||
|
||||
@@ -427,6 +434,7 @@ class SerialTransport(Transport):
|
||||
elif focus == "url":
|
||||
serial_obj = serial.serial_for_url(kwargs.get("url"), do_not_open=True)
|
||||
return serial_obj
|
||||
|
||||
else:
|
||||
raise ValueError(f"Invalid connect_via: {focus}")
|
||||
|
||||
|
||||
@@ -32,6 +32,9 @@ class VirtualSerialTransport(SerialTransport):
|
||||
|
||||
self._serial = virtual.VirtualPrinter(self.pluginSettings, self.datafolder)
|
||||
|
||||
def check_connection(self, *args, **kwargs):
|
||||
return True
|
||||
|
||||
|
||||
class VirtualPrinterPlugin(
|
||||
octoprint.plugin.SettingsPlugin, octoprint.plugin.TemplatePlugin
|
||||
|
||||
@@ -388,6 +388,27 @@ class Printer(
|
||||
|
||||
# ~~ PrinterInterface implementation
|
||||
|
||||
def autoconnect(self):
|
||||
default_connection = self._connection_profile_manager.get_default()
|
||||
if default_connection is None:
|
||||
return
|
||||
|
||||
from octoprint.comm.transport import lookup_transport
|
||||
|
||||
transport_class = lookup_transport(default_connection.transport)
|
||||
if not transport_class:
|
||||
return
|
||||
|
||||
transport = transport_class(
|
||||
settings=settings(),
|
||||
plugin_manager=plugin_manager(),
|
||||
event_bus=eventManager(),
|
||||
printer_profile=default_connection.printer_profile,
|
||||
)
|
||||
|
||||
if transport.can_connect(**default_connection.transport_parameters):
|
||||
self.connect(connection=default_connection.id)
|
||||
|
||||
def connect(self, **kwargs):
|
||||
"""
|
||||
Connects to the printer. If port and/or baudrate is provided, uses these settings, otherwise autodetection
|
||||
|
||||
@@ -1093,37 +1093,19 @@ class Server:
|
||||
# analysis backlog
|
||||
fileManager.process_backlog()
|
||||
|
||||
# TODO auto connect
|
||||
# if self._settings.getBoolean(["serial", "autoconnect"]):
|
||||
# self._logger.info(
|
||||
# "Autoconnect on startup is configured, trying to connect to the printer..."
|
||||
# )
|
||||
# try:
|
||||
# (port, baudrate) = (
|
||||
# self._settings.get(["serial", "port"]),
|
||||
# self._settings.getInt(["serial", "baudrate"]),
|
||||
# )
|
||||
# printer_profile = printerProfileManager.get_default()
|
||||
# connectionOptions = printer.__class__.get_connection_options()
|
||||
# if port in connectionOptions["ports"] or port == "AUTO" or port is None:
|
||||
# self._logger.info(
|
||||
# "Trying to connect to configured serial port {}".format(port)
|
||||
# )
|
||||
# printer.connect(
|
||||
# port=port,
|
||||
# baudrate=baudrate,
|
||||
# profile=printer_profile["id"]
|
||||
# if "id" in printer_profile
|
||||
# else "_default",
|
||||
# )
|
||||
# else:
|
||||
# self._logger.info(
|
||||
# "Could not find configured serial port {} in the system, cannot automatically connect to a non existing printer. Is it plugged in and booted up yet?"
|
||||
# )
|
||||
# except Exception:
|
||||
# self._logger.exception(
|
||||
# "Something went wrong while attempting to automatically connect to the printer"
|
||||
# )
|
||||
if self._settings.getBoolean(["connection", "profiles", "autoconnect_default"]):
|
||||
self._logger.info(
|
||||
"Autoconnect on startup is configured, "
|
||||
"trying to connect via the default connection profile"
|
||||
)
|
||||
try:
|
||||
printer.autoconnect()
|
||||
except Exception:
|
||||
self._logger.exception(
|
||||
"Something went wrong while attempting to "
|
||||
"automatically connect to the default connection "
|
||||
"profile"
|
||||
)
|
||||
|
||||
# start up watchdogs
|
||||
try:
|
||||
|
||||
@@ -77,15 +77,10 @@ def connectionCommand():
|
||||
abort(400, description="printerProfile is invalid")
|
||||
kwargs["profile"] = printerProfile
|
||||
|
||||
if "save" in data and data["save"]: # TODO
|
||||
"""
|
||||
settings().set(["serial", "port"], port)
|
||||
settings().setInt(["serial", "baudrate"], baudrate)
|
||||
printerProfileManager.set_default(kwargs.get("profile"))
|
||||
"""
|
||||
|
||||
if "autoconnect" in data:
|
||||
settings().setBoolean(["serial", "autoconnect"], data["autoconnect"])
|
||||
settings().setBoolean(
|
||||
["connection", "profiles", "autoconnect_default"], data["autoconnect"]
|
||||
)
|
||||
|
||||
##~~ legacy
|
||||
|
||||
|
||||
@@ -168,7 +168,7 @@ default_settings = {
|
||||
},
|
||||
"connection": {
|
||||
"log": {"connection": False, "commdebug": False},
|
||||
"profiles": {"default": None},
|
||||
"profiles": {"default": None, "autoconnect_default": False},
|
||||
"protocols": {},
|
||||
"transports": {},
|
||||
},
|
||||
@@ -1994,7 +1994,8 @@ class Settings:
|
||||
config["connection"] = {}
|
||||
if "profiles" not in config["connection"]:
|
||||
config["connection"]["profiles"] = {}
|
||||
config["connection"]["profiles"]["default"] = "migrated"
|
||||
if not config["connection"]["profiles"]["default"]:
|
||||
config["connection"]["profiles"]["default"] = "migrated"
|
||||
except Exception:
|
||||
self._logger.exception(
|
||||
"Error while trying to save migrated connection profile"
|
||||
|
||||
Reference in new issue
Block a user