linx5o commited on
Commit
fd63cb6
·
1 Parent(s): eccddd2

added temp automation control

Browse files
Files changed (1) hide show
  1. app.py +196 -0
app.py CHANGED
@@ -280,3 +280,199 @@ if st.session_state["experiment"] is not None:
280
 
281
  time.sleep(3)
282
  st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
 
281
  time.sleep(3)
282
  st.rerun()
283
+
284
+ st.divider()
285
+
286
+ st.header("Temperature Automation")
287
+
288
+ if st.session_state["jobs"]["temperature_automation"]:
289
+ temp = None
290
+ if st.session_state.get("temp_auto", None) is None:
291
+ st.write("Current Temperature Automation Unknown, changes will result in restart of the automation to new settings.")
292
+ temp_auto = st.selectbox("Temperature Automation", ["Record Temp Only", "Heat To Temp"], placeholder="UNKNOWN")
293
+ with st.form("temperature_form"):
294
+ if temp_auto == "Heat To Temp":
295
+ temp = st.slider("Temperature", min_value=0.0, max_value=60.0, step=0.1, value=30.0)
296
+ cols = st.columns(2)
297
+ with cols[0]:
298
+ temp_submit = st.form_submit_button("Restart Temperature Automation")
299
+ with cols[1]:
300
+ temp_stop = st.form_submit_button("Stop Temperature Automation")
301
+ elif temp_auto == "Record Temp Only":
302
+ cols = st.columns(2)
303
+ with cols[0]:
304
+ temp_submit = st.form_submit_button("Restart Temperature Automation")
305
+ with cols[1]:
306
+ temp_stop = st.form_submit_button("Stop Temperature Automation")
307
+ else:
308
+ if st.session_state["temp_auto"] == "Heat To Temp":
309
+ index = 1
310
+ elif st.session_state["temp_auto"] == "Record Temp Only":
311
+ index = 0
312
+ temp_auto = st.selectbox("Temperature Automation", ["Record Temp Only", "Heat To Temp"], index=index)
313
+ with st.form("temperature_form"):
314
+ st.write(f"Current Temperature Automation: {st.session_state['temp_auto']}")
315
+ if temp_auto == "Heat To Temp":
316
+ if st.session_state.get("temp", None) is not None:
317
+ st.write(f"Current Temperature: {st.session_state['temp']} °C")
318
+ temp = st.slider("Temperature", min_value=0.0, max_value=60.0, step=0.1, value=st.session_state["temp"] if st.session_state["temp"] is not None else 30.0)
319
+ cols = st.columns(2)
320
+ with cols[0]:
321
+ temp_submit = st.form_submit_button("Update Temperature Automation")
322
+ with cols[1]:
323
+ temp_stop = st.form_submit_button("Stop Temperature Automation")
324
+ elif temp_auto == "Record Temp Only":
325
+ cols = st.columns(2)
326
+ with cols[0]:
327
+ temp_submit = st.form_submit_button("Update Temperature Automation", disabled=(st.session_state["temp_auto"] == "Record Temp Only"))
328
+ with cols[1]:
329
+ temp_stop = st.form_submit_button("Stop Temperature Automation")
330
+
331
+ else:
332
+ temp_auto = st.selectbox("Temperature Automation", ["Record Temp Only", "Heat To Temp"], placeholder="Select an option")
333
+ with st.form("temperature_form"):
334
+ if temp_auto == "Heat To Temp":
335
+ temp = st.slider("Temperature", min_value=0.0, max_value=60.0, step=0.1, value=30.0)
336
+ temp_submit = st.form_submit_button("Start Temperature Automation")
337
+ temp_stop = False
338
+ elif temp_auto == "Record Temp Only":
339
+ temp_submit = st.form_submit_button("Start Temperature Automation")
340
+ temp_stop = False
341
+
342
+ if temp_submit:
343
+ # Convert the temperature automation to the payload
344
+ if temp_auto == "Heat To Temp":
345
+ payload_auto = "thermostat"
346
+ elif temp_auto == "Record Temp Only":
347
+ payload_auto = "only_record_temperature"
348
+
349
+ # Update the temperature automation
350
+ if st.session_state["jobs"]["temperature_automation"]:
351
+ #Check if the temperature automation is the same as the current one
352
+ if temp_auto == st.session_state.get("temp_auto", None):
353
+ # Same automation, update the temperature
354
+ # Update the temperature automation
355
+ payload = {
356
+ "command": "temp_update",
357
+ "reactor": reactor,
358
+ "experiment": st.session_state["experiment"],
359
+ "settings": {
360
+ "target_temperature": temp
361
+ }
362
+ }
363
+ else:
364
+ # Different automation, restart the automation
365
+ payload = {
366
+ "command": "temp_restart",
367
+ "reactor": reactor,
368
+ "experiment": st.session_state["experiment"],
369
+ "automation": payload_auto,
370
+ "temp": temp
371
+ }
372
+ else:
373
+ # Start the temperature automation
374
+ payload = {
375
+ "command": "set_temperature_automation",
376
+ "reactor": reactor,
377
+ "experiment": st.session_state["experiment"],
378
+ "automation": payload_auto,
379
+ "temp": temp
380
+ }
381
+
382
+ payload_str = json.dumps(payload)
383
+
384
+ # Start looping
385
+ st.session_state["client"].loop_start()
386
+ st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
387
+
388
+ time.sleep(1)
389
+
390
+ # Check if the stirring job is running
391
+ payload = {
392
+ "command": "get_worker",
393
+ "reactor": reactor
394
+ }
395
+
396
+ experiment = None
397
+ running = []
398
+
399
+ st.session_state["client"].subscribe(f"pioreactor/{reactor}/worker")
400
+ st.session_state["client"].message_callback_add(f"pioreactor/{reactor}/worker", on_message_worker)
401
+
402
+ payload_str = json.dumps(payload)
403
+ st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
404
+
405
+ timeout = 10
406
+ start_time = time.time()
407
+
408
+ while experiment is None and (time.time() - start_time) < timeout:
409
+ time.sleep(1)
410
+
411
+ st.session_state["client"].loop_stop()
412
+
413
+ if "temperature_automation" in running and st.session_state["jobs"]["temperature_automation"]:
414
+ st.success("Temperature automation updated!")
415
+ st.session_state["temp_auto"] = temp_auto
416
+ st.session_state["temp"] = temp
417
+ elif "temperature_automation" in running:
418
+ st.success("Temperature automation started successfully!")
419
+ st.session_state["jobs"]["temperature_automation"] = True
420
+ st.session_state["temp_auto"] = temp_auto
421
+ st.session_state["temp"] = temp
422
+ else:
423
+ st.error("Failed to update temperature automation speed.")
424
+
425
+ time.sleep(3)
426
+ st.rerun()
427
+
428
+ if temp_stop:
429
+ payload = {
430
+ "command": "temp_update",
431
+ "reactor": reactor,
432
+ "experiment": st.session_state["experiment"],
433
+ "settings": {
434
+ "$state": "disconnect"
435
+ }
436
+ }
437
+
438
+ payload_str = json.dumps(payload)
439
+
440
+ st.session_state["client"].loop_start()
441
+ st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
442
+
443
+ time.sleep(1)
444
+
445
+ # Check if the temperature automation job has stopped
446
+ payload = {
447
+ "command": "get_worker",
448
+ "reactor": reactor
449
+ }
450
+
451
+ experiment = None
452
+ running = []
453
+
454
+ st.session_state["client"].subscribe(f"pioreactor/{reactor}/worker")
455
+ st.session_state["client"].message_callback_add(f"pioreactor/{reactor}/worker", on_message_worker)
456
+
457
+ payload_str = json.dumps(payload)
458
+
459
+ st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
460
+
461
+ timeout = 10
462
+ start_time = time.time()
463
+
464
+ while experiment is None and (time.time() - start_time) < timeout:
465
+ time.sleep(1)
466
+
467
+ if "temperature_automation" not in running:
468
+ st.success("Temperature automation stopped successfully!")
469
+ st.session_state["temp_auto"] = None
470
+ st.session_state["temp"] = None
471
+ st.session_state["jobs"]["temperature_automation"] = False
472
+ else:
473
+ st.error("Failed to stop temperature automation.")
474
+
475
+ time.sleep(3)
476
+ st.rerun()
477
+
478
+ st.divider()