1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
5 import httplib as httpclient
9 from Cura.util.printerConnection import printerConnectionBase
11 #Class to connect and print files with the doodle3d.com wifi box
12 # Auto-detects if the Doodle3D box is available with a printer
13 class doodle3dConnect(printerConnectionBase.printerConnectionBase):
15 super(doodle3dConnect, self).__init__()
18 self._isAvailable = False
19 self._printing = False
21 self._blockIndex = None
23 self._progressLine = 0
24 self._hotendTemperature = [0] * 4
25 self._bedTemperature = 0
27 self.checkThread = threading.Thread(target=self._doodle3Dthread)
28 self.checkThread.daemon = True
29 self.checkThread.start()
31 #Load the file into memory for printing.
32 def loadFile(self, filename):
39 f = open(filename, "r")
42 line = line[:line.index(';')]
48 if blockSize + len(line) > 2048:
49 self._fileBlocks.append('\n'.join(block) + '\n')
52 blockSize += len(line) + 1
54 self._fileBlocks.append('\n'.join(block) + '\n')
57 #Start printing the previously loaded file
59 if self._printing or len(self._fileBlocks) < 1:
61 self._progressLine = 0
65 #Abort the previously loaded print file
66 def cancelPrint(self):
67 if not self._printing:
69 if self._request('POST', '/d3dapi/printer/stop', {'gcode': 'M104 S0\nG28'}):
70 self._printing = False
75 # Return if the printer with this connection type is available
76 def isAvailable(self):
77 return self._isAvailable
79 # Get the connection status string. This is displayed to the user and can be used to communicate
80 # various information to the user.
81 def getStatusString(self):
84 def _doodle3Dthread(self):
86 stateReply = self._request('GET', '/d3dapi/printer/state')
87 if stateReply is None: #No API, wait 15 seconds before looking for Doodle3D again.
88 self._isAvailable = False
91 if not stateReply: #API gave back an error (this can happen if the Doodle3D box is connecting to the printer)
92 self._isAvailable = False
95 self._isAvailable = True
97 if stateReply['data']['state'] == 'idle':
99 if self._blockIndex < len(self._fileBlocks):
100 if self._request('POST', '/d3dapi/printer/print', {'gcode': self._fileBlocks[self._blockIndex], 'start': 'True', 'first': 'True'}):
101 self._blockIndex += 1
103 self._printing = False
104 if stateReply['data']['state'] == 'printing':
106 if self._blockIndex < len(self._fileBlocks):
107 for n in xrange(0, 5):
108 if self._blockIndex < len(self._fileBlocks):
109 if self._request('POST', '/d3dapi/printer/print', {'gcode': self._fileBlocks[self._blockIndex]}):
110 self._blockIndex += 1
112 #If we are no longer sending new GCode delay a bit so we request the status less often.
114 progress = self._request('GET', '/d3dapi/printer/progress')
116 self._progressLine = progress['data']['current_line']
117 temperature = self._request('GET', '/d3dapi/printer/temperature')
119 self._hotendTemperature[0] = temperature['data']['hotend']
120 self._bedTemperature = temperature['data']['bed']
122 #Got a printing state without us having send the print file, set the state to printing, but make sure we never send anything.
123 progress = self._request('GET', '/d3dapi/printer/progress')
125 self._printing = True
126 self._blockIndex = len(self._fileBlocks)
127 self._lineCount = progress['data']['total_lines']
129 def _request(self, method, path, postData = None):
130 if self._http is None:
131 self._http = httpclient.HTTPConnection('draw.doodle3d.com')
133 if postData is not None:
134 self._http.request(method, path, urllib.urlencode(postData), {"Content-type": "application/x-www-form-urlencoded"})
136 self._http.request(method, path, headers={"Content-type": "application/x-www-form-urlencoded"})
141 response = self._http.getresponse()
142 responseText = response.read()
147 response = json.loads(responseText)
151 if response['status'] != 'success':
156 if __name__ == '__main__':
157 d = doodle3dConnect()
158 print 'Searching for Doodle3D box'
159 while not d.isAvailable():
162 while d.isPrinting():
163 print 'Doodle3D already printing! Requesting stop!'
167 print 'Doodle3D box found, printing!'
168 d.loadFile("C:/Models/belt-tensioner-wave_export.gcode")
170 while d.isPrinting() and d.isAvailable():
172 print d._progressLine, d._lineCount, d._blockIndex, len(d._fileBlocks)