SissiFeng commited on
Commit
9502e34
·
1 Parent(s): ebe5556
Files changed (1) hide show
  1. app.py +88 -59
app.py CHANGED
@@ -218,6 +218,51 @@ def health_check():
218
  logger.info(f"Health check: {status}")
219
  return status
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  # 初始化MQTT客户端
222
  try:
223
  logger.info("Initializing MQTT client")
@@ -228,7 +273,7 @@ except Exception as e:
228
  logger.error(f"Failed to initialize MQTT: {e}")
229
  logger.warning("Starting without MQTT connection")
230
 
231
- # Gradio界面
232
  with gr.Blocks(title="Bambu A1 Mini Print Control") as demo:
233
  gr.Markdown("# Bambu A1 Mini Print Control")
234
 
@@ -256,78 +301,57 @@ with gr.Blocks(title="Bambu A1 Mini Print Control") as demo:
256
 
257
  send_params_btn = gr.Button("Send Print Parameters")
258
 
259
- # API端点
260
- def api_get_status(serial=DEFAULT_SERIAL):
261
- """API端点:获取状态"""
262
- logger.info(f"API call: get_status for {serial}")
263
- try:
264
- return get_data(serial)
265
- except Exception as e:
266
- logger.error(f"Error in api_get_status: {e}")
267
- return ("Error", "N/A", "N/A", str(e))
268
-
269
- def api_get_image():
270
- """API端点:获取图像"""
271
- logger.info("API call: get_image")
272
- try:
273
- img = capture_image()
274
- return get_image_base64(img)
275
- except Exception as e:
276
- logger.error(f"Error in api_get_image: {e}")
277
- return None
278
-
279
- def api_send_parameters(params):
280
- """API端点:发送参数"""
281
- logger.info(f"API call: send_parameters with {params}")
282
- try:
283
- serial = params.get('serial', DEFAULT_SERIAL)
284
- return send_print_parameters(
285
- params.get('nozzle_temp', 200),
286
- params.get('bed_temp', 60),
287
- params.get('print_speed', 60),
288
- params.get('fan_speed', 100),
289
- serial
290
- )
291
- except Exception as e:
292
- logger.error(f"Error in api_send_parameters: {e}")
293
- return f"Error: {str(e)}"
294
-
295
- def api_health_check():
296
- """API端点:健康检查"""
297
- logger.info("API call: health_check")
298
- try:
299
- return health_check()
300
- except Exception as e:
301
- logger.error(f"Error in api_health_check: {e}")
302
- return {"status": "error", "error": str(e)}
303
-
304
  # 连接按钮
305
  refresh_btn.click(
306
  fn=get_data,
307
  inputs=[serial],
308
- outputs=[current_status, current_bed_temp, current_nozzle_temp, last_update],
309
- api_name="refresh_status"
310
  )
311
 
312
  capture_btn.click(
313
  fn=capture_image,
314
- outputs=[captured_image],
315
- api_name="capture_image"
316
  )
317
 
318
  send_params_btn.click(
319
  fn=send_print_parameters,
320
  inputs=[nozzle_temp, bed_temp, print_speed, fan_speed, serial],
321
- outputs=[current_status],
322
- api_name="send_parameters"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
  )
324
 
325
- # 定义API端点
326
- demo.queue()
327
- gr.Interface(fn=api_get_status, inputs=[serial], outputs="json", title="Get Printer Status").launch(share=True)
328
- gr.Interface(fn=api_get_image, inputs=None, outputs="json", title="Get Print Image").launch(share=True)
329
- gr.Interface(fn=api_send_parameters, inputs="json", outputs="text", title="Send Print Parameters").launch(share=True)
330
- gr.Interface(fn=api_health_check, inputs=None, outputs="json", title="Health Check").launch(share=True)
 
 
 
 
 
 
 
 
 
331
 
332
  # 自动刷新线程
333
  def auto_refresh():
@@ -353,4 +377,9 @@ threading.Thread(target=auto_refresh, daemon=True).start()
353
  # 启动应用
354
  if __name__ == "__main__":
355
  logger.info("Starting Bambu A1 Mini Print Control application")
356
- demo.launch(show_error=True) # 启用详细错误报告
 
 
 
 
 
 
218
  logger.info(f"Health check: {status}")
219
  return status
220
 
221
+ # API端点函数
222
+ def api_get_status(serial=DEFAULT_SERIAL):
223
+ """API端点:获取状态"""
224
+ logger.info(f"API call: get_status for {serial}")
225
+ try:
226
+ return get_data(serial)
227
+ except Exception as e:
228
+ logger.error(f"Error in api_get_status: {e}")
229
+ return ("Error", "N/A", "N/A", str(e))
230
+
231
+ def api_get_image():
232
+ """API端点:获取图像"""
233
+ logger.info("API call: get_image")
234
+ try:
235
+ img = capture_image()
236
+ return get_image_base64(img)
237
+ except Exception as e:
238
+ logger.error(f"Error in api_get_image: {e}")
239
+ return None
240
+
241
+ def api_send_parameters(params):
242
+ """API端点:发送参数"""
243
+ logger.info(f"API call: send_parameters with {params}")
244
+ try:
245
+ serial = params.get('serial', DEFAULT_SERIAL)
246
+ return send_print_parameters(
247
+ params.get('nozzle_temp', 200),
248
+ params.get('bed_temp', 60),
249
+ params.get('print_speed', 60),
250
+ params.get('fan_speed', 100),
251
+ serial
252
+ )
253
+ except Exception as e:
254
+ logger.error(f"Error in api_send_parameters: {e}")
255
+ return f"Error: {str(e)}"
256
+
257
+ def api_health_check():
258
+ """API端点:健康检查"""
259
+ logger.info("API call: health_check")
260
+ try:
261
+ return health_check()
262
+ except Exception as e:
263
+ logger.error(f"Error in api_health_check: {e}")
264
+ return {"status": "error", "error": str(e)}
265
+
266
  # 初始化MQTT客户端
267
  try:
268
  logger.info("Initializing MQTT client")
 
273
  logger.error(f"Failed to initialize MQTT: {e}")
274
  logger.warning("Starting without MQTT connection")
275
 
276
+ # 创建主界面
277
  with gr.Blocks(title="Bambu A1 Mini Print Control") as demo:
278
  gr.Markdown("# Bambu A1 Mini Print Control")
279
 
 
301
 
302
  send_params_btn = gr.Button("Send Print Parameters")
303
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
304
  # 连接按钮
305
  refresh_btn.click(
306
  fn=get_data,
307
  inputs=[serial],
308
+ outputs=[current_status, current_bed_temp, current_nozzle_temp, last_update]
 
309
  )
310
 
311
  capture_btn.click(
312
  fn=capture_image,
313
+ outputs=[captured_image]
 
314
  )
315
 
316
  send_params_btn.click(
317
  fn=send_print_parameters,
318
  inputs=[nozzle_temp, bed_temp, print_speed, fan_speed, serial],
319
+ outputs=[current_status]
320
+ )
321
+
322
+ # 创建API端点
323
+ with gr.Blocks() as api_blocks:
324
+ gr.Interface(
325
+ fn=api_get_status,
326
+ inputs=gr.Textbox(value=DEFAULT_SERIAL, label="Serial Number"),
327
+ outputs="json",
328
+ title="Get Printer Status",
329
+ description="Get the current status of the printer"
330
+ )
331
+
332
+ gr.Interface(
333
+ fn=api_get_image,
334
+ inputs=None,
335
+ outputs="json",
336
+ title="Get Print Image",
337
+ description="Get the current print image as base64"
338
  )
339
 
340
+ gr.Interface(
341
+ fn=api_send_parameters,
342
+ inputs="json",
343
+ outputs="text",
344
+ title="Send Print Parameters",
345
+ description="Send parameters to the printer"
346
+ )
347
+
348
+ gr.Interface(
349
+ fn=api_health_check,
350
+ inputs=None,
351
+ outputs="json",
352
+ title="Health Check",
353
+ description="Check the health of the application"
354
+ )
355
 
356
  # 自动刷新线程
357
  def auto_refresh():
 
377
  # 启动应用
378
  if __name__ == "__main__":
379
  logger.info("Starting Bambu A1 Mini Print Control application")
380
+ # 启动主界面
381
+ demo.queue().launch(show_error=True)
382
+
383
+ # 启动API端点
384
+ # 注意:在实际部署中,这些API端点应该与主界面集成在一起
385
+ # 这里分开只是为了演示目的