- stateReply = self._request('GET', '/d3dapi/printer/state')
- if stateReply is None: #No API, wait 15 seconds before looking for Doodle3D again.
- self._isAvailable = False
- time.sleep(15)
- continue
- if not stateReply: #API gave back an error (this can happen if the Doodle3D box is connecting to the printer)
+ while self._host is None:
+ printerList = self._request('GET', self.PRINTER_LIST_PATH, host=self.PRINTER_LIST_HOST)
+ 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']:
+ status = self._request('GET', '/d3dapi/info/status', host=possiblePrinter['localip'])
+ if status and 'data' in status and status['data']['state'] == 'idle':
+ self._host = possiblePrinter['localip']
+ break
+
+ if self._host is None:
+ #If we cannot find a doodle3d box, delay a minute and request the list again.
+ # This so we do not stress the connect.doodle3d.com api too much
+ time.sleep(60)
+
+ stateReply = self._request('GET', '/d3dapi/info/status')
+ if stateReply is None or not stateReply or stateReply['data']['state'] == 'disconnected':
+ # No API, wait 5 seconds before looking for Doodle3D again.
+ # API gave back an error (this can happen if the Doodle3D box is connecting to the printer)
+ # Or no printer connected
+ self._host = None