Пример #1
0
def client(ctx, apikey, host, port, httpuser, httppass, https, prefix):
    """Basic API client."""
    try:
        if not host or not port or not apikey:
            settings = init_settings(
                get_ctx_obj_option(ctx, "basedir", None),
                get_ctx_obj_option(ctx, "configfile", None))

        if not host:
            host = settings.get(["server", "host"])
            host = host if host != "0.0.0.0" else "127.0.0.1"
        if not port:
            port = settings.getInt(["server", "port"])

        if not apikey:
            apikey = settings.get(["api", "key"])

        baseurl = octoprint_client.build_base_url(https=https,
                                                  httpuser=httpuser,
                                                  httppass=httppass,
                                                  host=host,
                                                  port=port,
                                                  prefix=prefix)

        ctx.obj.client = octoprint_client.Client(baseurl, apikey)
    except FatalStartupError as e:
        click.echo(e.message, err=True)
        click.echo("There was a fatal error initializing the client.",
                   err=True)
        ctx.exit(-1)
def listen_to_octoprint(settings, q, on_heartbeat):
    def on_error(ws, error):
        print("!!! Error: {}".format(error))

    def on_message(ws, message_type, message_payload):

        def __deplete_queue__(q):
            while q.qsize() > 10:
                q.get_nowait()

        if type(message_payload) is not dict:
            return

        __deplete_queue__(q)
        message_payload.update({"origin": "octoprint"})
        q.put(json.dumps(message_payload))

    if "init_client" in dir(octoprint_client): # OctoPrint <= 1.3.2
        octoprint_client.init_client(settings)
        socket = octoprint_client.connect_socket(on_error=on_error,
                                             on_heartbeat=on_heartbeat,
                                             on_message=on_message)
    else:
        host = settings.get(["server", "host"])
        host = host if host != "0.0.0.0" else "127.0.0.1"
        port = settings.getInt(["server", "port"])
        apikey = settings.get(["api", "key"])
        baseurl = octoprint_client.build_base_url(host=host, port=port)
        client = octoprint_client.Client(baseurl, apikey)
        client.create_socket(on_error=on_error,
                 on_heartbeat=on_heartbeat,
                 on_message=on_message)
Пример #3
0
def client(ctx, apikey, host, port, httpuser, httppass, https, prefix):
	"""Basic API client."""
	try:
		if not host or not port or not apikey:
			settings = init_settings(get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None))

		if not host:
			host = settings.get(["server", "host"])
			host = host if host != "0.0.0.0" else "127.0.0.1"
		if not port:
			port = settings.getInt(["server", "port"])

		if not apikey:
			apikey = settings.get(["api", "key"])

		baseurl = octoprint_client.build_base_url(https=https,
		                                          httpuser=httpuser,
		                                          httppass=httppass,
		                                          host=host,
		                                          port=port,
		                                          prefix=prefix)

		ctx.obj.client = octoprint_client.Client(baseurl, apikey)
	except FatalStartupError as e:
		click.echo(e.message, err=True)
		click.echo("There was a fatal error initializing the client.", err=True)
		ctx.exit(-1)
Пример #4
0
def create_client(settings=None,
                  apikey=None,
                  host=None,
                  port=None,
                  httpuser=None,
                  httppass=None,
                  https=False,
                  prefix=None):
    assert (host is not None or settings is not None)
    assert (port is not None or settings is not None)
    assert (apikey is not None or settings is not None)

    if not host:
        host = settings.get(["server", "host"])
        host = host if host != "0.0.0.0" else "127.0.0.1"
    if not port:
        port = settings.getInt(["server", "port"])

    if not apikey:
        apikey = settings.get(["api", "key"])

    baseurl = octoprint_client.build_base_url(https=https,
                                              httpuser=httpuser,
                                              httppass=httppass,
                                              host=host,
                                              port=port,
                                              prefix=prefix)

    return octoprint_client.Client(baseurl, apikey)
Пример #5
0
    def init(self, host, port, apikey):
        Logger.info("Client: Connecting to Octoprint")
        Logger.debug("Client: Host: " + str(host))
        Logger.debug("Client: Port: " + str(port))
        Logger.debug("Client: API Key: " + str(apikey))

        self.octoprintClient = octoprint_client.Client(
            octoprint_client.build_base_url(https=None,
                                            httpuser=None,
                                            httppass=None,
                                            host=host,
                                            port=port,
                                            prefix=None), apikey)

        #self.octoprintClient.apikey = apikey
        #self.octoprintClient.baseurl = self.octoprintClient.build_base_url(https=None, httpuser=None, httppass=None, host=host, port=port, prefix=None)

        Clock.schedule_once(self.connect)
Пример #6
0
def listen_to_octoprint(settings, q):
    def on_connect(ws):
        print(">>> Connected!")

    def on_close(ws):
        print(">>> Oh No! Connection closed! What happened?")

    def on_error(ws, error):
        print("!!! Error: {}".format(error))

    def on_heartbeat(ws):
        q.put(json.dumps({'hb': {'ipAddrs': ip_addr()}}))

    def on_message(ws, message_type, message_payload):
        def __deplete_queue__(q):
            while q.qsize() > 10:
                q.get_nowait()

        if type(message_payload) is not dict:
            return

        __deplete_queue__(q)
        q.put(json.dumps(message_payload))

    if "init_client" in dir(octoprint_client):  # OctoPrint <= 1.3.2
        octoprint_client.init_client(settings)
        socket = octoprint_client.connect_socket(on_connect=on_connect,
                                                 on_close=on_close,
                                                 on_error=on_error,
                                                 on_heartbeat=on_heartbeat,
                                                 on_message=on_message)
    else:
        host = settings.get(["server", "host"])
        host = host if host != "0.0.0.0" else "127.0.0.1"
        port = settings.getInt(["server", "port"])
        apikey = settings.get(["api", "key"])
        baseurl = octoprint_client.build_base_url(host=host, port=port)
        client = octoprint_client.Client(baseurl, apikey)
        client.create_socket(on_connect=on_connect,
                             on_close=on_close,
                             on_error=on_error,
                             on_heartbeat=on_heartbeat,
                             on_message=on_message)
Пример #7
0
def create_client(settings=None, apikey=None, host=None, port=None, httpuser=None, httppass=None, https=False, prefix=None):
	assert(host is not None or settings is not None)
	assert(port is not None or settings is not None)
	assert(apikey is not None or settings is not None)

	if not host:
		host = settings.get(["server", "host"])
		host = host if host != "0.0.0.0" else "127.0.0.1"
	if not port:
		port = settings.getInt(["server", "port"])

	if not apikey:
		apikey = settings.get(["api", "key"])

	baseurl = octoprint_client.build_base_url(https=https,
	                                          httpuser=httpuser,
	                                          httppass=httppass,
	                                          host=host,
	                                          port=port,
	                                          prefix=prefix)

	return octoprint_client.Client(baseurl, apikey)