# 主業務邏輯中的視圖和路由的定義 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 from concurrent.futures import ThreadPoolExecutor pool = ThreadPoolExecutor(25) s_sock = 0 lock = threading.Lock() # 主頁的訪問路徑 @main.route('/') def main_index(): # 獲取登入信息 if 'id' in session and 'uname' in session: return render_template('allindex.html') 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 and user.check_hash_password(password): 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 username 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實體對象 # username = request.form['username'] # password = request.form['password'] # user = User(username=username, password=password) 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('/') # 毛豆田的主頁面 @main.route('/aindex', methods=['POST', 'GET']) def aindex_views(): if 'id' not in session and 'uname' not in session: return redirect('/') return render_template('aindex.html') # 毛豆車的主頁面 @main.route('/cindex/', methods=['POST', 'GET']) def cindex_views(tid): if request.method == 'GET': if 'id' not in session and 'uname' not in session: return redirect('/') return render_template('cindex.html', params=locals()) else: dict = request.form.to_dict() queryDate = dict['date'] year = int(queryDate.split('-')[0]) month = int(queryDate.split('-')[1]) day = int(queryDate.split('-')[2]) #判斷月份,找出查詢日期的前一天與後一天 if month == 1 or month == 3 or month == 5 or month == 7 or month == 10 or month == 12: start_year = year end_year = year if day == 1: start_month = month - 1 start_day = 30 end_month = month end_day = day + 1 if month == 1: start_year = year - 1 start_month = 12 start_day = 31 if month == 3: if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: start_day = 29 else: start_day = 28 elif day == 31: start_year = year start_month = month start_day = day - 1 end_year = year end_month = month + 1 if month == 12: end_year = year + 1 end_month = 1 end_day = 1 else: start_month = month start_day = day - 1 end_month = month end_day = day + 1 elif month == 2 or month == 4 or month == 6 or month == 8 or month == 9 or month == 11: start_year = year end_year = year if day == 1: start_month = month - 1 start_day = 31 end_month = month end_day = day + 1 elif day == 31: start_month = month start_day = day - 1 end_month = month + 1 end_day = 1 elif day == (28 or 29): if month == 2: start_month = month start_day = day - 1 end_month = month + 1 end_day = 1 else: start_month = month start_day = day - 1 end_month = month end_day = day + 1 #print(start_year, start_month, start_day, sep='-') #print(year, month, day, sep='-') #print(end_year, end_month, end_day, sep='-') start = date(year=start_year, month=start_month, day=start_day) end = date(year=end_year, month=end_month, day=end_day) if tid == '1': gps = Skyeye5GpsNr1.query.filter(Skyeye5GpsNr1.datetime < end).filter(Skyeye5GpsNr1.datetime > start).all() elif tid == '2': gps = Skyeye5GpsNr2.query.filter(Skyeye5GpsNr2.datetime < end).filter(Skyeye5GpsNr2.datetime > start).all() elif tid == '3': gps = Skyeye5GpsNr3.query.filter(Skyeye5GpsNr3.datetime < end).filter(Skyeye5GpsNr3.datetime > start).all() return render_template('cindex.html', params=locals()) # 毛豆田拍攝設定的主頁面 @main.route('/ashoot_setting/', methods=['POST', 'GET']) def ashoot_setting_views(tid): if request.method == 'GET': if 'id' not in session and 'uname' not in session: return redirect('/') if tid == '1': nr = 'GTW009002001' dic = '1' elif tid == '2': nr = 'GTW009002002' dic = '2' elif tid == '3': nr = 'GTW009002003' dic = '3' elif tid == '4': nr = 'GTW009002004' dic = '4' elif tid == '5': nr = 'GTW009002005' dic = '5' elif tid == '6': nr = 'GTW009002006' dic = '6' elif tid == '7': nr = 'GTW009002007' dic = '7' elif tid == '8': nr = 'GTW009002008' dic = '8' elif tid == '9': nr = 'GTW009002009' dic = '9' elif tid == '10': nr = 'GTW009002010' dic = '10' elif tid == '11': nr = 'GTW009002011' dic = '11' elif tid == '12': nr = 'GTW009002012' dic = '12' elif tid == '13': nr = 'GTW009002013' dic = '13' elif tid == '14': nr = 'GTW009002014' dic = '14' elif tid == '15': nr = 'GTW009002015' dic = '15' elif tid == '16': nr = 'GTW009002016' dic = '16' elif tid == '17': nr = 'GTW009002017' dic = '17' elif tid == '18': nr = 'GTW009002018' dic = '18' elif tid == '19': nr = 'GTW009002019' dic = '19' #讓前端頁面顯示小兵現在的位置以及最新照片 try: orientation = Orientation.query.filter_by(nr=nr).order_by(text('datetime desc')).first() mode = orientation.mode begin_tilt_angle = orientation.tilt_angle begin_pan_angle = orientation.pan_angle begin_zoom = orientation.zoom except Exception as e: pass try: # 可見光(自動拍照)路徑 path = 'http://60.250.156.234/cust/' + dic + '/nj' + str(mode) res = requests.get(path).text p = re.compile(r'', methods=['POST', 'GET']) # def orientation_views(tid): # if request.method == 'GET': # dict = request.args.to_dict() # nr = dict['nr'] # # if nr == 'GTW009001001': # dic = '1m' # elif nr == 'GTW009001002': # dic = '2m' # elif nr == 'GTW009001003': # dic = '3m' # elif nr == 'GTW009002001': # dic = '1' # elif nr == 'GTW009002002': # dic = '2' # elif nr == 'GTW009002003': # dic = '3' # elif nr == 'GTW009002004': # dic = '4' # elif nr == 'GTW009002005': # dic = '5' # elif nr == 'GTW009002006': # dic = '6' # elif nr == 'GTW009002007': # dic = '7' # elif nr == 'GTW009002008': # dic = '8' # elif nr == 'GTW009002009': # dic = '9' # elif nr == 'GTW009002010': # dic = '10' # elif nr == 'GTW009002011': # dic = '11' # elif nr == 'GTW009002012': # dic = '12' # elif nr == 'GTW009002013': # dic = '13' # elif nr == 'GTW009002014': # dic = '14' # elif nr == 'GTW009002015': # dic = '15' # elif nr == 'GTW009002016': # dic = '16' # elif nr == 'GTW009002017': # dic = '17' # elif nr == 'GTW009002018': # dic = '18' # # # # try: # orientation = Orientation.query.filter_by(nr=nr).order_by(text('datetime desc')).first() # mode = orientation.mode # tilt_angle = orientation.tilt_angle # pan_angle = orientation.pan_angle # zoom = orientation.zoom # # # dict = {"tilt_angle":tilt_angle, "pan_angle":pan_angle, "zoom":zoom, "status":True} # # except Exception as e: # dict = {"status": False} # return json.dumps(dict) # try: # # 可見光(自動拍照)路徑 # path = 'http://60.250.156.234/cust/' + dic + '/nj' + str(mode) # res = requests.get(path).text # p = re.compile(r'', methods=['POST', 'GET']) def aimg_history_views(tid): if request.method == 'GET': if 'id' not in session and 'uname' not in session: return redirect('/') #手動拍攝訊息 res = requests.get('http://60.250.156.234/cust/c' + tid + '.txt') data = res.text #判斷是否沒有此文件且為空頁面 if "404 Not Found" not in data and data: # 如果頁面是空的,在正則匹配就會報錯,所以加上try捕捉異常 try: #匹配出年份 py = re.compile(r'(\d+)\-\d+\-\d+\_\d+\.\d+n', re.S) # 先轉成集合去重,在轉成列表做排序 Ly = list(set(py.findall(data))) Ly.sort(reverse = True) #匹配出月份 pm = re.compile(Ly[0] + r'\-(\d+)\-\d+\_\d+\.\d+n', re.S) Lm = list(set(pm.findall(data))) Lm.sort(reverse = True) #匹配出天數 pd = re.compile(Ly[0] + "-" + Lm[0] + r'\-(\d+)\_\d+\.\d+n', re.S) Ld = list(set(pd.findall(data))) Ld.sort(reverse=True) #匹配出時間 pt = re.compile(Ly[0] + "-" + Lm[0] + "-" + Ld[0] + r'\_(\d+\.\d+)n', re.S) Lt = list(set(pt.findall(data))) Lt.sort(reverse = True) except Exception as e: pass #1號方位訊息 res = requests.get('http://60.250.156.234/cust/c' + tid + '_1.txt') res.encoding = 'utf-8' data = res.text if "404 Not Found" not in data and data: #如果頁面是空的,在正則匹配就會報錯,所以加上try捕捉異常 try: #匹配出方位名稱 pn = re.compile(r'\d+\-\d+\-\d+\_\d+\.\d+n\_([a-zA-Z\u4e00-\u9fa5]+)', re.S) L = pn.findall(data)[::-1] # 先轉成集合去重,在轉成列表做排序 Ln1 = list(set(L)) #讓排序不受到中文編碼影響,加入index Ln1.sort(key=L.index) #匹配出年份 py = re.compile(r'(\d+)\-\d+\-\d+\_\d+\.\d+n', re.S) # 先轉成集合去重,在轉成列表做排序 Ly1 = list(set(py.findall(data))) Ly1.sort(reverse=True) #匹配出月份 pm = re.compile(Ly1[0] + r'\-(\d+)\-\d+\_\d+\.\d+n', re.S) Lm1 = list(set(pm.findall(data))) Lm1.sort(reverse=True) #匹配出天數 pd = re.compile(Ly1[0] + "-" + Lm1[0] + r'\-(\d+)\_\d+\.\d+n', re.S) Ld1 = list(set(pd.findall(data))) Ld1.sort(reverse=True) #匹配出時間 pt = re.compile(Ly1[0] + "-" + Lm1[0] + "-" + Ld1[0] + r'\_(\d+\.\d+)n', re.S) Lt1 = list(set(pt.findall(data))) Lt1.sort(reverse = True) except Exception as e: pass #2號方位訊息 res = requests.get('http://60.250.156.234/cust/c' + tid + '_2.txt') res.encoding = 'utf-8' data = res.text if "404 Not Found" not in data and data: # 如果頁面是空的,在正則匹配就會報錯,所以加上try捕捉異常 try: #匹配出方位名稱 pn = re.compile(r'\d+\-\d+\-\d+\_\d+\.\d+n\_([a-zA-Z\u4e00-\u9fa5]+)', re.S) L = pn.findall(data)[::-1] # 先轉成集合去重,在轉成列表做排序 Ln2 = list(set(L)) #讓排序不受到中文編碼影響,加入index Ln2.sort(key=L.index) #匹配出年份 py = re.compile(r'(\d+)\-\d+\-\d+\_\d+\.\d+n', re.S) # 先轉成集合去重,在轉成列表做排序 Ly2 = list(set(py.findall(data))) Ly2.sort(reverse = True) #匹配出月份 pm = re.compile(Ly2[0] + r'\-(\d+)\-\d+\_\d+\.\d+n', re.S) Lm2 = list(set(pm.findall(data))) Lm2.sort(reverse = True) #匹配出天數 pd = re.compile(Ly2[0] + "-" + Lm2[0] + r'\-(\d+)\_\d+\.\d+n', re.S) Ld2 = list(set(pd.findall(data))) Ld2.sort(reverse = True) #匹配出時間 pt = re.compile(Ly2[0] + "-" + Lm2[0] + "-" + Ld2[0] + r'\_(\d+\.\d+)n', re.S) Lt2 = list(set(pt.findall(data))) Lt2.sort(reverse = True) except Exception as e: pass #3號方位訊息 res = requests.get('http://60.250.156.234/cust/c' + tid + '_3.txt') res.encoding = 'utf-8' data = res.text if "404 Not Found" not in data and data: # 如果頁面是空的,在正則匹配就會報錯,所以加上try捕捉異常 try: #匹配出方位名稱 pn = re.compile(r'\d+\-\d+\-\d+\_\d+\.\d+n\_([a-zA-Z\u4e00-\u9fa5]+)', re.S) L = pn.findall(data)[::-1] # 先轉成集合去重,在轉成列表做排序 Ln3 = list(set(L)) #讓排序不受到中文編碼影響,加入index Ln3.sort(key=L.index) #匹配出年份 py = re.compile(r'(\d+)\-\d+\-\d+\_\d+\.\d+n', re.S) # 先轉成集合去重,在轉成列表做排序 Ly3 = list(set(py.findall(data))) Ly3.sort(reverse = True) #匹配出月份 pm = re.compile(Ly3[0] + r'\-(\d+)\-\d+\_\d+\.\d+n', re.S) Lm3 = list(set(pm.findall(data))) Lm3.sort(reverse = True) #匹配出天數 pd = re.compile(Ly3[0] + "-" + Lm3[0] + r'\-(\d+)\_\d+\.\d+n', re.S) Ld3 = list(set(pd.findall(data))) Ld3.sort(reverse = True) #匹配出時間 pt = re.compile(Ly3[0] + "-" + Lm3[0] + "-" + Ld3[0] + r'\_(\d+\.\d+)n', re.S) Lt3 = list(set(pt.findall(data))) Lt3.sort(reverse = True) except Exception as e: pass #4號方位訊息 res = requests.get('http://60.250.156.234/cust/c' + tid + '_4.txt') res.encoding = 'utf-8' data = res.text if "404 Not Found" not in data and data: # 如果頁面是空的,在正則匹配就會報錯,所以加上try捕捉異常 try: #匹配出方位名稱 pn = re.compile(r'\d+\-\d+\-\d+\_\d+\.\d+n\_([a-zA-Z\u4e00-\u9fa5]+)', re.S) L = pn.findall(data)[::-1] # 先轉成集合去重,在轉成列表做排序 Ln4 = list(set(L)) #讓排序不受到中文編碼影響,加入index Ln4.sort(key=L.index) #匹配出年份 py = re.compile(r'(\d+)\-\d+\-\d+\_\d+\.\d+n', re.S) # 先轉成集合去重,在轉成列表做排序 Ly4 = list(set(py.findall(data))) Ly4.sort(reverse = True) #匹配出月份 pm = re.compile(Ly4[0] + r'\-(\d+)\-\d+\_\d+\.\d+n', re.S) Lm4 = list(set(pm.findall(data))) Lm4.sort(reverse = True) #匹配出天數 pd = re.compile(Ly4[0] + "-" + Lm4[0] + r'\-(\d+)\_\d+\.\d+n', re.S) Ld4 = list(set(pd.findall(data))) Ld4.sort(reverse = True) #匹配出時間 pt = re.compile(Ly4[0] + "-" + Lm4[0] + "-" + Ld4[0] + r'\_(\d+\.\d+)n', re.S) Lt4 = list(set(pt.findall(data))) Lt4.sort(reverse = True) except Exception as e: pass #5號方位訊息 res = requests.get('http://60.250.156.234/cust/c' + tid + '_5.txt') res.encoding = 'utf-8' data = res.text if "404 Not Found" not in data and data: # 如果頁面是空的,在正則匹配就會報錯,所以加上try捕捉異常 try: #匹配出方位名稱 pn = re.compile(r'\d+\-\d+\-\d+\_\d+\.\d+n\_([a-zA-Z\u4e00-\u9fa5]+)', re.S) L = pn.findall(data)[::-1] # 先轉成集合去重,在轉成列表做排序 Ln5 = list(set(L)) #讓排序不受到中文編碼影響,加入index Ln5.sort(key=L.index) #匹配出年份 py = re.compile(r'(\d+)\-\d+\-\d+\_\d+\.\d+n', re.S) # 先轉成集合去重,在轉成列表做排序 Ly5 = list(set(py.findall(data))) Ly5.sort(reverse = True) #匹配出月份 pm = re.compile(Ly5[0] + r'\-(\d+)\-\d+\_\d+\.\d+n', re.S) Lm5 = list(set(pm.findall(data))) Lm5.sort(reverse = True) #匹配出天數 pd = re.compile(Ly5[0] + "-" + Lm5[0] + r'\-(\d+)\_\d+\.\d+n', re.S) Ld5 = list(set(pd.findall(data))) Ld5.sort(reverse = True) #匹配出時間 pt = re.compile(Ly5[0] + "-" + Lm5[0] + "-" + Ld5[0] + r'\_(\d+\.\d+)n', re.S) Lt5 = list(set(pt.findall(data))) Lt5.sort(reverse = True) except Exception as e: pass #6號方位訊息 res = requests.get('http://60.250.156.234/cust/c' + tid + '_6.txt') res.encoding = 'utf-8' data = res.text if "404 Not Found" not in data and data: # 如果頁面是空的,在正則匹配就會報錯,所以加上try捕捉異常 try: #匹配出方位名稱 pn = re.compile(r'\d+\-\d+\-\d+\_\d+\.\d+n\_([a-zA-Z\u4e00-\u9fa5]+)', re.S) L = pn.findall(data)[::-1] # 先轉成集合去重,在轉成列表做排序 Ln6 = list(set(L)) #讓排序不受到中文編碼影響,加入index Ln6.sort(key=L.index) #匹配出年份 py = re.compile(r'(\d+)\-\d+\-\d+\_\d+\.\d+n', re.S) # 先轉成集合去重,在轉成列表做排序 Ly6 = list(set(py.findall(data))) Ly6.sort(reverse = True) #匹配出月份 pm = re.compile(Ly6[0] + r'\-(\d+)\-\d+\_\d+\.\d+n', re.S) Lm6 = list(set(pm.findall(data))) Lm6.sort(reverse = True) #匹配出天數 pd = re.compile(Ly6[0] + "-" + Lm6[0] + r'\-(\d+)\_\d+\.\d+n', re.S) Ld6 = list(set(pd.findall(data))) Ld6.sort(reverse = True) #匹配出時間 pt = re.compile(Ly6[0] + "-" + Lm6[0] + "-" + Ld6[0] + r'\_(\d+\.\d+)n', re.S) Lt6 = list(set(pt.findall(data))) Lt6.sort(reverse = True) except Exception as e: pass #7號方位訊息 res = requests.get('http://60.250.156.234/cust/c' + tid + '_7.txt') res.encoding = 'utf-8' data = res.text if "404 Not Found" not in data and data: # 如果頁面是空的,在正則匹配就會報錯,所以加上try捕捉異常 try: #匹配出方位名稱 pn = re.compile(r'\d+\-\d+\-\d+\_\d+\.\d+n\_([a-zA-Z\u4e00-\u9fa5]+)', re.S) L = pn.findall(data)[::-1] # 先轉成集合去重,在轉成列表做排序 Ln7 = list(set(L)) #讓排序不受到中文編碼影響,加入index Ln7.sort(key=L.index) #匹配出年份 py = re.compile(r'(\d+)\-\d+\-\d+\_\d+\.\d+n', re.S) # 先轉成集合去重,在轉成列表做排序 Ly7 = list(set(py.findall(data))) Ly7.sort(reverse = True) #匹配出月份 pm = re.compile(Ly7[0] + r'\-(\d+)\-\d+\_\d+\.\d+n', re.S) Lm7 = list(set(pm.findall(data))) Lm7.sort(reverse = True) #匹配出天數 pd = re.compile(Ly7[0] + "-" + Lm7[0] + r'\-(\d+)\_\d+\.\d+n', re.S) Ld7 = list(set(pd.findall(data))) Ld7.sort(reverse = True) #匹配出時間 pt = re.compile(Ly7[0] + "-" + Lm7[0] + "-" + Ld7[0] + r'\_(\d+\.\d+)n', re.S) Lt7 = list(set(pt.findall(data))) Lt7.sort(reverse = True) except Exception as e: pass #8號方位訊息 res = requests.get('http://60.250.156.234/cust/c' + tid + '_8.txt') res.encoding = 'utf-8' data = res.text if "404 Not Found" not in data and data: # 如果頁面是空的,在正則匹配就會報錯,所以加上try捕捉異常 try: #匹配出方位名稱 pn = re.compile(r'\d+\-\d+\-\d+\_\d+\.\d+n\_([a-zA-Z\u4e00-\u9fa5]+)', re.S) L = pn.findall(data)[::-1] # 先轉成集合去重,在轉成列表做排序 Ln8 = list(set(L)) #讓排序不受到中文編碼影響,加入index Ln8.sort(key=L.index) #匹配出年份 py = re.compile(r'(\d+)\-\d+\-\d+\_\d+\.\d+n', re.S) # 先轉成集合去重,在轉成列表做排序 Ly8 = list(set(py.findall(data))) Ly8.sort(reverse = True) #匹配出月份 pm = re.compile(Ly8[0] + r'\-(\d+)\-\d+\_\d+\.\d+n', re.S) Lm8 = list(set(pm.findall(data))) Lm8.sort(reverse = True) #匹配出天數 pd = re.compile(Ly8[0] + "-" + Lm8[0] + r'\-(\d+)\_\d+\.\d+n', re.S) Ld8 = list(set(pd.findall(data))) Ld8.sort(reverse = True) #匹配出時間 pt = re.compile(Ly8[0] + "-" + Lm8[0] + "-" + Ld8[0] + r'\_(\d+\.\d+)n', re.S) Lt8 = list(set(pt.findall(data))) Lt8.sort(reverse = True) except Exception as e: pass if tid == '1': nr = 'GTW009002001' elif tid == '2': nr = 'GTW009002002' elif tid == '3': nr = 'GTW009002003' elif tid == '4': nr = 'GTW009002004' elif tid == '5': nr = 'GTW009002005' elif tid == '6': nr = 'GTW009002006' elif tid == '7': nr = 'GTW009002007' elif tid == '8': nr = 'GTW009002008' elif tid == '9': nr = 'GTW009002009' elif tid == '10': nr = 'GTW009002010' elif tid == "11": nr = 'GTW009002011' elif tid == '12': nr = 'GTW009002012' elif tid == '13': nr = 'GTW009002013' elif tid == '14': nr = 'GTW009002014' elif tid == '15': nr = 'GTW009002015' elif tid == '16': nr = 'GTW009002016' elif tid == '17': nr = 'GTW009002017' elif tid == '18': nr = 'GTW009002018' elif tid == '19': nr = 'GTW009002019' try: orientation = Orientation.query.filter_by(nr=nr).order_by(text('datetime desc')).first() mode = orientation.mode except Exception as e: pass try: # 可見光(自動拍照)路徑 path = 'http://60.250.156.234/cust/' + tid + '/nj' + str(mode) res = requests.get(path).text p = re.compile(r'', methods=['POST', 'GET']) def cshoot_setting_views(tid): if request.method == 'GET': if 'id' not in session and 'uname' not in session: return redirect('/') if tid == '1': nr = 'GTW009001001' dic = '1m' elif tid == '2': nr = 'GTW009001002' dic = '2m' elif tid == '3': nr = 'GTW009001003' dic = '3m' #讓前端頁面顯示小兵現在的位置以及最新照片 try: orientation = Orientation.query.filter_by(nr=nr).order_by(text('datetime desc')).first() mode = orientation.mode begin_tilt_angle = orientation.tilt_angle begin_pan_angle = orientation.pan_angle begin_zoom = orientation.zoom except Exception as e: pass try: # 可見光(自動拍照)路徑 path = 'http://60.250.156.234/cust/' + dic + '/nj' + str(mode) res = requests.get(path).text p = re.compile(r'', methods=['POST', 'GET']) def cimg_history_views(tid): if request.method == 'GET': if 'id' not in session and 'uname' not in session: return redirect('/') # 手動拍攝訊息 res = requests.get('http://60.250.156.234/cust/c' + tid + 'm.txt') data = res.text # 判斷是否沒有此文件且為空頁面 if "404 Not Found" not in data and data: # 如果頁面是空的,在正則匹配就會報錯,所以加上try捕捉異常 try: # 匹配出年份 py = re.compile(r'(\d+)\-\d+\-\d+\_\d+\.\d+n', re.S) # 先轉成集合去重,在轉成列表做排序 Ly = list(set(py.findall(data))) Ly.sort(reverse=True) # 匹配出月份 pm = re.compile(Ly[0] + r'\-(\d+)\-\d+\_\d+\.\d+n', re.S) Lm = list(set(pm.findall(data))) Lm.sort(reverse=True) # 匹配出天數 pd = re.compile(Ly[0] + "-" + Lm[0] + r'\-(\d+)\_\d+\.\d+n', re.S) Ld = list(set(pd.findall(data))) Ld.sort(reverse=True) # 匹配出時間 pt = re.compile(Ly[0] + "-" + Lm[0] + "-" + Ld[0] + r'\_(\d+\.\d+)n', re.S) Lt = list(set(pt.findall(data))) Lt.sort(reverse=True) except Exception as e: pass if tid == '1': nr = 'GTW009001001' dic = '1m' elif tid == '2': nr = 'GTW009001002' dic = '2m' elif tid == '3': nr = 'GTW009001003' dic = '3m' try: orientation = Orientation.query.filter_by(nr=nr).order_by(text('datetime desc')).first() mode = orientation.mode except Exception: pass try: # 可見光(自動拍照)路徑 path = 'http://60.250.156.234/cust/' + dic + '/nj' + str(mode) res = requests.get(path).text p = re.compile(r' waiting for connection") # 影像生成器函數,將影像以jpg格式傳給前端 def generate(): while True: with lock: global s_sock, frame_info, buffer, frame, encodedImage if s_sock == 0: break data, address = s_sock.recvfrom(max_length) if len(data) < 100: frame_info = pickle.loads(data) if frame_info: nums_of_packs = frame_info["packs"] for i in range(nums_of_packs): if s_sock == 0: break data, address = s_sock.recvfrom(max_length) if i == 0: buffer = data else: buffer += data frame = np.frombuffer(buffer, dtype=np.uint8) frame = frame.reshape(frame.shape[0], 1) frame = cv2.imdecode(frame, cv2.IMREAD_UNCHANGED) #如果frame為None就跳過 if frame is None: continue frame = cv2.resize(frame, (640, 360), interpolation=cv2.INTER_AREA) # encode the frame in JPEG format (flag, encodedImage) = cv2.imencode(".jpg", frame) # ensure the frame was successfully encoded if not flag: continue # yield the output frame in the byte format yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + bytearray(encodedImage) + b'\r\n') # return the response generated along with the specific media # type (mime type) return Response(generate(), mimetype="multipart/x-mixed-replace; boundary=frame") else: if s_sock != 0: s_sock.close() s_sock = 0 # print('s_sock is closed') return "s_sock is closed" # print("test") return "s_sock is closed" #控制循環退出的開關 D = {'GTW009001001':0, 'GTW009001002':0, 'GTW009001003':0, 'GTW009002001':0, 'GTW009002002':0, 'GTW009002003':0, 'GTW009002004':0, 'GTW009002005':0, 'GTW009002006':0, 'GTW009002007':0, 'GTW009002008':0, 'GTW009002009':0, 'GTW009002010':0, 'GTW009002011':0, 'GTW009002012':0, 'GTW009002013':0, 'GTW009002014':0, 'GTW009002015':0, 'GTW009002016':0, 'GTW009002017':0, 'GTW009002018':0, 'GTW009002019':0} #判斷小兵接收回傳訊息是否為這隻程式,還是另一個edamame_brake.py排程程式 d = {'GTW009001001_n':0, 'GTW009001002_n':0, 'GTW009001003_n':0, 'GTW009002001_n':0, 'GTW009002002_n':0, 'GTW009002003_n':0, 'GTW009002004_n':0, 'GTW009002005_n':0, 'GTW009002006_n':0, 'GTW009002007_n':0, 'GTW009002008_n':0, 'GTW009002009_n':0, 'GTW009002010_n':0, 'GTW009002011_n':0, 'GTW009002012_n':0, 'GTW009002013_n':0, 'GTW009002014_n':0, 'GTW009002015_n':0, 'GTW009002016_n':0, 'GTW009002017_n':0, 'GTW009002018_n':0, 'GTW009002019_n':0} #mqtt發布 @main.route('/mqtt/', methods=['GET','POST']) def mqtt_views(node_id): import json dict = request.args.to_dict() json = json.dumps(dict) #判斷毛豆車和毛豆田的node_id if node_id == 'GTW009001001': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:b7:52:9c' elif node_id == 'GTW009001002': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:c1:72:0c' elif node_id == 'GTW009001003': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:84:e9:3f' elif node_id == 'GTW009002001': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:95:00:53' elif node_id == 'GTW009002002': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:e7:51:44' elif node_id == 'GTW009002003': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:ce:a5:35' elif node_id == 'GTW009002004': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:fc:9d:34' elif node_id == 'GTW009002005': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:86:00:c9' elif node_id == 'GTW009002006': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:df:4b:0f' elif node_id == 'GTW009002007': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:6a:5d:17' elif node_id == 'GTW009002008': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:d2:d0:8f' elif node_id == 'GTW009002009': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:57:3c:da' elif node_id == 'GTW009002010': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:bd:29:b1' elif node_id == 'GTW009002011': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:7c:f6:06' elif node_id == 'GTW009002012': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:59:9d:bd' elif node_id == 'GTW009002013': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:e7:21:e5' elif node_id == 'GTW009002014': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:34:9e:39' elif node_id == 'GTW009002015': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:e3:f1:f4' elif node_id == 'GTW009002016': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:f8:24:92' elif node_id == 'GTW009002017': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:9d:68:05' elif node_id == 'GTW009002018': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:4d:e4:34' elif node_id == 'GTW009002019': topic = 'AISKY/AppleFarm/MK-G/b8:27:eb:e7:16:28' #假設MQTT命令為開啟影像串流,在3分鐘後自動發布關閉該影像串流 # if dict['command'] == 'a053': # def close_video_views(): # import json # sl(180) # dict['command'] = 'a054' # json = json.dumps(dict) # print(json) # mqtt.publish(topic, json) # print('close video test') # # pool.submit(close_video_views) num = node_id +'_n' mqtt.publish(topic, json) d[num] = 1 if dict['command'] != 'a053': if node_id != "GTW009002019": if dict['command'] == 'a051': sl(600) elif dict['command'] == 'a018' or dict['command'] == 'a016' or dict['command'] == 'a052': sl(40) else: sl(10) if D[node_id] == 0: mqtt.publish(topic, json) if dict['command'] == 'a051': sl(600) elif dict['command'] == 'a018' or dict['command'] == 'a016' or dict['command'] == 'a052': sl(40) else: sl(10) if D[node_id] == 0: mqtt.publish(topic, json) if dict['command'] == 'a051': sl(600) elif dict['command'] == 'a018' or dict['command'] == 'a016' or dict['command'] == 'a052': sl(40) else: sl(10) if D[node_id] == 0: status = Status() status.nr = node_id status.status = 'fail' status.datetime = dt.now() db.session.add(status) db.session.commit() d[num] = 0 D[node_id] = 0 return "Publish done" #處理mqtt訂閱的信息 @mqtt.on_message() def handle_mqtt_message(client, userdata, message): payload = message.payload.decode() print(payload) if payload: p = json.loads(payload) if d[p['node_id'] +'_n'] == 1: if 'filename' in p: img = ImageSignalLog() img.nr = p['node_id'] img.command = p['command'] img.datetime = dt.now() img.response = p['rqnn'] img.position = p['position'] img.time = p['time'] img.size_a = p['a'] img.size_b = p['b'] img.filename = p['filename'] db.session.add(img) else: sig = SignalLog() sig.nr = p['node_id'] sig.command = p['command'] sig.datetime = dt.now() sig.response = p['rqnn'] if 'tilt' in p: sig.tilt_angle = p['tilt'] if 'pan' in p: sig.pan_angle = p['pan'] if 'zoom' in p: sig.zoom = p['zoom'] if 'position' in p: sig.position = p['position'] db.session.add(sig) db.session.commit() D[p['node_id']] = 1 d[p['node_id'] +'_n'] = 0 # print("-------msg-------") # print('name :', p['name']) # print('email :', p['email'])