🚧 ports/baudrates can now be overridden
Connection settings (exact port & baudrate to use) and overall serial options (which ports and baudrates to offer) are now separated.
This commit is contained in:
1 parent
ecd2bf81e3
commit
566e214ee4
7 files changed
+188
-97
No files matched your search
@@ -4,9 +4,10 @@
|
||||
* auto connect on start
|
||||
* save and connect
|
||||
* keep connection settings after disconnect, currently they revert to default
|
||||
* Settings API for transport/protocol settings
|
||||
* UI for connection profiles
|
||||
* UI for transport/protocol settings
|
||||
* SerialTransport:
|
||||
* additional ports & baudrates for SerialTransport
|
||||
* port & baudrate blacklist for SerialTransport
|
||||
* RepRapProtocol:
|
||||
|
||||
TBC
|
||||
@@ -8,6 +8,7 @@ import logging
|
||||
import frozendict
|
||||
|
||||
from octoprint.comm.transport import TransportListener, TransportState
|
||||
from octoprint.comm.util.parameters import register_settings_overlay
|
||||
from octoprint.plugin import plugin_manager
|
||||
from octoprint.settings import SubSettings
|
||||
from octoprint.util import CountedEvent, to_unicode
|
||||
@@ -16,22 +17,21 @@ from octoprint.util.listener import ListenerAware
|
||||
_registry = {}
|
||||
|
||||
|
||||
def register_protocols():
|
||||
def register_protocols(settings, path):
|
||||
from .reprap import ReprapGcodeProtocol
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# stock protocols
|
||||
register_protocol(ReprapGcodeProtocol)
|
||||
register_protocol(ReprapGcodeProtocol, settings, path)
|
||||
|
||||
# more protocols provided by plugins
|
||||
logger = logging.getLogger(__name__)
|
||||
hooks = plugin_manager().get_hooks("octoprint.comm.protocol.register")
|
||||
for name, hook in hooks.items():
|
||||
try:
|
||||
protocols = hook()
|
||||
for protocol in protocols:
|
||||
try:
|
||||
register_protocol(protocol)
|
||||
register_protocol(protocol, settings, path)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Error while registering protocol class {} for plugin {}".format(
|
||||
@@ -46,18 +46,51 @@ def register_protocols():
|
||||
)
|
||||
|
||||
|
||||
def register_protocol(protocol_class):
|
||||
def register_protocol(protocol_class, settings, path):
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
if not hasattr(protocol_class, "key"):
|
||||
raise ValueError("Protocol class {} is missing key".format(protocol_class))
|
||||
_registry[protocol_class.key] = protocol_class
|
||||
if not hasattr(protocol_class, "settings"):
|
||||
raise ValueError("Protocol class {} is missing settings".format(protocol_class))
|
||||
|
||||
# register settings overlay
|
||||
overlay = register_settings_overlay(settings, path, protocol_class.settings)
|
||||
logger.debug(
|
||||
"Registered settings overlay for protocol {} under key {}".format(
|
||||
protocol_class, overlay
|
||||
)
|
||||
)
|
||||
|
||||
# register protocol
|
||||
key = protocol_class.key
|
||||
overrides = settings.get(path, merged=True)
|
||||
_registry[key] = (
|
||||
protocol_class,
|
||||
protocol_class.with_settings(**overrides.get(key, {})),
|
||||
)
|
||||
|
||||
|
||||
def refresh_protocols(settings, path):
|
||||
for key in _registry:
|
||||
refresh_protocol(key, settings, path)
|
||||
|
||||
|
||||
def refresh_protocol(key, settings, path):
|
||||
protocol_class, _ = _registry[key]
|
||||
overrides = settings.get(path, merged=True)
|
||||
_registry[key] = (
|
||||
protocol_class,
|
||||
protocol_class.with_settings(**overrides.get(key, {})),
|
||||
)
|
||||
|
||||
|
||||
def lookup_protocol(key):
|
||||
return _registry.get(key)
|
||||
return _registry.get(key, (None, None))[1]
|
||||
|
||||
|
||||
def all_protocols():
|
||||
return _registry.values()
|
||||
return map(lambda x: x[1], _registry.values())
|
||||
|
||||
|
||||
class ProtocolErrorStats(ListenerAware):
|
||||
@@ -183,14 +216,20 @@ class Protocol(ListenerAware, TransportListener, ProtocolErrorStatsListener):
|
||||
|
||||
supported_jobs = []
|
||||
|
||||
settings = []
|
||||
|
||||
@classmethod
|
||||
def with_settings(cls, **settings):
|
||||
return type(
|
||||
cls.__name__ + "WithSettings",
|
||||
(cls,),
|
||||
settings,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_connection_options(cls):
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
def get_settings_defaults(cls):
|
||||
return {}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Protocol, self).__init__()
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import copy
|
||||
import logging
|
||||
import time
|
||||
|
||||
from octoprint.comm.util.parameters import get_param_dict
|
||||
from octoprint.comm.util.parameters import get_param_dict, register_settings_overlay
|
||||
from octoprint.plugin import plugin_manager
|
||||
from octoprint.settings import SubSettings
|
||||
from octoprint.util.listener import ListenerAware
|
||||
@@ -13,26 +13,28 @@ from octoprint.util.listener import ListenerAware
|
||||
_registry = {}
|
||||
|
||||
|
||||
def register_transports():
|
||||
def register_transports(settings, path):
|
||||
from .serialtransport import SerialTransport, SerialUrlTransport
|
||||
from .sockettransport import SerialOverTcpTransport, TcpTransport
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# stock transports
|
||||
register_transport(SerialTransport)
|
||||
register_transport(SerialUrlTransport)
|
||||
register_transport(TcpTransport)
|
||||
register_transport(SerialOverTcpTransport)
|
||||
for transport in (
|
||||
SerialTransport,
|
||||
SerialUrlTransport,
|
||||
TcpTransport,
|
||||
SerialOverTcpTransport,
|
||||
):
|
||||
register_transport(transport, settings, path)
|
||||
|
||||
# more transports provided by plugins
|
||||
logger = logging.getLogger(__name__)
|
||||
hooks = plugin_manager().get_hooks("octoprint.comm.transport.register")
|
||||
for name, hook in hooks.items():
|
||||
try:
|
||||
transports = hook()
|
||||
for transport in transports:
|
||||
try:
|
||||
register_transport(transport)
|
||||
register_transport(transport, settings, path)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Error while registering transport class {} for plugin {}".format(
|
||||
@@ -47,18 +49,59 @@ def register_transports():
|
||||
)
|
||||
|
||||
|
||||
def register_transport(transport_class):
|
||||
def register_transport(transport_class, settings, path):
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
if not hasattr(transport_class, "key"):
|
||||
raise ValueError("Transport class {} is missing key".format(transport_class))
|
||||
_registry[transport_class.key] = transport_class
|
||||
|
||||
# register settings overlay
|
||||
overlay = register_transport_overlay(transport_class, settings, path)
|
||||
logger.debug(
|
||||
"Registered settings overlay for transport {} under key {}".format(
|
||||
transport_class, overlay
|
||||
)
|
||||
)
|
||||
|
||||
# register transport
|
||||
key = transport_class.key
|
||||
overrides = settings.get(path, merged=True)
|
||||
_registry[key] = (
|
||||
transport_class,
|
||||
transport_class.with_settings(**overrides.get(key, {})),
|
||||
)
|
||||
|
||||
|
||||
def register_transport_overlay(transport_class, settings, path):
|
||||
if not hasattr(transport_class, "key"):
|
||||
raise ValueError("Transport class {} is missing key".format(transport_class))
|
||||
if not hasattr(transport_class, "settings"):
|
||||
raise ValueError("Transport class {} is missing settings".format(transport_class))
|
||||
|
||||
params = transport_class.settings
|
||||
return register_settings_overlay(settings, path + [transport_class.key], params)
|
||||
|
||||
|
||||
def refresh_transports(settings, path):
|
||||
for key in _registry:
|
||||
refresh_transport(key, settings, path)
|
||||
|
||||
|
||||
def refresh_transport(key, settings, path):
|
||||
transport_class, _ = _registry[key]
|
||||
overrides = settings.get(path, merged=True)
|
||||
_registry[key] = (
|
||||
transport_class,
|
||||
transport_class.with_settings(**overrides.get(key, {})),
|
||||
)
|
||||
|
||||
|
||||
def lookup_transport(key):
|
||||
return _registry.get(key)
|
||||
return _registry.get(key, (None, None))[1]
|
||||
|
||||
|
||||
def all_transports():
|
||||
return _registry.values()
|
||||
return map(lambda x: x[1], _registry.values())
|
||||
|
||||
|
||||
class Transport(ListenerAware):
|
||||
@@ -73,14 +116,20 @@ class Transport(ListenerAware):
|
||||
|
||||
message_integrity = False
|
||||
|
||||
settings = []
|
||||
|
||||
@classmethod
|
||||
def with_settings(cls, **settings):
|
||||
return type(
|
||||
cls.__name__ + "WithSettings",
|
||||
(cls,),
|
||||
settings,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_connection_options(cls):
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
def get_settings_defaults(cls):
|
||||
return {}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Transport, self).__init__()
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ from octoprint.comm.util.parameters import (
|
||||
BooleanType,
|
||||
ChoiceType,
|
||||
IntegerType,
|
||||
ListType,
|
||||
SmallChoiceType,
|
||||
SuggestionType,
|
||||
UrlType,
|
||||
@@ -25,33 +26,30 @@ class SerialTransport(Transport):
|
||||
key = "serial"
|
||||
message_integrity = False
|
||||
|
||||
suggested_baudrates = [250000, 230400, 115200, 57600, 38400, 19200, 9600]
|
||||
unix_port_patterns = [
|
||||
"/dev/ttyUSB*",
|
||||
"/dev/ttyACM*",
|
||||
"/dev/tty.usb*",
|
||||
"/dev/cu.*",
|
||||
"/dev/cuaU*",
|
||||
"/dev/rfcomm*",
|
||||
]
|
||||
|
||||
max_write_passes = 5
|
||||
|
||||
@classmethod
|
||||
def for_additional_ports_and_baudrates(cls, additional_ports, additional_baudrates):
|
||||
patterns = SerialTransport.unix_port_patterns
|
||||
if additional_ports:
|
||||
patterns += additional_ports
|
||||
baudrates = [250000, 230400, 115200, 57600, 38400, 19200, 9600]
|
||||
ignored_ports = [
|
||||
"/dev/ttyAMA0",
|
||||
"/dev/ttyS*",
|
||||
]
|
||||
|
||||
baudrates = SerialTransport.suggested_baudrates
|
||||
if additional_baudrates:
|
||||
baudrates += additional_baudrates
|
||||
|
||||
return type(
|
||||
cls.__name__ + "WithAdditionalPorts",
|
||||
(cls,),
|
||||
{"unix_port_patterns": patterns, "suggested_baudrates": baudrates},
|
||||
)
|
||||
settings = [
|
||||
ListType(
|
||||
"baudrates",
|
||||
gettext("Baudrates"),
|
||||
sep="\n",
|
||||
factory=int,
|
||||
default=baudrates,
|
||||
),
|
||||
ListType(
|
||||
"ignored_ports",
|
||||
gettext("Ignored ports"),
|
||||
help=gettext("Serial ports you'd like to ignore"),
|
||||
sep="\n",
|
||||
default=ignored_ports,
|
||||
),
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def get_connection_options(cls):
|
||||
@@ -131,43 +129,14 @@ class SerialTransport(Transport):
|
||||
|
||||
@classmethod
|
||||
def get_available_serial_ports(cls):
|
||||
import sys
|
||||
|
||||
if sys.platform == "win32":
|
||||
# windows
|
||||
from serial.tools.list_ports import comports
|
||||
|
||||
ports = comports()
|
||||
|
||||
else:
|
||||
# posix
|
||||
import glob
|
||||
|
||||
devices = [
|
||||
device
|
||||
for pattern in cls.unix_port_patterns
|
||||
for device in glob.glob(pattern)
|
||||
]
|
||||
|
||||
plat = sys.platform.lower()
|
||||
|
||||
if plat[:5] == "linux":
|
||||
# linux
|
||||
from serial.tools.list_ports_linux import SysFS
|
||||
|
||||
ports = [
|
||||
info
|
||||
for info in [SysFS(d) for d in devices]
|
||||
if info.subsystem != "platform"
|
||||
]
|
||||
else:
|
||||
# other posix systems
|
||||
from serial.tools import list_ports_common
|
||||
|
||||
ports = [list_ports_common.ListPortInfo(d) for d in devices]
|
||||
from serial.tools.list_ports import comports
|
||||
|
||||
port_values = [Value(None, title="Auto detect")] + sorted(
|
||||
[Value(port.device, title=port.description) for port in ports],
|
||||
[
|
||||
Value(port.device, title=port.description)
|
||||
for port in comports()
|
||||
if port.device not in cls.ignored_ports
|
||||
],
|
||||
key=lambda x: x.title,
|
||||
)
|
||||
return port_values
|
||||
@@ -175,7 +144,7 @@ class SerialTransport(Transport):
|
||||
@classmethod
|
||||
def get_available_baudrates(cls):
|
||||
return [Value(0, title="Auto detect")] + sorted(
|
||||
[Value(baudrate) for baudrate in cls.suggested_baudrates],
|
||||
[Value(baudrate) for baudrate in cls.baudrates],
|
||||
key=lambda x: x.title,
|
||||
reverse=True,
|
||||
)
|
||||
|
||||
@@ -2,6 +2,32 @@ __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agp
|
||||
__copyright__ = "Copyright (C) 2018 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
|
||||
def register_settings_overlay(settings, path, params, key=None):
|
||||
overlay = {}
|
||||
|
||||
node = overlay
|
||||
for p in path:
|
||||
node[p] = {}
|
||||
node = node[p]
|
||||
node.update(**get_param_defaults(params))
|
||||
|
||||
# our overlay is now complete, register it
|
||||
return settings.add_overlay(overlay, key=key)
|
||||
|
||||
|
||||
def get_param_defaults(options):
|
||||
result = {}
|
||||
for option in options:
|
||||
if option.type == "group":
|
||||
result[option.name] = get_param_defaults(option.params)
|
||||
elif option.type == "groupchoice":
|
||||
result[option.name] = option.default
|
||||
result[option.group.name] = option.defaults
|
||||
else:
|
||||
result[option.name] = option.default
|
||||
return result
|
||||
|
||||
|
||||
def get_param_dict(data, options):
|
||||
options_by_name = dict((option.name, option) for option in options)
|
||||
|
||||
@@ -212,21 +238,26 @@ class SuggestionType(ParamType):
|
||||
class ListType(ParamType):
|
||||
type = "list"
|
||||
|
||||
def __init__(self, name, title, factory=None, **kwargs):
|
||||
def __init__(self, name, title, sep=",", factory=None, **kwargs):
|
||||
if factory is None:
|
||||
factory = lambda x: x
|
||||
|
||||
self.factory = factory
|
||||
self.sep = sep
|
||||
|
||||
if kwargs.get("default") is None:
|
||||
kwargs["default"] = []
|
||||
|
||||
ParamType.__init__(self, name, title, **kwargs)
|
||||
|
||||
def convert(self, value):
|
||||
if isinstance(value, str):
|
||||
items = map(str.strip, value.split(","))
|
||||
items = map(str.strip, value.split(self.sep))
|
||||
elif isinstance(value, list):
|
||||
items = value
|
||||
else:
|
||||
raise ValueError(
|
||||
"value {!r} must be either a comma-separated string or a list".format(
|
||||
"value {!r} must be either a self.sep separated string or a list".format(
|
||||
value
|
||||
)
|
||||
)
|
||||
|
||||
@@ -603,12 +603,12 @@ class Server:
|
||||
# register available transports
|
||||
from octoprint.comm.transport import register_transports
|
||||
|
||||
register_transports()
|
||||
register_transports(self._settings, ["connection", "transports"])
|
||||
|
||||
# register available protocols
|
||||
from octoprint.comm.protocol import register_protocols
|
||||
|
||||
register_protocols()
|
||||
register_protocols(self._settings, ["connection", "protocols"])
|
||||
|
||||
# log environment data now
|
||||
self._environment_detector.log_detected_environment()
|
||||
|
||||
@@ -167,6 +167,8 @@ default_settings = {
|
||||
"connection": {
|
||||
"log": {"connection": False, "commdebug": False},
|
||||
"profiles": {"default": None},
|
||||
"protocols": {},
|
||||
"transports": {},
|
||||
},
|
||||
"server": {
|
||||
"host": None,
|
||||
|
||||
Reference in new issue
Block a user