NetworkPrinterOutputDevice now uses reworked printerOutputDevice API

CURA-49
This commit is contained in:
Jaime van Kessel 2016-04-18 16:16:59 +02:00
parent 10b39c5ca4
commit 3bd25e5f7f
2 changed files with 14 additions and 34 deletions

View file

@ -1,19 +1,18 @@
from UM.OutputDevice.OutputDevice import OutputDevice
from UM.OutputDevice import OutputDeviceError
import threading import threading
import json
import time import time
import base64
import requests import requests
from . import HttpUploadDataStream
from UM.i18n import i18nCatalog from UM.i18n import i18nCatalog
from UM.Signal import Signal, SignalEmitter 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
from cura.PrinterOutputDevice import PrinterOutputDevice, ConnectionState
i18n_catalog = i18nCatalog("cura") i18n_catalog = i18nCatalog("cura")
class WifiConnection(OutputDevice, SignalEmitter):
class NetworkPrinterOutputDevice(PrinterOutputDevice):
def __init__(self, key, address, info): def __init__(self, key, address, info):
super().__init__(key) super().__init__(key)
self._address = address self._address = address
@ -27,8 +26,6 @@ class WifiConnection(OutputDevice, SignalEmitter):
self._json_printer_state = None self._json_printer_state = None
self._is_connected = False
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.setName(key) self.setName(key)
@ -36,38 +33,20 @@ class WifiConnection(OutputDevice, SignalEmitter):
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()
def getKey(self): def getKey(self):
return self._key return self._key
def isConnected(self):
return self._is_connected
## Set the connection state of this connection.
# Although we use a restfull API, we do poll the api to check if the machine is still responding.
def setConnectionState(self, state):
if state != self._is_connected:
Logger.log("i", "setting connection state of %s to %s " %(self._address, state))
self._is_connected = state
self.connectionStateChanged.emit(self._address)
else:
self._is_connected = state
def _update(self): def _update(self):
while self._thread: while self._connection_state == ConnectionState.connected or self._connection_state == ConnectionState.busy:
self.setConnectionState(True)
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 not self._is_connected:
self.setConnectionState(True)
else: else:
self.setConnectionState(False) self.setConnectionState(ConnectionState.error)
except: except:
self.setConnectionState(False) self.setConnectionState(ConnectionState.error)
time.sleep(1) time.sleep(1) # Poll every second for printer state.
def close(self): def close(self):
self._do_update = False self._do_update = False

View file

@ -1,5 +1,5 @@
from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin
from . import WifiConnection from . import NetworkPrinterOutputDevice
from zeroconf import Zeroconf, ServiceBrowser, ServiceStateChange from zeroconf import Zeroconf, ServiceBrowser, ServiceStateChange
from UM.Signal import Signal, SignalEmitter from UM.Signal import Signal, SignalEmitter
@ -35,7 +35,7 @@ class WifiOutputDevicePlugin(OutputDevicePlugin, SignalEmitter):
## 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):
connection = WifiConnection.WifiConnection(name, address, properties) connection = NetworkPrinterOutputDevice.NetworkPrinterOutputDevice(name, address, properties)
self._connections[address] = connection self._connections[address] = connection
if connection.getKey() == Application.getInstance().getMachineManager().getActiveMachineInstance().getKey(): if connection.getKey() == Application.getInstance().getMachineManager().getActiveMachineInstance().getKey():
self._connections[address].connect() self._connections[address].connect()
@ -60,4 +60,5 @@ class WifiOutputDevicePlugin(OutputDevicePlugin, SignalEmitter):
elif state_change == ServiceStateChange.Removed: elif state_change == ServiceStateChange.Removed:
info = zeroconf.get_service_info(service_type, name) info = zeroconf.get_service_info(service_type, name)
if info:
address = '.'.join(map(lambda n: str(n), info.address)) address = '.'.join(map(lambda n: str(n), info.address))