removed all test image stuff
Browse files46 unmodified lines474849505152535443 unmodified lines989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814952 unmodified lines2022032042052062072082092102112122 unmodified lines21521621721821922022122222322422522622716 unmodified lines24424524624724824925025125225325425525625725825926026126226326424 unmodified lines2892902912922932942952962972982993003013027 unmodified lines31031131231331431531631731831932032132232332432594 unmodified lines42042142242342442542642742842943043143215 unmodified lines4484494504514524534544554564574584594604614624634644654664679 unmodified lines47747847948048148248348448548688 unmodified lines5755765775785795805815827 unmodified lines59059159259359459559659746 unmodified lines"status": "N/A","update_time": "Waiting for data...","image_url": "N/A","progress": 0,"message": "",}bambu_client = None43 unmodified linesdef rpi_on_message(client, userdata, msg):global latest_datatry:payload = msg.payload.decode("utf-8")logger.info(f"Received message from RPI: {payload}")data = json.loads(payload)status = data.get("status", "Unknown")latest_data["status"] = statuslatest_data["update_time"] = data.get("update_time", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))if "nozzle_temperature" in data:latest_data["nozzle_temperature"] = data["nozzle_temperature"]if "bed_temperature" in data:latest_data["bed_temperature"] = data["bed_temperature"]if "error" in data:logger.error(f"Error from RPI: {data['error']}")if status == "Ready":latest_data["progress"] = 0latest_data["message"] = "Printer ready"elif status == "Processing":latest_data["progress"] = 25latest_data["message"] = "Processing G-code..."except Exception as e:logger.error(f"Error processing message from RPI: {e}")try:result = json.loads(payload)if "status" in result:status = result["status"]latest_data["status"] = statusif "command" in result and result["command"] == "capture_image" and result.get("auto_triggered", False):logger.info("receive capture command")threading.Thread(target=handle_auto_capture, args=(result,), daemon=True).start()except json.JSONDecodeError:logger.error(f"Invalid JSON in message: {payload}")except Exception as e:logger.error(f"Error processing RPI message: {e}")def get_data(serial=DEFAULT_SERIAL):52 unmodified linesjson.dumps({"command": "generate_gcode", "parameters": params}),)logger.info("Parameters sent successfully to RPi for G-code generation")global latest_datalatest_data["status"] = "Sent"latest_data["update_time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())return "Parameters sent successfully to RPi. Status: Sent"else:logger.warning("MQTT not connected, parameters not sent")2 unmodified lineslogger.error(f"Error sending parameters: {e}")return f"Error sending parameters: {e}"latest_data["status"] = "Sending"latest_data["progress"] = 10latest_data["message"] = "Sending parameters to printer..."def get_image_base64(image):if image is None:logger.warning("No image to encode")16 unmodified linesdef handle_auto_capture(message):try:logger.info(f"receive capture command: {message}")print_job = message.get("print_job", "unknown_job")timestamp = message.get("timestamp", time.strftime("%Y-%m-%d %H:%M:%S"))latest_data["auto_capture_requested"] = Truelatest_data["last_capture_job"] = print_joblatest_data["last_capture_time"] = timestampreturn capture_image()except Exception as e:logger.error(f"error: {e}")return None, f"capture failed: {str(e)}"def capture_image(url=None, use_test_image=False, test_image_name=None):global rpi_client, latest_dataif rpi_client is None:24 unmodified linesurl = latest_data["image_url"]if use_test_image:logger.info("Using test image instead of URL")test_img = get_test_image(test_image_name)if test_img:return test_imgelse:logger.warning("Failed to get specified test image, trying URL")if url != "N/A":try:logger.info(f"Capturing image from URL: {url}")7 unmodified lineselse:raise Exception("url is 'N/A'")def update_print_status():global latest_datareturn (latest_data["status"],latest_data["nozzle_temperature"],latest_data["bed_temperature"],latest_data["update_time"],latest_data.get("progress", 0),latest_data.get("message", ""),)def health_check():94 unmodified lineslogger.info("API call: get_data")return get_data()def api_capture_frame(url=None, use_test_image=False, test_image_name=None):logger.info(f"API call: capture_frame with URL: {url}, use_test_image: {use_test_image}")try:img = capture_image(url, use_test_image, test_image_name)if img:if img.mode == "RGBA":img = img.convert("RGB")15 unmodified linesparam_2=60,param_3=60,param_4=100,use_test_image=False,test_image_name=None,):logger.info(f"API call: lambda with params: {param_1}, {param_2}, {param_3}, {param_4}, use_test_image: {use_test_image}, test_image_name: {test_image_name}")try:img = Noneif use_test_image:logger.info(f"Lambda using test image: {test_image_name}")img = get_test_image(test_image_name)elif (img_dataand isinstance(img_data, str)and (img_data.startswith("http://") or img_data.startswith("https://"))9 unmodified linesexcept Exception as e:logger.error(f"Failed to decode base64 image: {e}")if img is None:logger.info("No valid image data received, using default test image")img = get_test_image()if img:img_array = np.array(img)88 unmodified linesfn=api_capture_frame,inputs=[gr.Textbox(label="Image URL"),gr.Checkbox(label="Use Test Image", value=False),gr.Textbox(label="Test Image Name", value=""),],outputs=api_json_output,api_name="capture_frame",7 unmodified linesgr.Number(label="Bed Temperature", value=60),gr.Number(label="Print Speed", value=60),gr.Number(label="Fan Speed", value=100),gr.Checkbox(label="Use Test Image", value=False),gr.Textbox(label="Test Image Name", value=""),],outputs=api_json_output,api_name="lambda",46 unmodified lines474849505152535443 unmodified lines989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315452 unmodified lines2072082092102112122132142152162172182192 unmodified lines22222322422522622722822923023123216 unmodified lines24925025125225325425525625725825926026126226326426526626726826924 unmodified lines2942952962972982997 unmodified lines30730830931031131231331431531631731831932032132232394 unmodified lines41841942042142242342442542642742815 unmodified lines4444454464474484494504514524534544554564579 unmodified lines46746846947047147288 unmodified lines5615625635645655667 unmodified lines57457557657757857946 unmodified lines"status": "N/A","update_time": "Waiting for data...","image_url": "N/A","progress": 0,"message": "",}bambu_client = None43 unmodified linesdef rpi_on_message(client, userdata, msg):global latest_datatry:payload = msg.payload.decode("utf-8")logger.info(f"Received message from RPI: {payload}")data = json.loads(payload)status = data.get("status", "Unknown")latest_data["status"] = statuslatest_data["update_time"] = data.get("update_time", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))if "nozzle_temperature" in data:latest_data["nozzle_temperature"] = data["nozzle_temperature"]if "bed_temperature" in data:latest_data["bed_temperature"] = data["bed_temperature"]if "error" in data:logger.error(f"Error from RPI: {data['error']}")if status == "Ready":latest_data["progress"] = 0latest_data["message"] = "Printer ready"elif status == "Processing":latest_data["progress"] = 25latest_data["message"] = "Processing G-code..."except Exception as e:logger.error(f"Error processing message from RPI: {e}")try:result = json.loads(payload)if "status" in result:status = result["status"]latest_data["status"] = statusif ("command" in resultand result["command"] == "capture_image"and result.get("auto_triggered", False)):logger.info("receive capture command")threading.Thread(target=handle_auto_capture, args=(result,), daemon=True).start()except json.JSONDecodeError:logger.error(f"Invalid JSON in message: {payload}")def get_data(serial=DEFAULT_SERIAL):52 unmodified linesjson.dumps({"command": "generate_gcode", "parameters": params}),)logger.info("Parameters sent successfully to RPi for G-code generation")global latest_datalatest_data["status"] = "Sent"latest_data["update_time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())return "Parameters sent successfully to RPi. Status: Sent"else:logger.warning("MQTT not connected, parameters not sent")2 unmodified lineslogger.error(f"Error sending parameters: {e}")return f"Error sending parameters: {e}"latest_data["status"] = "Sending"latest_data["progress"] = 10latest_data["message"] = "Sending parameters to printer..."def get_image_base64(image):if image is None:logger.warning("No image to encode")16 unmodified linesdef handle_auto_capture(message):try:logger.info(f"receive capture command: {message}")print_job = message.get("print_job", "unknown_job")timestamp = message.get("timestamp", time.strftime("%Y-%m-%d %H:%M:%S"))latest_data["auto_capture_requested"] = Truelatest_data["last_capture_job"] = print_joblatest_data["last_capture_time"] = timestampreturn capture_image()except Exception as e:logger.error(f"error: {e}")return None, f"capture failed: {str(e)}"def capture_image(url=None):global rpi_client, latest_dataif rpi_client is None:24 unmodified linesurl = latest_data["image_url"]if url != "N/A":try:logger.info(f"Capturing image from URL: {url}")7 unmodified lineselse:raise Exception("url is 'N/A'")def update_print_status():global latest_datareturn (latest_data["status"],latest_data["nozzle_temperature"],latest_data["bed_temperature"],latest_data["update_time"],latest_data.get("progress", 0),latest_data.get("message", ""),)def health_check():94 unmodified lineslogger.info("API call: get_data")return get_data()def api_capture_frame(url=None):logger.info(f"API call: capture_frame with URL: {url}")try:img = capture_image(url)if img:if img.mode == "RGBA":img = img.convert("RGB")15 unmodified linesparam_2=60,param_3=60,param_4=100,):logger.info(f"API call: lambda with params: {param_1}, {param_2}, {param_3}, {param_4}")try:img = Noneif (img_dataand isinstance(img_data, str)and (img_data.startswith("http://") or img_data.startswith("https://"))9 unmodified linesexcept Exception as e:logger.error(f"Failed to decode base64 image: {e}")if img:img_array = np.array(img)88 unmodified linesfn=api_capture_frame,inputs=[gr.Textbox(label="Image URL"),],outputs=api_json_output,api_name="capture_frame",7 unmodified linesgr.Number(label="Bed Temperature", value=60),gr.Number(label="Print Speed", value=60),gr.Number(label="Fan Speed", value=100),],outputs=api_json_output,api_name="lambda",
app.py+49-67