views.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # 主業務邏輯中的視圖和路由的定義
  2. import os
  3. import datetime
  4. from turtle import title
  5. from flask import Flask, redirect, render_template, request, session, Response, jsonify
  6. from sqlalchemy.sql.expression import desc, false, null
  7. # 導入藍圖程序,用於構建路由
  8. from werkzeug.utils import redirect
  9. from werkzeug.wrappers import response
  10. from . import main
  11. from coffee_manage20221221 import mqtt
  12. # 導入db,用於操作數據庫
  13. from coffee_manage20221221 import db
  14. # 導入實體類,用於操作數據庫
  15. from ..models import *
  16. import json
  17. from datetime import datetime as dt
  18. from datetime import timedelta
  19. from sqlalchemy import text
  20. import pymysql
  21. import pandas as pd
  22. from concurrent.futures import ThreadPoolExecutor
  23. import cv2
  24. import pickle
  25. import socket
  26. import time
  27. import threading
  28. import numpy as np
  29. import math
  30. from .mqtt import MQTT
  31. from matplotlib import pyplot as plt
  32. # import random
  33. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg as FigureCanvas
  34. # import io
  35. # from flask import make_response
  36. # from distutils.util import strtobool # 將字串 string 轉成布林 boolean
  37. # import psutil
  38. import subprocess
  39. import shutil
  40. import stat
  41. import datetime
  42. import paho.mqtt.publish as publish
  43. import paho.mqtt.client as mqtt_1
  44. import random
  45. import paramiko
  46. from flask_mqtt import Mqtt
  47. pool = ThreadPoolExecutor(25)
  48. s_sock = 0
  49. lock = threading.Lock()
  50. # 主頁的訪問路徑
  51. @main.route('/')
  52. def main_index():
  53. # # 獲取登入信息
  54. # if 'id' in session and 'uname' in session and 'status' in session:
  55. # username = session['uname']
  56. # status = session['status']
  57. #
  58. # if status == 9:
  59. # return render_template('signin_disable.html')
  60. # elif status == 8:
  61. # return render_template('signin_new.html')
  62. #
  63. # # Rita 參數 params 是用來取得參數的 locals=() 所有參數
  64. # return render_template('index.html', params=locals())
  65. # else:
  66. # return render_template('sign_in.html')
  67. return render_template("index.html", title='ERP WEB')
  68. # 發酵槽自動化測試頁
  69. @main.route('/ferment_auto', methods=['GET', 'POST'])
  70. def ferment_auto():
  71. os.system('python D:\\fatwolf\\company_files\\flask\\coffeeproject\\app\\test_logic.py')
  72. ferment_status = ferment_container_status.query.order_by(text('datetime desc')).first()
  73. db.session.close()
  74. FI1 = ferment_status.Ferment_Input_1
  75. FI2 = ferment_status.Ferment_Input_2
  76. F1 = ferment_status.Ferment_Tank_1
  77. F2 = ferment_status.Ferment_Tank_2
  78. F3 = ferment_status.Ferment_Tank_3
  79. F4 = ferment_status.Ferment_Tank_4
  80. F5 = ferment_status.Ferment_Tank_5
  81. F6 = ferment_status.Ferment_Tank_6
  82. F7 = ferment_status.Ferment_Tank_7
  83. F8 = ferment_status.Ferment_Tank_8
  84. F9 = ferment_status.Ferment_Tank_9
  85. F10 = ferment_status.Ferment_Tank_10
  86. F11 = ferment_status.Ferment_Tank_11
  87. F12 = ferment_status.Ferment_Tank_12
  88. FO1 = ferment_status.Ferment_Output_1
  89. FO2 = ferment_status.Ferment_Output_2
  90. clean_status = clean_container_status.query.order_by(text('datetime desc')).first()
  91. db.session.close()
  92. PO1 = clean_status.Peel_Output_1 # PO_Waiting # PO_OutputtingBean
  93. PO2 = clean_status.Peel_Output_2
  94. dry_status = dry_container_status.query.order_by(text('datetime desc')).first()
  95. db.session.close()
  96. DI1 = dry_status.Dry_Input_1
  97. DI2 = dry_status.Dry_Input_2
  98. # 發酵桶_入料高度
  99. input_UltraSonic = ferment_input_UltraSonic.query.filter_by(tank_num='F' + '1').order_by(
  100. text('datetime desc')).first()
  101. db.session.close()
  102. # UltraSonic = float("{:.2f}".format(34.6 - float(input_UltraSonic.UltraSonic)))
  103. UltraSonic_input = float(input_UltraSonic.UltraSonic)
  104. # 感測器_發酵桶_超音波感測器
  105. tank_UltraSonic = ferment_tank_UltraSonic.query.filter_by(tank_num='F' + '1').order_by(
  106. text('datetime desc')).first()
  107. db.session.close()
  108. UltraSonic_tank = (float(tank_UltraSonic.UltraSonic))
  109. # 感測器_發酵桶_SHT11
  110. tank_SHT11 = ferment_tank_SHT11.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first()
  111. db.session.close()
  112. SHT11_Temp = float(tank_SHT11.SHT11_Temp)
  113. SHT11_Humidity = float(tank_SHT11.SHT11_Humidity)
  114. # 感測器_發酵桶_二氧化碳
  115. tank_CO2 = ferment_tank_CO2.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first()
  116. db.session.close()
  117. CO2 = float(tank_CO2.CO2)
  118. # 感測器_發酵桶_酸鹼值
  119. tank_PH = ferment_tank_PH.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first()
  120. db.session.close()
  121. tank_ORP = ferment_tank_ORP.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first()
  122. db.session.close()
  123. tank_DO = ferment_tank_DO.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first()
  124. db.session.close()
  125. tank_EC = ferment_tank_EC.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first()
  126. db.session.close()
  127. PH = float(tank_PH.PH)
  128. ORP = float(tank_ORP.ORP)
  129. DO = float(tank_DO.DO)
  130. EC = float(tank_EC.EC)
  131. # 感測器_發酵桶_氣壓
  132. tank_PA = ferment_tank_PA.query.filter_by(tank_num='F' + '1').order_by(text('datetime desc')).first()
  133. db.session.close()
  134. PA = float(tank_PA.PA)
  135. # 感測器_發酵桶_保溫夾層水位計
  136. tank_WaterLevel = ferment_tank_WaterLevel.query.filter_by(tank_num='F' + '1').order_by(
  137. text('datetime desc')).first()
  138. db.session.close()
  139. WaterLevel = tank_WaterLevel.WaterLevel
  140. # 發酵桶_出料高度
  141. output_UltraSonic = ferment_output_UltraSonic.query.filter_by(tank_num='F' + '1').order_by(
  142. text('datetime desc')).first()
  143. db.session.close()
  144. UltraSonic_output = float(output_UltraSonic.UltraSonic)
  145. return render_template('ferment_auto.html', title="[測試] 發酵自動化", **locals())
  146. # else:
  147. # return render_template('sign_in.html')
  148. # 乾燥槽自動化測試頁
  149. @main.route('/dry_auto', methods=['GET', 'POST'])
  150. def dry_auto():
  151. dry_status = dry_container_status.query.order_by(text('datetime desc')).first()
  152. db.session.close()
  153. DI1 = dry_status.Dry_Input_1
  154. DI2 = dry_status.Dry_Input_2
  155. D1 = dry_status.Dry_Tank_1
  156. D2 = dry_status.Dry_Tank_2
  157. D3 = dry_status.Dry_Tank_3
  158. D4 = dry_status.Dry_Tank_4
  159. D5 = dry_status.Dry_Tank_5
  160. D6 = dry_status.Dry_Tank_6
  161. D7 = dry_status.Dry_Tank_7
  162. D8 = dry_status.Dry_Tank_8
  163. D9 = dry_status.Dry_Tank_9
  164. D10 = dry_status.Dry_Tank_10
  165. D11 = dry_status.Dry_Tank_11
  166. D12 = dry_status.Dry_Tank_12
  167. DO1 = dry_status.Dry_Output_1
  168. DO2 = dry_status.Dry_Output_2
  169. #print(DI1,DI2,D1,D2,D3,D4,D5,D6,D7,D8,D9,D10,D11,D12,DO1,DO2)
  170. input_seneor = dry_input_sensor.query.filter_by(tank_num='D' + '1').order_by(
  171. text('datetime desc')).first()
  172. db.session.close()
  173. UltraSonic_input = float(input_seneor.UltraSonic)
  174. # tank_SHT11 = dry_tank_SHT11.query.filter_by(tank_num='D' + '1').order_by(
  175. # text('datetime desc')).first()
  176. # SHT11_Temp = float(tank_SHT11.SHT11_Temp)
  177. # SHT11_Humidity = float(tank_SHT11.SHT11_Humidity)
  178. tank_sht11 = dry_tank_sht11.query.filter_by(tank_num='D' + '1').order_by(
  179. text('datetime desc')).first()
  180. db.session.close()
  181. sht11_Temp = float(tank_sht11.SHT11_Temp)
  182. sht11_Humidity = float(tank_sht11.SHT11_Humidity)
  183. tank_Soil = dry_tank_Soil.query.filter_by(tank_num='D' + '1').order_by(
  184. text('datetime desc')).first()
  185. db.session.close()
  186. Soil_Temp = float(tank_Soil.soil_Temp)
  187. Soil_Humidity = float(tank_Soil.soil_Humidity)
  188. Soil_EC = float(tank_Soil.soil_EC)
  189. # tank_Soil = dry_tank_Soil.query.filter_by(tank_num='D' + '1').order_by(
  190. # text('datetime desc')).first()
  191. # Soil_Temp = float(tank_Soil.Soil_Temp)
  192. # Soil_Humidity = float(tank_Soil.Soil_Humidity)
  193. # Soil_EC = float(tank_Soil.Soil_EC)
  194. tank_PA = dry_tank_PA.query.filter_by(tank_num='D' + '1').order_by(
  195. text('datetime desc')).first()
  196. db.session.close()
  197. PA = float(tank_PA.PA)
  198. tank_UltraSonic = dry_tank_UltraSonic.query.filter_by(tank_num='D' + '1').order_by(
  199. text('datetime desc')).first()
  200. db.session.close()
  201. UltraSonic_tank = float(tank_UltraSonic.UltraSonic)
  202. output_sensor = dry_output_sensor.query.filter_by(tank_num='D' + '1').order_by(
  203. text('datetime desc')).first()
  204. db.session.close()
  205. UltraSonic_output = float(output_sensor.UltraSonic)
  206. return render_template('dry_auto.html', title="[測試] 乾燥自動化", **locals())