Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if not info.address:
Logger.log("d", "Trying to get address of %s", name)
info = zeroconf.get_service_info(service_type, name)
if info:
type_of_device = info.properties.get(b"type", None)
if type_of_device:
if type_of_device == b"printer":
address = '.'.join(map(lambda n: str(n), info.address))
self.addPrinterSignal.emit(str(name), address, info.properties)
else:
Logger.log("w", "The type of the found device is '%s', not 'printer'! Ignoring.." % type_of_device )
else:
Logger.log("w", "Could not get information about %s" % name)
elif state_change == ServiceStateChange.Removed:
Logger.log("d", "Bonjour service removed: %s" % name)
self.removePrinterSignal.emit(str(name))
# Request more data if info is not complete
if not info.address or not info.port:
Logger.log("d", "Trying to get address of %s", name)
info = zeroconf.get_service_info(service_type, key)
if not info:
Logger.log("w", "Could not get information about %s" % name)
return
if info.address and info.port:
address = '.'.join(map(lambda n: str(n), info.address))
self.addInstanceSignal.emit(name, address, info.port, info.properties)
else:
Logger.log("d", "Discovered instance named %s but received no address", name)
elif state_change == ServiceStateChange.Removed:
self.removeInstanceSignal.emit(str(name))
info = zero_conf.get_service_info(service_type, name)
if info:
type_of_device = info.properties.get(b"type", None)
if type_of_device:
if type_of_device == b"printer":
address = '.'.join(map(lambda n: str(n), info.address))
self.addDeviceSignal.emit(str(name), address, info.properties)
else:
Logger.log("w",
"The type of the found device is '%s', not 'printer'! Ignoring.." % type_of_device)
else:
Logger.log("w", "Could not get information about %s" % name)
return False
elif state_change == ServiceStateChange.Removed:
Logger.log("d", "Bonjour service removed: %s" % name)
self.removeDeviceSignal.emit(str(name))
return True
if info.address:
break
# Request more data if info is not complete
if not info.address:
Logger.log("d", "Trying to get address of %s", name)
info = zeroconf.get_service_info(service_type, name)
if info:
if info.properties.get(b"type", None) == b'printer':
address = '.'.join(map(lambda n: str(n), info.address))
self.addPrinterSignal.emit(str(name), address, info.properties)
else:
Logger.log("w", "Could not get information about %s" % name)
elif state_change == ServiceStateChange.Removed:
Logger.log("d", "Bonjour service removed: %s" % name)
self.removePrinterSignal.emit(str(name))
if info:
type_of_device = info.properties.get(b"type", None)
if type_of_device:
if type_of_device == b"printer":
address = '.'.join(map(lambda n: str(n), info.address))
if address in self._excluded_addresses:
Logger.log("d", "The IP address %s of the printer \'%s\' is not correct. Trying to reconnect.", address, name)
return False # When getting the localhost IP, then try to reconnect
self.addPrinterSignal.emit(str(name), address, info.properties)
else:
Logger.log("w", "The type of the found device is '%s', not 'printer'! Ignoring.." % type_of_device )
else:
Logger.log("w", "Could not get information about %s" % name)
return False
elif state_change == ServiceStateChange.Removed:
Logger.log("d", "Bonjour service removed: %s" % name)
self.removePrinterSignal.emit(str(name))
return True
info = zero_conf.get_service_info(service_type, name)
if info:
type_of_device = info.properties.get(b"type", None)
if type_of_device:
if type_of_device == b"printer":
address = '.'.join(map(lambda n: str(n), info.address))
self.addDeviceSignal.emit(str(name), address, info.properties)
else:
Logger.log("w",
"The type of the found device is '%s', not 'printer'! Ignoring.." % type_of_device)
else:
Logger.log("w", "Could not get information about %s" % name)
return False
elif state_change == ServiceStateChange.Removed:
Logger.log("d", "Bonjour service removed: %s" % name)
self.removeDeviceSignal.emit(str(name))
return True
def find_service(self, zeroconf, service_type, name, state_change):
"""Check for published MQTT. If it finds port 1883 of type '_mqtt', update broker name."""
# Get the service we want (port 1883 and type '_mqtt._tcp.local.'
info = zeroconf.get_service_info(service_type, name)
if not (info.port == 1883 and service_type == "_mqtt._tcp.local."):
return
if state_change is ServiceStateChange.Added:
# If this is our service, update mqtt broker name and ip on self.comm (Communicator)
self.comm.broker_name = info.server
self.comm.broker_ip = str(socket.inet_ntoa(info.address))
elif state_change is ServiceStateChange.Removed:
# Implement way to handle removed MQTT service
# It only makes sense if leave zeroconf connection opened. It could be interesting.
pass
def _onServiceChanged(self, zero_conf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange
) -> bool:
if state_change == ServiceStateChange.Added:
return self._onServiceAdded(zero_conf, service_type, name)
elif state_change == ServiceStateChange.Removed:
return self._onServiceRemoved(name)
return True