def __init__(self, key, name, scheme, host, port, path, apiKey, group): super(OctoPrintPrinterConnection, self).__init__(name) print "{} Connection created: {}://{}:{}{}".format( name, scheme, host, port, path) self._group = group self._key = key self._httpClient = OctoPrintHttpClient(scheme, host, port, path, "json") if apiKey: self.setApiKey(apiKey) self._isAvailable = False self._printing = False self._jobFileName = None self._progress = 0.0 self._printFilename = None self._hotendTemperature = [None] * 4 self._bedTemperature = None self._needsAuthorization = False self._errorCount = 0 self._interruptSleep = False self.checkThread = threading.Thread(target=self._octoprintThread) self.checkThread.daemon = True self.checkThread.start() self.getApiKeyThread = threading.Thread( target=self._octoprintGetApiKeyThread) self.getApiKeyThread.daemon = True self.getApiKeyThread.start()
def _octoprintThread(self): self._waitDelay = 0 while True: updated = self._config.updateConfigFromFile() if updated: for printer in self._config.getPrinters(): connection = self._connectionMap[printer.getKey()] if not connection: self._addPrinter(printer) else: connection.setApiKey(printer.getApiKey()) if self._config.getDiscovery(): for ssdpResponse in discover("ssdp:all"): try: if ssdpResponse.usn not in self._ignored and ssdpResponse.usn not in self._connectionMap.keys( ): scheme, host, port, path = self._parseLocation( ssdpResponse.location) httpResponse = OctoPrintHttpClient( scheme, host, port, "", "xml").request("GET", path) if not httpResponse.isOk( ) or httpResponse.body is None: self._ignored.append(ssdpResponse.usn) continue manufacturer = httpResponse.body.find( "{urn:schemas-upnp-org:device-1-0}device/{urn:schemas-upnp-org:device-1-0}manufacturer" ) if manufacturer is not None: manufacturer = manufacturer.text presentationURL = httpResponse.body.find( "{urn:schemas-upnp-org:device-1-0}device/{urn:schemas-upnp-org:device-1-0}presentationURL" ) if presentationURL is not None: presentationURL = presentationURL.text friendlyName = httpResponse.body.find( "{urn:schemas-upnp-org:device-1-0}device/{urn:schemas-upnp-org:device-1-0}friendlyName" ) if friendlyName is not None: friendlyName = friendlyName.text if manufacturer and presentationURL and "octoprint" in manufacturer.lower( ): scheme, host, port, path = self._parseLocation( presentationURL) printer = OctoPrintPrinter( friendlyName, scheme, host, port, path, None, ssdpResponse.usn) self._config.updatePrinter(printer) self._addPrinter(printer) else: self._ignored.append(ssdpResponse.usn) time.sleep(0.01) except: print "Response Error: ({}:{})".format( ssdpResponse.usn, ssdpResponse.location) traceback.print_exc() self._ignored.append(ssdpResponse.usn) # Delay a bit more after every request. This so we do not stress the ssdp services too much if self._waitDelay < 10: self._waitDelay += 1 time.sleep(self._waitDelay * 30)