Exemple #1
0
def check_printers():
    app.logger.debug("Checking known printers...")
    for raw_printer in printers.get_printers():
        printer = drivers.get_printer_instance(raw_printer)
        printer.is_alive()

        if printer.client.connected:
            webcam = printer.webcam()
            try:
                if "stream" in webcam:
                    redis.set("webcam_%s" % (printer.ip, ), webcam["stream"])
                else:
                    redis.delete("webcam_%s" % (printer.ip, ))
            except Exception as e:
                app.logger.error(
                    "Cannot save webcam proxy information into cache: %s", e)

        printers.update_printer(
            name=printer.name,
            hostname=printer.hostname,
            ip=printer.ip,
            client=printer.client_name(),
            client_props={
                "version": printer.client.version,
                "connected": printer.client.connected,
                "read_only": printer.client.read_only,
            },
        )
Exemple #2
0
def discover_printers():
    if not settings.get_val("network_discovery"):
        return
    app.logger.debug("Discovering network printers...")
    now = datetime.utcnow()
    to_deactivate = printers.get_printers()
    to_skip_ip = [
        device["ip"] for device in network_devices.get_network_devices()
        if (device["retry_after"] and device["retry_after"] > now)
        or device["disabled"]
    ]
    for line in do_arp_scan(settings.get_val("network_interface")):
        (ip, _) = line
        to_deactivate = [
            printer for printer in to_deactivate if printer["ip"] != ip
        ]
        if ip in to_skip_ip:
            continue
        hostname = get_avahi_hostname(ip)
        # it's communicating, let's sniff it for a printer
        sniff_printer.delay(hostname, ip)

    for printer in to_deactivate:
        app.logger.debug(
            "%s (%s) was not encountered on the network, deactivating" %
            (printer["hostname"], printer["ip"]))
        printer["client_props"]["connected"] = False
        printers.update_printer(**printer)
Exemple #3
0
def check_printers():
    app.logger.debug("Checking known printers...")
    for raw_printer in printers.get_printers():
        printer = clients.get_printer_instance(raw_printer)
        if printer.hostname is not None:
            current_ip = network.get_avahi_address(printer.hostname)
            if current_ip is not None and current_ip != printer.ip:
                printer.ip = current_ip
                printer.update_network_host()
        else:
            hostname = network.get_avahi_hostname(printer.ip)
            if hostname is not None:
                printer.hostname = hostname
                printer.update_network_host()
        printer.is_alive()
        printers.update_printer(
            uuid=printer.uuid,
            name=printer.name,
            hostname=printer.hostname,
            ip=printer.ip,
            port=printer.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(),
            },
            printer_props=printer.get_printer_props(),
        )
Exemple #4
0
 def start_update(self):
     response = self._http_post(
         "/update-system",
         force=True,
         data='{"action": "download-start"}',
         headers={"Content-Type": "application/json"},
     )
     app.logger.debug(response)
     app.logger.debug(response.text)
     print(response, response.text)
     if (
         response
         and response.status_code == 200
         and response.json()["status"] == "OK"
     ):
         self.client_info.pill_info["update_status"] = "downloading"
         printer = self
         printers.update_printer(
             uuid=self.uuid,
             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,
                 "pill_info": printer.client_info.pill_info,
             },
         )
         return True
     return False
Exemple #5
0
def check_printer(printer_uuid):
    app.logger.debug("Checking printer %s" % printer_uuid)
    raw_printer = printers.get_printer(printer_uuid)
    # todo: The `get_printer` method should raise an exception instead of returning None
    if raw_printer is None:
        app.logger.info(
            "Printer was deleted after it was scheduled for update.")
        return
    raw_client = network_clients.get_network_client(
        raw_printer["network_client_uuid"])
    printer_data = dict(raw_client)
    printer_data.update(raw_printer)
    printer = clients.get_printer_instance(printer_data)
    # websocket printers are not expected to change
    if printer.protocol in ["http", "https"]:
        if printer.hostname is not None:
            current_ip = network.get_avahi_address(printer.hostname)
            if current_ip is not None and current_ip != printer.ip:
                printer.ip = current_ip
                printer.update_network_base()
        hostname = network.get_avahi_hostname(printer.ip)
        if hostname is not None and hostname != printer.hostname:
            printer.hostname = hostname
            printer.update_network_base()
    now = datetime.now()
    if now.minute % 15 == 0 and printer.client_info.connected:
        printer.sniff()
        printer.karmen_sniff()
    else:
        printer.is_alive()

    if (printer.client_info.pill_info and printer.client_info.pill_info.get(
            "update_status", None) is not None):
        printer.karmen_sniff()

    if printer.hostname != raw_client.get(
            "hostname") or printer.ip != raw_client.get("ip"):
        network_clients.update_network_client(
            uuid=raw_client["uuid"],
            hostname=printer.hostname,
            ip=printer.ip,
        )
    printers.update_printer(
        uuid=printer.uuid,
        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,
            "pill_info": printer.client_info.pill_info,
        },
    )
Exemple #6
0
def printer_patch(org_uuid, printer_uuid):
    printer_inst = get_printer_inst(org_uuid, printer_uuid)
    data = request.json
    if not data:
        return abort(make_response(jsonify(message="Missing payload"), 400))
    name = data.get("name", printer_inst.name)
    api_key = data.get("api_key", printer_inst.client_info.api_key)
    printer_props = data.get("printer_props", {})
    if not name:
        return abort(make_response(jsonify(message="Missing name"), 400))

    printer_inst.add_api_key(api_key)
    if (data.get("api_key", "-1") !=
            "-1"  # FIXME: robin - change to `if 'api_key' in data`
            and data.get("api_key", "-1") != printer_inst.client_info.api_key):
        printer_inst.sniff(
        )  # this can probably be offloaded to check_printer task
    if printer_props:
        if not printer_inst.get_printer_props():
            printer_inst.printer_props = {}
        # This is effectively the only place where printer_props "validation" happens
        printer_inst.get_printer_props().update({
            k: printer_props[k]
            for k in [
                "filament_type",
                "filament_color",
                "bed_type",
                "tool0_diameter",
                "note",
            ] if k in printer_props
        })

    printer_inst.name = name
    printers.update_printer(
        uuid=printer_inst.uuid,
        name=printer_inst.name,
        client_props={
            "version": printer_inst.client_info.version,
            "connected": printer_inst.client_info.connected,
            "access_level": printer_inst.client_info.access_level,
            "api_key": printer_inst.client_info.api_key,
            "webcam": printer_inst.client_info.webcam,
            "plugins": printer_inst.client_info.plugins,
        },
        printer_props=printer_inst.get_printer_props(),
    )
    return (
        jsonify(
            make_printer_response(printer_inst, ["status", "webcam", "job"])),
        200,
    )
Exemple #7
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"]}}
        )
def check_printer(printer_uuid):
    app.logger.debug("Checking printer %s" % printer_uuid)
    raw_printer = printers.get_printer(printer_uuid)
    raw_client = network_clients.get_network_client(
        raw_printer["network_client_uuid"])
    printer_data = dict(raw_client)
    printer_data.update(raw_printer)
    printer = clients.get_printer_instance(printer_data)
    # websocket printers are not expected to change
    if printer.protocol in ["http", "https"]:
        if printer.hostname is not None:
            current_ip = network.get_avahi_address(printer.hostname)
            if current_ip is not None and current_ip != printer.ip:
                printer.ip = current_ip
                printer.update_network_base()
        hostname = network.get_avahi_hostname(printer.ip)
        if hostname is not None and hostname != printer.hostname:
            printer.hostname = hostname
            printer.update_network_base()
    now = datetime.now()
    if now.minute % 15 == 0 and printer.client_info.connected:
        printer.sniff()
    else:
        printer.is_alive()

    if printer.hostname != raw_client.get(
            "hostname") or printer.ip != raw_client.get("ip"):
        network_clients.update_network_client(
            uuid=raw_client["uuid"],
            hostname=printer.hostname,
            ip=printer.ip,
        )

    printers.update_printer(
        uuid=printer.uuid,
        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,
        },
    )
Exemple #9
0
def printer_patch(ip):
    printer = printers.get_printer(ip)
    if printer is None:
        return abort(404)
    data = request.json
    if not data:
        return abort(400)
    name = data.get("name", None)
    if not name:
        return abort(400)
    printer_inst = drivers.get_printer_instance(printer)
    printers.update_printer(
        name=name,
        hostname=printer_inst.hostname,
        ip=printer_inst.ip,
        client=printer_inst.client_name(),
        client_props={
            "version": printer_inst.client.version,
            "connected": printer_inst.client.connected,
            "read_only": printer_inst.client.read_only,
        },
    )
    return "", 204
Exemple #10
0
def printer_patch(uuid):
    try:
        uuidmodule.UUID(uuid, version=4)
    except ValueError:
        return abort(make_response("", 400))
    printer = printers.get_printer(uuid)
    if printer is None:
        return abort(make_response("", 404))
    data = request.json
    if not data:
        return abort(make_response("", 400))
    name = data.get("name", printer["name"])
    protocol = data.get("protocol", printer["protocol"])
    api_key = data.get("api_key", printer["client_props"].get("api_key", None))
    printer_props = data.get("printer_props", {})

    # TODO it might be necessary to update ip, hostname, port as well eventually
    if not name or protocol not in ["http", "https"]:
        return abort(make_response("", 400))
    printer_inst = clients.get_printer_instance(printer)
    printer_inst.add_api_key(api_key)
    if data.get("api_key", "-1") != "-1" and data.get(
            "api_key", "-1") != printer["client_props"].get("api_key", None):
        printer_inst.sniff(
        )  # this can probably be offloaded to check_printer task
    if printer_props:
        if not printer_inst.get_printer_props():
            printer_inst.printer_props = {}
        # This is effectively the only place where printer_props "validation" happens
        printer_inst.get_printer_props().update({
            k: printer_props[k]
            for k in [
                "filament_type",
                "filament_color",
                "bed_type",
                "tool0_diameter",
                "note",
            ] if k in printer_props
        })
    printers.update_printer(
        uuid=printer_inst.uuid,
        name=name,
        hostname=printer_inst.hostname,
        ip=printer_inst.ip,
        port=printer_inst.port,
        protocol=protocol,
        client=printer_inst.client_name(),
        client_props={
            "version": printer_inst.client_info.version,
            "connected": printer_inst.client_info.connected,
            "access_level": printer_inst.client_info.access_level,
            "api_key": printer_inst.client_info.api_key,
            "webcam": printer_inst.webcam(),
        },
        printer_props=printer_inst.get_printer_props(),
    )
    # TODO cache webcam, job, status for a small amount of time in client
    return (
        jsonify(
            make_printer_response(printer_inst, ["status", "webcam", "job"])),
        200,
    )