From fe212fdba34423d11a47f150c8e2775159fb0ce1 Mon Sep 17 00:00:00 2001 From: daid Date: Tue, 26 Nov 2013 20:39:27 +0100 Subject: [PATCH] Updated printer connections. Not used, but getting closer. --- .../util/printerConnection/doodle3dConnect.py | 10 ++- .../util/printerConnection/dummyConnection.py | 79 +++++++++++++++++++ .../printerConnectionBase.py | 5 +- .../printerConnectionManager.py | 35 ++++++++ 4 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 Cura/util/printerConnection/dummyConnection.py create mode 100644 Cura/util/printerConnection/printerConnectionManager.py diff --git a/Cura/util/printerConnection/doodle3dConnect.py b/Cura/util/printerConnection/doodle3dConnect.py index a0eb4353..ada27fe3 100644 --- a/Cura/util/printerConnection/doodle3dConnect.py +++ b/Cura/util/printerConnection/doodle3dConnect.py @@ -79,7 +79,7 @@ class doodle3dConnect(printerConnectionBase.printerConnectionBase): def printProgress(self): if self._lineCount < 1: return 0.0 - return float(d._progressLine) / float(d._lineCount) + return float(self._progressLine) / float(self._lineCount) # Return if the printer with this connection type is available def isAvailable(self): @@ -88,6 +88,8 @@ class doodle3dConnect(printerConnectionBase.printerConnectionBase): # Get the connection status string. This is displayed to the user and can be used to communicate # various information to the user. def getStatusString(self): + if not self._isAvailable: + return "Doodle3D box not found" return "TODO" def _doodle3Dthread(self): @@ -95,13 +97,17 @@ class doodle3dConnect(printerConnectionBase.printerConnectionBase): stateReply = self._request('GET', '/d3dapi/printer/state') if stateReply is None: #No API, wait 15 seconds before looking for Doodle3D again. self._isAvailable = False + self._doCallback() time.sleep(15) continue if not stateReply: #API gave back an error (this can happen if the Doodle3D box is connecting to the printer) self._isAvailable = False + self._doCallback() time.sleep(5) continue - self._isAvailable = True + if not self._isAvailable: + self._isAvailable = True + self._doCallback() if stateReply['data']['state'] == 'idle': if self._printing: diff --git a/Cura/util/printerConnection/dummyConnection.py b/Cura/util/printerConnection/dummyConnection.py new file mode 100644 index 00000000..e9bc14eb --- /dev/null +++ b/Cura/util/printerConnection/dummyConnection.py @@ -0,0 +1,79 @@ +__copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License" + +import threading +import json +import httplib as httpclient +import urllib +import time + +from Cura.util.printerConnection import printerConnectionBase + +#Dummy printer class which is always +class dummyConnection(printerConnectionBase.printerConnectionBase): + def __init__(self): + super(dummyConnection, self).__init__() + + self._printing = False + self._lineCount = 0 + self._progressLine = 0 + + self.printThread = threading.Thread(target=self._dummyThread) + self.printThread.daemon = True + self.printThread.start() + + #Load the file into memory for printing. + def loadFile(self, filename): + if self._printing: + return + self._lineCount = 0 + f = open(filename, "r") + for line in f: + #Strip out comments, we do not need to send comments + if ';' in line: + line = line[:line.index(';')] + #Strip out whitespace at the beginning/end this saves data to send. + line = line.strip() + + if len(line) < 1: + continue + self._lineCount += 1 + + #Start printing the previously loaded file + def startPrint(self): + if self._printing or self._lineCount < 1: + return + self._progressLine = 0 + self._printing = True + + #Abort the previously loaded print file + def cancelPrint(self): + self._printing = False + + def isPrinting(self): + return self._printing + + #Amount of progression of the current print file. 0.0 to 1.0 + def printProgress(self): + if self._lineCount < 1: + return 0.0 + return float(self._progressLine) / float(self._lineCount) + + # Return if the printer with this connection type is available + def isAvailable(self): + return True + + # Get the connection status string. This is displayed to the user and can be used to communicate + # various information to the user. + def getStatusString(self): + return "DUMMY!" + + def _dummyThread(self): + while True: + if not self._printing: + time.sleep(5) + else: + time.sleep(0.01) + self._progressLine += 1 + if self._progressLine == self._lineCount: + self._printing = False + self._doCallback() diff --git a/Cura/util/printerConnection/printerConnectionBase.py b/Cura/util/printerConnection/printerConnectionBase.py index 9e0a7941..5503d004 100644 --- a/Cura/util/printerConnection/printerConnectionBase.py +++ b/Cura/util/printerConnection/printerConnectionBase.py @@ -65,10 +65,11 @@ class printerConnectionBase(object): def sendCommand(self, command): pass - # Return if the printer with this connection type is available + # Return if the printer with this connection type is available for possible printing right now. + # It is used to auto-detect which connection should default to the print button. # This means the printer is detected, but no connection has been made yet. # Example: COM port is detected, but no connection has been made. - # Example: WiFi box is detected + # Example: WiFi box is detected and is ready to print with a printer connected def isAvailable(self): return False diff --git a/Cura/util/printerConnection/printerConnectionManager.py b/Cura/util/printerConnection/printerConnectionManager.py new file mode 100644 index 00000000..55afb244 --- /dev/null +++ b/Cura/util/printerConnection/printerConnectionManager.py @@ -0,0 +1,35 @@ +__copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License" + +from Cura.util import version +from Cura.util.printerConnection import dummyConnection +from Cura.util.printerConnection import doodle3dConnect + +class connectionEntry(object): + def __init__(self, name, priority, icon, connection): + self.name = name + self.priority = priority + self.icon = icon + self.connection = connection + + def __cmp__(self, other): + return self.priority - other.priority + + def __repr__(self): + return self.name + +class PrinterConnectionManager(object): + def __init__(self): + self._connectionList = [] + if version.isDevVersion(): + self._connectionList.append(connectionEntry('Dummy', -1, 5, dummyConnection.dummyConnection())) + self._connectionList.append(connectionEntry('Doodle3D', 100, 27, doodle3dConnect.doodle3dConnect())) + + #Sort the connections by highest priority first. + self._connectionList.sort(reverse=True) + + #Return the highest priority available connection. + def getAvailableConnection(self): + for e in self._connectionList: + if e.connection.isAvailable(): + return e + return None -- 2.30.2