j-woo commited on
Commit
0ab1fe2
·
1 Parent(s): 26dfcef

removed all test image stuff

Browse files
Files changed (1) hide show
  1. app.py +49 -67
app.py CHANGED
@@ -47,8 +47,8 @@ latest_data = {
47
  "status": "N/A",
48
  "update_time": "Waiting for data...",
49
  "image_url": "N/A",
50
- "progress": 0,
51
- "message": "",
52
  }
53
 
54
  bambu_client = None
@@ -98,52 +98,57 @@ def bambu_on_message(client, userdata, message):
98
 
99
  def rpi_on_message(client, userdata, msg):
100
  global latest_data
101
-
102
  try:
103
  payload = msg.payload.decode("utf-8")
104
  logger.info(f"Received message from RPI: {payload}")
105
-
106
  data = json.loads(payload)
107
  status = data.get("status", "Unknown")
108
-
109
  latest_data["status"] = status
110
- latest_data["update_time"] = data.get("update_time", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
111
-
 
 
112
  if "nozzle_temperature" in data:
113
  latest_data["nozzle_temperature"] = data["nozzle_temperature"]
114
-
115
  if "bed_temperature" in data:
116
  latest_data["bed_temperature"] = data["bed_temperature"]
117
-
118
  if "error" in data:
119
  logger.error(f"Error from RPI: {data['error']}")
120
-
121
  if status == "Ready":
122
  latest_data["progress"] = 0
123
  latest_data["message"] = "Printer ready"
124
  elif status == "Processing":
125
  latest_data["progress"] = 25
126
  latest_data["message"] = "Processing G-code..."
127
-
128
  except Exception as e:
129
  logger.error(f"Error processing message from RPI: {e}")
130
-
131
  try:
132
  result = json.loads(payload)
133
-
134
  if "status" in result:
135
  status = result["status"]
136
  latest_data["status"] = status
137
-
138
- if "command" in result and result["command"] == "capture_image" and result.get("auto_triggered", False):
 
 
 
 
139
  logger.info("receive capture command")
140
- threading.Thread(target=handle_auto_capture, args=(result,), daemon=True).start()
141
-
 
 
142
  except json.JSONDecodeError:
143
  logger.error(f"Invalid JSON in message: {payload}")
144
-
145
- except Exception as e:
146
- logger.error(f"Error processing RPI message: {e}")
147
 
148
 
149
  def get_data(serial=DEFAULT_SERIAL):
@@ -202,11 +207,13 @@ def send_print_parameters(nozzle_temp, bed_temp, print_speed, fan_speed):
202
  json.dumps({"command": "generate_gcode", "parameters": params}),
203
  )
204
  logger.info("Parameters sent successfully to RPi for G-code generation")
205
-
206
  global latest_data
207
  latest_data["status"] = "Sent"
208
- latest_data["update_time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
209
-
 
 
210
  return "Parameters sent successfully to RPi. Status: Sent"
211
  else:
212
  logger.warning("MQTT not connected, parameters not sent")
@@ -215,13 +222,11 @@ def send_print_parameters(nozzle_temp, bed_temp, print_speed, fan_speed):
215
  logger.error(f"Error sending parameters: {e}")
216
  return f"Error sending parameters: {e}"
217
 
218
-
219
  latest_data["status"] = "Sending"
220
  latest_data["progress"] = 10
221
  latest_data["message"] = "Sending parameters to printer..."
222
 
223
 
224
-
225
  def get_image_base64(image):
226
  if image is None:
227
  logger.warning("No image to encode")
@@ -244,21 +249,21 @@ def get_image_base64(image):
244
  def handle_auto_capture(message):
245
  try:
246
  logger.info(f"receive capture command: {message}")
247
-
248
  print_job = message.get("print_job", "unknown_job")
249
  timestamp = message.get("timestamp", time.strftime("%Y-%m-%d %H:%M:%S"))
250
-
251
  latest_data["auto_capture_requested"] = True
252
  latest_data["last_capture_job"] = print_job
253
  latest_data["last_capture_time"] = timestamp
254
-
255
  return capture_image()
256
  except Exception as e:
257
  logger.error(f"error: {e}")
258
  return None, f"capture failed: {str(e)}"
259
 
260
 
261
- def capture_image(url=None, use_test_image=False, test_image_name=None):
262
  global rpi_client, latest_data
263
 
264
  if rpi_client is None:
@@ -289,14 +294,6 @@ def capture_image(url=None, use_test_image=False, test_image_name=None):
289
 
290
  url = latest_data["image_url"]
291
 
292
- if use_test_image:
293
- logger.info("Using test image instead of URL")
294
- test_img = get_test_image(test_image_name)
295
- if test_img:
296
- return test_img
297
- else:
298
- logger.warning("Failed to get specified test image, trying URL")
299
-
300
  if url != "N/A":
301
  try:
302
  logger.info(f"Capturing image from URL: {url}")
@@ -310,16 +307,17 @@ def capture_image(url=None, use_test_image=False, test_image_name=None):
310
  else:
311
  raise Exception("url is 'N/A'")
312
 
 
313
  def update_print_status():
314
- global latest_data
315
- return (
316
- latest_data["status"],
317
- latest_data["nozzle_temperature"],
318
- latest_data["bed_temperature"],
319
- latest_data["update_time"],
320
- latest_data.get("progress", 0),
321
- latest_data.get("message", ""),
322
- )
323
 
324
 
325
  def health_check():
@@ -420,13 +418,11 @@ with demo:
420
  logger.info("API call: get_data")
421
  return get_data()
422
 
423
- def api_capture_frame(url=None, use_test_image=False, test_image_name=None):
424
- logger.info(
425
- f"API call: capture_frame with URL: {url}, use_test_image: {use_test_image}"
426
- )
427
 
428
  try:
429
- img = capture_image(url, use_test_image, test_image_name)
430
  if img:
431
  if img.mode == "RGBA":
432
  img = img.convert("RGB")
@@ -448,20 +444,14 @@ with demo:
448
  param_2=60,
449
  param_3=60,
450
  param_4=100,
451
- use_test_image=False,
452
- test_image_name=None,
453
  ):
454
  logger.info(
455
- 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}"
456
  )
457
  try:
458
  img = None
459
 
460
- if use_test_image:
461
- logger.info(f"Lambda using test image: {test_image_name}")
462
- img = get_test_image(test_image_name)
463
-
464
- elif (
465
  img_data
466
  and isinstance(img_data, str)
467
  and (img_data.startswith("http://") or img_data.startswith("https://"))
@@ -477,10 +467,6 @@ with demo:
477
  except Exception as e:
478
  logger.error(f"Failed to decode base64 image: {e}")
479
 
480
- if img is None:
481
- logger.info("No valid image data received, using default test image")
482
- img = get_test_image()
483
-
484
  if img:
485
  img_array = np.array(img)
486
 
@@ -575,8 +561,6 @@ with demo:
575
  fn=api_capture_frame,
576
  inputs=[
577
  gr.Textbox(label="Image URL"),
578
- gr.Checkbox(label="Use Test Image", value=False),
579
- gr.Textbox(label="Test Image Name", value=""),
580
  ],
581
  outputs=api_json_output,
582
  api_name="capture_frame",
@@ -590,8 +574,6 @@ with demo:
590
  gr.Number(label="Bed Temperature", value=60),
591
  gr.Number(label="Print Speed", value=60),
592
  gr.Number(label="Fan Speed", value=100),
593
- gr.Checkbox(label="Use Test Image", value=False),
594
- gr.Textbox(label="Test Image Name", value=""),
595
  ],
596
  outputs=api_json_output,
597
  api_name="lambda",
 
47
  "status": "N/A",
48
  "update_time": "Waiting for data...",
49
  "image_url": "N/A",
50
+ "progress": 0,
51
+ "message": "",
52
  }
53
 
54
  bambu_client = None
 
98
 
99
  def rpi_on_message(client, userdata, msg):
100
  global latest_data
101
+
102
  try:
103
  payload = msg.payload.decode("utf-8")
104
  logger.info(f"Received message from RPI: {payload}")
105
+
106
  data = json.loads(payload)
107
  status = data.get("status", "Unknown")
108
+
109
  latest_data["status"] = status
110
+ latest_data["update_time"] = data.get(
111
+ "update_time", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
112
+ )
113
+
114
  if "nozzle_temperature" in data:
115
  latest_data["nozzle_temperature"] = data["nozzle_temperature"]
116
+
117
  if "bed_temperature" in data:
118
  latest_data["bed_temperature"] = data["bed_temperature"]
119
+
120
  if "error" in data:
121
  logger.error(f"Error from RPI: {data['error']}")
122
+
123
  if status == "Ready":
124
  latest_data["progress"] = 0
125
  latest_data["message"] = "Printer ready"
126
  elif status == "Processing":
127
  latest_data["progress"] = 25
128
  latest_data["message"] = "Processing G-code..."
129
+
130
  except Exception as e:
131
  logger.error(f"Error processing message from RPI: {e}")
132
+
133
  try:
134
  result = json.loads(payload)
135
+
136
  if "status" in result:
137
  status = result["status"]
138
  latest_data["status"] = status
139
+
140
+ if (
141
+ "command" in result
142
+ and result["command"] == "capture_image"
143
+ and result.get("auto_triggered", False)
144
+ ):
145
  logger.info("receive capture command")
146
+ threading.Thread(
147
+ target=handle_auto_capture, args=(result,), daemon=True
148
+ ).start()
149
+
150
  except json.JSONDecodeError:
151
  logger.error(f"Invalid JSON in message: {payload}")
 
 
 
152
 
153
 
154
  def get_data(serial=DEFAULT_SERIAL):
 
207
  json.dumps({"command": "generate_gcode", "parameters": params}),
208
  )
209
  logger.info("Parameters sent successfully to RPi for G-code generation")
210
+
211
  global latest_data
212
  latest_data["status"] = "Sent"
213
+ latest_data["update_time"] = time.strftime(
214
+ "%Y-%m-%d %H:%M:%S", time.localtime()
215
+ )
216
+
217
  return "Parameters sent successfully to RPi. Status: Sent"
218
  else:
219
  logger.warning("MQTT not connected, parameters not sent")
 
222
  logger.error(f"Error sending parameters: {e}")
223
  return f"Error sending parameters: {e}"
224
 
 
225
  latest_data["status"] = "Sending"
226
  latest_data["progress"] = 10
227
  latest_data["message"] = "Sending parameters to printer..."
228
 
229
 
 
230
  def get_image_base64(image):
231
  if image is None:
232
  logger.warning("No image to encode")
 
249
  def handle_auto_capture(message):
250
  try:
251
  logger.info(f"receive capture command: {message}")
252
+
253
  print_job = message.get("print_job", "unknown_job")
254
  timestamp = message.get("timestamp", time.strftime("%Y-%m-%d %H:%M:%S"))
255
+
256
  latest_data["auto_capture_requested"] = True
257
  latest_data["last_capture_job"] = print_job
258
  latest_data["last_capture_time"] = timestamp
259
+
260
  return capture_image()
261
  except Exception as e:
262
  logger.error(f"error: {e}")
263
  return None, f"capture failed: {str(e)}"
264
 
265
 
266
+ def capture_image(url=None):
267
  global rpi_client, latest_data
268
 
269
  if rpi_client is None:
 
294
 
295
  url = latest_data["image_url"]
296
 
 
 
 
 
 
 
 
 
297
  if url != "N/A":
298
  try:
299
  logger.info(f"Capturing image from URL: {url}")
 
307
  else:
308
  raise Exception("url is 'N/A'")
309
 
310
+
311
  def update_print_status():
312
+ global latest_data
313
+ return (
314
+ latest_data["status"],
315
+ latest_data["nozzle_temperature"],
316
+ latest_data["bed_temperature"],
317
+ latest_data["update_time"],
318
+ latest_data.get("progress", 0),
319
+ latest_data.get("message", ""),
320
+ )
321
 
322
 
323
  def health_check():
 
418
  logger.info("API call: get_data")
419
  return get_data()
420
 
421
+ def api_capture_frame(url=None):
422
+ logger.info(f"API call: capture_frame with URL: {url}")
 
 
423
 
424
  try:
425
+ img = capture_image(url)
426
  if img:
427
  if img.mode == "RGBA":
428
  img = img.convert("RGB")
 
444
  param_2=60,
445
  param_3=60,
446
  param_4=100,
 
 
447
  ):
448
  logger.info(
449
+ f"API call: lambda with params: {param_1}, {param_2}, {param_3}, {param_4}"
450
  )
451
  try:
452
  img = None
453
 
454
+ if (
 
 
 
 
455
  img_data
456
  and isinstance(img_data, str)
457
  and (img_data.startswith("http://") or img_data.startswith("https://"))
 
467
  except Exception as e:
468
  logger.error(f"Failed to decode base64 image: {e}")
469
 
 
 
 
 
470
  if img:
471
  img_array = np.array(img)
472
 
 
561
  fn=api_capture_frame,
562
  inputs=[
563
  gr.Textbox(label="Image URL"),
 
 
564
  ],
565
  outputs=api_json_output,
566
  api_name="capture_frame",
 
574
  gr.Number(label="Bed Temperature", value=60),
575
  gr.Number(label="Print Speed", value=60),
576
  gr.Number(label="Fan Speed", value=100),
 
 
577
  ],
578
  outputs=api_json_output,
579
  api_name="lambda",