123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- #啟動和管理項目
- from app import create_app
- from flask import request
- app, db, mqtt = create_app()
- #mqtt發布
- @app.route('/mqtt', methods=['GET','POST'])
- def mqtt_views():
- import json
- dict = request.args.to_dict()
- json = json.dumps(dict)
- print(json)
- mqtt.publish('AISKY/AppleFarm/MK-G/b8:27:eb:f8:24:92', json)
- return "Publish done"
- #mqtt訂閱
- @mqtt.on_connect()
- def handle_connect(client, userdata, flags, rc):
- mqtt.subscribe('AISKY/AppleFarm/MK-G/b8:27:eb:f8:24:92/Log')
- #處理mqtt訂閱的信息
- @mqtt.on_message()
- def handle_mqtt_message(client, userdata, message):
- payload = message.payload.decode()
- # p = json.loads(payload)
- # print("-------msg-------")
- # print('name :', p['name'])
- # print('email :', p['email'])
- print(payload)
- #調用日誌訊息
- @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=True, threaded=True, host='0.0.0.0', port=5555)
|