diff --git a/WifiConnection.py b/WifiConnection.py index 535971e840..996cb2e5ea 100644 --- a/WifiConnection.py +++ b/WifiConnection.py @@ -14,9 +14,10 @@ from UM.Logger import Logger i18n_catalog = i18nCatalog("cura") class WifiConnection(OutputDevice, SignalEmitter): - def __init__(self, address, info): - super().__init__(address) + def __init__(self, key, address, info): + super().__init__(key) self._address = address + self._key = key self._info = info self._http_lock = threading.Lock() self._http_connection = None @@ -30,14 +31,16 @@ class WifiConnection(OutputDevice, SignalEmitter): self._api_version = "1" self._api_prefix = "/api/v" + self._api_version + "/" - self.connect() - self.setName(address) + self.setName(key) self.setShortDescription(i18n_catalog.i18nc("@action:button", "Print with WIFI")) self.setDescription(i18n_catalog.i18nc("@info:tooltip", "Print with WIFI")) self.setIconName("print") connectionStateChanged = Signal() + def getKey(self): + return self._key + def isConnected(self): return self._is_connected @@ -54,12 +57,15 @@ class WifiConnection(OutputDevice, SignalEmitter): def _update(self): while self._thread: self.setConnectionState(True) - reply = self._httpGet("printer") - if reply.status_code == 200: - self._json_printer_state = reply.json() - if not self._is_connected: - self.setConnectionState(True) - else: + try: + reply = self._httpGet("printer") + if reply.status_code == 200: + self._json_printer_state = reply.json() + if not self._is_connected: + self.setConnectionState(True) + else: + self.setConnectionState(False) + except: self.setConnectionState(False) time.sleep(1) diff --git a/WifiOutputDevicePlugin.py b/WifiOutputDevicePlugin.py index f4294c094c..7b89002a43 100644 --- a/WifiOutputDevicePlugin.py +++ b/WifiOutputDevicePlugin.py @@ -3,6 +3,7 @@ from . import WifiConnection from zeroconf import Zeroconf, ServiceBrowser, ServiceStateChange from UM.Signal import Signal, SignalEmitter +from UM.Application import Application class WifiOutputDevicePlugin(OutputDevicePlugin, SignalEmitter): def __init__(self): @@ -11,7 +12,7 @@ class WifiOutputDevicePlugin(OutputDevicePlugin, SignalEmitter): self._browser = None self._connections = {} self.addConnectionSignal.connect(self.addConnection) #Because the model needs to be created in the same thread as the QMLEngine, we use a signal. - + Application.getInstance().getMachineManager().activeMachineInstanceChanged.connect(self._onActiveMachineInstanceChanged) addConnectionSignal = Signal() ## Start looking for devices on network. @@ -22,13 +23,23 @@ class WifiOutputDevicePlugin(OutputDevicePlugin, SignalEmitter): def stop(self): self._zero_conf.close() + def _onActiveMachineInstanceChanged(self): + active_machine_key = Application.getInstance().getMachineManager().getActiveMachineInstance().getKey() + for address in self._connections: + if self._connections[address].getKey() == active_machine_key: + self._connections[address].connect() + self._connections[address].connectionStateChanged.connect(self._onPrinterConnectionStateChanged) + else: + self._connections[address].close() + print("on active machine instance changed" , active_machine_key) + ## Because the model needs to be created in the same thread as the QMLEngine, we use a signal. def addConnection(self, name, address, properties): - if address == "10.180.1.30": #DEBUG - connection = WifiConnection.WifiConnection(address, properties) - connection.connect() - self._connections[address] = connection - connection.connectionStateChanged.connect(self._onPrinterConnectionStateChanged) + connection = WifiConnection.WifiConnection(name, address, properties) + self._connections[address] = connection + if connection.getKey() == Application.getInstance().getMachineManager().getActiveMachineInstance().getKey(): + self._connections[address].connect() + connection.connectionStateChanged.connect(self._onPrinterConnectionStateChanged) def _onPrinterConnectionStateChanged(self, address): if self._connections[address].isConnected():