Beispiel #1
0
def printer_create():
    data = request.json
    if not data:
        return abort(400)
    ip = data.get("ip", None)
    name = data.get("name", None)
    if (not ip or not name or re.match(
            r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:?\d{0,5}$", ip) is None):
        return abort(400)
    if printers.get_printer(ip) is not None:
        return abort(409)
    hostname = network.get_avahi_hostname(ip)
    printer = drivers.get_printer_instance({
        "hostname": hostname,
        "ip": ip,
        "name": name,
        "client": "octoprint",  # TODO make this more generic
    })
    printer.sniff()
    network_devices.upsert_network_device(ip=ip,
                                          retry_after=None,
                                          disabled=False)
    printers.add_printer(
        name=name,
        hostname=hostname,
        ip=ip,
        client=printer.client_name(),
        client_props={
            "version": printer.client.version,
            "connected": printer.client.connected,
            "read_only": printer.client.read_only,
        },
    )
    return "", 201
Beispiel #2
0
 def setUp(self):
     printers.delete_printer("1.2.3.4")
     printers.add_printer(
         name="name",
         hostname="hostname",
         ip="1.2.3.4",
         client="octoprint",
         client_props={"version": "123"},
     )
Beispiel #3
0
 def setUp(self):
     self.uuid = uuid.uuid4()
     printers.delete_printer(self.uuid)
     printers.add_printer(
         uuid=self.uuid,
         name="name",
         hostname="hostname",
         ip="192.168.%s" %
         ".".join([str(random.randint(0, 255)) for _ in range(2)]),
         client="octoprint",
         client_props={"version": "123"},
         printer_props={"filament_type": "PLA"},
     )
Beispiel #4
0
def save_printer_data(**kwargs):
    has_record = printers.get_printer(kwargs["ip"])
    if has_record is None and not kwargs["client_props"]["connected"]:
        return
    if has_record is None:
        printers.add_printer(
            **{
                "name": None,
                "client_props": {"connected": False, "version": {}, "read_only": True},
                **kwargs,
            }
        )
    else:
        printers.update_printer(
            **{**has_record, **kwargs, **{"name": has_record["name"]}}
        )
Beispiel #5
0
def save_printer_data(**kwargs):
    if not kwargs["client_props"]["connected"]:
        app.logger.info("Printer on %s is not responding as connected" %
                        kwargs.get("ip"))
        return
    has_record = printers.get_printer_by_network_props(kwargs.get("hostname"),
                                                       kwargs.get("ip"),
                                                       kwargs.get("port"))
    # No need to update registered printers
    if has_record is not None:
        app.logger.info("Printer on %s is already registered within karmen" %
                        kwargs.get("ip"))
        return
    printers.add_printer(
        **{
            "name": None,
            "client_props": {
                "connected": False,
                "version": {},
                "access_level":
                clients.utils.PrinterClientAccessLevel.PROTECTED,
            },
            **kwargs,
        })
Beispiel #6
0
def printer_create(org_uuid):
    data = request.json
    if not data:
        return abort(make_response(jsonify(message="Missing payload"), 400))
    uuid = guid.uuid4()
    ip = data.get("ip", None)
    port = data.get("port", None)
    hostname = data.get("hostname", None)
    name = data.get("name", None)
    api_key = data.get("api_key", None)
    protocol = data.get("protocol", "http")
    path = data.get("path", "")
    token = data.get("token", None)

    if token is not None and token != "":
        if not app.config.get("CLOUD_MODE"):
            return abort(make_response(jsonify(message="Missing token"), 400))
        existing_network_client = network_clients.get_network_client_by_socket_token(
            token)
        if existing_network_client:
            existing_printer = printers.get_printer_by_network_client_uuid(
                org_uuid, existing_network_client.get("uuid"))
            if existing_printer is not None:
                return abort(
                    make_response(jsonify(message="Printer exists"), 409))
        path = ""
        hostname = ""
        ip = ""
        protocol = ""
        port = 0
    else:
        if app.config.get("CLOUD_MODE"):
            return abort(
                make_response(
                    jsonify(
                        message=
                        "Cannot add printer without a token in CLOUD_MODE"),
                    500,
                ))
        if ((not ip and not hostname) or not name
                or protocol not in ["http", "https"]
                or (hostname
                    and re.match(r"^[0-9a-zA-Z.-]+\.local$", hostname) is None)
                or
            (ip
             and re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip) is None)
                or (port and re.match(r"^\d{0,5}$", str(port)) is None)):
            return abort(
                make_response(
                    jsonify(
                        message="Missing some network data about the printer"),
                    400))

        if hostname and not ip:
            ip = network.get_avahi_address(hostname)
            if not ip:
                return abort(
                    make_response(
                        jsonify(message="Cannot resolve %s with mDNS" %
                                hostname), 500))
        if ip and not hostname:
            hostname = network.get_avahi_hostname(ip)

        existing_network_client = network_clients.get_network_client_by_props(
            hostname, ip, port, path)
        if existing_network_client:
            existing_printer = printers.get_printer_by_network_client_uuid(
                org_uuid, existing_network_client.get("uuid"))
            if existing_printer is not None:
                return abort(
                    make_response(jsonify(message="Printer exists"), 409))

    if not existing_network_client:
        existing_network_client = {
            "uuid": guid.uuid4(),
            "hostname": hostname,
            "client": "octoprint",  # TODO make this more generic
            "protocol": protocol,
            "ip": ip,
            "port": port,
            "path": path,
            "token": token,
        }
        network_clients.add_network_client(**existing_network_client)

    printer = clients.get_printer_instance({
        "uuid":
        uuid,
        "network_client_uuid":
        existing_network_client["uuid"],
        "organization_uuid":
        org_uuid,
        "name":
        name,
        "client":
        existing_network_client["client"],
        "protocol":
        existing_network_client["protocol"],
        "hostname":
        existing_network_client["hostname"],
        "ip":
        existing_network_client["ip"],
        "port":
        existing_network_client["port"],
        "path":
        existing_network_client["path"],
        "token":
        existing_network_client["token"],
    })
    printer.add_api_key(api_key)
    printer.update_network_base()
    printer.sniff()
    printers.add_printer(
        uuid=uuid,
        network_client_uuid=existing_network_client["uuid"],
        organization_uuid=org_uuid,
        name=name,
        client=printer.client_name(),
        client_props={
            "version": printer.client_info.version,
            "connected": printer.client_info.connected,
            "access_level": printer.client_info.access_level,
            "api_key": printer.client_info.api_key,
            "webcam": printer.client_info.webcam,
            "plugins": printer.client_info.plugins,
        },
    )
    # TODO cache webcam, job, status for a small amount of time in this client
    return jsonify(make_printer_response(printer,
                                         ["status", "webcam", "job"])), 201
Beispiel #7
0
def printer_create():
    data = request.json
    if not data:
        return abort(make_response("", 400))
    uuid = uuidmodule.uuid4()
    ip = data.get("ip", None)
    port = data.get("port", None)
    hostname = data.get("hostname", None)
    name = data.get("name", None)
    api_key = data.get("api_key", None)
    protocol = data.get("protocol", "http")

    if ((not ip and not hostname) or not name
            or protocol not in ["http", "https"] or
        (hostname and re.match(r"^[0-9a-zA-Z.-]+\.local$", hostname) is None)
            or
        (ip and re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip) is None)
            or (port and re.match(r"^\d{0,5}$", str(port)) is None)):
        return abort(make_response("", 400))

    if hostname and not ip:
        ip = network.get_avahi_address(hostname)
        if not ip:
            return abort(
                make_response("Cannot resolve %s with mDNS" % hostname, 500))
    if ip and not hostname:
        hostname = network.get_avahi_hostname(ip)

    if printers.get_printer_by_network_props(hostname, ip, port) is not None:
        return abort(make_response("", 409))

    printer = clients.get_printer_instance({
        "uuid": uuid,
        "hostname": hostname,
        "ip": ip,
        "port": port,
        "name": name,
        "protocol": protocol,
        "client": "octoprint",  # TODO make this more generic
    })
    printer.add_api_key(api_key)
    printer.sniff()
    printers.add_printer(
        uuid=uuid,
        name=name,
        hostname=hostname,
        ip=ip,
        port=port,
        protocol=printer.protocol,
        client=printer.client_name(),
        client_props={
            "version": printer.client_info.version,
            "connected": printer.client_info.connected,
            "access_level": printer.client_info.access_level,
            "api_key": printer.client_info.api_key,
            "webcam": printer.webcam(),
        },
    )
    # TODO cache webcam, job, status for a small amount of time in client
    return jsonify(make_printer_response(printer,
                                         ["status", "webcam", "job"])), 201