Spaces:
Sleeping
Sleeping
test uuid
Browse files
app.py
CHANGED
@@ -1,181 +1,27 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
client.subscribe(device_id + "/response")
|
20 |
-
client.on_message = on_message
|
21 |
-
client.loop_start()
|
22 |
-
|
23 |
-
def publish_and_wait(client, device_id, payload, response_queue):
|
24 |
-
client.publish(device_id, payload=json.dumps(payload), qos=2)
|
25 |
-
try:
|
26 |
-
response = response_queue.get(block=True, timeout=10)
|
27 |
-
except queue.Empty:
|
28 |
-
response = None
|
29 |
-
return response
|
30 |
-
|
31 |
-
def handle_image_display(image):
|
32 |
-
b64_bytes = base64.b64decode(image)
|
33 |
-
img_bytes = io.BytesIO(b64_bytes)
|
34 |
-
img = Image.open(img_bytes)
|
35 |
-
st.image(img, caption="Cobot camera view")
|
36 |
-
|
37 |
-
st.title("MyCobot280 Pi control demo")
|
38 |
-
|
39 |
-
st.markdown("""
|
40 |
-
This app is a public demo of ...
|
41 |
-
""")
|
42 |
-
|
43 |
-
use_own_creds = st.checkbox("Use custom broker", value=False)
|
44 |
-
|
45 |
-
if use_own_creds:
|
46 |
-
HIVEMQ_USERNAME = st.text_input("HIVEMQ Username", type="password")
|
47 |
-
HIVEMQ_PASSWORD = st.text_input("HIVEMQ Password", type="password")
|
48 |
-
HIVEMQ_HOST = st.text_input("HIVEMQ Host", type="password")
|
49 |
-
DEVICE_ID = st.text_input("Device ID", type="password")
|
50 |
-
PORT = st.number_input("Port", min_value=1, step=1, value=8883)
|
51 |
-
else:
|
52 |
-
HIVEMQ_USERNAME = os.environ.get("HIVEMQ_USERNAME")
|
53 |
-
HIVEMQ_PASSWORD = os.environ.get("HIVEMQ_PASSWORD")
|
54 |
-
HIVEMQ_HOST = os.environ.get("HIVEMQ_HOST")
|
55 |
-
DEVICE_ID = os.environ.get("DEVICE_ID")
|
56 |
-
PORT = int(os.environ.get("PORT", 8883))
|
57 |
-
|
58 |
-
if 'client' not in st.session_state:
|
59 |
-
st.session_state['client'] = None
|
60 |
-
st.session_state['response_queue'] = None
|
61 |
-
st.session_state['HIVEMQ_USERNAME'] = None
|
62 |
-
st.session_state['HIVEMQ_PASSWORD'] = None
|
63 |
-
st.session_state['HIVEMQ_HOST'] = None
|
64 |
-
st.session_state['DEVICE_ID'] = None
|
65 |
-
st.session_state['PORT'] = None
|
66 |
-
|
67 |
-
st.markdown("## Commands")
|
68 |
-
commands = [
|
69 |
-
"query/angles",
|
70 |
-
"query/coords",
|
71 |
-
"query/gripper",
|
72 |
-
"query/camera",
|
73 |
-
"control/angles",
|
74 |
-
"control/coords",
|
75 |
-
"control/gripper"
|
76 |
-
]
|
77 |
-
selected = st.selectbox("Select command: ", commands, key="selected_command", disabled=False)
|
78 |
-
|
79 |
-
args = {}
|
80 |
-
if st.session_state.selected_command == "query/angles":
|
81 |
-
pass
|
82 |
-
elif st.session_state.selected_command == "query/coords":
|
83 |
-
pass
|
84 |
-
elif st.session_state.selected_command == "query/gripper":
|
85 |
-
pass
|
86 |
-
elif st.session_state.selected_command == "query/camera":
|
87 |
-
quality = st.slider("Image quality", 1, 100, 50)
|
88 |
-
args = {"quality": quality}
|
89 |
-
elif st.session_state.selected_command == "control/angles":
|
90 |
-
col1, col2, col3 = st.columns(3)
|
91 |
-
with col1:
|
92 |
-
angle_1 = st.number_input("Angle 1", format="%.2f", step=5.0)
|
93 |
-
angle_4 = st.number_input("Angle 4", format="%.2f", step=5.0)
|
94 |
-
with col2:
|
95 |
-
angle_2 = st.number_input("Angle 2", format="%.2f", step=5.0)
|
96 |
-
angle_5 = st.number_input("Angle 5", format="%.2f", step=5.0)
|
97 |
-
with col3:
|
98 |
-
angle_3 = st.number_input("Angle 3", format="%.2f", step=5.0)
|
99 |
-
angle_6 = st.number_input("Angle 6", format="%.2f", step=5.0)
|
100 |
-
speed = st.slider("Speed", 1, 100, 50)
|
101 |
-
args = {"angles": [angle_1, angle_2, angle_3, angle_4, angle_5, angle_6], "speed": speed}
|
102 |
-
elif st.session_state.selected_command == "control/coords":
|
103 |
-
col1, col2, col3 = st.columns(3)
|
104 |
-
with col1:
|
105 |
-
x = st.number_input("x", format="%.2f", step=5.0)
|
106 |
-
rx = st.number_input("rx (Rotation x)", format="%.2f", step=5.0)
|
107 |
-
with col2:
|
108 |
-
y = st.number_input("y", format="%.2f", step=5.0)
|
109 |
-
ry = st.number_input("ry (Rotation y)", format="%.2f", step=5.0)
|
110 |
-
with col3:
|
111 |
-
z = st.number_input("z", format="%.2f", step=5.0)
|
112 |
-
rz = st.number_input("rz (Rotation z)", format="%.2f", step=5.0)
|
113 |
-
speed = st.slider("Speed", 1, 100, 50)
|
114 |
-
args = {"coords": [x, y, z, rx, ry, rz], "speed": speed}
|
115 |
-
elif st.session_state.selected_command == "control/gripper":
|
116 |
-
gripper_value = st.slider("Gripper value", 1, 100, 50)
|
117 |
-
speed = st.slider("Speed", 1, 100, 50)
|
118 |
-
args = {"gripper_value": gripper_value, "speed": speed}
|
119 |
-
|
120 |
-
with st.form("mqtt_form"):
|
121 |
-
submitted = st.form_submit_button("Send Command")
|
122 |
-
if submitted:
|
123 |
-
if st.session_state['client'] is not None:
|
124 |
-
credentials_changed = (
|
125 |
-
st.session_state.get('HIVEMQ_USERNAME') != HIVEMQ_USERNAME or
|
126 |
-
st.session_state.get('HIVEMQ_PASSWORD') != HIVEMQ_PASSWORD or
|
127 |
-
st.session_state.get('HIVEMQ_HOST') != HIVEMQ_HOST or
|
128 |
-
st.session_state.get('DEVICE_ID') != DEVICE_ID or
|
129 |
-
st.session_state.get('PORT') != PORT
|
130 |
-
)
|
131 |
-
if credentials_changed:
|
132 |
-
st.session_state['client'].disconnect()
|
133 |
-
st.session_state['client'] = None
|
134 |
-
|
135 |
-
if st.session_state['client'] is None:
|
136 |
-
st.session_state['response_queue'] = queue.Queue()
|
137 |
-
st.session_state['client'] = create_paho_client()
|
138 |
-
setup_paho_client(
|
139 |
-
st.session_state['client'],
|
140 |
-
HIVEMQ_USERNAME,
|
141 |
-
HIVEMQ_PASSWORD,
|
142 |
-
HIVEMQ_HOST,
|
143 |
-
PORT,
|
144 |
-
DEVICE_ID,
|
145 |
-
st.session_state['response_queue']
|
146 |
-
)
|
147 |
-
st.session_state['HIVEMQ_USERNAME'] = HIVEMQ_USERNAME
|
148 |
-
st.session_state['HIVEMQ_PASSWORD'] = HIVEMQ_PASSWORD
|
149 |
-
st.session_state['HIVEMQ_HOST'] = HIVEMQ_HOST
|
150 |
-
st.session_state['DEVICE_ID'] = DEVICE_ID
|
151 |
-
st.session_state['PORT'] = PORT
|
152 |
-
|
153 |
-
payload = {"command": st.session_state.selected_command, "args": args}
|
154 |
-
st.markdown("### Payload")
|
155 |
-
st.write(payload)
|
156 |
-
|
157 |
-
response = publish_and_wait(
|
158 |
-
st.session_state['client'],
|
159 |
-
DEVICE_ID,
|
160 |
-
payload,
|
161 |
-
st.session_state['response_queue']
|
162 |
)
|
163 |
-
st.markdown("### Response")
|
164 |
-
if response is not None:
|
165 |
-
if "image" in response:
|
166 |
-
image = response.pop("image")
|
167 |
-
st.write(response)
|
168 |
-
handle_image_display(image)
|
169 |
-
else:
|
170 |
-
st.write(response)
|
171 |
-
else:
|
172 |
-
st.write("Timed out waiting for response. Is the on-device server running?")
|
173 |
|
174 |
-
|
|
|
|
|
|
|
175 |
|
176 |
-
|
177 |
-
|
178 |
-
# Optionally enable autoplay which requires muting as browsers don't allow autoplay of unmuted video
|
179 |
-
# muted=True,
|
180 |
-
# autoplay=True
|
181 |
-
)
|
|
|
1 |
import streamlit as st
|
2 |
+
import uuid
|
3 |
+
|
4 |
+
def get_user_id():
|
5 |
+
if "user_id" not in st.session_state:
|
6 |
+
# JavaScript to check if 'user_id' exists in localStorage; if not, set it
|
7 |
+
st.components.v1.html(
|
8 |
+
"""
|
9 |
+
<script>
|
10 |
+
let userId = localStorage.getItem('user_id');
|
11 |
+
if (!userId) {
|
12 |
+
userId = '""" + str(uuid.uuid4()) + """';
|
13 |
+
localStorage.setItem('user_id', userId);
|
14 |
+
}
|
15 |
+
window.parent.postMessage(userId, "*");
|
16 |
+
</script>
|
17 |
+
""",
|
18 |
+
height=0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
# Capture user_id from the message
|
22 |
+
st.session_state.user_id = st.experimental_get_query_params().get("user_id", [None])[0]
|
23 |
+
|
24 |
+
return st.session_state.user_id
|
25 |
|
26 |
+
user_id = get_user_id()
|
27 |
+
st.write("Your user ID:", user_id)
|
|
|
|
|
|
|
|