gursi26 commited on
Commit
bcde10d
·
1 Parent(s): 5821d03

i think it worked

Browse files
Files changed (1) hide show
  1. app.py +41 -32
app.py CHANGED
@@ -3,7 +3,6 @@ import paho.mqtt.client as mqtt
3
  import queue, json, base64, io, os
4
  from PIL import Image
5
 
6
- @st.cache_resource
7
  def create_paho_client(tls=True):
8
  client = mqtt.Client(client_id="", userdata=None, protocol=mqtt.MQTTv5)
9
  if tls:
@@ -41,13 +40,14 @@ st.markdown("""
41
  This app is a public demo of ...
42
  """)
43
 
 
44
  use_own_creds = st.checkbox("Use your own credentials", value=False)
45
 
46
  if use_own_creds:
47
  HIVEMQ_USERNAME = st.text_input("HIVEMQ Username", type="password")
48
  HIVEMQ_PASSWORD = st.text_input("HIVEMQ Password", type="password")
49
- HIVEMQ_HOST = st.text_input("HIVEMQ Host", type="password")
50
- DEVICE_ID = st.text_input("Device ID", type="password")
51
  PORT = st.number_input("Port", min_value=1, step=1, value=8883)
52
  else:
53
  HIVEMQ_USERNAME = os.environ.get("HIVEMQ_USERNAME")
@@ -56,7 +56,7 @@ else:
56
  DEVICE_ID = os.environ.get("DEVICE_ID")
57
  PORT = int(os.environ.get("PORT", 8883))
58
 
59
- # Initialize or update the MQTT client and response queue in session state
60
  if 'client' not in st.session_state:
61
  st.session_state['client'] = None
62
  st.session_state['response_queue'] = None
@@ -66,32 +66,6 @@ if 'client' not in st.session_state:
66
  st.session_state['DEVICE_ID'] = None
67
  st.session_state['PORT'] = None
68
 
69
- credentials_changed = (
70
- st.session_state['HIVEMQ_USERNAME'] != HIVEMQ_USERNAME or
71
- st.session_state['HIVEMQ_PASSWORD'] != HIVEMQ_PASSWORD or
72
- st.session_state['HIVEMQ_HOST'] != HIVEMQ_HOST or
73
- st.session_state['DEVICE_ID'] != DEVICE_ID or
74
- st.session_state['PORT'] != PORT
75
- )
76
-
77
- if st.session_state['client'] is None or credentials_changed:
78
- st.session_state['HIVEMQ_USERNAME'] = HIVEMQ_USERNAME
79
- st.session_state['HIVEMQ_PASSWORD'] = HIVEMQ_PASSWORD
80
- st.session_state['HIVEMQ_HOST'] = HIVEMQ_HOST
81
- st.session_state['DEVICE_ID'] = DEVICE_ID
82
- st.session_state['PORT'] = PORT
83
- st.session_state['response_queue'] = queue.Queue()
84
- st.session_state['client'] = create_paho_client()
85
- setup_paho_client(
86
- st.session_state['client'],
87
- HIVEMQ_USERNAME,
88
- HIVEMQ_PASSWORD,
89
- HIVEMQ_HOST,
90
- PORT,
91
- DEVICE_ID,
92
- st.session_state['response_queue']
93
- )
94
-
95
  st.markdown("## Commands")
96
  commands = [
97
  "query/angles",
@@ -146,7 +120,42 @@ elif st.session_state.selected_command == "control/gripper":
146
  args = {"gripper_value": gripper_value, "speed": speed}
147
 
148
  with st.form("mqtt_form"):
149
- if st.form_submit_button("Send Command"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  payload = {"command": st.session_state.selected_command, "args": args}
151
  st.markdown("### Payload")
152
  st.write(payload)
@@ -166,4 +175,4 @@ with st.form("mqtt_form"):
166
  else:
167
  st.write(response)
168
  else:
169
- st.write("Timed out waiting for response. Is the on-device server running?")
 
3
  import queue, json, base64, io, os
4
  from PIL import Image
5
 
 
6
  def create_paho_client(tls=True):
7
  client = mqtt.Client(client_id="", userdata=None, protocol=mqtt.MQTTv5)
8
  if tls:
 
40
  This app is a public demo of ...
41
  """)
42
 
43
+ # Checkbox to use own credentials
44
  use_own_creds = st.checkbox("Use your own credentials", value=False)
45
 
46
  if use_own_creds:
47
  HIVEMQ_USERNAME = st.text_input("HIVEMQ Username", type="password")
48
  HIVEMQ_PASSWORD = st.text_input("HIVEMQ Password", type="password")
49
+ HIVEMQ_HOST = st.text_input("HIVEMQ Host")
50
+ DEVICE_ID = st.text_input("Device ID")
51
  PORT = st.number_input("Port", min_value=1, step=1, value=8883)
52
  else:
53
  HIVEMQ_USERNAME = os.environ.get("HIVEMQ_USERNAME")
 
56
  DEVICE_ID = os.environ.get("DEVICE_ID")
57
  PORT = int(os.environ.get("PORT", 8883))
58
 
59
+ # Initialize st.session_state variables if not already initialized
60
  if 'client' not in st.session_state:
61
  st.session_state['client'] = None
62
  st.session_state['response_queue'] = None
 
66
  st.session_state['DEVICE_ID'] = None
67
  st.session_state['PORT'] = None
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  st.markdown("## Commands")
70
  commands = [
71
  "query/angles",
 
120
  args = {"gripper_value": gripper_value, "speed": speed}
121
 
122
  with st.form("mqtt_form"):
123
+ submitted = st.form_submit_button("Send Command")
124
+ if submitted:
125
+ # Check if client exists in session_state
126
+ if st.session_state['client'] is not None:
127
+ # Check if credentials have changed
128
+ credentials_changed = (
129
+ st.session_state.get('HIVEMQ_USERNAME') != HIVEMQ_USERNAME or
130
+ st.session_state.get('HIVEMQ_PASSWORD') != HIVEMQ_PASSWORD or
131
+ st.session_state.get('HIVEMQ_HOST') != HIVEMQ_HOST or
132
+ st.session_state.get('DEVICE_ID') != DEVICE_ID or
133
+ st.session_state.get('PORT') != PORT
134
+ )
135
+ if credentials_changed:
136
+ # Disconnect old client and create new one
137
+ st.session_state['client'].disconnect()
138
+ st.session_state['client'] = None # Set to None to force reinitialization
139
+ if st.session_state['client'] is None:
140
+ # Create new client
141
+ st.session_state['response_queue'] = queue.Queue()
142
+ st.session_state['client'] = create_paho_client()
143
+ setup_paho_client(
144
+ st.session_state['client'],
145
+ HIVEMQ_USERNAME,
146
+ HIVEMQ_PASSWORD,
147
+ HIVEMQ_HOST,
148
+ PORT,
149
+ DEVICE_ID,
150
+ st.session_state['response_queue']
151
+ )
152
+ # Store the credentials in session_state
153
+ st.session_state['HIVEMQ_USERNAME'] = HIVEMQ_USERNAME
154
+ st.session_state['HIVEMQ_PASSWORD'] = HIVEMQ_PASSWORD
155
+ st.session_state['HIVEMQ_HOST'] = HIVEMQ_HOST
156
+ st.session_state['DEVICE_ID'] = DEVICE_ID
157
+ st.session_state['PORT'] = PORT
158
+
159
  payload = {"command": st.session_state.selected_command, "args": args}
160
  st.markdown("### Payload")
161
  st.write(payload)
 
175
  else:
176
  st.write(response)
177
  else:
178
+ st.write("Timed out waiting for response. Is the on-device server running?")