# 主業務邏輯中的視圖和路由的定義 import os import datetime from turtle import title from flask import Flask, redirect, render_template, request, session, Response, jsonify from sqlalchemy.sql.expression import desc, false, null # 導入藍圖程序,用於構建路由 from werkzeug.utils import redirect from werkzeug.wrappers import response from . import main from coffee_manage20221221 import mqtt # 導入db,用於操作數據庫 from coffee_manage20221221 import db # 導入實體類,用於操作數據庫 from ..models import * import json from datetime import datetime as dt from datetime import timedelta from sqlalchemy import text import pymysql import pandas as pd from concurrent.futures import ThreadPoolExecutor import cv2 import pickle import socket import time import threading import numpy as np import math from .mqtt import MQTT from matplotlib import pyplot as plt # import random from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg as FigureCanvas # import io # from flask import make_response # from distutils.util import strtobool # 將字串 string 轉成布林 boolean # import psutil import subprocess import shutil import stat import datetime import paho.mqtt.publish as publish import paho.mqtt.client as mqtt_1 import random import paramiko from flask_mqtt import Mqtt pool = ThreadPoolExecutor(25) s_sock = 0 lock = threading.Lock() # 主頁的訪問路徑 @main.route('/') def main_index(): # # 獲取登入信息 # if 'id' in session and 'uname' in session and 'status' in session: # username = session['uname'] # status = session['status'] # # if status == 9: # return render_template('signin_disable.html') # elif status == 8: # return render_template('signin_new.html') # # # Rita 參數 params 是用來取得參數的 locals=() 所有參數 # return render_template('index.html', params=locals()) # else: # return render_template('sign_in.html') return render_template("index.html", title='ERP WEB') # 發酵槽自動化測試頁 @main.route('/ferment_auto', methods=['GET', 'POST']) def ferment_auto(): os.system('python D:\\fatwolf\\company_files\\flask\\coffeeproject\\app\\test_logic.py') ferment_status = ferment_container_status.query.order_by(text('datetime desc')).first() db.session.close() FI1 = ferment_status.Ferment_Input_1 FI2 = ferment_status.Ferment_Input_2 F1 = ferment_status.Ferment_Tank_1 F2 = ferment_status.Ferment_Tank_2 F3 = ferment_status.Ferment_Tank_3 F4 = ferment_status.Ferment_Tank_4 F5 = ferment_status.Ferment_Tank_5 F6 = ferment_status.Ferment_Tank_6 F7 = ferment_status.Ferment_Tank_7 F8 = ferment_status.Ferment_Tank_8 F9 = ferment_status.Ferment_Tank_9 F10 = ferment_status.Ferment_Tank_10 F11 = ferment_status.Ferment_Tank_11 F12 = ferment_status.Ferment_Tank_12 FO1 = ferment_status.Ferment_Output_1 FO2 = ferment_status.Ferment_Output_2 clean_status = clean_container_status.query.order_by(text('datetime desc')).first() db.session.close() PO1 = clean_status.Peel_Output_1 # PO_Waiting # PO_OutputtingBean PO2 = clean_status.Peel_Output_2 dry_status = dry_container_status.query.order_by(text('datetime desc')).first() db.session.close() DI1 = dry_status.Dry_Input_1 DI2 = dry_status.Dry_Input_2 # 發酵桶_入料高度 input_UltraSonic = ferment_input_UltraSonic.query.filter_by(tank_num='F' + '1').order_by( text('datetime desc')).first() db.session.close() # UltraSonic = float("{:.2f}".format(34.6 - float(input_UltraSonic.UltraSonic))) UltraSonic_input = float(input_UltraSonic.UltraSonic) # 感測器_發酵桶_超音波感測器 tank_UltraSonic = ferment_tank_UltraSonic.query.filter_by(tank_num='F' + '1').order_by( text('datetime desc')).first() db.session.close() UltraSonic_tank = (float(tank_UltraSonic.UltraSonic)) # 感測器_發酵桶_SHT11 tank_SHT11 = ferment_tank_SHT11.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first() db.session.close() SHT11_Temp = float(tank_SHT11.SHT11_Temp) SHT11_Humidity = float(tank_SHT11.SHT11_Humidity) # 感測器_發酵桶_二氧化碳 tank_CO2 = ferment_tank_CO2.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first() db.session.close() CO2 = float(tank_CO2.CO2) # 感測器_發酵桶_酸鹼值 tank_PH = ferment_tank_PH.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first() db.session.close() tank_ORP = ferment_tank_ORP.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first() db.session.close() tank_DO = ferment_tank_DO.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first() db.session.close() tank_EC = ferment_tank_EC.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first() db.session.close() PH = float(tank_PH.PH) ORP = float(tank_ORP.ORP) DO = float(tank_DO.DO) EC = float(tank_EC.EC) # 感測器_發酵桶_氣壓 tank_PA = ferment_tank_PA.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first() db.session.close() PA = float(tank_PA.PA) # 感測器_發酵桶_保溫夾層水位計 tank_WaterLevel = ferment_tank_WaterLevel.query.filter_by(tank_num='F' + '1').order_by( text('datetime desc')).first() db.session.close() WaterLevel = tank_WaterLevel.WaterLevel # 發酵桶_出料高度 output_UltraSonic = ferment_output_UltraSonic.query.filter_by(tank_num='F' + '1').order_by( text('datetime desc')).first() db.session.close() UltraSonic_output = float(output_UltraSonic.UltraSonic) return render_template('ferment_auto.html', title="[測試] 發酵自動化", **locals()) # else: # return render_template('sign_in.html') # 乾燥槽自動化測試頁 @main.route('/dry_auto', methods=['GET', 'POST']) def dry_auto(): dry_status = dry_container_status.query.order_by(text('datetime desc')).first() db.session.close() DI1 = dry_status.Dry_Input_1 DI2 = dry_status.Dry_Input_2 D1 = dry_status.Dry_Tank_1 D2 = dry_status.Dry_Tank_2 D3 = dry_status.Dry_Tank_3 D4 = dry_status.Dry_Tank_4 D5 = dry_status.Dry_Tank_5 D6 = dry_status.Dry_Tank_6 D7 = dry_status.Dry_Tank_7 D8 = dry_status.Dry_Tank_8 D9 = dry_status.Dry_Tank_9 D10 = dry_status.Dry_Tank_10 D11 = dry_status.Dry_Tank_11 D12 = dry_status.Dry_Tank_12 DO1 = dry_status.Dry_Output_1 DO2 = dry_status.Dry_Output_2 #print(DI1,DI2,D1,D2,D3,D4,D5,D6,D7,D8,D9,D10,D11,D12,DO1,DO2) input_seneor = dry_input_sensor.query.filter_by(tank_num='D' + '1').order_by( text('datetime desc')).first() db.session.close() UltraSonic_input = float(input_seneor.UltraSonic) # tank_SHT11 = dry_tank_SHT11.query.filter_by(tank_num='D' + '1').order_by( # text('datetime desc')).first() # SHT11_Temp = float(tank_SHT11.SHT11_Temp) # SHT11_Humidity = float(tank_SHT11.SHT11_Humidity) tank_sht11 = dry_tank_sht11.query.filter_by(tank_num='D' + '1').order_by( text('datetime desc')).first() db.session.close() sht11_Temp = float(tank_sht11.SHT11_Temp) sht11_Humidity = float(tank_sht11.SHT11_Humidity) tank_Soil = dry_tank_Soil.query.filter_by(tank_num='D' + '1').order_by( text('datetime desc')).first() db.session.close() Soil_Temp = float(tank_Soil.soil_Temp) Soil_Humidity = float(tank_Soil.soil_Humidity) Soil_EC = float(tank_Soil.soil_EC) # tank_Soil = dry_tank_Soil.query.filter_by(tank_num='D' + '1').order_by( # text('datetime desc')).first() # Soil_Temp = float(tank_Soil.Soil_Temp) # Soil_Humidity = float(tank_Soil.Soil_Humidity) # Soil_EC = float(tank_Soil.Soil_EC) tank_PA = dry_tank_PA.query.filter_by(tank_num='D' + '1').order_by( text('datetime desc')).first() db.session.close() PA = float(tank_PA.PA) tank_UltraSonic = dry_tank_UltraSonic.query.filter_by(tank_num='D' + '1').order_by( text('datetime desc')).first() db.session.close() UltraSonic_tank = float(tank_UltraSonic.UltraSonic) output_sensor = dry_output_sensor.query.filter_by(tank_num='D' + '1').order_by( text('datetime desc')).first() db.session.close() UltraSonic_output = float(output_sensor.UltraSonic) return render_template('dry_auto.html', title="[測試] 乾燥自動化", **locals())