Starting work on a better version of the comm refactoring
This commit is contained in:
1 parent
3b35d921d1
commit
581cd19446
4 files changed
+265
No files matched your search
@@ -0,0 +1,8 @@
|
||||
# coding=utf-8
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
__author__ = "Gina Häußge <osd@foosel.net>"
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2016 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
# coding=utf-8
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
__author__ = "Gina Häußge <osd@foosel.net>"
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2016 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
from .types import get_param_dict
|
||||
|
||||
class Transport(object):
|
||||
|
||||
name = None
|
||||
url_scheme = None
|
||||
message_integrity = False
|
||||
|
||||
@classmethod
|
||||
def get_connection_options(cls):
|
||||
return []
|
||||
|
||||
def __init__(self):
|
||||
self._listeners = set()
|
||||
|
||||
def connect(self, params, options=None):
|
||||
if options is None:
|
||||
options = self.get_connection_options()
|
||||
|
||||
param_dict = get_param_dict(params, options)
|
||||
self.create_connection(**param_dict)
|
||||
|
||||
def create_connection(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def send(self, message):
|
||||
pass
|
||||
|
||||
def register_listener(self, transport_listener):
|
||||
self._listeners.add(transport_listener)
|
||||
|
||||
def unregister_listener(self, transport_listener):
|
||||
try:
|
||||
self._listeners.remove(transport_listener)
|
||||
except KeyError:
|
||||
# not registered, ah well, we'll ignore that
|
||||
pass
|
||||
|
||||
class TransportListener(object):
|
||||
|
||||
def on_transport_message_received(self, message):
|
||||
pass
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
# coding=utf-8
|
||||
from __future__ import absolute_import, unicode_literals, print_function
|
||||
|
||||
__author__ = "Gina Häußge <osd@foosel.net>"
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2016 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
from octoprint.comm.transport import Transport
|
||||
from octoprint.comm.transport.types import TextType, IntegerType, SuggestionType, ConstantNameType
|
||||
|
||||
import serial
|
||||
|
||||
class SerialTransport(Transport):
|
||||
name = "Serial Connection"
|
||||
url_scheme = "serial"
|
||||
message_integrity = False
|
||||
|
||||
suggested_baudrates = [0, 250000, 230400, 115200, 57600, 38400, 19200, 9600]
|
||||
unix_port_patterns = ["/dev/ttyUSB*", "/dev/ttyACM*", "/dev/tty.usb*",
|
||||
"/dev/cu.*", "/dev/cuaU*", "/dev/rfcomm*"]
|
||||
|
||||
@staticmethod
|
||||
def for_additional_ports(additional_ports):
|
||||
patterns = SerialTransport.unix_port_patterns + additional_ports
|
||||
return type("SerialTransportWithAdditionalPorts",
|
||||
(SerialTransport,),
|
||||
{"unix_port_patterns": patterns})
|
||||
|
||||
@classmethod
|
||||
def get_connection_options(cls):
|
||||
return [
|
||||
SuggestionType("port", "Port", TextType, cls.get_available_serial_ports(), default="AUTO"),
|
||||
SuggestionType("baudrate", "Baudrate", IntegerType, cls.suggested_baudrates, default=0)
|
||||
]
|
||||
|
||||
@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]
|
||||
|
||||
return [ConstantNameType("AUTO", "Autodetect")] \
|
||||
+ [ConstantNameType(port.device, port.description) for port in ports]
|
||||
|
||||
def __init__(self):
|
||||
Transport.__init__(self)
|
||||
|
||||
self._serial = None
|
||||
|
||||
def create_connection(self, port, baudrate):
|
||||
self._serial = serial.Serial(port=port, baudrate=baudrate)
|
||||
|
||||
def send(self, message):
|
||||
self._serial.write(message)
|
||||
|
||||
def close_connection(self):
|
||||
self._serial.close()
|
||||
|
||||
|
||||
class VirtualSerialTransport(SerialTransport):
|
||||
virtual_serial = None
|
||||
|
||||
@staticmethod
|
||||
def for_virtual_serial(virtual_serial):
|
||||
return type("CustomizedVirtualSerialTransport",
|
||||
(VirtualSerialTransport,),
|
||||
{"virtual_serial": virtual_serial})
|
||||
|
||||
@classmethod
|
||||
def get_connection_options(cls):
|
||||
return []
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
for option in SerialTransport.get_connection_options():
|
||||
print(repr(option))
|
||||
@@ -0,0 +1,107 @@
|
||||
# coding=utf-8
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
__author__ = "Gina Häußge <osd@foosel.net>"
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2016 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
|
||||
def get_param_dict(data, options):
|
||||
options_by_name = dict((option.name, option) for option in options)
|
||||
|
||||
result = dict()
|
||||
for key, value in data.items():
|
||||
if not key in options_by_name:
|
||||
continue
|
||||
option = options_by_name[key]
|
||||
result[key] = option.convert(value)
|
||||
return result
|
||||
|
||||
|
||||
class ParamType(object):
|
||||
def __init__(self, name, title, default=None):
|
||||
self.name = name
|
||||
self.title = title
|
||||
self.default = default
|
||||
|
||||
def convert(self, value):
|
||||
return value
|
||||
|
||||
def __repr__(self):
|
||||
return "{}(".format(self.__class__.__name__) \
|
||||
+ ", ".join(["{}={!r}".format(key, value) for key, value in self.__dict__.items() if not key.startswith("_")]) + ")"
|
||||
|
||||
class TextType(ParamType):
|
||||
def convert(self, value):
|
||||
if isinstance(value, bytes):
|
||||
value = value.decode("utf-8", "replace")
|
||||
return value
|
||||
|
||||
class IntegerType(ParamType):
|
||||
def __init__(self, name, title, min=None, max=None, default=None):
|
||||
self.min = min
|
||||
self.max = max
|
||||
ParamType.__init__(self, name, title, default=default)
|
||||
|
||||
def convert(self, value):
|
||||
try:
|
||||
value = int(value)
|
||||
except ValueError:
|
||||
raise ValueError("value {!r} is not a valid integer".format(value))
|
||||
|
||||
if self.min is not None and value < self.min:
|
||||
raise ValueError("value {} is less than minimum valid value {}".format(value, self.min))
|
||||
if self.max is not None and value > self.max:
|
||||
raise ValueError("value {} is greater than maximum valid value {}".format(value, self.max))
|
||||
|
||||
return value
|
||||
|
||||
class ChoiceType(ParamType):
|
||||
def __init__(self, name, title, choice_type, choices, default=None):
|
||||
self.choice_type = choice_type
|
||||
self.choices = choices
|
||||
ParamType.__init__(self, name, title, default=default)
|
||||
|
||||
def convert(self, value):
|
||||
value = self.choice_type.convert(value)
|
||||
if not value in self.choices:
|
||||
raise ValueError("value {} is not a valid choice")
|
||||
return value
|
||||
|
||||
class SuggestionType(ParamType):
|
||||
def __init__(self, name, title, suggestion_type, suggestions, default=None):
|
||||
self.suggestion_type = suggestion_type
|
||||
self.suggestions = suggestions
|
||||
ParamType.__init__(self, name, title, default=default)
|
||||
|
||||
def convert(self, value):
|
||||
value = self.suggestion_type.convert(value)
|
||||
return value
|
||||
|
||||
class ListType(ParamType):
|
||||
def __init__(self, name, title, item_type, default=None):
|
||||
self.item_type = item_type
|
||||
ParamType.__init__(self, name, title, default=default)
|
||||
|
||||
def convert(self, value):
|
||||
if isinstance(value, unicode):
|
||||
items = map(unicode.strip, value.split(","))
|
||||
elif isinstance(value, list):
|
||||
items = value
|
||||
else:
|
||||
raise ValueError("value {!r} must be either a comma-separated string or a list".format(value))
|
||||
|
||||
return map(self.item_type.convert, items)
|
||||
|
||||
class ConstantType(ParamType):
|
||||
def __init__(self, name, title, constant, default=None):
|
||||
ParamType.__init__(self, name, title, default=default)
|
||||
self.constant = constant
|
||||
self.default = constant
|
||||
|
||||
def convert(self, value):
|
||||
return self.constant
|
||||
|
||||
class ConstantNameType(ConstantType):
|
||||
def __init__(self, name, title, default=None):
|
||||
ConstantType.__init__(self, name, title, constant=name, default=default)
|
||||
Reference in new issue
Block a user