def get_printer_inst(org_uuid, uuid): validate_uuid(uuid) printer = printers.get_printer(uuid) if printer is None or printer.get("organization_uuid") != org_uuid: return abort(make_response(jsonify(message="Not found"), 404)) network_client = network_clients.get_network_client( printer["network_client_uuid"]) printer_data = dict(network_client) printer_data.update(dict(printer)) printer_inst = clients.get_printer_instance(printer_data) return printer_inst
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, }, )
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, }, )
def make_printer_response(printer, fields): if not isinstance(printer, clients.utils.PrinterClient): network_client = network_clients.get_network_client( printer["network_client_uuid"]) printer_data = dict(network_client) printer_data.update(dict(printer)) printer_inst = clients.get_printer_instance(printer_data) else: printer_inst = printer data = { "uuid": printer_inst.uuid, "client": { "name": printer_inst.client_name(), "version": printer_inst.client_info.version, "connected": bool(printer_inst.client_info.connected), "access_level": printer_inst.client_info.access_level, "api_key": printer_inst.client_info.api_key[0:2] + "*" * 30 # Add star padding to 32 characters if printer_inst.client_info.api_key else None, "plugins": printer_inst.client_info.plugins, }, "printer_props": printer_inst.get_printer_props(), "name": printer_inst.name, "protocol": printer_inst.protocol, "token": printer_inst.token[0:2] + "*" * 30 # Add star padding to 32 characters if printer_inst.token else None, "hostname": printer_inst.hostname, "ip": printer_inst.ip, "port": printer_inst.port, "path": printer_inst.path, } if "status" in fields: data["status"] = printer_inst.status() if "webcam" in fields: webcam_data = printer_inst.client_info.webcam or {} data["webcam"] = { "url": "/organizations/%s/printers/%s/webcam-snapshot" % ( printer_inst.organization_uuid, printer_inst.uuid, ), "flipHorizontal": webcam_data.get("flipHorizontal"), "flipVertical": webcam_data.get("flipVertical"), "rotate90": webcam_data.get("rotate90"), } if "job" in fields: data["job"] = printer_inst.job() if "lights" in fields: try: data["lights"] = "on" if printer_inst.are_lights_on() else "off" except PrinterClientException: data["lights"] = "unavailable" return data
def printjob_create(org_uuid): data = request.json if not data: return abort(make_response(jsonify(message="Missing payload"), 400)) gcode_uuid = data.get("gcode", None) printer_uuid = data.get("printer", None) if not gcode_uuid or not printer_uuid: return abort( make_response(jsonify(message="Missing gcode_uuid or printer_uuid"), 400) ) validate_uuid(gcode_uuid) validate_uuid(printer_uuid) printer = printers.get_printer(printer_uuid) if printer is None or printer["organization_uuid"] != org_uuid: return abort(make_response(jsonify(message="Not found"), 404)) gcode = gcodes.get_gcode(gcode_uuid) if gcode is None: return abort(make_response(jsonify(message="Not found"), 404)) try: network_client = network_clients.get_network_client( printer["network_client_uuid"] ) printer_data = dict(network_client) printer_data.update(dict(printer)) printer_inst = clients.get_printer_instance(printer_data) uploaded = printer_inst.upload_and_start_job( gcode["absolute_path"], gcode["path"] ) if not uploaded: return abort( make_response( jsonify(message="Cannot upload the g-code to the printer"), 500 ) ) printjob_uuid = guid.uuid4() printjobs.add_printjob( uuid=printjob_uuid, gcode_uuid=gcode["uuid"], organization_uuid=org_uuid, printer_uuid=printer["uuid"], user_uuid=get_current_user()["uuid"], gcode_data={ "uuid": gcode["uuid"], "filename": gcode["filename"], "size": gcode["size"], "available": True, }, printer_data={ "ip": printer_inst.ip, "port": printer_inst.port, "hostname": printer_inst.hostname, "name": printer_inst.name, "client": printer_inst.client, }, ) return ( jsonify({"uuid": printjob_uuid, "user_uuid": get_current_user()["uuid"]}), 201, ) except clients.utils.PrinterClientException as e: app.logger.error(e) return abort( make_response( jsonify(message="Cannot schedule a printjob: %s" % str(e)), 409 ) )
def printjob_create(org_uuid): data = request.json if not data: return abort(make_response(jsonify(message="Missing payload"), 400)) gcode_uuid = data.get("gcode", None) printer_uuid = data.get("printer", None) # FIXME: this should be part of the path if not gcode_uuid or not printer_uuid: return abort( make_response( jsonify(message="Missing gcode_uuid or printer_uuid"), 400)) printer = printers.get_printer(printer_uuid) if not printer or printer['organization_uuid'] != org_uuid: raise http_exceptions.UnprocessableEntity( f"Invalid printer {printer_uuid} - does not exist.") gcode = gcodes.get_gcode(gcode_uuid) if not gcode: raise http_exceptions.UnprocessableEntity( "Invalid gcode {gcode_uuid} - does not exist.") network_client = network_clients.get_network_client( printer["network_client_uuid"]) printer_data = dict(network_client) printer_data.update(dict(printer)) printer_inst = clients.get_printer_instance(printer_data) try: printer_inst.upload_and_start_job(gcode["absolute_path"], gcode["path"]) except DeviceInvalidState as e: raise http_exceptions.Conflict(*e.args) except DeviceCommunicationError as e: raise http_exceptions.GatewayTimeout(*e.args) # TODO: robin - add_printjob should be method of printer and printer a # method of organization printjob_uuid = printjobs.add_printjob( gcode_uuid=gcode["uuid"], organization_uuid=org_uuid, printer_uuid=printer["uuid"], user_uuid=get_current_user()["uuid"], gcode_data={ "uuid": gcode["uuid"], "filename": gcode["filename"], "size": gcode["size"], "available": True, }, # FIXME: printer data should be kept in printer object only printer_data={ "ip": printer_inst.ip, "port": printer_inst.port, "hostname": printer_inst.hostname, "name": printer_inst.name, "client": printer_inst.client, }, ) return ( jsonify({ "uuid": printjob_uuid, "user_uuid": get_current_user()["uuid"] }), 201, )