Reformat files according to PEP8

master
esensar 2018-04-26 15:03:38 +02:00
parent 6af6cf1d18
commit 75d69e8d22
3 changed files with 10 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea/
*.py[co] *.py[co]
*.DS_* *.DS_*

View File

@ -8,13 +8,16 @@ app.config.from_object('config')
app.config.from_pyfile('config.py') app.config.from_pyfile('config.py')
db = SQLAlchemy(app) db = SQLAlchemy(app)
def on_stop(): def on_stop():
print('Application stopping') print('Application stopping')
tear_down_mqtt() tear_down_mqtt()
setup_mqtt(app) setup_mqtt(app)
atexit.register(on_stop) atexit.register(on_stop)
@app.route("/") @app.route("/")
def hello(): def hello():
return "Hello World!" return "Hello World!"

View File

@ -2,30 +2,36 @@ from flask_mqtt import Mqtt
mqtt = Mqtt() mqtt = Mqtt()
def setup_mqtt(app): def setup_mqtt(app):
mqtt.init_app(app) mqtt.init_app(app)
mqtt.client.on_message = handle_mqtt_message mqtt.client.on_message = handle_mqtt_message
mqtt.client.on_subscribe = handle_subscribe mqtt.client.on_subscribe = handle_subscribe
print('MQTT client initialized') print('MQTT client initialized')
def tear_down_mqtt(): def tear_down_mqtt():
mqtt.unsubscribe_all() mqtt.unsubscribe_all()
if hasattr(mqtt, 'client') and mqtt.client is not None: if hasattr(mqtt, 'client') and mqtt.client is not None:
mqtt.client.disconnect() mqtt.client.disconnect()
print('MQTT client destroyed') print('MQTT client destroyed')
@mqtt.on_connect() @mqtt.on_connect()
def handle_connect(client, userdata, flags, rc): def handle_connect(client, userdata, flags, rc):
print('MQTT client connected') print('MQTT client connected')
mqtt.subscribe('topic/state') mqtt.subscribe('topic/state')
@mqtt.on_disconnect() @mqtt.on_disconnect()
def handle_disconnect(): def handle_disconnect():
print('MQTT client disconnected') print('MQTT client disconnected')
def handle_subscribe(client, userdata, mid, granted_qos): def handle_subscribe(client, userdata, mid, granted_qos):
print('MQTT client subscribed') print('MQTT client subscribed')
def handle_mqtt_message(client, userdata, message): def handle_mqtt_message(client, userdata, message):
data = dict( data = dict(
topic=message.topic, topic=message.topic,