diff --git a/NetworkPrinterOutputDevice.py b/NetworkPrinterOutputDevice.py index e67826eff5..bd6e8f77f8 100644 --- a/NetworkPrinterOutputDevice.py +++ b/NetworkPrinterOutputDevice.py @@ -3,7 +3,6 @@ import time import requests from UM.i18n import i18nCatalog -from UM.Signal import Signal from UM.Application import Application from UM.Logger import Logger @@ -11,7 +10,7 @@ from cura.PrinterOutputDevice import PrinterOutputDevice, ConnectionState i18n_catalog = i18nCatalog("cura") - +## Network connected (wifi / lan) printer that uses the Ultimaker API class NetworkPrinterOutputDevice(PrinterOutputDevice): def __init__(self, key, address, info): super().__init__(key) @@ -21,7 +20,6 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice): self._http_lock = threading.Lock() self._http_connection = None self._file = None - self._do_update = True self._thread = None self._json_printer_state = None @@ -37,16 +35,26 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice): return self._key def _update(self): - while self._connection_state == ConnectionState.connected or self._connection_state == ConnectionState.busy: + Logger.log("d", "Update thread of printer with key %s and ip %s started", self._key, self._address) + while self.isConnected(): try: reply = self._httpGet("printer") if reply.status_code == 200: self._json_printer_state = reply.json() + if self._connection_state == ConnectionState.connecting: + # First successful response, so we are now "connected" + self.setConnectionState(ConnectionState.connected) else: self.setConnectionState(ConnectionState.error) except: self.setConnectionState(ConnectionState.error) time.sleep(1) # Poll every second for printer state. + Logger.log("d", "Update thread of printer with key %s and ip %s stopped", self._key, self._address) + + ## Convenience function that gets information from the recieved json data and converts it to the right internal + # values / variables + def _spliceJsonData(self): + pass def close(self): self._connection_state == ConnectionState.closed @@ -56,10 +64,13 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice): self._file = getattr(Application.getInstance().getController().getScene(), "gcode_list") self.startPrint() + def isConnected(self): + return self._connection_state != ConnectionState.closed and self._connection_state != ConnectionState.error + ## Start the polling thread. def connect(self): if self._thread is None: - self._do_update = True + self.setConnectionState(ConnectionState.connecting) self._thread = threading.Thread(target = self._update) self._thread.daemon = True self._thread.start() diff --git a/NetworkPrinterOutputDevicePlugin.py b/NetworkPrinterOutputDevicePlugin.py index 84f8b89097..cd8d6a3742 100644 --- a/NetworkPrinterOutputDevicePlugin.py +++ b/NetworkPrinterOutputDevicePlugin.py @@ -29,26 +29,26 @@ class WifiOutputDevicePlugin(OutputDevicePlugin, SignalEmitter): def _onActiveMachineInstanceChanged(self): active_machine_key = Application.getInstance().getMachineManager().getActiveMachineInstance().getKey() - for address in self._printers: - if self._printers[address].getKey() == active_machine_key: - self._printers[address].connect() - self._printers[address].connectionStateChanged.connect(self._onPrinterConnectionStateChanged) + for key in self._printers: + if key == active_machine_key: + self._printers[key].connect() + self._printers[key].connectionStateChanged.connect(self._onPrinterConnectionStateChanged) else: - self._printers[address].close() + self._printers[key].close() ## Because the model needs to be created in the same thread as the QMLEngine, we use a signal. def addPrinter(self, name, address, properties): printer = NetworkPrinterOutputDevice.NetworkPrinterOutputDevice(name, address, properties) - self._printers[address] = printer + self._printers[printer.getKey()] = printer if printer.getKey() == Application.getInstance().getMachineManager().getActiveMachineInstance().getKey(): - self._printers[address].connect() + self._printers[printer.getKey()].connect() printer.connectionStateChanged.connect(self._onPrinterConnectionStateChanged) - def _onPrinterConnectionStateChanged(self, address): - if self._printers[address].isConnected(): - self.getOutputDeviceManager().addOutputDevice(self._printers[address]) + def _onPrinterConnectionStateChanged(self, key): + if self._printers[key].isConnected(): + self.getOutputDeviceManager().addOutputDevice(self._printers[key]) else: - self.getOutputDeviceManager().removeOutputDevice(self._printers[address]) + self.getOutputDeviceManager().removeOutputDevice(self._printers[key]) def removePrinter(self): pass