123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- import eventlet
- import json
- from flask import Flask, render_template
- from flask_mqtt import Mqtt
- from flask_socketio import SocketIO
- from flask_bootstrap import Bootstrap
- from flask_sqlalchemy import SQLAlchemy
- import pymysql
- from models import *
- from sqlalchemy import text
- from datetime import datetime as dt
- import json
- #綠化(綠色線程)所有系統模組,實現IO多路複用
- eventlet.monkey_patch()
- pymysql.install_as_MySQLdb()
- app = Flask(__name__)
- app.config['SECRET'] = 'my secret key'
- app.config['TEMPLATES_AUTO_RELOAD'] = True
- app.config['MQTT_BROKER_URL'] = '54.248.68.32'
- app.config['MQTT_BROKER_PORT'] = 1883
- app.config['MQTT_USERNAME'] = 'aisky-client'
- app.config['MQTT_PASSWORD'] = 'aiskyc'
- app.config['MQTT_KEEPALIVE'] = 60
- app.config['MQTT_TLS_ENABLED'] = False
- db = SQLAlchemy(app)
- # 配置數據庫的連接字符串
- app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://land:skyeye@13.113.114.87:3306/LandGreen'
- # 配置數據庫內容再更新時自動提交
- app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
- # 如果設置成True(默認情況),Flask-SQLAlchemy 將會追蹤對象的修改並且發送信號
- app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
- # 配置session所需要的秘鑰
- app.config['SECRET_KEY'] = 'you guess'
- # template 有修改後,會自動去更新
- app.config['TEMPLATES_AUTO_RELOAD'] = True
- # Parameters for SSL enabled
- # app.config['MQTT_BROKER_PORT'] = 8883
- # app.config['MQTT_TLS_ENABLED'] = True
- # app.config['MQTT_TLS_INSECURE'] = True
- # app.config['MQTT_TLS_CA_CERTS'] = 'ca.crt'
- mqtt = Mqtt(app)
- socketio = SocketIO(app)
- bootstrap = Bootstrap(app)
- @app.route('/')
- def index():
- return render_template('index.html')
- #監聽來自publish事件
- @socketio.on('publish')
- def handle_publish(json_str):
- data = json.loads(json_str)
- topic = data['topic']
- del data['topic']
- del data['qos']
- # command_list = ["air_temp1", "air_humidity1", "ph1", "do1", "ec1", "water_level1", "water_temp1",
- # "TDS1", "O3", "CO2", "H2", "weight1", "weight2"]
- command_list = ['all']
- for command in command_list:
- data['command'] = command
- json_data = json.dumps(data)
- mqtt.publish(topic=topic, payload=json_data)
- #mqtt訂閱
- @mqtt.on_connect()
- def handle_connect(client, userdata, flags, rc):
- mqtt.subscribe('AISKY/AppleFarm/MK-G/9c:65:f9:1e:66:b3/Log')
- #處理mqtt訂閱訊息
- @mqtt.on_message()
- def handle_mqtt_message(client, userdata, message):
- payload = message.payload.decode()
- json_payload = json.loads(payload)
- data = dict(
- payload=message.payload.decode()
- )
- print(json_payload)
- fitolab1 = Fitolab1Sensor()
- fitolab1.datetime = dt.now()
- fitolab1.s_air_tem1 = json_payload['air_temp1']
- fitolab1.s_humidity = json_payload['air_humidity1']
- fitolab1.r_ph = json_payload['ph']
- fitolab1.r_do = json_payload['do']
- fitolab1.r_ec = json_payload['ec']
- fitolab1.r_water_level = json_payload['water_level']
- fitolab1.r_turbidity = json_payload['TDS1']
- fitolab1.s_o3 = json_payload['O3']
- fitolab1.s_co2 = json_payload['CO2']
- fitolab1.s_h2 = json_payload['H2']
- fitolab1.s_wgt_sensor1 = json_payload['weight1']
- fitolab1.s_wgt_sensor2 = json_payload['weight2']
- db.session.add(fitolab1)
- db.session.commit()
- socketio.emit('mqtt_message', data=data)
- #調用日誌訊息
- @mqtt.on_log()
- def handle_logging(client, userdata, level, buf):
- print(level, buf)
- if __name__ == '__main__':
- socketio.run(app, host='0.0.0.0', port=5001, use_reloader=True, debug=False)
|