#啟動和管理項目
from app import create_app

app, db, mqtt = create_app()


#mqtt訂閱
@mqtt.on_connect()
def handle_connect(client, userdata, flags, rc):
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:b7:52:9c/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:c1:72:0c/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:84:e9:3f/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:95:00:53/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:e7:51:44/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:ce:a5:35/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:fc:9d:34/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:86:00:c9/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:df:4b:0f/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:6a:5d:17/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:d2:d0:8f/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:57:3c:da/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:bd:29:b1/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:7c:f6:06/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:59:9d:bd/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:e7:21:e5/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:34:9e:39/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:e3:f1:f4/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:f8:24:92/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:9d:68:05/Log')
    mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:4d:e4:34/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, host='0.0.0.0', port=5555)
