test_logic.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import pymysql
  2. import time
  3. import paho.mqtt.publish as publish
  4. import paho.mqtt.client as mqtt
  5. import datetime
  6. import random
  7. import json
  8. import paramiko
  9. from flask_mqtt import Mqtt
  10. #插入一筆新資料(發酵表)
  11. def insert_data_ferment():
  12. insert = conn.cursor()
  13. insert_status = "INSERT INTO `ferment_container_status`(`Ferment_Input_1`,`Ferment_Input_2`," \
  14. "`Ferment_Tank_1`,`Ferment_Tank_2`, `Ferment_Tank_3`, `Ferment_Tank_4`, `Ferment_Tank_5`," \
  15. "`Ferment_Tank_6`, `Ferment_Tank_7`,`Ferment_Tank_8`, `Ferment_Tank_9`, `Ferment_Tank_10`," \
  16. "`Ferment_Tank_11`, `Ferment_Tank_12`, `Ferment_Output_1`,`Ferment_Output_2`)" \
  17. "SELECT `Ferment_Input_1`, `Ferment_Input_2`," \
  18. "`Ferment_Tank_1`,`Ferment_Tank_2`, `Ferment_Tank_3`, `Ferment_Tank_4`, `Ferment_Tank_5`," \
  19. "`Ferment_Tank_6`, `Ferment_Tank_7`,`Ferment_Tank_8`, `Ferment_Tank_9`, `Ferment_Tank_10`," \
  20. "`Ferment_Tank_11`, `Ferment_Tank_12`, `Ferment_Output_1`,`Ferment_Output_2`" \
  21. "FROM ferment_container_status ORDER BY `datetime` DESC LIMIT 1"
  22. insert.execute(insert_status)
  23. conn.commit()
  24. #插入一筆新資料(乾燥表)
  25. def insert_data_dry():
  26. insert = conn.cursor()
  27. insert_status = "INSERT INTO `dry_container_status`(`Dry_Input_1`,`Dry_Input_2`," \
  28. "`Dry_Tank_1`,`Dry_Tank_2`, `Dry_Tank_3`, `Dry_Tank_4`, `Dry_Tank_5`," \
  29. "`Dry_Tank_6`, `Dry_Tank_7`,`Dry_Tank_8`, `Dry_Tank_9`, `Dry_Tank_10`," \
  30. "`Dry_Tank_11`, `Dry_Tank_12`, `Dry_Output_1`,`Dry_Output_2`)" \
  31. "SELECT `Dry_Input_1`,`Dry_Input_2`," \
  32. "`Dry_Tank_1`,`Dry_Tank_2`, `Dry_Tank_3`, `Dry_Tank_4`, `Dry_Tank_5`," \
  33. "`Dry_Tank_6`, `Dry_Tank_7`,`Dry_Tank_8`, `Dry_Tank_9`, `Dry_Tank_10`," \
  34. "`Dry_Tank_11`, `Dry_Tank_12`, `Dry_Output_1`,`Dry_Output_2`" \
  35. "FROM dry_container_status ORDER BY `datetime` DESC LIMIT 1"
  36. insert.execute(insert_status)
  37. conn.commit()
  38. #修改發酵入料儲豆槽狀態 FI_Waiting改成FI_InputtingBean。
  39. #f = 1,2(發酵入料儲豆槽編號)
  40. def update_Ferment_Input_status(f):
  41. insert_data_ferment()
  42. update_status = conn.cursor()
  43. Ferment_name = 'Ferment_Input_'
  44. re = "UPDATE `ferment_container_status` SET " + Ferment_name + str(f) + " = 'FI_InputtingBean' ORDER BY `datetime` DESC LIMIT 1 "
  45. update_status.execute(re)
  46. conn.commit()
  47. #修改發酵入料儲豆槽狀態,FI_InputtingBean改成FI_Stand_by。
  48. #f = 1,2(發酵入料儲豆槽編號)
  49. def update_Fer_Input_status(f):
  50. insert_data_ferment()
  51. update_status_fer = conn.cursor()
  52. Ferment_name = 'Ferment_Input_'
  53. re_fer = "UPDATE `ferment_container_status` SET "+Ferment_name+str(f)+"= 'FI_Stand_by' " \
  54. "ORDER BY `datetime` DESC LIMIT 1 "
  55. update_status_fer.execute(re_fer)
  56. conn.commit()
  57. #修改脫皮出料儲豆槽狀態,PO_OutputtingBean改成PO_Stand_by。
  58. #p = 1,2(脫皮出料儲豆槽編號)
  59. def update_Peel_Output_status(p):
  60. insert = conn.cursor()
  61. insert_status = "INSERT INTO `clean_container_status`(`Clean_Input_1`,`Clean_Input_2`," \
  62. "`Clean_Tank_1`,`Clean_Tank_2`, `Clean_Tank_3`, `Clean_Tank_4`, `Reclaimed_Tank_1`," \
  63. "`ColorSelect_Input_1`, `ColorSelect_Input_2`,`ColorSelect_Tank_1`, `ColorSelect_Tank_2`, `ColorSelect_Output_g1`," \
  64. "`ColorSelect_Output_b1`, `ColorSelect_Output_g2`, `ColorSelect_Output_b2`,`Peel_Tank_1`,`Peel_Tank_2`,`Peel_Output_1`," \
  65. "`Peel_Output_2`,`Peel_Output_b1`)" \
  66. "SELECT `Clean_Input_1`,`Clean_Input_2`," \
  67. "`Clean_Tank_1`,`Clean_Tank_2`, `Clean_Tank_3`, `Clean_Tank_4`, `Reclaimed_Tank_1`," \
  68. "`ColorSelect_Input_1`, `ColorSelect_Input_2`,`ColorSelect_Tank_1`, `ColorSelect_Tank_2`, `ColorSelect_Output_g1`," \
  69. "`ColorSelect_Output_b1`, `ColorSelect_Output_g2`, `ColorSelect_Output_b2`,`Peel_Tank_1`,`Peel_Tank_2`,`Peel_Output_1`," \
  70. "`Peel_Output_2`,`Peel_Output_b1`" \
  71. "FROM clean_container_status ORDER BY `datetime` DESC LIMIT 1"
  72. insert.execute(insert_status)
  73. conn.commit()
  74. update_status_peel = conn.cursor()
  75. Peel_name = 'Peel_Output_'
  76. re_peel = "UPDATE `clean_container_status` SET " + Peel_name + str(p) + "= 'PO_Stand_by' " \
  77. "ORDER BY `datetime` DESC LIMIT 1 "
  78. update_status_peel.execute(re_peel)
  79. conn.commit()
  80. #修改發酵桶槽狀態:F_InputtingBeans改成F_Stand_by
  81. def update_Fer_Tank_status(f_tank):
  82. insert_data_ferment()
  83. update_status_fer_Tank = conn.cursor()
  84. Ferment_Tank_name = 'Ferment_Tank_'
  85. re_fer_Tank = "UPDATE `ferment_container_status` SET "+Ferment_Tank_name+str(f_tank)+"= 'F_Stand_by' " \
  86. "ORDER BY `datetime` DESC LIMIT 1 "
  87. update_status_fer_Tank.execute(re_fer_Tank)
  88. conn.commit()
  89. #修改發酵桶槽狀態:入豆中 F_Waiting改成F_InputtingBean
  90. def update_Fer_Tank_status1(f_tank):
  91. insert_data_ferment()
  92. update_status_fer_Tank = conn.cursor()
  93. Ferment_Tank_name = 'Ferment_Tank_'
  94. re_fer_Tank = "UPDATE `ferment_container_status` SET "+Ferment_Tank_name+str(f_tank)+"= 'F_InputtingBean' " \
  95. "ORDER BY `datetime` DESC LIMIT 1 "
  96. update_status_fer_Tank.execute(re_fer_Tank)
  97. conn.commit()
  98. #修改發酵出料儲豆槽狀態:入豆中 FO_Waiting改成FO_InputtingBean
  99. def update_Fer_Output_status(f_Output):
  100. insert_data_ferment()
  101. update_status_fer = conn.cursor()
  102. Ferment_name = 'Ferment_Output_'
  103. re_fer = "UPDATE `ferment_container_status` SET "+Ferment_name+str(f_Output)+"= 'FO_InputtingBean' " \
  104. "ORDER BY `datetime` DESC LIMIT 1 "
  105. update_status_fer.execute(re_fer)
  106. conn.commit()
  107. #修改發酵出料儲豆槽狀態:FO_InputtingBean改成FO_Stand_by。
  108. #FO_Stand_by
  109. def update_Fer_Output_status_Stand_by(f_Output):
  110. insert_data_ferment()
  111. update_status_peel = conn.cursor()
  112. Fer_Output_name = 'Ferment_Output_'
  113. re_peel = "UPDATE `ferment_container_status` SET "+Fer_Output_name+str(f_Output)+"= 'FO_Stand_by' " \
  114. "ORDER BY `datetime` DESC LIMIT 1 "
  115. update_status_peel.execute(re_peel)
  116. conn.commit()
  117. #修改乾燥入料儲豆槽狀態:入豆
  118. def update_Dry_Input_status_input(p):
  119. insert_data_dry()
  120. update_status_dry = conn.cursor()
  121. Dry_name = 'Dry_Input_'
  122. re_dry = "UPDATE `dry_container_status` SET "+Dry_name+str(p)+"= 'DI_InputtingBean' " \
  123. "ORDER BY `datetime` DESC LIMIT 1 "
  124. update_status_dry.execute(re_dry)
  125. conn.commit()
  126. #修改乾燥入料儲豆槽狀態:待命
  127. def update_Dry_Input_status(p):
  128. insert_data_dry()
  129. update_status_dry = conn.cursor()
  130. Dry_name = 'Dry_Input_'
  131. re_dry = "UPDATE `dry_container_status` SET "+Dry_name+str(p)+"= 'DI_Stand_by' " \
  132. "ORDER BY `datetime` DESC LIMIT 1 "
  133. update_status_dry.execute(re_dry)
  134. conn.commit()
  135. #傳送MQTT訊號
  136. def send_mqtt():
  137. # 連線設定
  138. # 初始化地端程式
  139. client = mqtt.Client()
  140. # 設置日期時間的格式
  141. ISOTIMEFORMAT = '%m/%d %H:%M:%S'
  142. t = datetime.datetime.now().strftime(ISOTIMEFORMAT)
  143. # 設定登入帳號密碼
  144. client.username_pw_set("aisky-server","aisky")
  145. # 設定連線資訊(IP, Port, 連線時間)
  146. client.connect("60.250.156.234", 1883, 60)
  147. payload = {"command": "F_InputtingBean"}
  148. print(json.dumps(payload),t)
  149. #要發布的主題和內容
  150. #自行測試用
  151. client.publish("AISKY/Coffee/MK-G/b8:27:eb:7e:24:78", json.dumps(payload))
  152. #正式測試用
  153. #client.publish("AISKY/Coffee/MK-G/b8:27:eb:b4:59:3e", json.dumps(payload))
  154. #time.sleep(5)
  155. #迴圈判斷式
  156. #第一部分判斷脫皮出料儲豆槽+發酵入料儲豆槽(完成)
  157. #第二部分判斷發酵入料儲豆槽+發酵桶槽(完成)
  158. #第三部分判斷發酵桶槽+發酵出料儲豆槽(完成)
  159. #第四部份判斷發酵出料儲豆槽+乾燥入料儲豆槽(完成)
  160. #while True:
  161. conn = pymysql.connect(
  162. host='127.0.0.1',
  163. user='root',
  164. password='g53743001',
  165. db='coffee',
  166. charset='utf8'
  167. )
  168. # conn = pymysql.connect(
  169. # host='60.250.156.230',
  170. # user='user',
  171. # password='Gold@53743001',
  172. # db='CoffeeManage',
  173. # charset='utf8'
  174. # )
  175. cur1 = conn.cursor()
  176. sql1 = "SELECT `Ferment_Input_1`,`Ferment_Input_2` FROM `ferment_container_status` ORDER BY `sn` DESC LIMIT 1 "
  177. cur1.execute(sql1)
  178. get_Ferment_Input = cur1.fetchone()
  179. # 發酵入料儲豆槽1
  180. FI1 = get_Ferment_Input[0]
  181. # 發酵入料儲豆槽2
  182. FI2 = get_Ferment_Input[1]
  183. ferment_input_status_N = [FI1, FI2]
  184. f = 1
  185. # 第一部分檢查脫皮出料與發酵入料儲豆槽狀態
  186. for i in range(len(ferment_input_status_N)):
  187. f = i + 1
  188. cur = conn.cursor()
  189. sql = "SELECT `Peel_Output_1`,`Peel_Output_2` FROM `clean_container_status` ORDER BY `sn` DESC LIMIT 1"
  190. cur.execute(sql)
  191. get_Peel_Output_status = cur.fetchone()
  192. # 清洗脫皮出料儲豆槽1
  193. PO1 = get_Peel_Output_status[0]
  194. # 清洗脫皮出料儲豆槽2
  195. PO2 = get_Peel_Output_status[1]
  196. get_Peel_Output_status_N = [PO1, PO2]
  197. p = 1
  198. if (ferment_input_status_N[i] == 'FI_Waiting'):
  199. N = 'FI' + str(f)
  200. print(N + ' ' + ferment_input_status_N[i] + ' 空桶等待')
  201. update_Ferment_Input_status(f)
  202. time.sleep(1)
  203. elif (ferment_input_status_N[i] == 'FI_InputtingBean'):
  204. print('FI' + str(f) + ' ' + ferment_input_status_N[i] + ' 入豆中')
  205. for b in range(len(get_Peel_Output_status_N)):
  206. p = b + 1
  207. if (get_Peel_Output_status_N[b] == 'PO_InputtingBean'):
  208. print('PO' + str(p) + ' ' + get_Peel_Output_status_N[b] + ' 入豆中')
  209. elif (get_Peel_Output_status_N[b] == 'PO_Waiting'):
  210. print('PO' + str(p) + ' ' + get_Peel_Output_status_N[b] + ' 空桶等待')
  211. elif (get_Peel_Output_status_N[b] == 'PO_Stand_by'):
  212. print('PO' + str(p) + '待命')
  213. elif (get_Peel_Output_status_N[b] == 'PO_OutputtingBean'):
  214. print('PO' + str(p) + ' ' + get_Peel_Output_status_N[b] + ' 可出豆')
  215. send_mqtt()
  216. print('發送MQTT訊號通知', 'PO' + str(p) + ' 可出豆')
  217. # 修改發酵入料槽狀態
  218. update_Fer_Input_status(f)
  219. # 修改脫皮出料狀態
  220. update_Peel_Output_status(p)
  221. break
  222. # 第二部分開始判斷發酵入料是否是可出豆狀態
  223. elif (ferment_input_status_N[i] == 'FI_OutputtingBean'):
  224. print('FI' + str(f) + ' ' + ferment_input_status_N[i] + ' 可出豆')
  225. cur1 = conn.cursor()
  226. sql1 = "SELECT `Ferment_Tank_1`,`Ferment_Tank_2`,`Ferment_Tank_3`,`Ferment_Tank_4`,`Ferment_Tank_5`,`Ferment_Tank_6`" \
  227. ",`Ferment_Tank_7`,`Ferment_Tank_8`,`Ferment_Tank_9`,`Ferment_Tank_10`,`Ferment_Tank_11`,`Ferment_Tank_12` " \
  228. "FROM `ferment_container_status` ORDER BY `sn` DESC LIMIT 1 "
  229. cur1.execute(sql1)
  230. get_Ferment_Tank = cur1.fetchone()
  231. # print(get_Ferment_Tank)
  232. # 發酵槽1-12號
  233. F1 = get_Ferment_Tank[0]
  234. F2 = get_Ferment_Tank[1]
  235. F3 = get_Ferment_Tank[2]
  236. F4 = get_Ferment_Tank[3]
  237. F5 = get_Ferment_Tank[4]
  238. F6 = get_Ferment_Tank[5]
  239. F7 = get_Ferment_Tank[6]
  240. F8 = get_Ferment_Tank[7]
  241. F9 = get_Ferment_Tank[8]
  242. F10 = get_Ferment_Tank[9]
  243. F11 = get_Ferment_Tank[10]
  244. F12 = get_Ferment_Tank[11]
  245. ferment_Tank_status = [F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12]
  246. f_tank = 1
  247. for i_tank in range(len(ferment_Tank_status)):
  248. f_tank = i_tank + 1
  249. if (ferment_Tank_status[i_tank] == 'F_Waiting'):
  250. # 修改發酵桶槽狀態F_Waiting->F_InputtingBean
  251. update_Fer_Tank_status1(f_tank)
  252. print('F' + str(f_tank) + '修改發酵桶槽狀態F_Waiting->F_InputtingBean')
  253. elif (ferment_Tank_status[i_tank] == 'F_InputtingBean'):
  254. # 修改發酵桶槽狀態F_InputtingBean->F_Standby
  255. update_Fer_Tank_status(f_tank)
  256. print('F' + str(f_tank) + ' ' + ferment_Tank_status[i_tank] + ' 入豆中')
  257. send_mqtt()
  258. break
  259. elif (ferment_Tank_status[i_tank] == 'F_OutputtingBean'):
  260. cur1 = conn.cursor()
  261. sql1 = "SELECT `Ferment_Output_1`,`Ferment_Output_2` FROM `ferment_container_status` ORDER BY `sn` DESC LIMIT 1 "
  262. cur1.execute(sql1)
  263. get_Ferment_Output = cur1.fetchone()
  264. # 發酵出料儲豆槽1
  265. FO1 = get_Ferment_Output[0]
  266. # 發酵出料儲豆槽2
  267. FO2 = get_Ferment_Output[1]
  268. ferment_Output_status = [FO1, FO2]
  269. f_Output = 1
  270. for i_Output in range(len(ferment_Output_status)):
  271. f_Output = i_Output + 1
  272. if (ferment_Output_status[i_Output] == 'FO_Waiting'):
  273. update_Fer_Output_status(f_Output)
  274. print('FO' + str(f_Output) + ' 修改發酵桶槽狀態FO_Waiting->FO_InputtingBean')
  275. elif (ferment_Output_status[i_Output] == 'FO_InputtingBean'):
  276. update_Fer_Output_status_Stand_by(f_Output)
  277. print('修改' + 'FO' + str(f_Output) + '出料槽狀態,並發送MQTT訊號。')
  278. send_mqtt()
  279. elif (ferment_Output_status[i_Output] == 'FO_OutputtingBean'):
  280. cur = conn.cursor()
  281. sql = "SELECT `Dry_Input_1`,`Dry_Input_2` FROM `dry_container_status` ORDER BY `sn` DESC LIMIT 1"
  282. cur.execute(sql)
  283. get_Dry_Input_status = cur.fetchone()
  284. # 清洗脫皮出料儲豆槽1
  285. DI1 = get_Dry_Input_status[0]
  286. # 清洗脫皮出料儲豆槽2
  287. DI2 = get_Dry_Input_status[1]
  288. get_Dry_Input_status_N = [DI1, DI2]
  289. p = 1
  290. for b in range(len(get_Dry_Input_status_N)):
  291. p = b + 1
  292. if (get_Dry_Input_status_N[b] == 'DI_Waiting'):
  293. update_Dry_Input_status_input(p)
  294. print('DI' + str(p) + ' ' + get_Dry_Input_status_N[
  295. b] + ' 空桶等待,並修改狀態變成"DI_InputtingBean"')
  296. elif (get_Dry_Input_status_N[b] == 'DI_InputtingBean'):
  297. update_Dry_Input_status(p)
  298. print('DI' + str(p) + ' ' + get_Dry_Input_status_N[b] + ' 入豆中,並修改狀態變成"DI_Standby"')
  299. send_mqtt()
  300. print('發送MQTT', 'DI' + str(p))
  301. elif (get_Dry_Input_status_N[b] == 'DI_Stand_by'):
  302. print('DI' + str(p) + ' ' + get_Dry_Input_status_N[b] + '待命中')
  303. elif (ferment_Output_status[i_Output] == 'FO_Standby'):
  304. print('FO' + str(f_Output) + ' 待命中')
  305. elif (ferment_Tank_status[i_tank] == 'F_Fermenting'):
  306. print('F_Fermenting,發酵中')
  307. elif (ferment_Tank_status[i_tank] == 'F_Standby'):
  308. print('F' + str(f_tank) + '待命')
  309. elif (ferment_Tank_status[i_tank] == 'F_InputtingBean_Pause'):
  310. print('F' + str(f_tank) + '入豆暫停')
  311. elif (ferment_Tank_status[i_tank] == 'F_InputtingBean_Finish'):
  312. print('F' + str(f_tank) + '入豆完成')
  313. elif (ferment_Tank_status[i_tank] == 'F_InputtingWater'):
  314. print('F' + str(f_tank) + '入水中')
  315. elif (ferment_Tank_status[i_tank] == 'F_Cleaning'):
  316. print('F' + str(f_tank) + '清洗中')
  317. elif (ferment_Tank_status[i_tank] == 'F_Warning'):
  318. print('F' + str(f_tank) + '發生錯誤')
  319. elif (ferment_input_status_N[i] == 'FI_Standby'):
  320. print('FI' + str(f) + ' ' + ferment_input_status_N[i] + '待命')
  321. elif (ferment_input_status_N[i] == 'FI_Error'):
  322. print('FI' + str(f) + ' ' + ferment_input_status_N[i] + '執行緊急停止流程')