diff options
Diffstat (limited to 'plugins/UM3NetworkPrinting/src/Network/ZeroConfClient.py')
-rw-r--r-- | plugins/UM3NetworkPrinting/src/Network/ZeroConfClient.py | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/plugins/UM3NetworkPrinting/src/Network/ZeroConfClient.py b/plugins/UM3NetworkPrinting/src/Network/ZeroConfClient.py index 466638d99e..ce5d9ce868 100644 --- a/plugins/UM3NetworkPrinting/src/Network/ZeroConfClient.py +++ b/plugins/UM3NetworkPrinting/src/Network/ZeroConfClient.py @@ -12,9 +12,11 @@ from UM.Signal import Signal from cura.CuraApplication import CuraApplication -## The ZeroConfClient handles all network discovery logic. -# It emits signals when new network services were found or disappeared. class ZeroConfClient: + """The ZeroConfClient handles all network discovery logic. + + It emits signals when new network services were found or disappeared. + """ # The discovery protocol name for Ultimaker printers. ZERO_CONF_NAME = u"_ultimaker._tcp.local." @@ -30,10 +32,13 @@ class ZeroConfClient: self._service_changed_request_event = None # type: Optional[Event] self._service_changed_request_thread = None # type: Optional[Thread] - ## The ZeroConf service changed requests are handled in a separate thread so we don't block the UI. - # We can also re-schedule the requests when they fail to get detailed service info. - # Any new or re-reschedule requests will be appended to the request queue and the thread will process them. def start(self) -> None: + """The ZeroConf service changed requests are handled in a separate thread so we don't block the UI. + + We can also re-schedule the requests when they fail to get detailed service info. + Any new or re-reschedule requests will be appended to the request queue and the thread will process them. + """ + self._service_changed_request_queue = Queue() self._service_changed_request_event = Event() try: @@ -56,16 +61,18 @@ class ZeroConfClient: self._zero_conf_browser.cancel() self._zero_conf_browser = None - ## Handles a change is discovered network services. def _queueService(self, zeroconf: Zeroconf, service_type, name: str, state_change: ServiceStateChange) -> None: + """Handles a change is discovered network services.""" + item = (zeroconf, service_type, name, state_change) if not self._service_changed_request_queue or not self._service_changed_request_event: return self._service_changed_request_queue.put(item) self._service_changed_request_event.set() - ## Callback for when a ZeroConf service has changes. def _handleOnServiceChangedRequests(self) -> None: + """Callback for when a ZeroConf service has changes.""" + if not self._service_changed_request_queue or not self._service_changed_request_event: return @@ -98,19 +105,23 @@ class ZeroConfClient: for request in reschedule_requests: self._service_changed_request_queue.put(request) - ## Handler for zeroConf detection. - # Return True or False indicating if the process succeeded. - # Note that this function can take over 3 seconds to complete. Be careful calling it from the main thread. - def _onServiceChanged(self, zero_conf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange - ) -> bool: + def _onServiceChanged(self, zero_conf: Zeroconf, service_type: str, name: str, + state_change: ServiceStateChange) -> bool: + """Handler for zeroConf detection. + + Return True or False indicating if the process succeeded. + Note that this function can take over 3 seconds to complete. Be careful calling it from the main thread. + """ + if state_change == ServiceStateChange.Added: return self._onServiceAdded(zero_conf, service_type, name) elif state_change == ServiceStateChange.Removed: return self._onServiceRemoved(name) return True - ## Handler for when a ZeroConf service was added. def _onServiceAdded(self, zero_conf: Zeroconf, service_type: str, name: str) -> bool: + """Handler for when a ZeroConf service was added.""" + # First try getting info from zero-conf cache info = ServiceInfo(service_type, name, properties={}) for record in zero_conf.cache.entries_with_name(name.lower()): @@ -118,20 +129,20 @@ class ZeroConfClient: for record in zero_conf.cache.entries_with_name(info.server): info.update_record(zero_conf, time(), record) - if info.address: + if info.addresses: break # Request more data if info is not complete - if not info.address: + if not info.addresses: new_info = zero_conf.get_service_info(service_type, name) if new_info is not None: info = new_info - if info and info.address: + if info and info.addresses: type_of_device = info.properties.get(b"type", None) if type_of_device: if type_of_device == b"printer": - address = '.'.join(map(str, info.address)) + address = '.'.join(map(str, info.addresses[0])) self.addedNetworkCluster.emit(str(name), address, info.properties) else: Logger.log("w", "The type of the found device is '%s', not 'printer'." % type_of_device) @@ -141,8 +152,9 @@ class ZeroConfClient: return True - ## Handler for when a ZeroConf service was removed. def _onServiceRemoved(self, name: str) -> bool: + """Handler for when a ZeroConf service was removed.""" + Logger.log("d", "ZeroConf service removed: %s" % name) self.removedNetworkCluster.emit(str(name)) return True |