views.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # 主業務邏輯中的視圖和路由的定義
  2. import os
  3. import datetime
  4. from flask import render_template, request, session, Response
  5. # 導入藍圖程序,用於構建路由
  6. from werkzeug.utils import redirect
  7. from . import main
  8. from manage import mqtt
  9. # 導入db,用於操作數據庫
  10. from manage import db
  11. # 導入實體類,用於操作數據庫
  12. from ..models import *
  13. import json
  14. from datetime import datetime as dt, date
  15. from sqlalchemy import text
  16. import socket
  17. import pickle
  18. import cv2
  19. import numpy as np
  20. import math
  21. import threading
  22. import time
  23. from flask_mqtt import Mqtt
  24. import requests
  25. import re
  26. from time import sleep as sl
  27. import rospy
  28. from std_msgs.msg import Float64
  29. s_sock = 0
  30. lock = threading.Lock()
  31. # 主頁的訪問路徑
  32. @main.route('/')
  33. def main_index():
  34. # 獲取登入信息
  35. if 'id' in session and 'uname' in session:
  36. username = session['uname']
  37. return render_template('index.html', params=locals())
  38. else:
  39. return render_template('sign_in.html')
  40. # 登入頁面的訪問路徑
  41. @main.route('/login', methods=['GET', 'POST'])
  42. def login_views():
  43. if request.method == 'GET':
  44. if 'id' in session and 'uname' in session:
  45. return redirect('/')
  46. else:
  47. return render_template('sign_in.html')
  48. else:
  49. # 接收前端傳過來的資料
  50. username = request.form['username']
  51. password = request.form['password']
  52. # 使用接收的用戶和密碼到資料庫中查詢
  53. user = User.query.filter_by(username=username, password=password).first()
  54. # 如果用戶存在,將信息保存置session並重定向回首頁,否則重定向回登入頁
  55. if user:
  56. resp = redirect('/')
  57. # 判斷是否有記住密碼
  58. if 'rem' in request.form:
  59. sn = str(user.sn)
  60. max_age = 60 * 60 * 24 * 365
  61. resp.set_cookie("username", username, max_age=max_age)
  62. resp.set_cookie("sn", sn, max_age=max_age)
  63. session['uname'] = user.username
  64. session['id'] = user.sn
  65. return resp
  66. else:
  67. errMsg = "Wrong login or password"
  68. return render_template('sign_in.html', errMsg=errMsg)
  69. # 註冊頁面的訪問路徑
  70. @main.route('/register', methods=['POST', "GET"])
  71. def register_views():
  72. if request.method == 'GET':
  73. return render_template('registration.html')
  74. else:
  75. # 獲取文本框的值並賦值給user實體對象
  76. user = User()
  77. user.username = request.form['username']
  78. user.password = request.form['password']
  79. # 將數據保存進資料庫 - 註冊
  80. db.session.add(user)
  81. # 手動提交,目的是為了獲取提交後的user的id
  82. db.session.commit()
  83. # 當user成功插入進資料庫之後,程序會自動將所有信息取出來在賦值給user
  84. # 完成登入的操作
  85. user = User.query.filter_by(username=user.username).first()
  86. session['id'] = user.sn
  87. session['uname'] = user.username
  88. return redirect('/')
  89. #XY控制的訪問路徑
  90. @main.route('/xz_console', methods=['POST', 'GET'])
  91. def xy_console_views():
  92. if request.method == 'GET':
  93. username = session['uname']
  94. return render_template('xz_console.html', params=locals())
  95. #制動器MQTT訪問路徑
  96. @main.route('/swh_mqtt', methods=['POST', 'GET'])
  97. def swh_mqtt_views():
  98. import json
  99. dict = request.args.to_dict()
  100. json = json.dumps(dict)
  101. if dict['command'] == 'fan':
  102. topic= 1
  103. elif dict['command'] == 'water_bump':
  104. topic = 1
  105. elif dict['command'] == 'water_charge':
  106. topic = 1
  107. elif dict['command'] == 'charge':
  108. topic = 1
  109. elif dict['command'] == 'sonic':
  110. topic = 1
  111. elif dict['command'] == 'red_light':
  112. topic = 1
  113. elif dict['command'] == 'yellow_light':
  114. topic = 1
  115. elif dict['command'] == 'green_light':
  116. topic = 1
  117. mqtt.publish(topic, json)
  118. return "Success"
  119. #處理mqtt訂閱的信息
  120. @mqtt.on_message()
  121. def handle_mqtt_message(client, userdata, message):
  122. payload = message.payload.decode()
  123. print(payload)
  124. #處理XZ軸馬達作動的訪問路徑
  125. @main.route('/ros_msg', methods=['POST', 'GET'])
  126. def ros_msg_views():
  127. dict = request.form.to_dict()
  128. ros_msg = dict['ros_msg']
  129. # 定义发布器pub
  130. pub = rospy.Publisher('toggle_led', Float64, queue_size=10)
  131. # 接下来这里初始化了一个名为motor的节点
  132. rospy.init_node('motor', anonymous=True, log_level=rospy.INFO, disable_signals=True)
  133. # 这里初始化一个Rate对象,通过后面的sleep()可以设定循环的频率
  134. rate = rospy.Rate(10)
  135. # rate.sleep()
  136. pub.publish(float(ros_msg))
  137. return "Success"
  138. # 退出的訪問路徑
  139. @main.route('/logout')
  140. def logout_views():
  141. if 'id' in session and 'uname' in session:
  142. del session['id']
  143. del session['uname']
  144. return redirect('/')