app.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import eventlet
  2. import json
  3. from flask import Flask, render_template
  4. from flask_mqtt import Mqtt
  5. from flask_socketio import SocketIO
  6. from flask_bootstrap import Bootstrap
  7. from flask_sqlalchemy import SQLAlchemy
  8. import pymysql
  9. from models import *
  10. from sqlalchemy import text
  11. from datetime import datetime as dt
  12. import json
  13. #綠化(綠色線程)所有系統模組,實現IO多路複用
  14. eventlet.monkey_patch()
  15. pymysql.install_as_MySQLdb()
  16. app = Flask(__name__)
  17. app.config['SECRET'] = 'my secret key'
  18. app.config['TEMPLATES_AUTO_RELOAD'] = True
  19. app.config['MQTT_BROKER_URL'] = '54.248.68.32'
  20. app.config['MQTT_BROKER_PORT'] = 1883
  21. app.config['MQTT_USERNAME'] = 'aisky-client'
  22. app.config['MQTT_PASSWORD'] = 'aiskyc'
  23. app.config['MQTT_KEEPALIVE'] = 60
  24. app.config['MQTT_TLS_ENABLED'] = False
  25. db = SQLAlchemy(app)
  26. # 配置數據庫的連接字符串
  27. app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://land:skyeye@13.113.114.87:3306/LandGreen'
  28. # 配置數據庫內容再更新時自動提交
  29. app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
  30. # 如果設置成True(默認情況),Flask-SQLAlchemy 將會追蹤對象的修改並且發送信號
  31. app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
  32. # 配置session所需要的秘鑰
  33. app.config['SECRET_KEY'] = 'you guess'
  34. # template 有修改後,會自動去更新
  35. app.config['TEMPLATES_AUTO_RELOAD'] = True
  36. # Parameters for SSL enabled
  37. # app.config['MQTT_BROKER_PORT'] = 8883
  38. # app.config['MQTT_TLS_ENABLED'] = True
  39. # app.config['MQTT_TLS_INSECURE'] = True
  40. # app.config['MQTT_TLS_CA_CERTS'] = 'ca.crt'
  41. mqtt = Mqtt(app)
  42. socketio = SocketIO(app)
  43. bootstrap = Bootstrap(app)
  44. @app.route('/')
  45. def index():
  46. return render_template('index.html')
  47. #監聽來自publish事件
  48. @socketio.on('publish')
  49. def handle_publish(json_str):
  50. data = json.loads(json_str)
  51. topic = data['topic']
  52. del data['topic']
  53. del data['qos']
  54. # command_list = ["air_temp1", "air_humidity1", "ph1", "do1", "ec1", "water_level1", "water_temp1",
  55. # "TDS1", "O3", "CO2", "H2", "weight1", "weight2"]
  56. command_list = ['all']
  57. for command in command_list:
  58. data['command'] = command
  59. json_data = json.dumps(data)
  60. mqtt.publish(topic=topic, payload=json_data)
  61. #mqtt訂閱
  62. @mqtt.on_connect()
  63. def handle_connect(client, userdata, flags, rc):
  64. mqtt.subscribe('AISKY/AppleFarm/MK-G/9c:65:f9:1e:66:b3/Log')
  65. #處理mqtt訂閱訊息
  66. @mqtt.on_message()
  67. def handle_mqtt_message(client, userdata, message):
  68. payload = message.payload.decode()
  69. json_payload = json.loads(payload)
  70. data = dict(
  71. payload=message.payload.decode()
  72. )
  73. print(json_payload)
  74. fitolab1 = Fitolab1Sensor()
  75. fitolab1.datetime = dt.now()
  76. fitolab1.s_air_tem1 = json_payload['air_temp1']
  77. fitolab1.s_humidity = json_payload['air_humidity1']
  78. fitolab1.r_ph = json_payload['ph']
  79. fitolab1.r_do = json_payload['do']
  80. fitolab1.r_ec = json_payload['ec']
  81. fitolab1.r_water_level = json_payload['water_level']
  82. fitolab1.r_turbidity = json_payload['TDS1']
  83. fitolab1.s_o3 = json_payload['O3']
  84. fitolab1.s_co2 = json_payload['CO2']
  85. fitolab1.s_h2 = json_payload['H2']
  86. fitolab1.s_wgt_sensor1 = json_payload['weight1']
  87. fitolab1.s_wgt_sensor2 = json_payload['weight2']
  88. db.session.add(fitolab1)
  89. db.session.commit()
  90. socketio.emit('mqtt_message', data=data)
  91. #調用日誌訊息
  92. @mqtt.on_log()
  93. def handle_logging(client, userdata, level, buf):
  94. print(level, buf)
  95. if __name__ == '__main__':
  96. socketio.run(app, host='0.0.0.0', port=5001, use_reloader=True, debug=False)