Skip to content
Snippets Groups Projects
Commit 07ada146 authored by René Schöne's avatar René Schöne
Browse files

*

parents
No related branches found
No related tags found
No related merge requests found
/venv/
/.idea/
main.py 0 → 100644
# 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)
paho-mqtt~=1.5.1
dash~=1.20.0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment