123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- # 主業務邏輯中的視圖和路由的定義
- import os
- import datetime
- from flask import render_template, request, session, Response, url_for
- # 導入藍圖程序,用於構建路由
- from werkzeug.utils import redirect, secure_filename
- from . import main
- from manage import mqtt
- # 導入db,用於操作數據庫
- from manage import db, app
- # 導入實體類,用於操作數據庫
- from ..models import *
- import json
- from datetime import datetime as dt
- from sqlalchemy import text
- import socket
- import pickle
- import cv2
- import numpy as np
- import math
- import threading
- import time
- from flask_mqtt import Mqtt
- import requests
- import re
- import copy
- from selenium import webdriver
- from selenium.webdriver.support.ui import Select
- from bs4 import BeautifulSoup
- from time import sleep as sl
- from app import ALLOWED_EXTENSIONS
- # 主頁的訪問路徑
- @main.route('/')
- def main_index():
- # 獲取登入信息
- if 'id' in session and 'uname' in session and 'status' in session:
- username = session['uname']
- if session['status'] == 1:
- return redirect('/index')
- elif session['status'] == 2:
- return redirect('/index')
- else:
- return render_template('sign_in.html')
- # 登入頁面的訪問路徑
- @main.route('/login', methods=['GET', 'POST'])
- def login_views():
- if request.method == 'GET':
- if 'id' in session and 'uname' in session and 'status' in session:
- return redirect('/')
- else:
- return render_template('sign_in.html')
- else:
- # 接收前端傳過來的資料
- username = request.form['username']
- password = request.form['password']
- # 使用接收的用戶和密碼到資料庫中查詢
- user = User.query.filter_by(username=username, password=password).first()
- # 如果用戶存在,將信息保存置session並重定向回首頁,否則回登入頁
- if user:
- resp = redirect('/')
- # 判斷是否有記住密碼
- if 'rem' in request.form:
- sn = str(user.sn)
- max_age = 60 * 60 * 24 * 365
- resp.set_cookie("username", username, max_age=max_age)
- resp.set_cookie("sn", sn, max_age=max_age)
- session['uname'] = user.username
- session['id'] = user.sn
- session['status'] = user.status
- return resp
- else:
- errMsg = "Wrong login or password"
- return render_template('sign_in.html', errMsg=errMsg)
- # 註冊頁面的訪問路徑
- @main.route('/register', methods=['POST', "GET"])
- def register_views():
- if request.method == 'GET':
- return render_template('registration.html')
- else:
- # 獲取文本框的值並賦值給user實體對象
- user = User()
- user.firstname = request.form['firstname']
- user.lastname = request.form['lastname']
- user.mail = request.form['email']
- user.phone = request.form['phone']
- user.username = request.form['username']
- user.password = request.form['password']
- user.status = 1
- user.isActive = True
- # 將數據保存進資料庫 - 註冊
- db.session.add(user)
- # 手動提交,目的是為了獲取提交後的user的id
- db.session.commit()
- # 當user成功插入進資料庫之後,程序會自動將所有信息取出來在賦值給user
- # 完成登入的操作
- user = User.query.filter_by(username=user.username).first()
- session['id'] = user.sn
- session['uname'] = user.username
- session['status'] = user.status
- return redirect('/')
- #登入重設密碼的頁面
- @main.route('/reset_password', methods=['POST', 'GET'])
- def reset_password_views():
- if request.method == 'GET':
- if 'mail' in session:
- del session['mail']
- return render_template('reset_pwd1.html')
- else:
- #如果有id在session裡,代表從reset_pwd2過來的
- if "mail" in session:
- new_pwd = request.form['new_pwd']
- confirm_pwd = request.form['confirm_pwd']
- #判斷密碼是否一致
- if new_pwd == confirm_pwd:
- mail = session['mail']
- user = User.query.filter_by(mail=mail).first()
- user.password = new_pwd
- db.session.add(user)
- del session['mail']
- #修改完後回登入頁
- return redirect('/login')
- else:
- errMsg = "Passwords does not match"
- return render_template('reset_pwd2.html', errMsg=errMsg)
- email = request.form['email']
- user = User.query.filter_by(mail=email).first()
- if user:
- session['mail'] = user.mail
- return render_template('reset_pwd2.html')
- else:
- errMsg = "Wrong email.Please try again"
- return render_template('reset_pwd1.html', errMsg=errMsg)
- #驗證email訪問路徑
- @main.route('/check_email')
- def check_email_views():
- email = request.args['email']
- user = User.query.filter_by(mail=email).first()
- if user:
- result = {"errMsg":" "}
- else:
- result = {"pass":" "}
- return json.dumps(result)
- #驗證username訪問路徑
- @main.route('/check_username')
- def check_username_views():
- username = request.args['username']
- user = User.query.filter_by(username=username).first()
- if user:
- result = {"errMsg":" "}
- else:
- result = {"pass":" "}
- return json.dumps(result)
- #用戶主介面的訪問路徑
- @main.route('/index', methods=['POST', 'GET'])
- def index_views():
- username = session['uname']
- farm_info = FarmInfo.query.filter_by(user_id=session['id']).first()
- purchase_info = PurchaseInfo.query.filter_by(user_id=session['id']).first()
- monitor_num = purchase_info.monitor_num
- nodes = NodePosition.query.filter_by(user_id=session['id']).all()
- node_imgs = NodeImage.query.filter_by(user_id=session['id']).all()
- nodeimg_paths = []
- for node_img in node_imgs:
- nodeimg_paths.append("http://localhost:5555" + node_img.path[3:])
- if request.method == 'GET':
- path = farm_info.path
- if path == '0':
- path = False
- else:
- path = 'http://localhost:5555' + path[3:]
- lat = farm_info.loc_lat
- lng = farm_info.loc_lng
- if lat== '0' or lng == '0':
- lat = False
- lng = False
- return render_template('index.html', params=locals())
- else:
- pass
- #過濾前端傳過來文件副檔名函數
- def allowed_file(filename):
- return '.' in filename and \
- filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
- #環境資訊的訪問路徑
- @main.route('/env_info/<tid>', methods=['POST', 'GET'])
- def env_info_views(tid):
- username = session['uname']
- purchase_info = PurchaseInfo.query.filter_by(user_id=session['id']).first()
- monitor_num = purchase_info.monitor_num
- node_info = NodeInfo.query.filter_by(user_id=session['id']).first()
- node_info = eval(node_info.node_info)
- # print(node_info)
- first_monitor = json.dumps(node_info['node' + str(tid)])
- if request.method == 'GET':
- sensors = SensorList.query.all()
- sensor_list = []
- for sensor in sensors:
- sensor_list.append(sensor.toDict())
- return render_template('env_info.html', params=locals())
- else:
- pass
- #監控功能選取的訪問路徑
- @main.route('/monitor/<tid>', methods=['POST', 'GET'])
- def monitor_views(tid):
- node_info = NodeInfo.query.filter_by(user_id=session['id']).first()
- node_info = eval(node_info.node_info)
- # print(node_info)
- if request.method == 'GET':
- d = request.args.to_dict()
- if d['monitor']:
- module_list = node_info['node' + str(tid)][str(d['monitor'])]
- return json.dumps(module_list)
- return render_template('env_info.html', params=locals())
- else:
- pass
- # 退出的訪問路徑
- @main.route('/logout')
- def logout_views():
- if 'id' in session and 'uname' in session:
- del session['id']
- del session['uname']
- del session['status']
- return redirect('/')
|