views.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. # 主業務邏輯中的視圖和路由的定義
  2. import os
  3. import datetime
  4. from flask import render_template, request, session, Response, url_for, make_response
  5. # 導入藍圖程序,用於構建路由
  6. from werkzeug.utils import redirect, secure_filename
  7. from . import main
  8. from manage import mqtt
  9. # 導入db,用於操作數據庫
  10. from manage import db, app
  11. # 導入實體類,用於操作數據庫
  12. from ..models import *
  13. import json
  14. from datetime import datetime as dt
  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. import copy
  27. from selenium import webdriver
  28. from selenium.webdriver.support.ui import Select
  29. from bs4 import BeautifulSoup
  30. from time import sleep as sl
  31. from app import ALLOWED_EXTENSIONS
  32. from flask import jsonify
  33. from flask_jwt_extended import create_access_token, jwt_required, get_jwt_identity
  34. import paramiko
  35. import git
  36. import shutil
  37. from manage import socketio
  38. # 主頁的訪問路徑
  39. @main.route('/')
  40. # @jwt_required
  41. def main_index():
  42. # 獲取登入信息
  43. if 'token' in session and 'uname' in session:
  44. username = session['uname']
  45. return redirect('/index')
  46. else:
  47. return render_template('sign_in.html')
  48. # 登入頁面的訪問路徑
  49. @main.route('/login', methods=['GET', 'POST'])
  50. def login_views():
  51. if request.method == 'GET':
  52. if 'id' in session and 'uname' in session and 'status' in session:
  53. return redirect('/')
  54. else:
  55. return render_template('sign_in.html')
  56. else:
  57. # 接收前端傳過來的資料
  58. username = request.form['username']
  59. password = request.form['password']
  60. # 使用接收的用戶和密碼到資料庫中查詢
  61. user = User.query.filter_by(username=username, password=password).first()
  62. #檢查原密碼與哈希過的密碼是否吻合
  63. # if user and user.check_hash_password(password):
  64. # 如果用戶存在,將信息保存置session並重定向回首頁,否則回登入頁
  65. if user:
  66. # resp = redirect('/')
  67. # resp = make_response('')
  68. # 判斷是否有記住密碼
  69. if 'rem' in request.form:
  70. # sn = str(user.sn)
  71. # 如果設置了 session.permanent 為 True,那麽過期時間是31天
  72. session.permanent = True
  73. #設置cookie
  74. # max_age = 60 * 60 * 24 * 365
  75. # resp.set_cookie("username", username, max_age=max_age)
  76. # resp.set_cookie("sn", sn, max_age=max_age)
  77. #可設置tokens時效
  78. expires = datetime.timedelta(minutes=30)
  79. access_token = create_access_token(identity=user.username, expires_delta=expires)
  80. # access_token = create_access_token(identity=user.username)
  81. print(access_token)
  82. session['token'] = access_token
  83. session['uname'] = user.username
  84. # session['id'] = user.sn
  85. # session['status'] = user.status
  86. return jsonify(access_token=access_token)
  87. else:
  88. errMsg = "Wrong login or password"
  89. # return render_template('sign_in.html', errMsg=errMsg)
  90. return jsonify(errMsg=errMsg)
  91. # 註冊頁面的訪問路徑
  92. @main.route('/register', methods=['POST', "GET"])
  93. def register_views():
  94. if request.method == 'GET':
  95. return render_template('registration.html')
  96. else:
  97. # 獲取文本框的值並賦值給user實體對象
  98. #哈希加密版本
  99. # username = request.form['username']
  100. # password = request.form['password']
  101. # user = User(username=username, password=password)
  102. user = User()
  103. user.firstname = request.form['firstname']
  104. user.lastname = request.form['lastname']
  105. user.mail = request.form['email']
  106. user.phone = request.form['phone']
  107. user.username = request.form['username']
  108. user.password = request.form['password']
  109. user.status = 1
  110. user.isActive = True
  111. # 將數據保存進資料庫 - 註冊
  112. db.session.add(user)
  113. # 手動提交,目的是為了獲取提交後的user的id
  114. db.session.commit()
  115. # 當user成功插入進資料庫之後,程序會自動將所有信息取出來在賦值給user
  116. # 完成登入的操作
  117. user = User.query.filter_by(username=user.username).first()
  118. # 可設置tokens時效
  119. expires = datetime.timedelta(minutes=30)
  120. access_token = create_access_token(identity=user.username, expires_delta=expires)
  121. # access_token = create_access_token(identity=user.username)
  122. session['token'] = access_token
  123. # session['id'] = user.sn
  124. session['uname'] = user.username
  125. # session['status'] = user.status
  126. return jsonify(access_token=access_token)
  127. # return redirect('/')
  128. #登入重設密碼的頁面
  129. @main.route('/reset_password', methods=['POST', 'GET'])
  130. def reset_password_views():
  131. if request.method == 'GET':
  132. if 'mail' in session:
  133. del session['mail']
  134. return render_template('reset_pwd1.html')
  135. else:
  136. #如果有id在session裡,代表從reset_pwd2過來的
  137. if "mail" in session:
  138. new_pwd = request.form['new_pwd']
  139. confirm_pwd = request.form['confirm_pwd']
  140. #判斷密碼是否一致
  141. if new_pwd == confirm_pwd:
  142. mail = session['mail']
  143. user = User.query.filter_by(mail=mail).first()
  144. user.password = new_pwd
  145. db.session.add(user)
  146. del session['mail']
  147. #修改完後回登入頁
  148. return redirect('/login')
  149. else:
  150. errMsg = "Passwords does not match"
  151. return render_template('reset_pwd2.html', errMsg=errMsg)
  152. email = request.form['email']
  153. user = User.query.filter_by(mail=email).first()
  154. if user:
  155. session['mail'] = user.mail
  156. return render_template('reset_pwd2.html')
  157. else:
  158. errMsg = "Wrong email.Please try again"
  159. return render_template('reset_pwd1.html', errMsg=errMsg)
  160. #驗證email訪問路徑
  161. @main.route('/check_email')
  162. def check_email_views():
  163. email = request.args['email']
  164. user = User.query.filter_by(mail=email).first()
  165. if user:
  166. result = {"errMsg":" "}
  167. else:
  168. result = {"pass":" "}
  169. return json.dumps(result)
  170. #驗證username訪問路徑
  171. @main.route('/check_username')
  172. def check_username_views():
  173. username = request.args['username']
  174. user = User.query.filter_by(username=username).first()
  175. if user:
  176. result = {"errMsg":" "}
  177. else:
  178. result = {"pass":" "}
  179. return json.dumps(result)
  180. #用戶主介面的訪問路徑
  181. @main.route('/index', methods=['POST', 'GET'])
  182. def index_views():
  183. token = session['token']
  184. username = session['uname']
  185. if request.method == 'GET':
  186. return render_template('index.html', params=locals())
  187. else:
  188. pass
  189. #進貨檢測的訪問路徑
  190. @main.route('/incoming_check', methods=['POST', 'GET'])
  191. def incoming_check_views():
  192. # print("test")
  193. username = session['uname']
  194. if request.method == 'GET':
  195. # print("test2")
  196. # return jsonify(status=200)
  197. return render_template('incoming_check.html', params=locals())
  198. # 方法內可透過get_jwt_identity之前放在token的identity內的內容:使用者名稱
  199. # identity = get_jwt_identity()
  200. # return jsonify(identity=identity)
  201. else:
  202. pass
  203. #感測器燒錄的訪問路徑
  204. @main.route('/burn_program', methods=['POST', 'GET'])
  205. def burn_program_views():
  206. username = session['uname']
  207. if request.method == 'GET':
  208. sensors = SensorList.query.all()
  209. sensor_list = []
  210. for sensor in sensors:
  211. sensor_list.append(sensor.toDict())
  212. return render_template('burn_program.html', params=locals())
  213. else:
  214. import json
  215. payload = {}
  216. if request.form['airTem']:
  217. payload = {"command":"system_update", "url":"http://60.250.156.230:3000/Benson/TestOTA.git"}
  218. json = json.dumps(payload)
  219. mqtt.publish(json)
  220. res = {"status": 1}
  221. return json.dumps(res)
  222. #修改權限的訪問路徑
  223. @main.route('/modify_permissions', methods=['POST', 'GET'])
  224. def modify_permissions_views():
  225. username = session['uname']
  226. if request.method == 'GET':
  227. return render_template('modify_permissions.html', params=locals())
  228. else:
  229. pass
  230. #新增感測器的訪問路徑
  231. @main.route('/add_sensor', methods=['POST', 'GET'])
  232. def add_sensor_views():
  233. username = session['uname']
  234. if request.method == 'GET':
  235. return render_template('add_sensor.html', params=locals())
  236. else:
  237. try:
  238. ch_name = request.form['ch_name']
  239. en_name = request.form['en_name']
  240. version = request.form['version']
  241. sensor = SensorList()
  242. sensor.ch_name = ch_name
  243. sensor.en_name = en_name
  244. sensor.version = version
  245. sensor.datetime = dt.now()
  246. db.session.add(sensor)
  247. db.session.commit()
  248. return "新增成功!"
  249. except Exception as e:
  250. return 'ERROR:' + e
  251. # @socketio.on('connect_event')
  252. # def connected_msg(msg):
  253. # print("connect success")
  254. # print(msg)
  255. # socketio.emit('stdout', 'success')
  256. # print(socketio)
  257. #
  258. # D = {"ssh_content":0}
  259. #
  260. # #建置伺服器的訪問路徑
  261. # @main.route('/add_server', methods=['POST', 'GET'])
  262. # def add_server_views():
  263. # username = session['uname']
  264. # L = []
  265. # if request.method == 'GET':
  266. # repo = git.Repo.init(path='.')
  267. # new_repo = git.Repo.clone_from(url='http://60.250.156.230:3000/benson/TestSSH.git', to_path='../new')
  268. # with open('/home/benson/Project/new/All_installv3.2.sh') as f:
  269. # D['ssh_content'] = f.read().split('\n')[0:-1]
  270. # ssh_content = D['ssh_content']
  271. # print(D['ssh_content'])
  272. #
  273. # #刪除非空資料夾
  274. # try:
  275. # shutil.rmtree('/home/benson/Project/new')
  276. # except OSError as e:
  277. # print(f"Error:{e.strerror}")
  278. #
  279. # for i in range(0, len(D['ssh_content'])):
  280. # if "#" in D['ssh_content'][i] or D['ssh_content'][i] == '' or D['ssh_content'][i] == ' ':
  281. # continue
  282. # if "sudo" in D['ssh_content'][i] or "echo" in D['ssh_content'][i]:
  283. # L.append(D['ssh_content'][i])
  284. # return render_template('add_server.html', params=locals())
  285. # else:
  286. # print(D['ssh_content'])
  287. # L = []
  288. # d = request.form.to_dict()
  289. # ip = d['ip']
  290. # print(ip)
  291. # try:
  292. # # 建立一個sshclient物件
  293. # ssh = paramiko.SSHClient()
  294. # # 允許將信任的主機自動加入到host_allow 列表,此方法必須放在connect方法的前面
  295. # ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  296. # # 指定本地的RSA私鑰檔案,如果建立金鑰對時設定的有密碼,password為設定的密碼,如無不用指定password引數
  297. # # pkey = paramiko.RSAKey.from_private_key_file('/home/super/.ssh/id_rsa', password='12345')
  298. # # pkey = paramiko.RSAKey.from_private_key_file('/home/ptop/topicjie/scripts/keys/id_rsa')
  299. # # 建立連線
  300. # # ssh.connect(hostname=ip,
  301. # # port=22,
  302. # # username='gs1801',
  303. # # pkey=pkey)
  304. # with open('/home/benson/test11.sh') as f:
  305. # content = f.read().split('\n')[0:-1]
  306. # print(content)
  307. # # L = []
  308. # # for i in range(0, len(D['ssh_content'])):
  309. # # if "#" in D['ssh_content'][i] or D['ssh_content'][i] == '' or D['ssh_content'][i] == ' ':
  310. # # continue
  311. # # L.append(D['ssh_content'][i])
  312. # # for i in L:
  313. # # print(i)
  314. #
  315. # ssh.connect(hostname=ip,
  316. # port=22,
  317. # username='gs1801',
  318. # password='g53743001')
  319. # # os.system("scp /home/benson/test10.sh gs1801@" + ip + ":/home/gs1801/")
  320. # # 執行命令
  321. # # stdin, stdout, stderr = ssh.exec_command("ls -l /home/gs1801;touch test7.py")
  322. # # stdin, stdout, stderr = ssh.exec_command("touch test10.sh;echo '" + content + "' >> test10.sh;bash test10.sh")
  323. # # print('test1')
  324. # # print(stdout.read().decode())
  325. # # print('test2')
  326. # # print(stderr.read().decode())
  327. #
  328. # for command in content:
  329. # stdin, stdout, stderr = ssh.exec_command(command)
  330. # #
  331. # # # print(stdout.read().decode())
  332. # # # print('----------------------------------------')
  333. # # # print(stderr.read().decode())
  334. # #
  335. # if stderr.read().decode():
  336. # L.append(0)
  337. # print('error')
  338. # socketio.emit('stderr', 'error')
  339. # else:
  340. # L.append(1)
  341. # print('success')
  342. # print(socketio)
  343. # socketio.emit('stdout', 'success')
  344. #
  345. # print(L)
  346. # #
  347. # # item_list = ItemList()
  348. # # item_list.ip = ip
  349. # # item_list.pymysql = L[0]
  350. # # item_list.eventlet = L[1]
  351. # # datetime = dt.now()
  352. # # item_list.datetime = datetime
  353. # # db.session.add(item_list)
  354. # # db.session.commit()
  355. #
  356. # # 關閉連線
  357. # ssh.close()
  358. # return "建置結束!"
  359. # except Exception as e:
  360. # print(e)
  361. # return e
  362. #拿取伺服器建置清單狀態的訪問路徑
  363. @main.route('/item_list/<ip>', methods=['POST', 'GET'])
  364. def item_list_views(ip):
  365. username = session['uname']
  366. if request.method == 'GET':
  367. item_list = ItemList.query.filter_by(ip=ip).order_by(text('datetime desc')).first()
  368. pymysql = item_list.pymysql
  369. eventlet = item_list.eventlet
  370. return jsonify(pymysql=pymysql, eventlet=eventlet)
  371. else:
  372. pass
  373. #過濾前端傳過來文件副檔名函數
  374. def allowed_file(filename):
  375. return '.' in filename and \
  376. filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
  377. #認證token的訪問路徑
  378. @main.route('/auth', methods=['POST', 'GET'])
  379. @jwt_required
  380. def auth_views():
  381. if request.method == 'GET':
  382. identity = get_jwt_identity()
  383. print(identity)
  384. return jsonify(identity=identity)
  385. # 退出的訪問路徑
  386. @main.route('/logout')
  387. def logout_views():
  388. if 'token' in session and 'uname' in session:
  389. # del session['id']
  390. del session['uname']
  391. del session['token']
  392. # del session['status']
  393. return redirect('/')