Added keys to wificonnection so they can be linked to machine instances

CURA-49
This commit is contained in:
Jaime van Kessel 2016-02-11 15:51:40 +01:00
parent 69a9ef4a64
commit 10b39c5ca4
2 changed files with 33 additions and 16 deletions

View file

@ -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)

View file

@ -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():