gursi26 commited on
Commit
99baf46
1 Parent(s): c5bd0e7
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -35,7 +35,7 @@ def handle_image_display(image):
35
  return img
36
 
37
  with gr.Blocks() as demo:
38
- gr.Markdown("# MyCobot280 Pi control demo")
39
 
40
  gr.Markdown("""
41
  This app is a public demo of ...
@@ -185,19 +185,19 @@ with gr.Blocks() as demo:
185
  PORT = int(os.environ.get("PORT", 8883))
186
 
187
  # Check if client is already connected and credentials haven't changed
188
- if client_state is not None and client_state.value is not None:
189
  credentials_changed = (
190
- previous_credentials_state.value['username'] != HIVEMQ_USERNAME or
191
- previous_credentials_state.value['password'] != HIVEMQ_PASSWORD or
192
- previous_credentials_state.value['host'] != HIVEMQ_HOST or
193
- previous_credentials_state.value['device_id'] != DEVICE_ID or
194
- previous_credentials_state.value['port'] != PORT
195
  )
196
  if credentials_changed:
197
- client_state.value.disconnect()
198
- client_state.value = None
199
 
200
- if client_state.value is None:
201
  response_queue = queue.Queue()
202
  client = create_paho_client()
203
  try:
@@ -214,10 +214,10 @@ with gr.Blocks() as demo:
214
  payload_md = ""
215
  response_md = f"### Error\nFailed to connect to broker: {e}"
216
  return payload_md, response_md, None, client_state, response_queue_state, previous_credentials_state
217
-
218
- client_state.value = client
219
- response_queue_state.value = response_queue
220
- previous_credentials_state.value = {
221
  'username': HIVEMQ_USERNAME,
222
  'password': HIVEMQ_PASSWORD,
223
  'host': HIVEMQ_HOST,
@@ -225,8 +225,8 @@ with gr.Blocks() as demo:
225
  'port': PORT
226
  }
227
  else:
228
- client = client_state.value
229
- response_queue = response_queue_state.value
230
 
231
  # Prepare payload
232
  payload = {"command": selected_command, "args": args}
@@ -253,7 +253,7 @@ with gr.Blocks() as demo:
253
  else:
254
  response_md = "### Response\nTimed out waiting for response. Is the on-device server running?"
255
  return payload_md, response_md, None, client_state, response_queue_state, previous_credentials_state
256
-
257
  # Now, set up the function to be called when the "Send Command" button is clicked
258
  send_command_button.click(
259
  fn=send_command,
 
35
  return img
36
 
37
  with gr.Blocks() as demo:
38
+ gr.Markdown("# MyCobot280 Pi Control Demo")
39
 
40
  gr.Markdown("""
41
  This app is a public demo of ...
 
185
  PORT = int(os.environ.get("PORT", 8883))
186
 
187
  # Check if client is already connected and credentials haven't changed
188
+ if client_state is not None:
189
  credentials_changed = (
190
+ previous_credentials_state['username'] != HIVEMQ_USERNAME or
191
+ previous_credentials_state['password'] != HIVEMQ_PASSWORD or
192
+ previous_credentials_state['host'] != HIVEMQ_HOST or
193
+ previous_credentials_state['device_id'] != DEVICE_ID or
194
+ previous_credentials_state['port'] != PORT
195
  )
196
  if credentials_changed:
197
+ client_state.disconnect()
198
+ client_state = None
199
 
200
+ if client_state is None:
201
  response_queue = queue.Queue()
202
  client = create_paho_client()
203
  try:
 
214
  payload_md = ""
215
  response_md = f"### Error\nFailed to connect to broker: {e}"
216
  return payload_md, response_md, None, client_state, response_queue_state, previous_credentials_state
217
+
218
+ client_state = client
219
+ response_queue_state = response_queue
220
+ previous_credentials_state = {
221
  'username': HIVEMQ_USERNAME,
222
  'password': HIVEMQ_PASSWORD,
223
  'host': HIVEMQ_HOST,
 
225
  'port': PORT
226
  }
227
  else:
228
+ client = client_state
229
+ response_queue = response_queue_state
230
 
231
  # Prepare payload
232
  payload = {"command": selected_command, "args": args}
 
253
  else:
254
  response_md = "### Response\nTimed out waiting for response. Is the on-device server running?"
255
  return payload_md, response_md, None, client_state, response_queue_state, previous_credentials_state
256
+
257
  # Now, set up the function to be called when the "Send Command" button is clicked
258
  send_command_button.click(
259
  fn=send_command,