linx5o commited on
Commit
5c3c458
·
1 Parent(s): 1458d90

added od and growth control

Browse files
Files changed (1) hide show
  1. app.py +172 -0
app.py CHANGED
@@ -129,6 +129,27 @@ async def run_mqtt_client(client):
129
  client.loop_start()
130
  await periodic_temp_reading(client, reactor, st.session_state["experiment"])
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  if submit_button:
133
  client = create_mqtt_client(hivemq_host, hivemq_port, hivemq_username, hivemq_password)
134
 
@@ -524,6 +545,157 @@ if st.session_state["experiment"] is not None:
524
 
525
  st.divider()
526
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
527
  # Display the temperature graph
528
  # Using asyncio to run the loop in the background and periodically update the graph
529
 
 
129
  client.loop_start()
130
  await periodic_temp_reading(client, reactor, st.session_state["experiment"])
131
 
132
+ def get_running_jobs(client, reactor):
133
+ payload = {
134
+ "command": "get_worker",
135
+ "reactor": reactor
136
+ }
137
+ payload_str = json.dumps(payload)
138
+ client.publish("pioreactor/control", payload_str)
139
+
140
+ experiment = None
141
+ running = []
142
+
143
+ client.subscribe(f"pioreactor/{reactor}/worker")
144
+ client.message_callback_add(f"pioreactor/{reactor}/worker", on_message_worker)
145
+
146
+ timeout = 10
147
+ start_time = time.time()
148
+
149
+ while experiment is None and (time.time() - start_time) < timeout:
150
+ time.sleep(1)
151
+
152
+
153
  if submit_button:
154
  client = create_mqtt_client(hivemq_host, hivemq_port, hivemq_username, hivemq_password)
155
 
 
545
 
546
  st.divider()
547
 
548
+ cols = st.columns(2)
549
+
550
+ start_od = False
551
+ stop_od = False
552
+ start_growth = False
553
+ stop_growth = False
554
+
555
+ with cols[0]:
556
+ st.header("OD Reading")
557
+ if st.session_state["jobs"]["od_reading"]:
558
+ stop_od = st.button("Stop OD Reading")
559
+ else:
560
+ start_od = st.button("Start OD Reading")
561
+
562
+ with cols[1]:
563
+ st.header("Growth Rate")
564
+ if st.session_state["jobs"]["growth_rate_calculating"]:
565
+ stop_growth = st.button("Stop Growth Rate Calculation")
566
+ else:
567
+ start_growth = st.button("Start Growth Rate Calculation")
568
+
569
+ if start_od:
570
+ payload = {
571
+ "command": "start_od_reading",
572
+ "reactor": reactor,
573
+ "experiment": st.session_state["experiment"]
574
+ }
575
+ payload_str = json.dumps(payload)
576
+
577
+ st.session_state["client"].loop_start()
578
+ st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
579
+
580
+ time.sleep(1)
581
+
582
+ # Check if the OD reading job is running
583
+ get_running_jobs(st.session_state["client"], reactor)
584
+
585
+ st.session_state["client"].loop_stop()
586
+
587
+ if "od_reading" in running:
588
+ st.success("OD Reading started successfully!")
589
+ st.session_state["jobs"]["od_reading"] = True
590
+ else:
591
+ st.error("Failed to start OD Reading.")
592
+
593
+ for run in running:
594
+ st.session_state["jobs"][run] = True
595
+
596
+ st.session_state["experiment"] = experiment
597
+
598
+ time.sleep(3)
599
+ st.rerun()
600
+
601
+ if stop_od:
602
+ payload = {
603
+ "command": "stop_od_reading",
604
+ "reactor": reactor,
605
+ "experiment": st.session_state["experiment"]
606
+ }
607
+ payload_str = json.dumps(payload)
608
+
609
+ st.session_state["client"].loop_start()
610
+ st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
611
+
612
+ time.sleep(1)
613
+
614
+ # Check if the OD reading job has stopped
615
+ get_running_jobs(st.session_state["client"], reactor)
616
+
617
+ st.session_state["client"].loop_stop()
618
+
619
+ if "od_reading" not in running:
620
+ st.success("OD Reading stopped successfully!")
621
+ st.session_state["jobs"]["od_reading"] = False
622
+ else:
623
+ st.error("Failed to stop OD Reading.")
624
+
625
+ for run in running:
626
+ st.session_state["jobs"][run] = True
627
+
628
+ st.session_state["experiment"] = experiment
629
+
630
+ time.sleep(3)
631
+ st.rerun()
632
+
633
+ if start_growth:
634
+ payload = {
635
+ "command": "start_growth_rate",
636
+ "reactor": reactor,
637
+ "experiment": st.session_state["experiment"]
638
+ }
639
+ payload_str = json.dumps(payload)
640
+
641
+ st.session_state["client"].loop_start()
642
+ st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
643
+
644
+ time.sleep(1)
645
+
646
+ # Check if the growth rate job is running
647
+ get_running_jobs(st.session_state["client"], reactor)
648
+
649
+ st.session_state["client"].loop_stop()
650
+
651
+ if "growth_rate_calculating" in running:
652
+ st.success("Growth Rate Calculation started successfully!")
653
+ st.session_state["jobs"]["growth_rate_calculating"] = True
654
+ else:
655
+ st.error("Failed to start Growth Rate Calculation.")
656
+
657
+ for run in running:
658
+ st.session_state["jobs"][run] = True
659
+
660
+ st.session_state["experiment"] = experiment
661
+
662
+ time.sleep(3)
663
+ st.rerun()
664
+
665
+ if stop_growth:
666
+ payload = {
667
+ "command": "stop_growth_rate",
668
+ "reactor": reactor,
669
+ "experiment": st.session_state["experiment"]
670
+ }
671
+ payload_str = json.dumps(payload)
672
+
673
+ st.session_state["client"].loop_start()
674
+ st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
675
+
676
+ time.sleep(1)
677
+
678
+ # Check if the growth rate job has stopped
679
+ get_running_jobs(st.session_state["client"], reactor)
680
+
681
+ st.session_state["client"].loop_stop()
682
+
683
+ if "growth_rate_calculating" not in running:
684
+ st.success("Growth Rate Calculation stopped successfully!")
685
+ st.session_state["jobs"]["growth_rate_calculating"] = False
686
+ else:
687
+ st.error("Failed to stop Growth Rate Calculation.")
688
+
689
+ for run in running:
690
+ st.session_state["jobs"][run] = True
691
+
692
+ st.session_state["experiment"] = experiment
693
+
694
+ time.sleep(3)
695
+ st.rerun()
696
+
697
+ st.divider()
698
+
699
  # Display the temperature graph
700
  # Using asyncio to run the loop in the background and periodically update the graph
701