Exemplo n.º 1
0
    def __init__(self):
        super(OctoPrintPrinterConnectionGroup, self).__init__("OctoPrint")
        self._connectionMap = {}
        self._ignored = []

        self._config = OctoPrintConfiguration()

        for printer in self._config.getPrinters():
            self._addPrinter(printer)

        self._thread = threading.Thread(target=self._octoprintThread)
        self._thread.daemon = True
        self._thread.start()
	def __init__(self):
		super(OctoPrintPrinterConnectionGroup, self).__init__("OctoPrint")
		self._connectionMap = {}
		self._ignored = []
		
		self._config = OctoPrintConfiguration()
		
		for printer in self._config.getPrinters():
			self._addPrinter(printer)

		self._thread = threading.Thread(target = self._octoprintThread)
		self._thread.daemon = True
		self._thread.start()
Exemplo n.º 3
0
class OctoPrintPrinterConnectionGroup(
        printerConnectionBase.printerConnectionGroup):
    """
	The OctoPrint connection group runs a thread to poll for OctoPrint boxes.
	For each OctoPrint box it finds, it creates a OctoPrint object.
	"""
    def __init__(self):
        super(OctoPrintPrinterConnectionGroup, self).__init__("OctoPrint")
        self._connectionMap = {}
        self._ignored = []

        self._config = OctoPrintConfiguration()

        for printer in self._config.getPrinters():
            self._addPrinter(printer)

        self._thread = threading.Thread(target=self._octoprintThread)
        self._thread.daemon = True
        self._thread.start()

    def getAvailableConnections(self):
        return filter(lambda c: c.isAvailable(), self._connectionMap.values())

    def remove(self, key):
        del self._connectionMap[key]

    def getIconID(self):
        return 27

    def getPriority(self):
        return 100

    def _addPrinter(self, printer):
        self._connectionMap[printer.getKey()] = OctoPrintPrinterConnection(
            printer.getKey(), printer.getName(), printer.getScheme(),
            printer.getHost(), printer.getPort(), printer.getPath(),
            printer.getApiKey(), self)

    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)

    def _parseLocation(self, location):
        parts = urlparse(location)

        netloc = parts.netloc.split(':')

        host = netloc[0]

        port = None
        if len(netloc) > 1:
            port = int(netloc[1])

        path = parts.path
        if len(path) > 0 and path[-1] == '/':
            path = path[:-1]

        return parts.scheme, host, port, path
class OctoPrintPrinterConnectionGroup(printerConnectionBase.printerConnectionGroup):
	"""
	The OctoPrint connection group runs a thread to poll for OctoPrint boxes.
	For each OctoPrint box it finds, it creates a OctoPrint object.
	"""

	def __init__(self):
		super(OctoPrintPrinterConnectionGroup, self).__init__("OctoPrint")
		self._connectionMap = {}
		self._ignored = []
		
		self._config = OctoPrintConfiguration()
		
		for printer in self._config.getPrinters():
			self._addPrinter(printer)

		self._thread = threading.Thread(target = self._octoprintThread)
		self._thread.daemon = True
		self._thread.start()

	def getAvailableConnections(self):
		return filter(lambda c: c.isAvailable(), self._connectionMap.values())

	def remove(self, key):
		del self._connectionMap[key]

	def getIconID(self):
		return 27

	def getPriority(self):
		return 100

	def _addPrinter(self, printer):
		self._connectionMap[printer.getKey()] = OctoPrintPrinterConnection(
			printer.getKey(),
			printer.getName(),
			printer.getScheme(),
			printer.getHost(),
			printer.getPort(),
			printer.getPath(),
			printer.getApiKey(),
			self)
	
	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)

			
	def _parseLocation(self, location):
		parts = urlparse(location)
		
		netloc = parts.netloc.split(':')
		
		host = netloc[0]
		
		port = None
		if len(netloc) > 1:
			port = int(netloc[1])
			
		path = parts.path
		if len(path) > 0 and path[-1] == '/':
			path = path[:-1]
			
		return parts.scheme, host, port, path