Connection now correctly uses connection state

CURA-49
This commit is contained in:
Jaime van Kessel 2016-04-19 10:51:24 +02:00
parent 2a90c76cb8
commit 404ea89ff7
2 changed files with 27 additions and 16 deletions

View file

@ -3,7 +3,6 @@ import time
import requests import requests
from UM.i18n import i18nCatalog from UM.i18n import i18nCatalog
from UM.Signal import Signal
from UM.Application import Application from UM.Application import Application
from UM.Logger import Logger from UM.Logger import Logger
@ -11,7 +10,7 @@ from cura.PrinterOutputDevice import PrinterOutputDevice, ConnectionState
i18n_catalog = i18nCatalog("cura") i18n_catalog = i18nCatalog("cura")
## Network connected (wifi / lan) printer that uses the Ultimaker API
class NetworkPrinterOutputDevice(PrinterOutputDevice): class NetworkPrinterOutputDevice(PrinterOutputDevice):
def __init__(self, key, address, info): def __init__(self, key, address, info):
super().__init__(key) super().__init__(key)
@ -21,7 +20,6 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
self._http_lock = threading.Lock() self._http_lock = threading.Lock()
self._http_connection = None self._http_connection = None
self._file = None self._file = None
self._do_update = True
self._thread = None self._thread = None
self._json_printer_state = None self._json_printer_state = None
@ -37,16 +35,26 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
return self._key return self._key
def _update(self): 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: try:
reply = self._httpGet("printer") reply = self._httpGet("printer")
if reply.status_code == 200: if reply.status_code == 200:
self._json_printer_state = reply.json() 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: else:
self.setConnectionState(ConnectionState.error) self.setConnectionState(ConnectionState.error)
except: except:
self.setConnectionState(ConnectionState.error) self.setConnectionState(ConnectionState.error)
time.sleep(1) # Poll every second for printer state. 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): def close(self):
self._connection_state == ConnectionState.closed self._connection_state == ConnectionState.closed
@ -56,10 +64,13 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
self._file = getattr(Application.getInstance().getController().getScene(), "gcode_list") self._file = getattr(Application.getInstance().getController().getScene(), "gcode_list")
self.startPrint() self.startPrint()
def isConnected(self):
return self._connection_state != ConnectionState.closed and self._connection_state != ConnectionState.error
## Start the polling thread. ## Start the polling thread.
def connect(self): def connect(self):
if self._thread is None: if self._thread is None:
self._do_update = True self.setConnectionState(ConnectionState.connecting)
self._thread = threading.Thread(target = self._update) self._thread = threading.Thread(target = self._update)
self._thread.daemon = True self._thread.daemon = True
self._thread.start() self._thread.start()

View file

@ -29,26 +29,26 @@ class WifiOutputDevicePlugin(OutputDevicePlugin, SignalEmitter):
def _onActiveMachineInstanceChanged(self): def _onActiveMachineInstanceChanged(self):
active_machine_key = Application.getInstance().getMachineManager().getActiveMachineInstance().getKey() active_machine_key = Application.getInstance().getMachineManager().getActiveMachineInstance().getKey()
for address in self._printers: for key in self._printers:
if self._printers[address].getKey() == active_machine_key: if key == active_machine_key:
self._printers[address].connect() self._printers[key].connect()
self._printers[address].connectionStateChanged.connect(self._onPrinterConnectionStateChanged) self._printers[key].connectionStateChanged.connect(self._onPrinterConnectionStateChanged)
else: 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. ## Because the model needs to be created in the same thread as the QMLEngine, we use a signal.
def addPrinter(self, name, address, properties): def addPrinter(self, name, address, properties):
printer = NetworkPrinterOutputDevice.NetworkPrinterOutputDevice(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(): if printer.getKey() == Application.getInstance().getMachineManager().getActiveMachineInstance().getKey():
self._printers[address].connect() self._printers[printer.getKey()].connect()
printer.connectionStateChanged.connect(self._onPrinterConnectionStateChanged) printer.connectionStateChanged.connect(self._onPrinterConnectionStateChanged)
def _onPrinterConnectionStateChanged(self, address): def _onPrinterConnectionStateChanged(self, key):
if self._printers[address].isConnected(): if self._printers[key].isConnected():
self.getOutputDeviceManager().addOutputDevice(self._printers[address]) self.getOutputDeviceManager().addOutputDevice(self._printers[key])
else: else:
self.getOutputDeviceManager().removeOutputDevice(self._printers[address]) self.getOutputDeviceManager().removeOutputDevice(self._printers[key])
def removePrinter(self): def removePrinter(self):
pass pass