manage.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #啟動和管理項目
  2. from app import create_app
  3. from flask import request
  4. app, db, mqtt = create_app()
  5. #mqtt發布
  6. @app.route('/mqtt', methods=['GET','POST'])
  7. def mqtt_views():
  8. import json
  9. dict = request.args.to_dict()
  10. json = json.dumps(dict)
  11. print(json)
  12. mqtt.publish('AISKY/AppleFarm/MK-G/b8:27:eb:f8:24:92', json)
  13. return "Publish done"
  14. #mqtt訂閱
  15. @mqtt.on_connect()
  16. def handle_connect(client, userdata, flags, rc):
  17. mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:f8:24:92/Log')
  18. #處理mqtt訂閱的信息
  19. @mqtt.on_message()
  20. def handle_mqtt_message(client, userdata, message):
  21. payload = message.payload.decode()
  22. # p = json.loads(payload)
  23. # print("-------msg-------")
  24. # print('name :', p['name'])
  25. # print('email :', p['email'])
  26. print(payload)
  27. #調用日誌訊息
  28. @mqtt.on_log()
  29. def handle_logging(client, userdata, level, buf):
  30. print(level, buf)
  31. #自動關閉所有未使用、掛著的連接
  32. @app.teardown_appcontext
  33. def shutdown_session(exception=None):
  34. db.session.remove()
  35. if __name__ == '__main__':
  36. app.run(debug=True, threaded=True, host='0.0.0.0', port=5000)