from flask import Flask, render_template, request, session, redirect
from flask_mqtt import Mqtt
import numpy as np
import json
import re
from utils import query
from utils import getData
app = Flask(__name__)
app.secret_key = 'nongpi'
app.config['MQTT_BROKER_URL'] = '101.43.164.154'
app.config['MQTT_BROKER_PORT'] = 1883
app.config['MQTT_USERNAME'] = 'admin' # 当你需要验证用户名和密码时,请设置该项
app.config['MQTT_PASSWORD'] = 'zhangxinke233' # 当你需要验证用户名和密码时,请设置该项
app.config['MQTT_KEEPALIVE'] = 5 # 设置心跳时间,单位为秒
app.config['MQTT_CLIENT_ID'] = 'something'
topic = 'test'
mqtt_client = Mqtt(app)
@mqtt_client.on_connect()
def handle_connect(client, userdata, flags, rc):
if rc == 0:
print('Connected successfully')
mqtt_client.subscribe(topic)
else:
print('Bad connection. Code:', rc)
@mqtt_client.on_message()
def handle_mqtt_message(client, userdata, message):
print("client: ", client)
jsondata = json.loads(message.payload)
print("msg payload", jsondata)
query.sqlsave(jsondata)
@app.route('/')
def all_request():
return redirect('/login')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
elif request.method == 'POST':
request.form = dict(request.form)
def filter_fn(item):
return request.form['email'] in item and request.form['password'] in item
users = query.querys('select * from user', [], 'select')
filter_user = list(filter(filter_fn, users))
if len(filter_user):
session['email'] = request.form['email']
return redirect('/home')
else:
return render_template('error.html', message='邮箱或密码错误')
@app.route('/loginOut')
def login_out():
session.clear()
return redirect('/login')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'GET':
return render_template('register.html')
elif request.method == 'POST':
request.form = dict(request.form)
print(request.form)
if request.form['password'] != request.form['passwordChecked']:
return render_template("error.html", message="两次密码不匹配")
def filter_fn(item):
return request.form['email'] in item
users = query.querys('select * from user', [], 'select')
filter_list = list(filter(filter_fn, users))
if len(filter_list):
return render_template('error.html', message='该用户已被注册')
else:
query.querys('insert into user(email,password) values(%s,%s)',
[request.form['email'], request.form['password']])
return redirect('/login')
@app.route('/home', methods=['GET', 'POST'])
def home():
email = session.get('email')
df = query.read_sql()
device_num, device_data = getData.get_device_data(df)
return render_template(
'index.html',
email=email,
devicenum=device_num,
device_id=device_data.tolist(),
)
@app.route('/system_chart_refresh', methods=['GET', 'POST'])
def system_chart_refresh():
df = query.read_sql()
device_num, device_data = getData.get_device_data(df)
timenow = query.gettime()
num = getData.get_alarm_num(df)
return [[timenow, device_num],
[timenow, device_num*8],
[timenow, int(num)]]
@app.route('/tables', methods=['GET', 'POST'])
def tables():
email = session.get('email')
df = query.read_sql()
device_num, device_data = getData.get_device_data(df)
return render_template(
'tables.html',
email=email,
devicenum=device_num,
device_id=device_data.tolist(),
sqldata=df,
)
@app.route('/table_refresh')
def table_refresh():
df = query.read_sql()
X = np.array(df).tolist()
# print(df)
return X
@app.route('/date')
def get_date():
return getData.get_date()
@app.route('/time')
def get_time():
return getData.get_time()
@app.route('/device')
def get_device():
df = query.read_sql()
device_num, device_data = getData.get_device_data(df)
return str(device_num)
@app.route('/alarm')
def get_alarm():
df = query.read_sql()
num = getData.get_alarm_num(df)
return str(num)
@app.route('/device_card_refresh')
def device_card_refresh():
df = query.read_sql()
device_num, device_data = getData.get_device_data(df)
return [device_num, device_data.tolist()]
@app.route('/devicepage/<int:deviceid>')
def devicepage(deviceid, methods=['GET', 'POST']):
email = session.get('email')
df = query.read_sql()
device_num, device_data = getData.get_device_data(df)
return render_template(
'devicepage.html',
email=email,
deviceid=deviceid,
devicenum=device_num,
device_id=device_data.tolist(),
)
@app.route('/device_chart_refresh/<string:id>')
def device_chart_refresh(id):
df = query.read_sql()
df = getData.alive(df)
df = df[df['id'] == id]
na = np.array(df).tolist()
k = []
for i in range(2, 10):
p = []
for j in na:
p = p + [[j[0], j[i]]]
k = k + [p]
return k
@app.route('/alarm_svg_refresh/<string:id>')
def alarm_svg_refresh(id):
df = query.read_sql()
df = getData.get_alarm(df)
return np.array(df.loc[df['id'] == id]).tolist()[0]
@app.before_request
def before_requre():
pat = re.compile(r'^/static')
if re.search(pat, request.path):
return
if request.path == "/login":
return
if request.path == '/register':
return
email = session.get('email')
if email:
return None
return redirect('/login')
if __name__ == '__main__':
app.run(host='0.0.0.0')
程序员张小妍
- 粉丝: 1w+
- 资源: 3691
最新资源
- 博途S7-1200主站与S7-200从站实现RS485通讯程序 S7-200可以当作一个仪表
- C#、C++分别开发的OPC DA CLIENT软件. 1、枚举服务器名称; 2、连接服务器以后枚举出TAG; 3、根据TAG名称自动读取服务器数据; 4、图片内有OPC SERVER和CLIENT实
- python-workspace.zip.005
- 龙门上下料样本程序,四轴 用台达AS228T和台达触摸屏编写 注意软件是用台达新款软件ISPSOFT ,借鉴价值高,程序有注释
- 一款window下的串口监视抓包工具
- 欧姆龙CP1H与3台力士乐VFC-x610变频器通讯程序 功能:原创程序,可直接用于现场程序 欧姆龙CP1H的CIF11通讯板,实现对3台力士乐VFC-x610变频器 设定频率,控制正反转,读取实际
- dp111113333
- CV-密集人群图像数据集(5800张图片).rar
- 福特汽车主观评价规范,性能开发参考,英文原版直译,评价条目、规则描述非常细致 包含平顺舒适性,转向,操稳,NVH,制动,加速感,驾驶性等等性能,并详细描述了评价的准备工作 评价条目细分至第四级,共
- 三菱FX3S两轴标准程序,XZ两轴,包含轴点动,回零,相对与绝对定位,只要弄明白这个程序,就可以非常了解整个项目的程序如何去编写,从哪里开始下手,可提供程序问题解答,程序流程清晰明了,注释完整
- MATLAB代码:考虑P2G与碳捕集机组的多能微网低碳经济调度 关键词:碳交易 阶梯碳交易 碳捕集 多能微网 低碳调度 仿真平台:MATLAB+yalmip+cplex 主要内容:代码主要做的是一个
- 本程序采用matlab编写,主要是实现电流注入型牛拉法 除此之外,本人还编写了很多种关于潮流计算的程序,主要有牛拉法,前推回代法,以还有相和三相潮流计算程序
- 智能门锁架构图,供大家参考
- 三菱FX3U六轴标准程序,程序包含本体3轴控制,扩展3个1PG定位模块,一共六轴 程序有轴点动控制,回零控制,相对定位,绝对定位 另有气缸数个,一个大是DD马达控制的转盘,整个是转盘多工位流水作业
- 批量登录到远程Linux服务器检查服务器时间差的shell
- MATLAB电动车七自由度整车模型 MATLAB Simulink电动车转弯制动abs模型asr转弯制动防抱死abs模型+模糊控制算法+七自由度整车模型+纵向运动+侧向运动+横摆运动+四轮魔术公式+四
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈