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") i18n_catalog = i18nCatalog("cura")
class WifiConnection(OutputDevice, SignalEmitter): class WifiConnection(OutputDevice, SignalEmitter):
def __init__(self, address, info): def __init__(self, key, address, info):
super().__init__(address) super().__init__(key)
self._address = address self._address = address
self._key = key
self._info = info self._info = info
self._http_lock = threading.Lock() self._http_lock = threading.Lock()
self._http_connection = None self._http_connection = None
@ -30,14 +31,16 @@ class WifiConnection(OutputDevice, SignalEmitter):
self._api_version = "1" self._api_version = "1"
self._api_prefix = "/api/v" + self._api_version + "/" self._api_prefix = "/api/v" + self._api_version + "/"
self.connect() self.setName(key)
self.setName(address)
self.setShortDescription(i18n_catalog.i18nc("@action:button", "Print with WIFI")) self.setShortDescription(i18n_catalog.i18nc("@action:button", "Print with WIFI"))
self.setDescription(i18n_catalog.i18nc("@info:tooltip", "Print with WIFI")) self.setDescription(i18n_catalog.i18nc("@info:tooltip", "Print with WIFI"))
self.setIconName("print") self.setIconName("print")
connectionStateChanged = Signal() connectionStateChanged = Signal()
def getKey(self):
return self._key
def isConnected(self): def isConnected(self):
return self._is_connected return self._is_connected
@ -54,6 +57,7 @@ class WifiConnection(OutputDevice, SignalEmitter):
def _update(self): def _update(self):
while self._thread: while self._thread:
self.setConnectionState(True) self.setConnectionState(True)
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()
@ -61,6 +65,8 @@ class WifiConnection(OutputDevice, SignalEmitter):
self.setConnectionState(True) self.setConnectionState(True)
else: else:
self.setConnectionState(False) self.setConnectionState(False)
except:
self.setConnectionState(False)
time.sleep(1) time.sleep(1)
def close(self): def close(self):

View file

@ -3,6 +3,7 @@ from . import WifiConnection
from zeroconf import Zeroconf, ServiceBrowser, ServiceStateChange from zeroconf import Zeroconf, ServiceBrowser, ServiceStateChange
from UM.Signal import Signal, SignalEmitter from UM.Signal import Signal, SignalEmitter
from UM.Application import Application
class WifiOutputDevicePlugin(OutputDevicePlugin, SignalEmitter): class WifiOutputDevicePlugin(OutputDevicePlugin, SignalEmitter):
def __init__(self): def __init__(self):
@ -11,7 +12,7 @@ class WifiOutputDevicePlugin(OutputDevicePlugin, SignalEmitter):
self._browser = None self._browser = None
self._connections = {} 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. 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() addConnectionSignal = Signal()
## Start looking for devices on network. ## Start looking for devices on network.
@ -22,12 +23,22 @@ class WifiOutputDevicePlugin(OutputDevicePlugin, SignalEmitter):
def stop(self): def stop(self):
self._zero_conf.close() 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. ## Because the model needs to be created in the same thread as the QMLEngine, we use a signal.
def addConnection(self, name, address, properties): def addConnection(self, name, address, properties):
if address == "10.180.1.30": #DEBUG connection = WifiConnection.WifiConnection(name, address, properties)
connection = WifiConnection.WifiConnection(address, properties)
connection.connect()
self._connections[address] = connection self._connections[address] = connection
if connection.getKey() == Application.getInstance().getMachineManager().getActiveMachineInstance().getKey():
self._connections[address].connect()
connection.connectionStateChanged.connect(self._onPrinterConnectionStateChanged) connection.connectionStateChanged.connect(self._onPrinterConnectionStateChanged)
def _onPrinterConnectionStateChanged(self, address): def _onPrinterConnectionStateChanged(self, address):