#啟動和管理項目
from app import create_app
from flask import request
from gevent import pywsgi

app, db, mqtt = create_app()

#mqtt訂閱
@mqtt.on_connect()
def handle_connect(client, userdata, flags, rc):
    mqtt.subscribe('AISKY/Coffee/MK-G/b8:27:eb:b4:59:3e/Log')


#調用日誌訊息
@mqtt.on_log()
def handle_logging(client, userdata, level, buf):
    print(level, buf)

#自動關閉所有未使用、掛著的連接
@app.teardown_appcontext
def shutdown_session(exception=None):
        db.session.remove()



if __name__ == '__main__':
    app.run(debug=False, threaded=True, port=5006, host='0.0.0.0')
    #使用WSGI開關，避免出現WARNING: This is a development server. Do not use it in a production deployment.Use a production WSGI server instead.
    # server = pywsgi.WSGIServer(('0.0.0.0', 5006), app)
    # server.serve_forever()