Skip to content

Commit 651705c

Browse files
authored
Allow using either serial or qtserial when device manifest mentions serial transport. (#52)
* fix available transport list changing after cutter has been restarted compared to available list right after device is created
1 parent b7057ab commit 651705c

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

inkcut/device/dialogs.enaml

+7-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ enamldef ConfigEditView(Include):
3636
attr configurable: Model # Pair of config model and config declaration
3737
attr plugin
3838
func load_config_view(config, declaration):
39+
if not declaration:
40+
return []
3941
View = declaration.config_view()
4042
kwargs = {
4143
'model': config
@@ -118,13 +120,13 @@ enamldef DeviceEditView(Notebook): notebook:
118120
Label:
119121
text = QApplication.translate("device", "Type")
120122
ObjectCombo:
121-
attr supported_transports << device.transports \
122-
if device and device.transports else plugin.transports
123+
attr supported_transports << [transport for transport in plugin.transports\
124+
if device.supports_transport(transport)]
123125
items << sorted(supported_transports, key=lambda t: t.name)
124126
to_string = lambda t: QApplication.translate("device", t.name)
125127

126128
func get_selected(device):
127-
if not device or not device.connection:
129+
if not device or not device.connection or not device.connection.declaration:
128130
return
129131
tid = device.connection.declaration.id
130132
for t in plugin.transports:
@@ -135,7 +137,8 @@ enamldef DeviceEditView(Notebook): notebook:
135137
selected ::
136138
# Since this will trigger when the device changes, only actually update
137139
# if the connection changes
138-
if selected and selected.id != device.connection.declaration.id:
140+
if selected and device.connection and device.connection.declaration and\
141+
selected.id != device.connection.declaration.id:
139142
declaration = selected
140143
protocol = None
141144
if device.connection and device.connection.protocol:

inkcut/device/extensions.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def default_device_factory(driver, transports, protocols, config=None):
2727
driver: DeviceDriver
2828
The declaration of the device driver that was selected by the user
2929
transports: List[DeviceTransport]
30-
A list of DeviceTransport declarations that this device driver supports
30+
A list of all available DeviceTransport declarations
3131
protocols: List[DeviceProtocol]
3232
A list of DeviceProtocol declarations that this device driver supports
3333
@@ -148,6 +148,9 @@ class DeviceTransport(Declarative):
148148
#: Id of the transport
149149
id = d_(Str())
150150

151+
#: transport category in case there are multiple backends supporting same type of transport
152+
category = d_(Str())
153+
151154
#: Name of the transport (optional)
152155
name = d_(Str())
153156

inkcut/device/plugin.py

+37-12
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,6 @@ class Device(Model):
533533
#: Protocols supported by this device (ex the HPGLProtocol)
534534
protocols = List(extensions.DeviceProtocol)
535535

536-
#: Transports supported by this device (ex the SerialPort
537-
transports = List(extensions.DeviceTransport)
538-
539536
#: Filters that this device applies to the output
540537
filters = List(DeviceFilter).tag(config=True)
541538

@@ -571,6 +568,9 @@ class Device(Model):
571568
status = Str()
572569

573570
def __init__(self, *args, **kwargs):
571+
transports = None
572+
if 'transports' in kwargs:
573+
transports = kwargs.pop('transports')
574574
super(Model, self).__init__(*args, **kwargs)
575575
(w, h) = kwargs.get('width'), kwargs.get('height')
576576
if w:
@@ -580,17 +580,46 @@ def __init__(self, *args, **kwargs):
580580
if not h:
581581
h = 900000
582582
self.config.area.size[1] = h
583+
if not kwargs.get('connection'):
584+
self.connection = self._make_default_connection(transports)
583585

584-
def _default_connection(self):
586+
def _make_default_connection(self, transports):
585587
""" If no connection is set when the device is created,
586588
create one using the first "connection" type the driver supports.
587589
"""
588-
if not self.transports:
590+
591+
if not transports or not self.declaration:
592+
return TestTransport(declaration=extensions.DeviceTransport())
593+
preferred_decl = None
594+
# prioritize available connections based on the order in device manifest
595+
for supported_transform in self.declaration.connections:
596+
if preferred_decl:
597+
break
598+
for transport in transports:
599+
if transport.id == supported_transform or transport.category == supported_transform:
600+
preferred_decl = transport
601+
break
602+
if not preferred_decl:
603+
for transport in transports:
604+
if self.supports_transport(transport):
605+
preferred_decl = transport
606+
break
607+
if not preferred_decl:
589608
return TestTransport()
590-
declaration = self.transports[0]
609+
591610
driver = self.declaration
592611
protocol = self._default_protocol()
593-
return declaration.factory(driver, declaration, protocol)
612+
return preferred_decl.factory(driver, preferred_decl, protocol)
613+
614+
def supports_transport(self, transport_manifest):
615+
if not self.declaration or not self.declaration.connections:
616+
return True
617+
conections = self.declaration.connections
618+
if transport_manifest.id in conections or \
619+
transport_manifest.id == "disk" or \
620+
(transport_manifest.category and transport_manifest.category in conections):
621+
return True
622+
return False
594623

595624
def _default_protocol(self):
596625
""" Create the protocol for this device. """
@@ -1273,17 +1302,13 @@ def get_device_from_driver(self, driver, config=None):
12731302
and processing the jobs.
12741303
12751304
"""
1276-
# Set the protocols based on the declaration
1277-
transports = [t for t in self.transports
1278-
if not driver.connections or t.id == 'disk' or
1279-
t.id in driver.connections]
12801305

12811306
# Set the protocols based on the declaration
12821307
protocols = [p for p in self.protocols
12831308
if not driver.protocols or p.id in driver.protocols]
12841309

12851310
# Generate the device
1286-
return driver.factory(driver, transports, protocols, config)
1311+
return driver.factory(driver, self.transports, protocols, config)
12871312

12881313
# -------------------------------------------------------------------------
12891314
# Device Extensions API

inkcut/device/transports/qtserialport/manifest.enaml

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ enamldef QtSerialManifest(PluginManifest):
4141

4242
DeviceTransport:
4343
id = 'qtserial'
44+
category = 'serial'
4445
name = 'Qt SerialPort'
4546
factory = transport_factory
4647
config_view = config_view_factory

0 commit comments

Comments
 (0)