Esempio n. 1
0
def printers_list(org_uuid):
    device_list = []
    fields = [f for f in request.args.get("fields", "").split(",") if f]
    futures = []

    def reqid():
        letters = string.ascii_lowercase
        return "".join(random.choice(letters) for i in range(10))

    uqid = reqid()
    printers_set = printers.get_printers(organization_uuid=org_uuid)
    network_clients_mapping = {
        nc["uuid"]: nc
        for nc in network_clients.get_network_clients_by_uuids(
            [p["network_client_uuid"] for p in printers_set])
    }
    # FIXME: robin - this should be:
    #        `for network_client in network_clients.get_network_clients_by_uuids([p["network_client_uuid"] for p in printers_set])``
    #        Even better organization should have method `get_network_clients`
    for printer in printers.get_printers(organization_uuid=org_uuid):
        try:
            network_client = network_clients_mapping.get(
                printer["network_client_uuid"])
            if network_client is None:
                # This is a race condition handling, there should always be a network_client for every printer
                # FIXME: robin - this is not a race condition, remove this
                continue
            printer_data = dict(network_client)
            printer_data.update(dict(printer))
            futures.append(
                executor.submit_stored(
                    "%s:%s" % (uqid, printer["uuid"]),
                    make_printer_response,
                    printer_data,
                    fields,
                ))
        # This means that the future already exists and has not been popped yet -
        # that's a race condition right there. It shouldn't happen as each request is identified by uqid though
        except ValueError as e:
            app.logger.error("Possible race condition in printer list: %s" % e)

    for future in futures:
        try:
            data = future.result()
        except Exception as e:
            app.logger.error("Error in resolving a printer list future: %s" %
                             e)
        else:
            device_list.append(data)
            executor.futures.pop("%s:%s" % (uqid, data["uuid"]))

    return jsonify({"items": device_list}), 200
Esempio n. 2
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,
            },
        )
Esempio n. 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(),
        )
Esempio n. 4
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)
Esempio n. 5
0
def printers_list():
    device_list = []
    fields = [f for f in request.args.get("fields", "").split(",") if f]
    for printer in printers.get_printers():
        # TODO this should somehow go in parallel
        device_list.append(make_printer_response(printer, fields))
    return jsonify({"items": device_list})
Esempio n. 6
0
def printers_list():
    device_list = []
    fields = [f for f in request.args.get("fields", "").split(",") if f]
    futures = []

    def reqid():
        letters = string.ascii_lowercase
        return "".join(random.choice(letters) for i in range(10))

    uqid = reqid()
    for printer in printers.get_printers():
        try:
            futures.append(
                executor.submit_stored(
                    "%s:%s" % (uqid, printer["uuid"]),
                    make_printer_response,
                    printer,
                    fields,
                ))
        # This means that the future already exists and has not been poped yet -
        # that's a race condition right there. It shouldn't happen as each request is identified by uqid though
        except ValueError as e:
            app.logger.error("ValueError %s" % e)

    for future in futures:
        try:
            data = future.result()
        except Exception as e:
            app.logger.error("Exception %s" % e)
        else:
            device_list.append(data)
            executor.futures.pop("%s:%s" % (uqid, data["uuid"]))

    return jsonify({"items": device_list}), 200
Esempio n. 7
0
def check_printers():
    app.logger.debug("Checking known printers...")
    for raw_printer in printers.get_printers():
        check_printer.delay(raw_printer["uuid"])