views.py 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  1. #主業務邏輯中的視圖和路由的定義
  2. import os
  3. import datetime
  4. from flask import render_template, request, session
  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
  15. from sqlalchemy import text
  16. #主頁的訪問路徑
  17. @main.route('/')
  18. def main_index():
  19. #獲取登入信息
  20. if 'id' in session and 'uname' in session:
  21. username = session['uname']
  22. try:
  23. liquid = LiquidTank.query.order_by(text('datetime desc')).first()
  24. except Exception:
  25. pass
  26. try:
  27. tank1_duration1 = liquid.tk1_duration1
  28. except Exception:
  29. pass
  30. try:
  31. tank1_duration2 = liquid.tk1_duration2
  32. except Exception:
  33. pass
  34. try:
  35. tank1_duration3 = liquid.tk1_duration3
  36. except Exception:
  37. pass
  38. try:
  39. tank2_duration1 = liquid.tk2_duration1
  40. except Exception:
  41. pass
  42. try:
  43. tank2_duration2 = liquid.tk2_duration2
  44. except Exception:
  45. pass
  46. try:
  47. tank2_duration3 = liquid.tk2_duration3
  48. except Exception:
  49. pass
  50. try:
  51. tank1_from_hr1, tank1_from_min1 = liquid.tk1_start1.split(':')[0], liquid.tk1_start1.split(':')[1]
  52. tank1_to_hr1, tank1_to_min1 = liquid.tk1_end1.split(':')[0], liquid.tk1_end1.split(':')[1]
  53. except Exception:
  54. pass
  55. try:
  56. tank1_from_hr2, tank1_from_min2 = liquid.tk1_start2.split(':')[0], liquid.tk1_start2.split(':')[1]
  57. tank1_to_hr2, tank1_to_min2 = liquid.tk1_end2.split(':')[0], liquid.tk1_end2.split(':')[1]
  58. except Exception:
  59. pass
  60. try:
  61. tank1_from_hr3, tank1_from_min3 = liquid.tk1_start3.split(':')[0], liquid.tk1_start3.split(':')[1]
  62. tank1_to_hr3, tank1_to_min3 = liquid.tk1_end3.split(':')[0], liquid.tk1_end3.split(':')[1]
  63. except Exception:
  64. pass
  65. try:
  66. tank2_from_hr1, tank2_from_min1 = liquid.tk2_start1.split(':')[0], liquid.tk2_start1.split(':')[1]
  67. tank2_to_hr1, tank2_to_min1 = liquid.tk2_end1.split(':')[0], liquid.tk2_end1.split(':')[1]
  68. except Exception:
  69. pass
  70. try:
  71. tank2_from_hr2, tank2_from_min2 = liquid.tk2_start2.split(':')[0], liquid.tk2_start2.split(':')[1]
  72. tank2_to_hr2, tank2_to_min2 = liquid.tk2_end2.split(':')[0], liquid.tk2_end2.split(':')[1]
  73. except Exception:
  74. pass
  75. try:
  76. tank2_from_hr3, tank2_from_min3 = liquid.tk2_start3.split(':')[0], liquid.tk2_start3.split(':')[1]
  77. tank2_to_hr3, tank2_to_min3 = liquid.tk2_end3.split(':')[0], liquid.tk2_end3.split(':')[1]
  78. except Exception:
  79. pass
  80. return render_template('set_liquid.html',params=locals())
  81. else:
  82. return render_template('sign_in.html')
  83. #登入頁面的訪問路徑
  84. @main.route('/login', methods=['GET','POST'])
  85. def login_views():
  86. if request.method == 'GET':
  87. if 'id' in session and 'uname' in session:
  88. return redirect('/')
  89. else:
  90. return render_template('sign_in.html')
  91. else:
  92. #接收前端傳過來的資料
  93. username = request.form['username']
  94. password = request.form['password']
  95. #使用接收的用戶和密碼到資料庫中查詢
  96. user = User.query.filter_by(username=username, password=password).first()
  97. #如果用戶存在,將信息保存置session並重定向回首頁,否則重定向回登入頁
  98. if user:
  99. resp = redirect('/')
  100. #判斷是否有記住密碼
  101. if 'rem' in request.form:
  102. userID = str(user.userID)
  103. max_age = 60*60*24*365
  104. resp.set_cookie("username", username, max_age=max_age)
  105. resp.set_cookie("userID", userID, max_age=max_age)
  106. session['uname'] = user.username
  107. session['id'] = user.userID
  108. return resp
  109. else:
  110. errMsg = "Wrong login or password"
  111. return render_template('sign_in.html',errMsg=errMsg)
  112. #登入重設密碼的頁面
  113. @main.route('/reset_password', methods=['POST', 'GET'])
  114. def reset_password_views():
  115. if request.method == 'GET':
  116. if 'mail' in session:
  117. del session['mail']
  118. return render_template('reset_pwd1.html')
  119. else:
  120. #如果有id在session裡,代表從reset_pwd2過來的
  121. if "mail" in session:
  122. new_pwd = request.form['new_pwd']
  123. confirm_pwd = request.form['confirm_pwd']
  124. #判斷密碼是否一致
  125. if new_pwd == confirm_pwd:
  126. mail = session['mail']
  127. user = User.query.filter_by(mail=mail).first()
  128. user.password = new_pwd
  129. db.session.add(user)
  130. del session['mail']
  131. #修改完後回登入頁
  132. return redirect('/login')
  133. else:
  134. errMsg = "Passwords does not match"
  135. return render_template('reset_pwd2.html', errMsg=errMsg)
  136. email = request.form['email']
  137. user = User.query.filter_by(mail=email).first()
  138. if user:
  139. session['mail'] = user.mail
  140. return render_template('reset_pwd2.html')
  141. else:
  142. errMsg = "Wrong email.Please try again"
  143. return render_template('reset_pwd1.html', errMsg=errMsg)
  144. #註冊頁面的訪問路徑
  145. @main.route('/register',methods=['POST',"GET"])
  146. def register_views():
  147. if request.method == 'GET':
  148. return render_template('registration.html')
  149. else:
  150. #獲取文本框的值並賦值給user實體對象
  151. user = User()
  152. user.firstname = request.form['firstname']
  153. user.lastname = request.form['lastname']
  154. user.mail = request.form['email']
  155. user.phone = request.form['phone']
  156. user.username = request.form['username']
  157. user.password = request.form['password']
  158. user.status = 1
  159. #將數據保存進資料庫 - 註冊
  160. db.session.add(user)
  161. #手動提交,目的是為了獲取提交後的user的id
  162. db.session.commit()
  163. #當user成功插入進資料庫之後,程序會自動將所有信息取出來在賦值給user
  164. #完成登入的操作
  165. user = User.query.filter_by(username=user.username).first()
  166. session['id'] = user.userID
  167. session['uname'] = user.username
  168. return redirect('/')
  169. #驗證email訪問路徑
  170. @main.route('/check_email')
  171. def check_email_views():
  172. email = request.args['email']
  173. user = User.query.filter_by(mail=email).first()
  174. if user:
  175. result = {"errMsg":" "}
  176. else:
  177. result = {"pass":" "}
  178. return json.dumps(result)
  179. #驗證username訪問路徑
  180. @main.route('/check_username')
  181. def check_username_views():
  182. username = request.args['username']
  183. user = User.query.filter_by(username=username).first()
  184. if user:
  185. result = {"errMsg":" "}
  186. else:
  187. result = {"pass":" "}
  188. return json.dumps(result)
  189. #liquid的訪問路徑
  190. @main.route('/setting_liquid', methods=['POST','GET'])
  191. def setting_liquid_views():
  192. username = session['uname']
  193. if request.method == 'GET':
  194. liquid = LiquidTank.query.order_by('datetime desc').limit(1)
  195. return render_template('set_liquid.html', params=locals())
  196. else:
  197. liquid = LiquidTank()
  198. liquid.datetime = dt.now()
  199. tank1_num = 1
  200. tank1_from_num = 1
  201. tank1_to_num = 1
  202. tank2_num = 1
  203. tank2_from_num = 1
  204. tank2_to_num = 1
  205. for i in request.form:
  206. if i[:14] == 'tank1_duration':
  207. if tank1_num == 1:
  208. liquid.tk1_duration1 = request.form[i]
  209. tank1_num += 1
  210. elif tank1_num == 2:
  211. liquid.tk1_duration2 = request.form[i]
  212. tank1_num += 1
  213. elif tank1_num == 3:
  214. liquid.tk1_duration3 = request.form[i]
  215. else:
  216. if i[:13] == 'tank1_from_hr':
  217. tk1_from_hr = request.form[i]
  218. elif i[:14] == 'tank1_from_min':
  219. tk1_from_min = request.form[i]
  220. if tank1_from_num == 1:
  221. liquid.tk1_start1 = tk1_from_hr + ":" + tk1_from_min
  222. tank1_from_num += 1
  223. elif tank1_from_num == 2:
  224. liquid.tk1_start2 = tk1_from_hr + ":" + tk1_from_min
  225. tank1_from_num += 1
  226. elif tank1_from_num == 3:
  227. liquid.tk1_start3 = tk1_from_hr + ":" + tk1_from_min
  228. elif i[:11] == 'tank1_to_hr':
  229. tk1_to_hr = request.form[i]
  230. elif i[:12] == 'tank1_to_min':
  231. tk1_to_min = request.form[i]
  232. if tank1_to_num ==1 :
  233. liquid.tk1_end1 = tk1_to_hr + ":" + tk1_to_min
  234. tank1_to_num += 1
  235. elif tank1_to_num == 2:
  236. liquid.tk1_end2 = tk1_to_hr + ":" + tk1_to_min
  237. tank1_to_num += 1
  238. elif tank1_to_num == 3:
  239. liquid.tk1_end3 = tk1_to_hr + ":" + tk1_to_min
  240. if i[:14] == 'tank2_duration':
  241. if tank2_num == 1:
  242. liquid.tk2_duration1 = request.form[i]
  243. tank2_num += 1
  244. elif tank2_num == 2:
  245. liquid.tk2_duration2 = request.form[i]
  246. tank2_num += 1
  247. elif tank2_num == 3:
  248. liquid.tk2_duration3 = request.form[i]
  249. else:
  250. if i[:13] == 'tank2_from_hr':
  251. tk2_from_hr = request.form[i]
  252. elif i[:14] == 'tank2_from_min':
  253. tk2_from_min = request.form[i]
  254. if tank2_from_num == 1:
  255. liquid.tk2_start1 = tk2_from_hr + ":" + tk2_from_min
  256. tank2_from_num += 1
  257. elif tank2_from_num == 2:
  258. liquid.tk2_start2 = tk2_from_hr + ":" + tk2_from_min
  259. tank2_from_num += 1
  260. elif tank2_from_num == 3:
  261. liquid.tk2_start3 = tk2_from_hr + ":" + tk2_from_min
  262. elif i[:11] == 'tank2_to_hr':
  263. tk2_to_hr = request.form[i]
  264. elif i[:12] == 'tank2_to_min':
  265. tk2_to_min = request.form[i]
  266. if tank2_to_num == 1:
  267. liquid.tk2_end1 = tk2_to_hr + ":" + tk2_to_min
  268. tank2_to_num += 1
  269. elif tank2_to_num == 2:
  270. liquid.tk2_end2 = tk2_to_hr + ":" + tk2_to_min
  271. tank2_to_num += 1
  272. elif tank2_to_num == 3:
  273. liquid.tk2_end3 = tk2_to_hr + ":" + tk2_to_min
  274. db.session.add(liquid)
  275. db.session.commit()
  276. # return render_template('set_root.html', params=locals())
  277. return redirect('/setting_root')
  278. #root systems的訪問路徑
  279. @main.route('/setting_root', methods=['POST','GET'])
  280. def setting_root_views():
  281. username = session['uname']
  282. if request.method == 'GET':
  283. root = RootSystem.query.order_by(text('datetime desc')).first()
  284. try:
  285. tem1 = root.tem_tem1
  286. except Exception:
  287. pass
  288. try:
  289. tem2 = root.tem_tem2
  290. except Exception:
  291. pass
  292. try:
  293. tem3 = root.tem_tem3
  294. except Exception:
  295. pass
  296. try:
  297. tem_from_hr1, tem_from_min1 = root.tem_start1.split(':')[0], root.tem_start1.split(':')[1]
  298. except Exception:
  299. pass
  300. try:
  301. tem_to_hr1, tem_to_min1 = root.tem_end1.split(':')[0], root.tem_end1.split(':')[1]
  302. except Exception:
  303. pass
  304. try:
  305. tem_from_hr2, tem_from_min2 = root.tem_start2.split(':')[0], root.tem_start2.split(':')[1]
  306. except Exception:
  307. pass
  308. try:
  309. tem_to_hr2, tem_to_min2 = root.tem_end2.split(':')[0], root.tem_end2.split(':')[1]
  310. except Exception:
  311. pass
  312. try:
  313. tem_from_hr3, tem_from_min3 = root.tem_start3.split(':')[0], root.tem_start3.split(':')[1]
  314. except Exception:
  315. pass
  316. try:
  317. tem_to_hr3, tem_to_min3 = root.tem_end3.split(':')[0], root.tem_end3.split(':')[1]
  318. except Exception:
  319. pass
  320. try:
  321. lamp_from_hr1, lamp_from_min1 = root.l_start1.split(':')[0], root.l_start1.split(':')[1]
  322. except Exception:
  323. pass
  324. try:
  325. lamp_to_hr1, lamp_to_min1 = root.l_end1.split(':')[0], root.l_end1.split(':')[1]
  326. except Exception:
  327. pass
  328. try:
  329. lamp_from_hr2, lamp_from_min2 = root.l_start2.split(':')[0], root.l_start2.split(':')[1]
  330. except Exception:
  331. pass
  332. try:
  333. lamp_to_hr2, lamp_to_min2 = root.l_end2.split(':')[0], root.l_end2.split(':')[1]
  334. except Exception:
  335. pass
  336. try:
  337. lamp_from_hr3, lamp_from_min3 = root.l_start3.split(':')[0], root.l_start3.split(':')[1]
  338. except Exception:
  339. pass
  340. try:
  341. lamp_to_hr3, lamp_to_min3 = root.l_end3.split(':')[0], root.l_end3.split(':')[1]
  342. except Exception:
  343. pass
  344. try:
  345. r = str(root.l_r)
  346. g = str(root.l_g)
  347. b = str(root.l_b)
  348. color = '(' + r + ',' + g + ',' + b + ')'
  349. except Exception:
  350. pass
  351. try:
  352. if root.l_bright_auto == 0:
  353. #+1是為了讓前端不要判定為假
  354. bright_on = root.l_bright_auto + 1
  355. bright_vol = root.l_bright_vol
  356. if root.fan_status == 0:
  357. fan_on = root.fan_status + 1
  358. periodicity = root.fan_periodicity
  359. duration = root.fan_duration
  360. speed = root.fan_speed
  361. humidity = root.humidity
  362. except Exception:
  363. pass
  364. return render_template('set_root.html', params=locals())
  365. else:
  366. root = RootSystem()
  367. root.datetime = dt.now()
  368. tem_num = 1
  369. tem_from_num = 1
  370. tem_to_num = 1
  371. lamp_from_num = 1
  372. lamp_to_num = 1
  373. for i in request.form:
  374. if i[:3] == 'tem' and len(i) <= 7:
  375. if tem_num == 1:
  376. root.tem_tem1 = request.form[i]
  377. tem_num += 1
  378. elif tem_num == 2:
  379. root.tem_tem2 = request.form[i]
  380. tem_num += 1
  381. elif tem_num == 3:
  382. root.tem_tem3 = request.form[i]
  383. else:
  384. if i[:11] == 'tem_from_hr':
  385. tem_from_hr = request.form[i]
  386. elif i[:12] == 'tem_from_min':
  387. tem_from_min = request.form[i]
  388. if tem_from_num == 1:
  389. root.tem_start1 = tem_from_hr + ":" + tem_from_min
  390. tem_from_num += 1
  391. elif tem_from_num == 2:
  392. root.tem_start2 = tem_from_hr + ":" + tem_from_min
  393. tem_from_num += 1
  394. elif tem_from_num == 3:
  395. root.tem_start3 = tem_from_hr + ":" + tem_from_min
  396. elif i[:9] == 'tem_to_hr':
  397. tem_to_hr = request.form[i]
  398. elif i[:10] == 'tem_to_min':
  399. tem_to_min = request.form[i]
  400. if tem_to_num ==1 :
  401. root.tem_end1 = tem_to_hr + ":" + tem_to_min
  402. tem_to_num += 1
  403. elif tem_to_num == 2:
  404. root.tem_end2 = tem_to_hr + ":" + tem_to_min
  405. tem_to_num += 1
  406. elif tem_to_num == 3:
  407. root.tem_end3 = tem_to_hr + ":" + tem_to_min
  408. if i[:12] == 'lamp_from_hr':
  409. lamp_from_hr = request.form[i]
  410. elif i[:13] == 'lamp_from_min':
  411. lamp_from_min = request.form[i]
  412. if lamp_from_num == 1:
  413. root.l_start1 = lamp_from_hr + ":" + lamp_from_min
  414. lamp_from_num += 1
  415. elif lamp_from_num == 2:
  416. root.l_start2 = lamp_from_hr + ":" + lamp_from_min
  417. lamp_from_num += 1
  418. elif lamp_from_num == 3:
  419. root.l_start3 = lamp_from_hr + ":" + lamp_from_min
  420. if i[:10] == 'lamp_to_hr':
  421. lamp_to_hr = request.form[i]
  422. elif i[:11] == 'lamp_to_min':
  423. lamp_to_min = request.form[i]
  424. if lamp_to_num ==1 :
  425. root.l_end1 = lamp_to_hr + ":" + lamp_to_min
  426. lamp_to_num += 1
  427. elif lamp_to_num == 2:
  428. root.l_end2 = lamp_to_hr + ":" + lamp_to_min
  429. lamp_to_num += 1
  430. elif lamp_to_num == 3:
  431. root.l_end3 = lamp_to_hr + ":" + lamp_to_min
  432. if i == 'color':
  433. try:
  434. color = request.form['color']
  435. root.l_r, root.l_g, root.l_b = color.split(',')[0], color.split(',')[1][1:], color.split(',')[2][1:]
  436. except Exception:
  437. pass
  438. if 'bright-on' in request.form:
  439. root.l_bright_auto = 1
  440. else:
  441. root.l_bright_auto = 0
  442. root.l_bright_vol = request.form['bright-vol']
  443. if 'fan-on' in request.form:
  444. root.fan_status = 1
  445. else:
  446. root.fan_status = 0
  447. root.fan_periodicity = request.form['periodicity']
  448. root.fan_duration = request.form['duration']
  449. root.fan_speed = request.form['speed']
  450. root.humidity = request.form['humidity']
  451. db.session.add(root)
  452. db.session.commit()
  453. # return render_template('set_stem.html', params=locals())
  454. return redirect('/setting_stem')
  455. #stem systems的訪問路徑
  456. @main.route('/setting_stem', methods=['POST','GET'])
  457. def setting_stem_views():
  458. username = session['uname']
  459. if request.method == 'GET':
  460. stem = StemSystem.query.order_by(text('datetime desc')).first()
  461. try:
  462. tem1 = stem.tem_tem1
  463. except Exception:
  464. pass
  465. try:
  466. tem2 = stem.tem_tem2
  467. except Exception:
  468. pass
  469. try:
  470. tem3 = stem.tem_tem3
  471. except Exception:
  472. pass
  473. try:
  474. tem_from_hr1, tem_from_min1 = stem.tem_start1.split(':')[0], stem.tem_start1.split(':')[1]
  475. except Exception:
  476. pass
  477. try:
  478. tem_to_hr1, tem_to_min1 = stem.tem_end1.split(':')[0], stem.tem_end1.split(':')[1]
  479. except Exception:
  480. pass
  481. try:
  482. tem_from_hr2, tem_from_min2 = stem.tem_start2.split(':')[0], stem.tem_start2.split(':')[1]
  483. except Exception:
  484. pass
  485. try:
  486. tem_to_hr2, tem_to_min2 = stem.tem_end2.split(':')[0], stem.tem_end2.split(':')[1]
  487. except Exception:
  488. pass
  489. try:
  490. tem_from_hr3, tem_from_min3 = stem.tem_start3.split(':')[0], stem.tem_start3.split(':')[1]
  491. except Exception:
  492. pass
  493. try:
  494. tem_to_hr3, tem_to_min3 = stem.tem_end3.split(':')[0], stem.tem_end3.split(':')[1]
  495. except Exception:
  496. pass
  497. try:
  498. led_from_hr1, led_from_min1 = stem.l_start1.split(':')[0], stem.l_start1.split(':')[1]
  499. except Exception:
  500. pass
  501. try:
  502. led_to_hr1, led_to_min1 = stem.l_end1.split(':')[0], stem.l_end1.split(':')[1]
  503. except Exception:
  504. pass
  505. try:
  506. led_from_hr2, led_from_min2 = stem.l_start2.split(':')[0], stem.l_start2.split(':')[1]
  507. except Exception:
  508. pass
  509. try:
  510. led_to_hr2, led_to_min2 = stem.l_end2.split(':')[0], stem.l_end2.split(':')[1]
  511. except Exception:
  512. pass
  513. try:
  514. led_from_hr3, led_from_min3 = stem.l_start3.split(':')[0], stem.l_start3.split(':')[1]
  515. except Exception:
  516. pass
  517. try:
  518. led_to_hr3, led_to_min3 = stem.l_end3.split(':')[0], stem.l_end3.split(':')[1]
  519. except Exception:
  520. pass
  521. try:
  522. r = str(stem.l_r)
  523. g = str(stem.l_g)
  524. b = str(stem.l_b)
  525. color = '(' + r + ',' + g + ',' + b + ')'
  526. except Exception:
  527. pass
  528. try:
  529. if stem.l_bright_auto == 0:
  530. #+1是為了讓前端不要判定為假
  531. bright_on = stem.l_bright_auto + 1
  532. bright_vol = stem.l_bright_vol
  533. except Exception:
  534. pass
  535. try:
  536. if stem.c_status == 0:
  537. # +1是為了讓前端不要判定為假
  538. camera_on = stem.c_status + 1
  539. except Exception:
  540. pass
  541. try:
  542. duration = stem.c_duration
  543. except Exception:
  544. pass
  545. try:
  546. tem_near_stem1 = stem.t_n_s_tem1
  547. except Exception:
  548. pass
  549. try:
  550. tem_near_stem2 = stem.t_n_s_tem2
  551. except Exception:
  552. pass
  553. try:
  554. tem_near_stem3 = stem.t_n_s_tem3
  555. except Exception:
  556. pass
  557. try:
  558. t_n_s_from_hr1, t_n_s_from_min1 = stem.t_n_s_start1.split(':')[0], stem.t_n_s_start1.split(':')[1]
  559. except Exception:
  560. pass
  561. try:
  562. t_n_s_to_hr1, t_n_s_to_min1 = stem.t_n_s_end1.split(':')[0], stem.t_n_s_end1.split(':')[1]
  563. except Exception:
  564. pass
  565. try:
  566. t_n_s_from_hr2, t_n_s_from_min2 = stem.t_n_s_start2.split(':')[0], stem.t_n_s_start2.split(':')[1]
  567. except Exception:
  568. pass
  569. try:
  570. t_n_s_to_hr2, t_n_s_to_min2 = stem.t_n_s_end2.split(':')[0], stem.t_n_s_end2.split(':')[1]
  571. except Exception:
  572. pass
  573. try:
  574. t_n_s_from_hr3, t_n_s_from_min3 = stem.t_n_s_start3.split(':')[0], stem.t_n_s_start3.split(':')[1]
  575. except Exception:
  576. pass
  577. try:
  578. t_n_s_to_hr3, t_n_s_to_min3 = stem.t_n_s_end3.split(':')[0], stem.t_n_s_end3.split(':')[1]
  579. except Exception:
  580. pass
  581. try:
  582. music1 = stem.m_playlist1
  583. except Exception:
  584. pass
  585. try:
  586. music2 = stem.m_playlist2
  587. except Exception:
  588. pass
  589. try:
  590. music3 = stem.m_playlist3
  591. except Exception:
  592. pass
  593. try:
  594. music_from_hr1, music_from_min1 = stem.m_start1.split(':')[0], stem.m_start1.split(':')[1]
  595. except Exception:
  596. pass
  597. try:
  598. music_to_hr1, music_to_min1 = stem.m_end1.split(':')[0], stem.m_end1.split(':')[1]
  599. except Exception:
  600. pass
  601. try:
  602. music_from_hr2, music_from_min2 = stem.m_start2.split(':')[0], stem.m_start2.split(':')[1]
  603. except Exception:
  604. pass
  605. try:
  606. music_to_hr2, music_to_min2 = stem.m_end2.split(':')[0], stem.m_end2.split(':')[1]
  607. except Exception:
  608. pass
  609. try:
  610. music_from_hr3, music_from_min3 = stem.m_start3.split(':')[0], stem.m_start3.split(':')[1]
  611. except Exception:
  612. pass
  613. try:
  614. music_to_hr3, music_to_min3 = stem.m_end3.split(':')[0], stem.m_end3.split(':')[1]
  615. except Exception:
  616. pass
  617. try:
  618. u_s_from_hr1, u_s_from_min1 = stem.u_s_start1.split(':')[0], stem.u_s_start1.split(':')[1]
  619. except Exception:
  620. pass
  621. try:
  622. u_s_to_hr1, u_s_to_min1 = stem.u_s_end1.split(':')[0], stem.u_s_end1.split(':')[1]
  623. except Exception:
  624. pass
  625. try:
  626. u_s_from_hr2, u_s_from_min2 = stem.u_s_start2.split(':')[0], stem.u_s_start2.split(':')[1]
  627. except Exception:
  628. pass
  629. try:
  630. u_s_to_hr2, u_s_to_min2 = stem.u_s_end2.split(':')[0], stem.u_s_end2.split(':')[1]
  631. except Exception:
  632. pass
  633. try:
  634. u_s_from_hr3, u_s_from_min3 = stem.u_s_start3.split(':')[0], stem.u_s_start3.split(':')[1]
  635. except Exception:
  636. pass
  637. try:
  638. u_s_to_hr3, u_s_to_min3 = stem.u_s_end3.split(':')[0], stem.u_s_end3.split(':')[1]
  639. except Exception:
  640. pass
  641. try:
  642. if stem.u_s_status == 0:
  643. # +1是為了讓前端不要判定為假
  644. ultra_sonic_on = stem.u_s_status + 1
  645. except Exception:
  646. pass
  647. try:
  648. volume = stem.u_s_vol
  649. except Exception:
  650. pass
  651. try:
  652. vibration1 = stem.v_vol1
  653. except Exception:
  654. pass
  655. try:
  656. vibration2 = stem.v_vol2
  657. except Exception:
  658. pass
  659. try:
  660. vibration3 = stem.v_vol3
  661. except Exception:
  662. pass
  663. try:
  664. v_from_hr1, v_from_min1 = stem.v_start1.split(':')[0], stem.v_start1.split(':')[1]
  665. except Exception:
  666. pass
  667. try:
  668. v_to_hr1, v_to_min1 = stem.v_end1.split(':')[0], stem.v_end1.split(':')[1]
  669. except Exception:
  670. pass
  671. try:
  672. v_from_hr2, v_from_min2 = stem.v_start2.split(':')[0], stem.v_start2.split(':')[1]
  673. except Exception:
  674. pass
  675. try:
  676. v_to_hr2, v_to_min2 = stem.v_end2.split(':')[0], stem.v_end2.split(':')[1]
  677. except Exception:
  678. pass
  679. try:
  680. v_from_hr3, v_from_min3 = stem.v_start3.split(':')[0], stem.v_start3.split(':')[1]
  681. except Exception:
  682. pass
  683. try:
  684. v_to_hr3, v_to_min3 = stem.v_end3.split(':')[0], stem.v_end3.split(':')[1]
  685. except Exception:
  686. pass
  687. try:
  688. wind_flow1 = stem.w_vol1
  689. except Exception:
  690. pass
  691. try:
  692. wind_flow2 = stem.w_vol2
  693. except Exception:
  694. pass
  695. try:
  696. wind_flow3 = stem.w_vol3
  697. except Exception:
  698. pass
  699. try:
  700. w_from_hr1, w_from_min1 = stem.w_start1.split(':')[0], stem.w_start1.split(':')[1]
  701. except Exception:
  702. pass
  703. try:
  704. w_to_hr1, w_to_min1 = stem.w_end1.split(':')[0], stem.w_end1.split(':')[1]
  705. except Exception:
  706. pass
  707. try:
  708. w_from_hr2, w_from_min2 = stem.w_start2.split(':')[0], stem.w_start2.split(':')[1]
  709. except Exception:
  710. pass
  711. try:
  712. w_to_hr2, w_to_min2 = stem.w_end2.split(':')[0], stem.w_end2.split(':')[1]
  713. except Exception:
  714. pass
  715. try:
  716. w_from_hr3, w_from_min3 = stem.w_start3.split(':')[0], stem.w_start3.split(':')[1]
  717. except Exception:
  718. pass
  719. try:
  720. w_to_hr3, w_to_min3 = stem.w_end3.split(':')[0], stem.w_end3.split(':')[1]
  721. except Exception:
  722. pass
  723. try:
  724. c_duration = stem.c_duration
  725. except Exception:
  726. pass
  727. try:
  728. rh = stem.rh
  729. except Exception:
  730. pass
  731. return render_template('set_stem.html', params=locals())
  732. else:
  733. stem = StemSystem()
  734. stem.datetime = dt.now()
  735. tem_num = 1
  736. tem_from_num = 1
  737. tem_to_num = 1
  738. led_from_num = 1
  739. led_to_num = 1
  740. t_n_s_num = 1
  741. t_n_s_from_num = 1
  742. t_n_s_to_num = 1
  743. music_num = 1
  744. music_from_num = 1
  745. music_to_num = 1
  746. u_s_from_num = 1
  747. u_s_to_num = 1
  748. v_num = 1
  749. v_from_num = 1
  750. v_to_num = 1
  751. w_num = 1
  752. w_from_num = 1
  753. w_to_num = 1
  754. for i in request.form:
  755. if i[:3] == 'tem' and len(i) <= 7:
  756. if tem_num == 1:
  757. stem.tem_tem1 = request.form[i]
  758. tem_num += 1
  759. elif tem_num == 2:
  760. stem.tem_tem2 = request.form[i]
  761. tem_num += 1
  762. elif tem_num == 3:
  763. stem.tem_tem3 = request.form[i]
  764. else:
  765. if i[:11] == 'tem_from_hr':
  766. tem_from_hr = request.form[i]
  767. elif i[:12] == 'tem_from_min':
  768. tem_from_min = request.form[i]
  769. if tem_from_num == 1:
  770. stem.tem_start1 = tem_from_hr + ":" + tem_from_min
  771. tem_from_num += 1
  772. elif tem_from_num == 2:
  773. stem.tem_start2 = tem_from_hr + ":" + tem_from_min
  774. tem_from_num += 1
  775. elif tem_from_num == 3:
  776. stem.tem_start3 = tem_from_hr + ":" + tem_from_min
  777. elif i[:9] == 'tem_to_hr':
  778. tem_to_hr = request.form[i]
  779. elif i[:10] == 'tem_to_min':
  780. tem_to_min = request.form[i]
  781. if tem_to_num ==1 :
  782. stem.tem_end1 = tem_to_hr + ":" + tem_to_min
  783. tem_to_num += 1
  784. elif tem_to_num == 2:
  785. stem.tem_end2 = tem_to_hr + ":" + tem_to_min
  786. tem_to_num += 1
  787. elif tem_to_num == 3:
  788. stem.tem_end3 = tem_to_hr + ":" + tem_to_min
  789. if i[:11] == 'led_from_hr':
  790. led_from_hr = request.form[i]
  791. elif i[:12] == 'led_from_min':
  792. led_from_min = request.form[i]
  793. if led_from_num == 1:
  794. stem.l_start1 = led_from_hr + ":" + led_from_min
  795. led_from_num += 1
  796. elif led_from_num == 2:
  797. stem.l_start2 = led_from_hr + ":" + led_from_min
  798. led_from_num += 1
  799. elif led_from_num == 3:
  800. stem.l_start3 = led_from_hr + ":" + led_from_min
  801. if i[:9] == 'led_to_hr':
  802. led_to_hr = request.form[i]
  803. elif i[:10] == 'led_to_min':
  804. led_to_min = request.form[i]
  805. if led_to_num ==1 :
  806. stem.l_end1 = led_to_hr + ":" + led_to_min
  807. led_to_num += 1
  808. elif led_to_num == 2:
  809. stem.l_end2 = led_to_hr + ":" + led_to_min
  810. led_to_num += 1
  811. elif led_to_num == 3:
  812. stem.l_end3 = led_to_hr + ":" + led_to_min
  813. if i == 'color':
  814. try:
  815. color = request.form['color']
  816. stem.l_r, stem.l_g, stem.l_b = color.split(',')[0], color.split(',')[1][1:], color.split(',')[2][1:]
  817. except Exception:
  818. pass
  819. if i[:13] == 'tem-near-stem':
  820. if t_n_s_num == 1:
  821. stem.t_n_s_tem1 = request.form[i]
  822. print(request.form[i])
  823. t_n_s_num += 1
  824. elif t_n_s_num == 2:
  825. stem.t_n_s_tem2 = request.form[i]
  826. t_n_s_num += 1
  827. elif t_n_s_num == 3:
  828. stem.t_n_s_tem3 = request.form[i]
  829. else:
  830. if i[:11] == 'tns_from_hr':
  831. t_n_s_from_hr = request.form[i]
  832. elif i[:12] == 'tns_from_min':
  833. t_n_s_from_min = request.form[i]
  834. if t_n_s_from_num == 1:
  835. stem.t_n_s_start1 = t_n_s_from_hr + ":" + t_n_s_from_min
  836. t_n_s_from_num += 1
  837. elif t_n_s_from_num == 2:
  838. stem.t_n_s_start2 = t_n_s_from_hr + ":" + t_n_s_from_min
  839. t_n_s_from_num += 1
  840. elif t_n_s_from_num == 3:
  841. stem.t_n_s_start3 = t_n_s_from_hr + ":" + t_n_s_from_min
  842. elif i[:9] == 'tns_to_hr':
  843. t_n_s_to_hr = request.form[i]
  844. elif i[:10] == 'tns_to_min':
  845. t_n_s_to_min = request.form[i]
  846. if t_n_s_to_num == 1:
  847. stem.t_n_s_end1 = t_n_s_to_hr + ":" + t_n_s_to_min
  848. t_n_s_to_num += 1
  849. elif t_n_s_to_num == 2:
  850. stem.t_n_s_end2 = t_n_s_to_hr + ":" + t_n_s_to_min
  851. t_n_s_to_num += 1
  852. elif t_n_s_to_num == 3:
  853. stem.t_n_s_end3 = t_n_s_to_hr + ":" + t_n_s_to_min
  854. if i[:5] == 'music' and len(i) <=11:
  855. if music_num == 1:
  856. stem.m_playlist1 = request.form[i]
  857. music_num += 1
  858. elif music_num == 2:
  859. stem.m_playlist2 = request.form[i]
  860. music_num += 1
  861. elif music_num == 3:
  862. stem.m_playlist3 = request.form[i]
  863. else:
  864. if i[:13] == 'music_from_hr':
  865. music_from_hr = request.form[i]
  866. elif i[:14] == 'music_from_min':
  867. music_from_min = request.form[i]
  868. if music_from_num == 1:
  869. stem.m_start1 = music_from_hr + ":" + music_from_min
  870. music_from_num += 1
  871. elif music_from_num == 2:
  872. stem.m_start2 = music_from_hr + ":" + music_from_min
  873. music_from_num += 1
  874. elif music_from_num == 3:
  875. stem.m_start3 = music_from_hr + ":" + music_from_min
  876. elif i[:11] == 'music_to_hr':
  877. music_to_hr = request.form[i]
  878. elif i[:12] == 'music_to_min':
  879. music_to_min = request.form[i]
  880. if music_to_num == 1:
  881. stem.m_end1 = music_to_hr + ":" + music_to_min
  882. music_to_num += 1
  883. elif music_to_num == 2:
  884. stem.m_end2 = music_to_hr + ":" + music_to_min
  885. music_to_num += 1
  886. elif music_to_num == 3:
  887. stem.m_end3 = music_to_hr + ":" + music_to_min
  888. if i[:9] == 'vibration':
  889. if v_num == 1:
  890. stem.v_vol1 = request.form[i]
  891. v_num += 1
  892. elif v_num == 2:
  893. stem.v_vol2 = request.form[i]
  894. v_num += 1
  895. elif v_num == 3:
  896. stem.v_vol3 = request.form[i]
  897. else:
  898. if i[:9] == 'v_from_hr':
  899. v_from_hr = request.form[i]
  900. elif i[:10] == 'v_from_min':
  901. v_from_min = request.form[i]
  902. if v_from_num == 1:
  903. stem.v_start1 = v_from_hr + ":" + v_from_min
  904. v_from_num += 1
  905. elif v_from_num == 2:
  906. stem.v_start2 = v_from_hr + ":" + v_from_min
  907. v_from_num += 1
  908. elif v_from_num == 3:
  909. stem.v_start3 = v_from_hr + ":" + v_from_min
  910. elif i[:7] == 'v_to_hr':
  911. v_to_hr = request.form[i]
  912. elif i[:8] == 'v_to_min':
  913. v_to_min = request.form[i]
  914. if v_to_num == 1:
  915. stem.v_end1 = v_to_hr + ":" + v_to_min
  916. v_to_num += 1
  917. elif v_to_num == 2:
  918. stem.v_end2 = v_to_hr + ":" + v_to_min
  919. v_to_num += 1
  920. elif v_to_num == 3:
  921. stem.v_end3 = v_to_hr + ":" + v_to_min
  922. if i[:9] == 'wind-flow':
  923. if w_num == 1:
  924. stem.w_vol1 = request.form[i]
  925. w_num += 1
  926. elif w_num == 2:
  927. stem.w_vol2 = request.form[i]
  928. w_num += 1
  929. elif w_num == 3:
  930. stem.w_vol3 = request.form[i]
  931. else:
  932. if i[:9] == 'w_from_hr':
  933. w_from_hr = request.form[i]
  934. elif i[:10] == 'w_from_min':
  935. w_from_min = request.form[i]
  936. if w_from_num == 1:
  937. stem.w_start1 = w_from_hr + ":" + w_from_min
  938. w_from_num += 1
  939. elif w_from_num == 2:
  940. stem.w_start2 = w_from_hr + ":" + w_from_min
  941. w_from_num += 1
  942. elif w_from_num == 3:
  943. stem.w_start3 = w_from_hr + ":" + w_from_min
  944. elif i[:7] == 'w_to_hr':
  945. w_to_hr = request.form[i]
  946. elif i[:8] == 'w_to_min':
  947. w_to_min = request.form[i]
  948. if w_to_num == 1:
  949. stem.w_end1 = w_to_hr + ":" + w_to_min
  950. w_to_num += 1
  951. elif w_to_num == 2:
  952. stem.w_end2 = w_to_hr + ":" + w_to_min
  953. w_to_num += 1
  954. elif w_to_num == 3:
  955. stem.w_end3 = w_to_hr + ":" + w_to_min
  956. if i[:10] == 'us_from_hr':
  957. u_s_from_hr = request.form[i]
  958. elif i[:11] == 'us_from_min':
  959. u_s_from_min = request.form[i]
  960. if u_s_from_num == 1:
  961. stem.u_s_start1 = u_s_from_hr + ":" + u_s_from_min
  962. u_s_from_num += 1
  963. elif u_s_from_num == 2:
  964. stem.u_s_start2 = u_s_from_hr + ":" + u_s_from_min
  965. u_s_from_num += 1
  966. elif u_s_from_num == 3:
  967. stem.u_s_start3 = u_s_from_hr + ":" + u_s_from_min
  968. elif i[:8] == 'us_to_hr':
  969. u_s_to_hr = request.form[i]
  970. elif i[:9] == 'us_to_min':
  971. u_s_to_min = request.form[i]
  972. if u_s_to_num == 1:
  973. stem.u_s_end1 = u_s_to_hr + ":" + u_s_to_min
  974. u_s_to_num += 1
  975. elif u_s_to_num == 2:
  976. stem.u_s_end2 = u_s_to_hr + ":" + u_s_to_min
  977. u_s_to_num += 1
  978. elif u_s_to_num == 3:
  979. stem.u_s_end3 = u_s_to_hr + ":" + u_s_to_min
  980. if 'ultra-sonic-on' in request.form:
  981. stem.u_s_status = 1
  982. else:
  983. stem.u_s_status = 0
  984. if 'volume' in request.form:
  985. stem.u_s_vol = request.form['volume']
  986. if 'bright-on' in request.form:
  987. stem.l_bright_auto = 1
  988. else:
  989. stem.l_bright_auto = 0
  990. stem.l_bright_vol = request.form['bright-vol']
  991. if 'camera-on' in request.form:
  992. stem.c_status = 1
  993. else:
  994. stem.c_status = 0
  995. stem.c_duration = request.form['duration']
  996. stem.rh = request.form['rh']
  997. db.session.add(stem)
  998. db.session.commit()
  999. print(request.form)
  1000. # return render_template('online_fitolab.html', params=locals())
  1001. return redirect('online_fitolab')
  1002. #online_fitolab的訪問路徑
  1003. @main.route('/online_fitolab', methods=['POST',"GET"])
  1004. def online_fitolib_views():
  1005. username = session['uname']
  1006. if request.method == 'GET':
  1007. stem = StemSystem.query.order_by(text('datetime desc')).first()
  1008. root = RootSystem.query.order_by(text('datetime desc')).first()
  1009. liquid = LiquidTank.query.order_by(text('datetime desc')).first()
  1010. fitolab1 = Fitolab1Sensor.query.order_by(text('datetime desc')).first()
  1011. try:
  1012. stem_r = str(stem.l_r)
  1013. stem_g = str(stem.l_g)
  1014. stem_b = str(stem.l_b)
  1015. stem_color = '(' + stem_r + ',' + stem_g + ',' + stem_b + ')'
  1016. except Exception:
  1017. pass
  1018. try:
  1019. if stem.l_bright_auto == 0:
  1020. #+1是為了讓前端不要判定為假
  1021. stem_bright_on = stem.l_bright_auto + 1
  1022. stem_bright_vol = stem.l_bright_vol
  1023. except Exception:
  1024. pass
  1025. try:
  1026. music1 = stem.m_playlist1
  1027. except Exception:
  1028. pass
  1029. try:
  1030. music2 = stem.m_playlist2
  1031. except Exception:
  1032. pass
  1033. try:
  1034. music3 = stem.m_playlist3
  1035. except Exception:
  1036. pass
  1037. try:
  1038. music_from_hr1, music_from_min1 = stem.m_start1.split(':')[0], stem.m_start1.split(':')[1]
  1039. except Exception:
  1040. pass
  1041. try:
  1042. music_to_hr1, music_to_min1 = stem.m_end1.split(':')[0], stem.m_end1.split(':')[1]
  1043. except Exception:
  1044. pass
  1045. try:
  1046. music_from_hr2, music_from_min2 = stem.m_start2.split(':')[0], stem.m_start2.split(':')[1]
  1047. except Exception:
  1048. pass
  1049. try:
  1050. music_to_hr2, music_to_min2 = stem.m_end2.split(':')[0], stem.m_end2.split(':')[1]
  1051. except Exception:
  1052. pass
  1053. try:
  1054. music_from_hr3, music_from_min3 = stem.m_start3.split(':')[0], stem.m_start3.split(':')[1]
  1055. except Exception:
  1056. pass
  1057. try:
  1058. music_to_hr3, music_to_min3 = stem.m_end3.split(':')[0], stem.m_end3.split(':')[1]
  1059. except Exception:
  1060. pass
  1061. try:
  1062. m_vol = stem.m_vol
  1063. except Exception:
  1064. pass
  1065. try:
  1066. root_r = str(root.l_r)
  1067. root_g = str(root.l_g)
  1068. root_b = str(root.l_b)
  1069. root_color = '(' + root_r + ',' + root_g + ',' + root_b + ')'
  1070. except Exception:
  1071. pass
  1072. try:
  1073. if root.l_bright_auto == 0:
  1074. #+1是為了讓前端不要判定為假
  1075. root_bright_on = root.l_bright_auto + 1
  1076. root_bright_vol = root.l_bright_vol
  1077. except Exception:
  1078. pass
  1079. try:
  1080. vibration1 = stem.v_vol1
  1081. except Exception:
  1082. pass
  1083. try:
  1084. vibration2 = stem.v_vol2
  1085. except Exception:
  1086. pass
  1087. try:
  1088. vibration3 = stem.v_vol3
  1089. except Exception:
  1090. pass
  1091. try:
  1092. v_from_hr1, v_from_min1 = stem.v_start1.split(':')[0], stem.v_start1.split(':')[1]
  1093. except Exception:
  1094. pass
  1095. try:
  1096. v_to_hr1, v_to_min1 = stem.v_end1.split(':')[0], stem.v_end1.split(':')[1]
  1097. except Exception:
  1098. pass
  1099. try:
  1100. v_from_hr2, v_from_min2 = stem.v_start2.split(':')[0], stem.v_start2.split(':')[1]
  1101. except Exception:
  1102. pass
  1103. try:
  1104. v_to_hr2, v_to_min2 = stem.v_end2.split(':')[0], stem.v_end2.split(':')[1]
  1105. except Exception:
  1106. pass
  1107. try:
  1108. v_from_hr3, v_from_min3 = stem.v_start3.split(':')[0], stem.v_start3.split(':')[1]
  1109. except Exception:
  1110. pass
  1111. try:
  1112. v_to_hr3, v_to_min3 = stem.v_end3.split(':')[0], stem.v_end3.split(':')[1]
  1113. except Exception:
  1114. pass
  1115. try:
  1116. v_pow = stem.v_power
  1117. except Exception:
  1118. pass
  1119. try:
  1120. tank1_duration1 = liquid.tk1_duration1
  1121. tank2_duration1 = liquid.tk2_duration1
  1122. tank1_from_hr1, tank1_from_min1 = liquid.tk1_start1.split(':')[0], liquid.tk1_start1.split(':')[1]
  1123. tank1_to_hr1, tank1_to_min1 = liquid.tk1_end1.split(':')[0], liquid.tk1_end1.split(':')[1]
  1124. except Exception:
  1125. pass
  1126. try:
  1127. tank1_from_hr2, tank1_from_min2 = liquid.tk1_start2.split(':')[0], liquid.tk1_start2.split(':')[1]
  1128. tank1_to_hr2, tank1_to_min2 = liquid.tk1_end2.split(':')[0], liquid.tk1_end2.split(':')[1]
  1129. except Exception:
  1130. pass
  1131. try:
  1132. tank1_from_hr3, tank1_from_min3 = liquid.tk1_start3.split(':')[0], liquid.tk1_start3.split(':')[1]
  1133. tank1_to_hr3, tank1_to_min3 = liquid.tk1_end3.split(':')[0], liquid.tk1_end3.split(':')[1]
  1134. except Exception:
  1135. pass
  1136. try:
  1137. tank2_from_hr1, tank2_from_min1 = liquid.tk2_start1.split(':')[0], liquid.tk2_start1.split(':')[1]
  1138. tank2_to_hr1, tank2_to_min1 = liquid.tk2_end1.split(':')[0], liquid.tk2_end1.split(':')[1]
  1139. except Exception:
  1140. pass
  1141. try:
  1142. tank2_from_hr2, tank2_from_min2 = liquid.tk2_start2.split(':')[0], liquid.tk2_start2.split(':')[1]
  1143. tank2_to_hr2, tank2_to_min2 = liquid.tk2_end2.split(':')[0], liquid.tk2_end2.split(':')[1]
  1144. except Exception:
  1145. pass
  1146. try:
  1147. tank2_from_hr3, tank2_from_min3 = liquid.tk2_start3.split(':')[0], liquid.tk2_start3.split(':')[1]
  1148. tank2_to_hr3, tank2_to_min3 = liquid.tk2_end3.split(':')[0], liquid.tk2_end3.split(':')[1]
  1149. except Exception:
  1150. pass
  1151. try:
  1152. if stem.c_status == 0:
  1153. # +1是為了讓前端不要判定為假
  1154. camera_on = stem.c_status + 1
  1155. except Exception:
  1156. pass
  1157. try:
  1158. c_duration = stem.c_duration
  1159. except Exception:
  1160. pass
  1161. try:
  1162. music1 = stem.m_playlist1
  1163. except Exception:
  1164. pass
  1165. try:
  1166. music2 = stem.m_playlist2
  1167. except Exception:
  1168. pass
  1169. try:
  1170. music3 = stem.m_playlist3
  1171. except Exception:
  1172. pass
  1173. try:
  1174. music_from_hr1, music_from_min1 = stem.m_start1.split(':')[0], stem.m_start1.split(':')[1]
  1175. except Exception:
  1176. pass
  1177. try:
  1178. music_to_hr1, music_to_min1 = stem.m_end1.split(':')[0], stem.m_end1.split(':')[1]
  1179. except Exception:
  1180. pass
  1181. try:
  1182. music_from_hr2, music_from_min2 = stem.m_start2.split(':')[0], stem.m_start2.split(':')[1]
  1183. except Exception:
  1184. pass
  1185. try:
  1186. music_to_hr2, music_to_min2 = stem.m_end2.split(':')[0], stem.m_end2.split(':')[1]
  1187. except Exception:
  1188. pass
  1189. try:
  1190. music_from_hr3, music_from_min3 = stem.m_start3.split(':')[0], stem.m_start3.split(':')[1]
  1191. except Exception:
  1192. pass
  1193. try:
  1194. music_to_hr3, music_to_min3 = stem.m_end3.split(':')[0], stem.m_end3.split(':')[1]
  1195. except Exception:
  1196. pass
  1197. try:
  1198. m_vol = stem.m_vol
  1199. except Exception:
  1200. pass
  1201. try:
  1202. if stem.c_status == 0:
  1203. #+1是為了讓前端不要判定為假
  1204. camara_on = stem.c_status + 1
  1205. except Exception:
  1206. pass
  1207. try:
  1208. vibration1 = stem.v_vol1
  1209. except Exception:
  1210. pass
  1211. try:
  1212. vibration2 = stem.v_vol2
  1213. except Exception:
  1214. pass
  1215. try:
  1216. vibration3 = stem.v_vol3
  1217. except Exception:
  1218. pass
  1219. try:
  1220. v_from_hr1, v_from_min1 = stem.v_start1.split(':')[0], stem.v_start1.split(':')[1]
  1221. except Exception:
  1222. pass
  1223. try:
  1224. v_to_hr1, v_to_min1 = stem.v_end1.split(':')[0], stem.v_end1.split(':')[1]
  1225. except Exception:
  1226. pass
  1227. try:
  1228. v_from_hr2, v_from_min2 = stem.v_start2.split(':')[0], stem.v_start2.split(':')[1]
  1229. except Exception:
  1230. pass
  1231. try:
  1232. v_to_hr2, v_to_min2 = stem.v_end2.split(':')[0], stem.v_end2.split(':')[1]
  1233. except Exception:
  1234. pass
  1235. try:
  1236. v_from_hr3, v_from_min3 = stem.v_start3.split(':')[0], stem.v_start3.split(':')[1]
  1237. except Exception:
  1238. pass
  1239. try:
  1240. v_to_hr3, v_to_min3 = stem.v_end3.split(':')[0], stem.v_end3.split(':')[1]
  1241. except Exception:
  1242. pass
  1243. try:
  1244. v_pow = stem.v_power
  1245. except Exception:
  1246. pass
  1247. try:
  1248. tank1_duration1 = liquid.tk1_duration1
  1249. except Exception:
  1250. pass
  1251. try:
  1252. tank1_duration2 = liquid.tk1_duration2
  1253. except Exception:
  1254. pass
  1255. try:
  1256. tank1_duration3 = liquid.tk1_duration3
  1257. except Exception:
  1258. pass
  1259. try:
  1260. tank2_duration1 = liquid.tk2_duration1
  1261. except Exception:
  1262. pass
  1263. try:
  1264. tank2_duration2 = liquid.tk2_duration2
  1265. except Exception:
  1266. pass
  1267. try:
  1268. tank2_duration3 = liquid.tk2_duration3
  1269. except Exception:
  1270. pass
  1271. try:
  1272. tank1_from_hr1, tank1_from_min1 = liquid.tk1_start1.split(':')[0], liquid.tk1_start1.split(':')[1]
  1273. tank1_to_hr1, tank1_to_min1 = liquid.tk1_end1.split(':')[0], liquid.tk1_end1.split(':')[1]
  1274. except Exception:
  1275. pass
  1276. try:
  1277. tank1_from_hr2, tank1_from_min2 = liquid.tk1_start2.split(':')[0], liquid.tk1_start2.split(':')[1]
  1278. tank1_to_hr2, tank1_to_min2 = liquid.tk1_end2.split(':')[0], liquid.tk1_end2.split(':')[1]
  1279. except Exception:
  1280. pass
  1281. try:
  1282. tank1_from_hr3, tank1_from_min3 = liquid.tk1_start3.split(':')[0], liquid.tk1_start3.split(':')[1]
  1283. tank1_to_hr3, tank1_to_min3 = liquid.tk1_end3.split(':')[0], liquid.tk1_end3.split(':')[1]
  1284. except Exception:
  1285. pass
  1286. try:
  1287. tank2_from_hr1, tank2_from_min1 = liquid.tk2_start1.split(':')[0], liquid.tk2_start1.split(':')[1]
  1288. tank2_to_hr1, tank2_to_min1 = liquid.tk2_end1.split(':')[0], liquid.tk2_end1.split(':')[1]
  1289. except Exception:
  1290. pass
  1291. try:
  1292. tank2_from_hr2, tank2_from_min2 = liquid.tk2_start2.split(':')[0], liquid.tk2_start2.split(':')[1]
  1293. tank2_to_hr2, tank2_to_min2 = liquid.tk2_end2.split(':')[0], liquid.tk2_end2.split(':')[1]
  1294. except Exception:
  1295. pass
  1296. try:
  1297. tank2_from_hr3, tank2_from_min3 = liquid.tk2_start3.split(':')[0], liquid.tk2_start3.split(':')[1]
  1298. tank2_to_hr3, tank2_to_min3 = liquid.tk2_end3.split(':')[0], liquid.tk2_end3.split(':')[1]
  1299. except Exception:
  1300. pass
  1301. try:
  1302. air_temp1 = fitolab1.s_air_tem1
  1303. except Exception:
  1304. pass
  1305. try:
  1306. air_humidity1 = fitolab1.s_humidity
  1307. except Exception:
  1308. pass
  1309. try:
  1310. ph = fitolab1.r_ph
  1311. except Exception:
  1312. pass
  1313. try:
  1314. do = fitolab1.r_do
  1315. except Exception:
  1316. pass
  1317. try:
  1318. ec = fitolab1.r_ec
  1319. except Exception:
  1320. pass
  1321. try:
  1322. water_level = fitolab1.r_water_level
  1323. except Exception:
  1324. pass
  1325. try:
  1326. tds1 = fitolab1.r_turbidity
  1327. except Exception:
  1328. pass
  1329. try:
  1330. o3 = fitolab1.s_o3
  1331. except Exception:
  1332. pass
  1333. try:
  1334. co2 = fitolab1.s_co2
  1335. except Exception:
  1336. pass
  1337. try:
  1338. h2 = fitolab1.s_h2
  1339. except Exception:
  1340. pass
  1341. try:
  1342. weight1 = fitolab1.s_wgt_sensor1
  1343. except Exception:
  1344. pass
  1345. try:
  1346. weight2 = fitolab1.s_wgt_sensor2
  1347. except Exception:
  1348. pass
  1349. return render_template('online_fitolab.html', params=locals())
  1350. else:
  1351. stem = StemSystem.query.order_by(text('datetime desc')).first()
  1352. root = RootSystem.query.order_by(text('datetime desc')).first()
  1353. liquid = LiquidTank.query.order_by(text('datetime desc')).first()
  1354. fitolab1 = Fitolab1Sensor.query.order_by(text('datetime desc')).first()
  1355. music_num = 1
  1356. music_from_num = 1
  1357. music_to_num = 1
  1358. v_num = 1
  1359. v_from_num = 1
  1360. v_to_num = 1
  1361. tank1_num = 1
  1362. tank1_from_num = 1
  1363. tank1_to_num = 1
  1364. tank2_num = 1
  1365. tank2_from_num = 1
  1366. tank2_to_num = 1
  1367. for i in request.form:
  1368. if i == 'stem-color':
  1369. try:
  1370. color = request.form['stem-color']
  1371. stem.l_r, stem.l_g, stem.l_b = color.split(',')[0], color.split(',')[1][1:], color.split(',')[2][1:]
  1372. except Exception:
  1373. pass
  1374. elif i == 'root-color':
  1375. try:
  1376. color = request.form['root-color']
  1377. root.l_r, root.l_g, root.l_b = color.split(',')[0], color.split(',')[1][1:], color.split(',')[2][1:]
  1378. except Exception:
  1379. pass
  1380. if 'stem-bright-on' in request.form:
  1381. stem.l_bright_auto = 1
  1382. else:
  1383. stem.l_bright_auto = 0
  1384. if 'root-bright-on' in request.form:
  1385. root.l_bright_auto = 1
  1386. else:
  1387. root.l_bright_auto = 0
  1388. if i[:5] == 'music' and len(i) <=11:
  1389. if music_num == 1:
  1390. stem.m_playlist1 = request.form[i]
  1391. stem.m_playlist2 = None
  1392. stem.m_playlist3 = None
  1393. music_num += 1
  1394. elif music_num == 2:
  1395. stem.m_playlist2 = request.form[i]
  1396. stem.m_playlist3 = None
  1397. music_num += 1
  1398. elif music_num == 3:
  1399. stem.m_playlist3 = request.form[i]
  1400. else:
  1401. if i[:13] == 'music_from_hr':
  1402. music_from_hr = request.form[i]
  1403. elif i[:14] == 'music_from_min':
  1404. music_from_min = request.form[i]
  1405. if music_from_num == 1:
  1406. stem.m_start1 = music_from_hr + ":" + music_from_min
  1407. stem.m_start2 = None
  1408. stem.m_start3 = None
  1409. music_from_num += 1
  1410. elif music_from_num == 2:
  1411. stem.m_start2 = music_from_hr + ":" + music_from_min
  1412. stem.m_start3 = None
  1413. music_from_num += 1
  1414. elif music_from_num == 3:
  1415. stem.m_start3 = music_from_hr + ":" + music_from_min
  1416. elif i[:11] == 'music_to_hr':
  1417. music_to_hr = request.form[i]
  1418. elif i[:12] == 'music_to_min':
  1419. music_to_min = request.form[i]
  1420. if music_to_num == 1:
  1421. stem.m_end1 = music_to_hr + ":" + music_to_min
  1422. stem.m_end2 = None
  1423. stem.m_end3 = None
  1424. music_to_num += 1
  1425. elif music_to_num == 2:
  1426. stem.m_end2 = music_to_hr + ":" + music_to_min
  1427. stem.m_end3 = None
  1428. music_to_num += 1
  1429. elif music_to_num == 3:
  1430. stem.m_end3 = music_to_hr + ":" + music_to_min
  1431. if 'camera-on' in request.form:
  1432. stem.c_status = 1
  1433. else:
  1434. stem.c_status = 0
  1435. if i[:9] == 'vibration':
  1436. if v_num == 1:
  1437. stem.v_vol1 = request.form[i]
  1438. stem.v_vol2 = None
  1439. stem.v_vol3 = None
  1440. v_num += 1
  1441. elif v_num == 2:
  1442. stem.v_vol2 = request.form[i]
  1443. stem.v_vol3 = None
  1444. v_num += 1
  1445. elif v_num == 3:
  1446. stem.v_vol3 = request.form[i]
  1447. else:
  1448. if i[:9] == 'v_from_hr':
  1449. v_from_hr = request.form[i]
  1450. elif i[:10] == 'v_from_min':
  1451. v_from_min = request.form[i]
  1452. if v_from_num == 1:
  1453. stem.v_start1 = v_from_hr + ":" + v_from_min
  1454. stem.v_start2 = None
  1455. stem.v_start3 = None
  1456. v_from_num += 1
  1457. elif v_from_num == 2:
  1458. stem.v_start2 = v_from_hr + ":" + v_from_min
  1459. stem.v_start3 = None
  1460. v_from_num += 1
  1461. elif v_from_num == 3:
  1462. stem.v_start3 = v_from_hr + ":" + v_from_min
  1463. elif i[:7] == 'v_to_hr':
  1464. v_to_hr = request.form[i]
  1465. elif i[:8] == 'v_to_min':
  1466. v_to_min = request.form[i]
  1467. if v_to_num == 1:
  1468. stem.v_end1 = v_to_hr + ":" + v_to_min
  1469. stem.v_end2 = None
  1470. stem.v_end3 = None
  1471. v_to_num += 1
  1472. elif v_to_num == 2:
  1473. stem.v_end2 = v_to_hr + ":" + v_to_min
  1474. stem.v_end3 = None
  1475. v_to_num += 1
  1476. elif v_to_num == 3:
  1477. stem.v_end3 = v_to_hr + ":" + v_to_min
  1478. if i[:14] == 'tank1_duration':
  1479. if tank1_num == 1:
  1480. liquid.tk1_duration1 = request.form[i]
  1481. liquid.tk1_duration2 = None
  1482. liquid.tk1_duration3 = None
  1483. tank1_num += 1
  1484. elif tank1_num == 2:
  1485. liquid.tk1_duration2 = request.form[i]
  1486. liquid.tk1_duration3 = None
  1487. tank1_num += 1
  1488. elif tank1_num == 3:
  1489. liquid.tk1_duration3 = request.form[i]
  1490. else:
  1491. if i[:13] == 'tank1_from_hr':
  1492. tk1_from_hr = request.form[i]
  1493. elif i[:14] == 'tank1_from_min':
  1494. tk1_from_min = request.form[i]
  1495. if tank1_from_num == 1:
  1496. liquid.tk1_start1 = tk1_from_hr + ":" + tk1_from_min
  1497. liquid.tk1_start2 = None
  1498. liquid.tk1_start3 = None
  1499. tank1_from_num += 1
  1500. elif tank1_from_num == 2:
  1501. liquid.tk1_start2 = tk1_from_hr + ":" + tk1_from_min
  1502. liquid.tk1_start3 = None
  1503. tank1_from_num += 1
  1504. elif tank1_from_num == 3:
  1505. liquid.tk1_start3 = tk1_from_hr + ":" + tk1_from_min
  1506. elif i[:11] == 'tank1_to_hr':
  1507. tk1_to_hr = request.form[i]
  1508. elif i[:12] == 'tank1_to_min':
  1509. tk1_to_min = request.form[i]
  1510. if tank1_to_num == 1:
  1511. liquid.tk1_end1 = tk1_to_hr + ":" + tk1_to_min
  1512. liquid.tk1_end2 = None
  1513. liquid.tk1_end3 = None
  1514. tank1_to_num += 1
  1515. elif tank1_to_num == 2:
  1516. liquid.tk1_end2 = tk1_to_hr + ":" + tk1_to_min
  1517. liquid.tk1_end3 = None
  1518. tank1_to_num += 1
  1519. elif tank1_to_num == 3:
  1520. liquid.tk1_end3 = tk1_to_hr + ":" + tk1_to_min
  1521. if i[:14] == 'tank2_duration':
  1522. if tank2_num == 1:
  1523. liquid.tk2_duration1 = request.form[i]
  1524. liquid.tk2_duration2 = None
  1525. liquid.tk2_duration3 = None
  1526. tank2_num += 1
  1527. elif tank2_num == 2:
  1528. liquid.tk2_duration2 = request.form[i]
  1529. liquid.tk2_duration3 = None
  1530. tank2_num += 1
  1531. elif tank2_num == 3:
  1532. liquid.tk2_duration3 = request.form[i]
  1533. else:
  1534. if i[:13] == 'tank2_from_hr':
  1535. tk2_from_hr = request.form[i]
  1536. elif i[:14] == 'tank2_from_min':
  1537. tk2_from_min = request.form[i]
  1538. if tank2_from_num == 1:
  1539. liquid.tk2_start1 = tk2_from_hr + ":" + tk2_from_min
  1540. liquid.tk2_start2 = None
  1541. liquid.tk2_start3 = None
  1542. tank2_from_num += 1
  1543. elif tank2_from_num == 2:
  1544. liquid.tk2_start2 = tk2_from_hr + ":" + tk2_from_min
  1545. liquid.tk2_start3 = None
  1546. tank2_from_num += 1
  1547. elif tank2_from_num == 3:
  1548. liquid.tk2_start3 = tk2_from_hr + ":" + tk2_from_min
  1549. elif i[:11] == 'tank2_to_hr':
  1550. tk2_to_hr = request.form[i]
  1551. elif i[:12] == 'tank2_to_min':
  1552. tk2_to_min = request.form[i]
  1553. if tank2_to_num == 1:
  1554. liquid.tk2_end1 = tk2_to_hr + ":" + tk2_to_min
  1555. liquid.tk2_end2 = None
  1556. liquid.tk2_end3 = None
  1557. tank2_to_num += 1
  1558. elif tank2_to_num == 2:
  1559. liquid.tk2_end2 = tk2_to_hr + ":" + tk2_to_min
  1560. liquid.tk2_end3 = None
  1561. tank2_to_num += 1
  1562. elif tank2_to_num == 3:
  1563. liquid.tk2_end3 = tk2_to_hr + ":" + tk2_to_min
  1564. if i == 'root-color':
  1565. try:
  1566. color = request.form['root-color']
  1567. root.l_r, root.l_g, root.l_b = color.split(',')[0], color.split(',')[1][1:], color.split(',')[2][1:]
  1568. except Exception:
  1569. pass
  1570. if i == 'stem-color':
  1571. try:
  1572. color = request.form['stem-color']
  1573. stem.l_r, stem.l_g, stem.l_b = color.split(',')[0], color.split(',')[1][1:], color.split(',')[2][1:]
  1574. except Exception:
  1575. pass
  1576. stem.l_bright_vol = request.form['stem-bright-vol']
  1577. root.l_bright_vol = request.form['root-bright-vol']
  1578. stem.m_vol = request.form['m-vol']
  1579. stem.c_duration = request.form['c-duration']
  1580. stem.v_power = request.form['v-pow']
  1581. db.session.add(stem)
  1582. db.session.add(root)
  1583. db.session.commit()
  1584. return redirect('/online_fitolab')
  1585. #退出的訪問路徑
  1586. @main.route('/logout')
  1587. def logout_views():
  1588. if 'id' in session and 'uname' in session:
  1589. del session['id']
  1590. del session['uname']
  1591. return redirect('/')