Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
web-ros3rag
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
JastAdd
web-ros3rag
Commits
07ada146
Commit
07ada146
authored
4 years ago
by
René Schöne
Browse files
Options
Downloads
Patches
Plain Diff
*
parents
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
.gitignore
+2
-0
2 additions, 0 deletions
.gitignore
main.py
+115
-0
115 additions, 0 deletions
main.py
requirements.txt
+2
-0
2 additions, 0 deletions
requirements.txt
with
119 additions
and
0 deletions
.gitignore
0 → 100644
+
2
−
0
View file @
07ada146
/venv/
/.idea/
This diff is collapsed.
Click to expand it.
main.py
0 → 100644
+
115
−
0
View file @
07ada146
# coding=utf-8
import
datetime
import
queue
import
threading
import
time
from
threading
import
Thread
import
dash
import
dash_core_components
as
dcc
import
dash_html_components
as
html
import
paho.mqtt.client
as
mqtt
from
dash.dependencies
import
Input
,
Output
,
State
external_stylesheets
=
[
'
https://codepen.io/chriddyp/pen/bWLwgP.css
'
]
MQTT_SERVER
=
'
localhost
'
mqttc
=
mqtt
.
Client
()
ready_event
=
threading
.
Event
()
message_queue
=
queue
.
Queue
()
app
=
dash
.
Dash
(
__name__
,
external_stylesheets
=
external_stylesheets
)
app
.
layout
=
html
.
Div
([
html
.
P
(
"
Send to scene/update
"
),
dcc
.
Textarea
(
id
=
'
place-a-input-json
'
,
placeholder
=
'
place-a-input-json
'
,
value
=
'
{}
'
,
style
=
{
'
width
'
:
'
100%
'
}
),
html
.
Button
(
'
Send
'
,
id
=
'
send-place-a-update
'
),
dcc
.
Markdown
(
"
---
"
),
html
.
H3
(
"
Commands
"
),
html
.
Button
(
'
Place A: Model
'
,
id
=
'
send-place-a-model
'
,
style
=
{
"
margin-right
"
:
"
15px
"
}),
html
.
Button
(
'
Place A: Exit
'
,
id
=
'
send-place-a-exit
'
,
style
=
{
"
margin-right
"
:
"
15px
"
}),
html
.
Button
(
'
Place B: Model
'
,
id
=
'
send-place-b-model
'
,
style
=
{
"
margin-right
"
:
"
15px
"
}),
html
.
Button
(
'
Place B: Exit
'
,
id
=
'
send-place-b-exit
'
,
style
=
{
"
margin-right
"
:
"
15px
"
}),
html
.
H3
(
"
MQTT Log
"
),
dcc
.
Textarea
(
id
=
'
mqtt-log
'
,
readOnly
=
True
,
rows
=
50
,
style
=
{
'
width
'
:
'
100%
'
,
'
height
'
:
'
200px
'
}
),
dcc
.
Interval
(
id
=
'
every-1-second
'
,
interval
=
1000
,
# in milliseconds
n_intervals
=
0
),
html
.
Button
(
"
add to mqtt log
"
,
id
=
"
add-to-mqtt-log
"
),
html
.
Div
(
id
=
'
hidden-div
'
,
style
=
{
'
display
'
:
'
none
'
})
])
@app.callback
(
Output
(
'
mqtt-log
'
,
'
value
'
),
Input
(
'
every-1-second
'
,
'
n_intervals
'
),
State
(
'
mqtt-log
'
,
'
value
'
)
)
def
append_to_mqtt_log
(
_
,
value
):
local_messages
=
[]
while
not
message_queue
.
empty
():
local_messages
.
append
(
message_queue
.
get_nowait
())
if
local_messages
:
if
value
:
value
+=
"
\n
"
else
:
value
=
""
return
value
+
(
'
\n
'
.
join
(
local_messages
))
return
value
@app.callback
(
Output
(
'
hidden-div
'
,
'
children
'
),
Input
(
'
add-to-mqtt-log
'
,
'
n_clicks
'
),
)
def
button_clicked_to_add_to_mqtt_log
(
n_clicks
):
if
n_clicks
:
print
(
"
button clicked
"
)
message_queue
.
put_nowait
(
"
Button clicked
"
+
str
(
n_clicks
)
+
"
times
"
)
return
dash
.
no_update
def
on_mqtt_connect
(
client
,
userdata
,
flags
,
rc
,
properties
=
None
):
print
(
'
Connected at
'
+
datetime
.
datetime
.
now
().
isoformat
())
ready_event
.
set
()
def
on_mqtt_message
(
client
,
userdata
,
message
):
payload
=
message
.
topic
+
"
:
"
+
message
.
payload
.
decode
(
'
utf-8
'
)
print
(
'
Got mqtt message:
'
+
payload
+
"
, userdata:
"
+
str
(
userdata
))
message_queue
.
put_nowait
(
payload
)
def
on_mqtt_subscribe
(
client
,
userdata
,
mid
,
granted_qos
,
properties
=
None
):
print
(
"
Subscribed
"
)
def
publish_test_message
():
time
.
sleep
(
2
)
mqttc
.
publish
(
topic
=
"
test
"
,
payload
=
datetime
.
datetime
.
now
().
isoformat
())
if
__name__
==
'
__main__
'
:
mqttc
.
on_connect
=
on_mqtt_connect
mqttc
.
on_message
=
on_mqtt_message
mqttc
.
on_subscribe
=
on_mqtt_subscribe
mqttc
.
connect_async
(
MQTT_SERVER
)
mqttc
.
loop_start
()
if
not
ready_event
.
wait
(
2.0
):
# wait 2 seconds
print
(
'
Could not connect to mqtt in time!
'
)
mqttc
.
subscribe
(
topic
=
'
#
'
)
Thread
(
target
=
publish_test_message
).
start
()
app
.
run_server
(
debug
=
True
)
This diff is collapsed.
Click to expand it.
requirements.txt
0 → 100644
+
2
−
0
View file @
07ada146
paho-mqtt
~=1.5.1
dash
~=1.20.0
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment