+ super(doodle3dConnectionGroup, self).__init__("Doodle3D")
+ self._http = None
+ self._host = self.PRINTER_LIST_HOST
+ self._connectionMap = {}
+
+ self._thread = threading.Thread(target=self._doodle3DThread)
+ self._thread.daemon = True
+ self._thread.start()
+
+ def getAvailableConnections(self):
+ return filter(lambda c: c.isAvailable(), self._connectionMap.values())
+
+ def getIconID(self):
+ return 27
+
+ def getPriority(self):
+ return 100
+
+ def __cmp__(self, other):
+ return self.getPriority() - other.getPriority()
+
+ def __repr__(self):
+ return self.name
+
+ def _doodle3DThread(self):
+ self._waitDelay = 0
+ while True:
+ printerList = self._request('GET', self.PRINTER_LIST_PATH)
+ if not printerList or type(printerList) is not dict or 'data' not in printerList or type(printerList['data']) is not list:
+ #Check if we are connected to the Doodle3D box in access point mode, as this gives an
+ # invalid reply on the printer list API
+ printerList = {'data': [{'localip': 'draw.doodle3d.com'}]}
+
+ #Add the 192.168.5.1 IP to the list of printers to check, as this is the LAN port IP, which could also be available.
+ # (connect.doodle3d.com also checks for this IP in the javascript code)
+ printerList['data'].append({'localip': '192.168.5.1'})
+
+ #Check the status of each possible IP, if we find a valid box with a printer connected. Use that IP.
+ for possiblePrinter in printerList['data']:
+ if possiblePrinter['localip'] not in self._connectionMap:
+ status = self._request('GET', '/d3dapi/config/?network.cl.wifiboxid=', host=possiblePrinter['localip'])
+ if status and 'data' in status and 'network.cl.wifiboxid' in status['data']:
+ self._connectionMap[possiblePrinter['localip']] = doodle3dConnect(possiblePrinter['localip'], status['data']['network.cl.wifiboxid'], self)
+
+ # Delay a bit more after every request. This so we do not stress the connect.doodle3d.com api too much
+ if self._waitDelay < 10:
+ self._waitDelay += 1
+ time.sleep(self._waitDelay * 60)
+
+ def _request(self, method, path, postData = None, host = None):
+ if host is None:
+ host = self._host
+ if self._http is None or self._http.host != host:
+ self._http = httpclient.HTTPConnection(host, timeout=30)
+
+ try:
+ if postData is not None:
+ self._http.request(method, path, urllib.urlencode(postData), {"Content-type": "application/x-www-form-urlencoded", "User-Agent": "Cura Doodle3D connection"})
+ else:
+ self._http.request(method, path, headers={"Content-type": "application/x-www-form-urlencoded", "User-Agent": "Cura Doodle3D connection"})
+ except:
+ self._http.close()
+ return None
+ try:
+ response = self._http.getresponse()
+ responseText = response.read()
+ except:
+ self._http.close()
+ return None
+ try:
+ response = json.loads(responseText)
+ except ValueError:
+ self._http.close()
+ return None
+ if response['status'] != 'success':
+ return False
+
+ return response
+
+#Class to connect and print files with the doodle3d.com wifi box
+# Auto-detects if the Doodle3D box is available with a printer
+class doodle3dConnect(printerConnectionBase.printerConnectionBase):
+ def __init__(self, host, name, group):
+ super(doodle3dConnect, self).__init__(name)