123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- # 主業務邏輯中的視圖和路由的定義
- import os
- import datetime
- from flask import render_template, request, session, Response
- # 導入藍圖程序,用於構建路由
- from werkzeug.utils import redirect
- from . import main
- from manage import mqtt
- # 導入db,用於操作數據庫
- from manage import db
- # 導入實體類,用於操作數據庫
- from ..models import *
- import json
- from datetime import datetime as dt, date
- 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
- from time import sleep as sl
- import rospy
- from std_msgs.msg import Float64
- s_sock = 0
- lock = threading.Lock()
- # 主頁的訪問路徑
- @main.route('/')
- def main_index():
- # 獲取登入信息
- if 'id' in session and 'uname' in session:
- username = session['uname']
- return render_template('index.html', params=locals())
- 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:
- 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
- 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.username = request.form['username']
- user.password = request.form['password']
- # 將數據保存進資料庫 - 註冊
- 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
- return redirect('/')
- #XY控制的訪問路徑
- @main.route('/xz_console', methods=['POST', 'GET'])
- def xy_console_views():
- if request.method == 'GET':
- username = session['uname']
- return render_template('xz_console.html', params=locals())
- #制動器MQTT訪問路徑
- @main.route('/swh_mqtt', methods=['POST', 'GET'])
- def swh_mqtt_views():
- import json
- dict = request.args.to_dict()
- json = json.dumps(dict)
- if dict['command'] == 'fan':
- topic= 1
- elif dict['command'] == 'water_bump':
- topic = 1
- elif dict['command'] == 'water_charge':
- topic = 1
- elif dict['command'] == 'charge':
- topic = 1
- elif dict['command'] == 'sonic':
- topic = 1
- elif dict['command'] == 'red_light':
- topic = 1
- elif dict['command'] == 'yellow_light':
- topic = 1
- elif dict['command'] == 'green_light':
- topic = 1
- mqtt.publish(topic, json)
- return "Success"
- #處理mqtt訂閱的信息
- @mqtt.on_message()
- def handle_mqtt_message(client, userdata, message):
- payload = message.payload.decode()
- print(payload)
- #處理XZ軸馬達作動的訪問路徑
- @main.route('/ros_msg', methods=['POST', 'GET'])
- def ros_msg_views():
- dict = request.form.to_dict()
- ros_msg = dict['ros_msg']
- # 定义发布器pub
- pub = rospy.Publisher('toggle_led', Float64, queue_size=10)
- # 接下来这里初始化了一个名为motor的节点
- rospy.init_node('motor', anonymous=True, log_level=rospy.INFO, disable_signals=True)
- # 这里初始化一个Rate对象,通过后面的sleep()可以设定循环的频率
- rate = rospy.Rate(10)
- # rate.sleep()
- pub.publish(float(ros_msg))
- return "Success"
- # 退出的訪問路徑
- @main.route('/logout')
- def logout_views():
- if 'id' in session and 'uname' in session:
- del session['id']
- del session['uname']
- return redirect('/')
|