Speed up Bonjour discovery and make it slightly more robust

CURA-2035
This commit is contained in:
fieldOfView 2016-08-09 13:49:47 +02:00
parent 5f51804ba6
commit a649bdcb32

View file

@ -1,11 +1,12 @@
from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin
from . import NetworkPrinterOutputDevice from . import NetworkPrinterOutputDevice
from zeroconf import Zeroconf, ServiceBrowser, ServiceStateChange from zeroconf import Zeroconf, ServiceBrowser, ServiceStateChange, ServiceInfo
from UM.Logger import Logger from UM.Logger import Logger
from UM.Signal import Signal, signalemitter from UM.Signal import Signal, signalemitter
from UM.Application import Application from UM.Application import Application
import time
## This plugin handles the connection detection & creation of output device objects for the UM3 printer. ## This plugin handles the connection detection & creation of output device objects for the UM3 printer.
# Zero-Conf is used to detect printers, which are saved in a dict. # Zero-Conf is used to detect printers, which are saved in a dict.
@ -79,7 +80,20 @@ class NetworkPrinterOutputDevicePlugin(OutputDevicePlugin):
def _onServiceChanged(self, zeroconf, service_type, name, state_change): def _onServiceChanged(self, zeroconf, service_type, name, state_change):
if state_change == ServiceStateChange.Added: if state_change == ServiceStateChange.Added:
Logger.log("d", "Bonjour service added: %s" % name) Logger.log("d", "Bonjour service added: %s" % name)
info = ServiceInfo(service_type, name, properties = {})
for record in zeroconf.cache.entries_with_name(name.lower()):
info.update_record(zeroconf, time.time(), record)
for record in zeroconf.cache.entries_with_name(info.server):
info.update_record(zeroconf, time.time(), record)
if info.address:
break
if not info.address:
Logger.log("d", "Trying to get address of %s", name)
info = zeroconf.get_service_info(service_type, name) info = zeroconf.get_service_info(service_type, name)
if info: if info:
if info.properties.get(b"type", None) == b'printer': if info.properties.get(b"type", None) == b'printer':
address = '.'.join(map(lambda n: str(n), info.address)) address = '.'.join(map(lambda n: str(n), info.address))