2018-04-26 14:45:28 +00:00
|
|
|
import atexit
|
|
|
|
from flask import Blueprint
|
2018-05-03 20:30:34 +00:00
|
|
|
from .mqtt_client import MqttClient
|
2018-04-26 12:12:20 +00:00
|
|
|
|
2018-04-26 14:45:28 +00:00
|
|
|
devices = Blueprint('devices', __name__)
|
2018-05-04 06:35:26 +00:00
|
|
|
mqtt_client = None
|
2018-04-26 12:12:20 +00:00
|
|
|
|
2018-05-03 07:48:24 +00:00
|
|
|
|
2018-04-27 09:15:44 +00:00
|
|
|
# When app dies, stop mqtt connection
|
2018-04-26 14:45:28 +00:00
|
|
|
def on_stop():
|
2018-05-04 06:35:26 +00:00
|
|
|
if mqtt_client:
|
|
|
|
mqtt_client.tear_down()
|
2018-04-26 13:03:38 +00:00
|
|
|
|
2018-05-03 07:48:24 +00:00
|
|
|
|
2018-04-26 14:45:28 +00:00
|
|
|
atexit.register(on_stop)
|
|
|
|
|
2018-05-03 07:48:24 +00:00
|
|
|
|
2018-04-27 09:15:44 +00:00
|
|
|
# Routes
|
2018-04-26 14:45:28 +00:00
|
|
|
@devices.route("/")
|
|
|
|
def hello():
|
|
|
|
return "Hello from devices!"
|
|
|
|
|
|
|
|
|
|
|
|
@devices.record
|
2018-04-27 09:15:44 +00:00
|
|
|
def on_blueprint_setup(setup_state):
|
2018-05-04 06:35:26 +00:00
|
|
|
print('Blueprint setup')
|
|
|
|
mqtt_client = MqttClient()
|
|
|
|
|
|
|
|
if mqtt_client:
|
|
|
|
mqtt_client.setup(setup_state.app)
|