coffee_manage20221221.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #啟動和管理項目
  2. from sqlalchemy import true
  3. from app import create_app
  4. from flask import request
  5. from gevent import pywsgi
  6. # Rita 原本都是 app = Flask(name), 若要建立多個"工廠", 則使用 create_app() 函式(在 __init__.py 中)
  7. app, db, mqtt = create_app()
  8. #mqtt訂閱
  9. @mqtt.on_connect()
  10. def handle_connect(client, userdata, flags, rc):
  11. # Rita MQTT 是以 broker 做分發, subscribe 接收 'Topic' 的資料
  12. # 'AISKY/Coffee/MK-G/b8:27:eb:7e:24:78/Log' 就是本次的 Topic, 此命名也有 AISKY 公司 Coffee 專案 MK-G ... 這樣的階層關係(不影響 Topic 和 MQTT 傳輸)
  13. mqtt.subscribe('AISKY/Coffee/MK-G/b8:27:eb:7e:24:78/Log')
  14. #調用日誌訊息
  15. @mqtt.on_log()
  16. def handle_logging(client, userdata, level, buf):
  17. print(client, userdata, level, buf)
  18. #自動關閉所有未使用、掛著的連接
  19. @app.teardown_appcontext
  20. def shutdown_session(exception=None):
  21. db.session.remove()
  22. if __name__ == '__main__':
  23. # Benson 測試網頁 http://192.168.50.65:5010/login
  24. app.run(host='0.0.0.0', port=5025, debug=True, threaded=True)
  25. #使用WSGI開關,避免出現WARNING: This is a development server. Do not use it in a production deployment.Use a production WSGI server instead.
  26. # server = pywsgi.WSGIServer(('0.0.0.0', 5006), app)
  27. # server.serve_forever()
  28. # 加上 threaded=True, 就會以多線程啟動, 以支持網頁 ajax 請求