Spaces:
Sleeping
Sleeping
added start of app
Browse filesincludes a dash showing running ops
app.py
CHANGED
|
@@ -1,4 +1,114 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import paho.mqtt.client as mqtt
|
| 3 |
+
import json
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
+
|
| 7 |
+
# Create a form to get the MQTT broker details
|
| 8 |
+
st.title("PioReactor MQTT Client")
|
| 9 |
+
with st.form("mqtt_form"):
|
| 10 |
+
hivemq_host = st.text_input("HiveMQ Host", "9bb69fc9bf6b4092abc4c92d76e602cd.s1.eu.hivemq.cloud", type="password")
|
| 11 |
+
hivemq_username = st.text_input("HiveMQ Username", "Website", type="password")
|
| 12 |
+
hivemq_password = st.text_input("HiveMQ Password", "Abcabcabc1", type="password")
|
| 13 |
+
hivemq_port = st.number_input("HiveMQ Port", min_value=1, max_value=65535, step=1, value=8883)
|
| 14 |
+
reactor = st.text_input("PioReactor", "pio1")
|
| 15 |
+
|
| 16 |
+
submit_button = st.form_submit_button("Connect")
|
| 17 |
+
|
| 18 |
+
experiment = None
|
| 19 |
+
running = []
|
| 20 |
+
jobs = {
|
| 21 |
+
"temperature_automation": False,
|
| 22 |
+
"growth_rate_calculating": False,
|
| 23 |
+
"stirring": False,
|
| 24 |
+
"od_reading": False,
|
| 25 |
+
"led_automation": False
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
# Create the MQTT client
|
| 29 |
+
@st.cache_resource
|
| 30 |
+
def create_mqtt_client(hivemq_host, hivemq_port, hivemq_username, hivemq_password):
|
| 31 |
+
client = mqtt.Client()
|
| 32 |
+
|
| 33 |
+
# Set authentication and TLS settings
|
| 34 |
+
client.username_pw_set(hivemq_username, hivemq_password)
|
| 35 |
+
client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS_CLIENT)
|
| 36 |
+
|
| 37 |
+
# Define callback functions
|
| 38 |
+
def on_connect(client, userdata, flags, rc):
|
| 39 |
+
if rc == 0:
|
| 40 |
+
st.success("Connected to HiveMQ successfully!")
|
| 41 |
+
else:
|
| 42 |
+
st.error(f"Failed to connect, return code {rc}")
|
| 43 |
+
|
| 44 |
+
client.on_connect = on_connect
|
| 45 |
+
|
| 46 |
+
return client
|
| 47 |
+
|
| 48 |
+
def on_message_worker(client, userdata, message):
|
| 49 |
+
payload = message.payload.decode("utf-8")
|
| 50 |
+
data = json.loads(payload)
|
| 51 |
+
global experiment
|
| 52 |
+
global running
|
| 53 |
+
experiment = data.get("experiment", None)
|
| 54 |
+
running = data.get("running", [])
|
| 55 |
+
|
| 56 |
+
if submit_button:
|
| 57 |
+
client = create_mqtt_client(hivemq_host, hivemq_port, hivemq_username, hivemq_password)
|
| 58 |
+
|
| 59 |
+
# Publish a message to the MQTT broker
|
| 60 |
+
if client:
|
| 61 |
+
client.connect(hivemq_host, hivemq_port)
|
| 62 |
+
client.loop_start()
|
| 63 |
+
|
| 64 |
+
# Subscribe to the worker topic
|
| 65 |
+
client.subscribe(f"pioreactor/{reactor}/worker")
|
| 66 |
+
client.message_callback_add(f"pioreactor/{reactor}/worker", on_message_worker)
|
| 67 |
+
|
| 68 |
+
payload = {
|
| 69 |
+
"command": "get_worker",
|
| 70 |
+
"reactor": reactor
|
| 71 |
+
}
|
| 72 |
+
payload_str = json.dumps(payload)
|
| 73 |
+
client.publish("pioreactor/control", payload_str)
|
| 74 |
+
|
| 75 |
+
timeout = 10
|
| 76 |
+
start_time = time.time()
|
| 77 |
+
|
| 78 |
+
while experiment is None and (time.time() - start_time) < timeout:
|
| 79 |
+
time.sleep(1)
|
| 80 |
+
|
| 81 |
+
client.loop_stop()
|
| 82 |
+
client.unsubscribe(f"pioreactor/{reactor}/worker")
|
| 83 |
+
|
| 84 |
+
if experiment is None:
|
| 85 |
+
st.error("No experiment assigned to the reactor.")
|
| 86 |
+
st.stop()
|
| 87 |
+
|
| 88 |
+
for run in running:
|
| 89 |
+
jobs[run] = True
|
| 90 |
+
|
| 91 |
+
if experiment is not None:
|
| 92 |
+
st.title(f"Experiment: {experiment}")
|
| 93 |
+
|
| 94 |
+
cols = st.columns(5)
|
| 95 |
+
|
| 96 |
+
with cols[0]:
|
| 97 |
+
st.write("Temperature")
|
| 98 |
+
st.write(jobs["temperature_automation"])
|
| 99 |
+
|
| 100 |
+
with cols[1]:
|
| 101 |
+
st.write("Growth Rate")
|
| 102 |
+
st.write(jobs["growth_rate_calculating"])
|
| 103 |
+
|
| 104 |
+
with cols[2]:
|
| 105 |
+
st.write("Stirring")
|
| 106 |
+
st.write(jobs["stirring"])
|
| 107 |
+
|
| 108 |
+
with cols[3]:
|
| 109 |
+
st.write("OD Reading")
|
| 110 |
+
st.write(jobs["od_reading"])
|
| 111 |
+
|
| 112 |
+
with cols[4]:
|
| 113 |
+
st.write("LED")
|
| 114 |
+
st.write(jobs["led_automation"])
|