chiark / gitweb /
Updated printer connections. Not used, but getting closer.
authordaid <daid303@gmail.com>
Tue, 26 Nov 2013 19:39:27 +0000 (20:39 +0100)
committerdaid <daid303@gmail.com>
Tue, 26 Nov 2013 19:39:27 +0000 (20:39 +0100)
Cura/util/printerConnection/doodle3dConnect.py
Cura/util/printerConnection/dummyConnection.py [new file with mode: 0644]
Cura/util/printerConnection/printerConnectionBase.py
Cura/util/printerConnection/printerConnectionManager.py [new file with mode: 0644]

index a0eb4353c917b46bf2644358a5f209be65bfa3fe..ada27fe33935ba94924206f4aa9dbb5f8d18db4d 100644 (file)
@@ -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 (file)
index 0000000..e9bc14e
--- /dev/null
@@ -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()
index 9e0a7941c32e44c9b3d5d82e03819eceb807a811..5503d004fc6187f366c80d307e2e51a666168c51 100644 (file)
@@ -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 (file)
index 0000000..55afb24
--- /dev/null
@@ -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