clean_logic.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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_clean():
  12. insert = conn.cursor()
  13. insert_status = "INSERT INTO `clean_container_status`( `Clean_Input_1`, `Clean_Input_2`, `Clean_Tank_1`, " \
  14. "`Clean_Tank_2`, `Clean_Tank_3`, `Clean_Tank_4`, `Reclaimed_Tank_1`, `ColorSelect_Input_1`, " \
  15. "`ColorSelect_Input_2`, `ColorSelect_Tank_1`, `ColorSelect_Tank_2`, `ColorSelect_Output_g1`, " \
  16. "`ColorSelect_Output_b1`, `ColorSelect_Output_g2`, `ColorSelect_Output_b2`, `Peel_Tank_1`, " \
  17. "`Peel_Tank_2`, `Peel_Output_1`, `Peel_Output_2`, `Peel_Output_b1`) SELECT `Clean_Input_1`, " \
  18. "`Clean_Input_2`, `Clean_Tank_1`, `Clean_Tank_2`, `Clean_Tank_3`, `Clean_Tank_4`, " \
  19. "`Reclaimed_Tank_1`, `ColorSelect_Input_1`, `ColorSelect_Input_2`, `ColorSelect_Tank_1`, " \
  20. "`ColorSelect_Tank_2`, `ColorSelect_Output_g1`, `ColorSelect_Output_b1`, `ColorSelect_Output_g2`," \
  21. " `ColorSelect_Output_b2`, `Peel_Tank_1`, `Peel_Tank_2`, `Peel_Output_1`, `Peel_Output_2`, " \
  22. "`Peel_Output_b1` FROM clean_container_status ORDER BY `datetime` DESC LIMIT 1"
  23. insert.execute(insert_status)
  24. conn.commit()
  25. #更新清洗入料儲豆槽狀態 CI_Waiting改成CI_InputtingBean
  26. def update_Clean_Input_status(c):
  27. insert_data_clean()
  28. update_Clean_status = conn.cursor()
  29. Clean_name = 'Clean_Input_'
  30. re_clean = "UPDATE `clean_container_status` SET " + Clean_name + str(c) + " = 'CI_InputtingBean' ORDER BY `datetime` DESC LIMIT 1 "
  31. update_Clean_status.execute(re_clean)
  32. conn.commit()
  33. #更新清洗桶槽狀態 C_Waiting改成C_InputtingBean
  34. def update_Clean_Tank_status1(c_tank):
  35. insert_data_clean()
  36. update_status_clean_Tank = conn.cursor()
  37. Clean_Tank_name = 'Clean_Tank_'
  38. re_clean_Tank = "UPDATE `clean_container_status` SET "+Clean_Tank_name+str(c_tank)+"= 'C_InputtingBean' " \
  39. "ORDER BY `datetime` DESC LIMIT 1 "
  40. update_status_clean_Tank.execute(re_clean_Tank)
  41. conn.commit()
  42. #更新清洗桶槽狀態 C_InputtingBean改成C_Stand_by
  43. def update_Clean_Tank_status(c_tank):
  44. insert_data_clean()
  45. update_status_clean_Tank = conn.cursor()
  46. clean_Tank_name = 'Clean_Tank_'
  47. re_clean_Tank = "UPDATE `clean_container_status` SET "+clean_Tank_name+str(c_tank)+"= 'C_Stand_by' " \
  48. "ORDER BY `datetime` DESC LIMIT 1 "
  49. update_status_clean_Tank.execute(re_clean_Tank)
  50. conn.commit()
  51. #更新色選機狀態S_Waiting改成S_InputtingBean
  52. def update_ColorSelect_Tank_status(s_Tank):
  53. insert_data_clean()
  54. update_status_colorSelect = conn.cursor()
  55. ColorSelect_name = 'ColorSelect_Tank_'
  56. re_colorSelect = "UPDATE `clean_container_status` SET "+ColorSelect_name+str(s_Tank)+"= 'S_InputtingBean' " \
  57. "ORDER BY `datetime` DESC LIMIT 1 "
  58. update_status_colorSelect.execute(re_colorSelect)
  59. conn.commit()
  60. #更新色選機狀態 S_InputtingBean改成S_Stand_by
  61. def update_ColorSelect_Tank_status_Stand_by(s_Tank):
  62. insert_data_clean()
  63. update_status_colorSelect = conn.cursor()
  64. ColorSelect_name = 'ColorSelect_Tank_'
  65. re_colorSelect = "UPDATE `clean_container_status` SET "+ColorSelect_name+str(s_Tank)+"= 'S_Stand_by' " \
  66. "ORDER BY `datetime` DESC LIMIT 1 "
  67. update_status_colorSelect.execute(re_colorSelect)
  68. conn.commit()
  69. #更新色選機出料儲豆槽狀態 SO_Waiting改成SO_InputtingBean
  70. def update_ColorSelect_Output_status(so_Output):
  71. insert_data_clean()
  72. update_status_colorSelect = conn.cursor()
  73. ColorSelect_name = 'ColorSelect_Output_g'
  74. re_colorSelect = "UPDATE `clean_container_status` SET "+ColorSelect_name+str(so_Output)+"= 'SO_InputtingBean' " \
  75. "ORDER BY `datetime` DESC LIMIT 1 "
  76. update_status_colorSelect.execute(re_colorSelect)
  77. conn.commit()
  78. #更新色選機出料儲豆槽狀態 SO_InputtingBean改成SO_Stand_by
  79. def update_ColorSelect_Output_status_Stand_by(so_Output):
  80. insert_data_clean()
  81. update_status_colorSelect = conn.cursor()
  82. ColorSelect_name = 'ColorSelect_Output_g'
  83. re_colorSelect = "UPDATE `clean_container_status` SET "+ColorSelect_name+str(so_Output)+"= 'SO_Stand_by' " \
  84. "ORDER BY `datetime` DESC LIMIT 1 "
  85. update_status_colorSelect.execute(re_colorSelect)
  86. conn.commit()
  87. #更新脫皮機狀態 P_Waiting改成P_InputtingBean
  88. def update_Peel_Tank_status(p_Output):
  89. insert_data_clean()
  90. update_status_Peel = conn.cursor()
  91. Peel_name = 'Peel_Tank_'
  92. re_Peel = "UPDATE `clean_container_status` SET "+Peel_name+str(p_Output)+"= 'P_InputtingBean' " \
  93. "ORDER BY `datetime` DESC LIMIT 1 "
  94. update_status_Peel.execute(re_Peel)
  95. conn.commit()
  96. #更新脫皮機狀態 P_InputtingBean改成P_Stand_by
  97. def update_Peel_Tank_status_Stand_by(p_Output):
  98. insert_data_clean()
  99. update_status_Peel = conn.cursor()
  100. Peel_name = 'Peel_Tank_'
  101. re_Peel = "UPDATE `clean_container_status` SET "+Peel_name+str(p_Output)+"= 'P_Stand_by' " \
  102. "ORDER BY `datetime` DESC LIMIT 1 "
  103. update_status_Peel.execute(re_Peel)
  104. conn.commit()
  105. #更新脫皮機出料儲豆槽狀態 PO_Waiting改成PO_InputtingBean
  106. def update_Peel_Output_status(po_Output):
  107. insert_data_clean()
  108. update_status_Peel = conn.cursor()
  109. Peel_name = 'Peel_Output_'
  110. re_Peel = "UPDATE `clean_container_status` SET "+Peel_name+str(po_Output)+"= 'PO_InputtingBean' " \
  111. "ORDER BY `datetime` DESC LIMIT 1 "
  112. update_status_Peel.execute(re_Peel)
  113. conn.commit()
  114. #更新脫皮機出料儲豆槽狀態 PO_InputtingBean改成PO_Stand_by
  115. def update_Peel_Output_status_Stand_by(po_Output):
  116. insert_data_clean()
  117. update_status_Peel = conn.cursor()
  118. Peel_name = 'Peel_Output_'
  119. re_Peel = "UPDATE `clean_container_status` SET "+Peel_name+str(po_Output)+"= 'PO_Stand_by' " \
  120. "ORDER BY `datetime` DESC LIMIT 1 "
  121. update_status_Peel.execute(re_Peel)
  122. conn.commit()
  123. #傳送MQTT訊號
  124. def send_mqtt():
  125. # 連線設定
  126. # 初始化地端程式
  127. client = mqtt.Client()
  128. # 設置日期時間的格式
  129. ISOTIMEFORMAT = '%m/%d %H:%M:%S'
  130. t = datetime.datetime.now().strftime(ISOTIMEFORMAT)
  131. # 設定登入帳號密碼
  132. client.username_pw_set("aisky-server","aisky")
  133. # 設定連線資訊(IP, Port, 連線時間)
  134. client.connect("60.250.156.234", 1883, 60)
  135. payload = {"command": "F_InputtingBean"}
  136. print(json.dumps(payload),t)
  137. #要發布的主題和內容
  138. #自行測試用
  139. client.publish("AISKY/Coffee/MK-G/b8:27:eb:7e:24:78", json.dumps(payload))
  140. #正式測試用
  141. #client.publish("AISKY/Coffee/MK-G/b8:27:eb:b4:59:3e", json.dumps(payload))
  142. #time.sleep(5)
  143. #while True:
  144. conn = pymysql.connect(
  145. host='127.0.0.1',
  146. user='root',
  147. password='g53743001',
  148. db='coffee',
  149. charset='utf8'
  150. )
  151. #抓取清洗入料儲豆槽狀態
  152. cur1 = conn.cursor()
  153. sql1 = "SELECT `clean_Input_1`,`clean_Input_2` FROM `clean_container_status` ORDER BY `sn` DESC LIMIT 1 "
  154. cur1.execute(sql1)
  155. get_Clean_Input = cur1.fetchone()
  156. # 清洗入料儲豆槽1
  157. CI1 = get_Clean_Input[0]
  158. # 清洗入料儲豆槽2
  159. CI2 = get_Clean_Input[1]
  160. get_Clean_Input_status_N = [CI1, CI2]
  161. c = 1
  162. for i in range(len(get_Clean_Input_status_N)):
  163. c = i + 1
  164. p = 1
  165. if(get_Clean_Input_status_N[i] == 'CI_Waiting'):
  166. N = 'CI' + str(c)
  167. print(N + ' ' + get_Clean_Input_status_N[i] + ' 空桶等待')
  168. update_Clean_Input_status(c)
  169. time.sleep(1)
  170. elif(get_Clean_Input_status_N[i] == 'CI_InputtingBean'):
  171. print('CI' + str(c) + ' ' + get_Clean_Input_status_N[i] + ' 入豆中')
  172. send_mqtt()
  173. print('發送MQTT訊號通知', '採收豆子儲存槽' + ' 可出豆')
  174. break
  175. # 修改清洗入料槽狀態
  176. #update_dry_Input_status(f)
  177. # 修改採收豆子儲存槽狀態
  178. #update_Ferment_Output_status(p)
  179. #要判斷咖啡豆採收豆子儲存槽狀態
  180. # for b in range(len(get_Ferment_Output_status_N)):
  181. # p = b + 1
  182. # if (get_Ferment_Output_status_N[b] == 'FO_InputtingBean'):
  183. # print('FO' + str(p) + ' ' + get_Ferment_Output_status_N[b] + ' 入豆中')
  184. # elif (get_Ferment_Output_status_N[b] == 'FO_Waiting'):
  185. # print('FO' + str(p) + ' ' + get_Ferment_Output_status_N[b] + ' 空桶等待')
  186. # elif (get_Ferment_Output_status_N[b] == 'FO_Stand_by'):
  187. # print('FO' + str(p) + '待命')
  188. # elif (get_Ferment_Output_status_N[b] == 'FO_OutputtingBean'):
  189. # print('FO' + str(p) + ' ' + get_Ferment_Output_status_N[b] + ' 可出豆')
  190. # send_mqtt()
  191. # print('發送MQTT訊號通知', 'FO' + str(p) + ' 可出豆')
  192. # # 修改乾燥入料槽狀態
  193. # update_dry_Input_status(f)
  194. # # 修改發酵出料狀態
  195. # update_Ferment_Output_status(p)
  196. # break
  197. elif(get_Clean_Input_status_N[i] == 'CI_OutputtingBean'):
  198. print('CI' + str(c) + ' ' + get_Clean_Input_status_N[i] + ' 可出豆')
  199. #抓取清洗統槽狀態
  200. cur1 = conn.cursor()
  201. sql1 = "SELECT `Clean_Tank_1`, `Clean_Tank_2`, `Clean_Tank_3`, `Clean_Tank_4` " \
  202. "FROM `clean_container_status` ORDER BY `sn` DESC LIMIT 1"
  203. cur1.execute(sql1)
  204. get_clean_Tank = cur1.fetchone()
  205. # 清洗槽1-4號
  206. C1 = get_clean_Tank[0]
  207. C2 = get_clean_Tank[1]
  208. C3 = get_clean_Tank[2]
  209. C4 = get_clean_Tank[3]
  210. clean_Tank_status = [C1, C2, C3, C4]
  211. c_tank = 1
  212. for i_tank in range(len(clean_Tank_status)):
  213. c_tank = i_tank + 1
  214. if (clean_Tank_status[i_tank] == 'C_Waiting'):
  215. update_Dry_Tank_status1(d_tank)
  216. print('D' + str(d_tank) + '修改發酵桶槽狀態C_Waiting->C_InputtingBean')
  217. elif (clean_Tank_status[i_tank] == 'C_InputtingBean'):
  218. # 修改發酵桶槽狀態C_InputtingBean->C_Standby
  219. update_Clean_Tank_status(c_tank)
  220. print('C' + str(c_tank) + ' ' + clean_Tank_status[i_tank] + ' 入豆中')
  221. send_mqtt()
  222. break
  223. elif (clean_Tank_status[i_tank] == 'C_OutputtingBean'):
  224. print('C' + str(c_tank) + ' ' + clean_Tank_status[i_tank] + ' 可出豆')
  225. #抓取色選機狀態
  226. cur1 = conn.cursor()
  227. sql1 = "SELECT `ColorSelect_Tank_1`,`ColorSelect_Tank_2` FROM `clean_container_status` ORDER BY `sn` DESC LIMIT 1 "
  228. cur1.execute(sql1)
  229. get_ColorSelect_Tank = cur1.fetchone()
  230. # 色選機狀態1
  231. S1 = get_ColorSelect_Tank[0]
  232. # 色選機狀態2
  233. S2 = get_ColorSelect_Tank[1]
  234. get_ColorSelect_Tank_status = [S1, S2]
  235. s_Tank = 1
  236. for i_Output in range(len(get_ColorSelect_Tank_status)):
  237. s_Input = i_Output + 1
  238. if (get_ColorSelect_Tank_status[i_Output] == 'S_Waiting'):
  239. update_ColorSelect_Tank_status(s_Tank)
  240. print('S' + str(s_Tank) + ' 修改色選機狀態S_Waiting->S_InputtingBean')
  241. elif (get_ColorSelect_Tank_status[i_Output] == 'S_InputtingBean'):
  242. update_ColorSelect_Tank_status_Stand_by(s_Tank)
  243. print('修改' + 'S' + str(s_Tank) + '色選機狀態,並發送MQTT訊號。')
  244. send_mqtt()
  245. elif (get_ColorSelect_Tank_status[i_Output] == 'S_OutputtingBean'):
  246. print('修改' + 'S' + str(s_Tank) + '可出豆')
  247. cur1 = conn.cursor()
  248. sql1 = "SELECT `ColorSelect_Output_g1`,`ColorSelect_Output_g2` FROM `clean_container_status` ORDER BY `sn` DESC LIMIT 1 "
  249. cur1.execute(sql1)
  250. get_ColorSelect_Output = cur1.fetchone()
  251. # 色選出料儲豆槽1
  252. SO1 = get_ColorSelect_Output[0]
  253. # 色選出料儲豆槽2
  254. SO2 = get_ColorSelect_Output[1]
  255. get_ColorSelect_Output_status = [SO1, SO2]
  256. so_Output = 1
  257. for i_Output in range(len(get_ColorSelect_Output_status)):
  258. so_Tank = i_Output + 1
  259. if (get_ColorSelect_Output_status[i_Output] == 'SO_Waiting'):
  260. update_ColorSelect_Output_status(so_Output)
  261. print('SO' + str(so_Output) + ' 修改色選機狀態SO_Waiting->SO_InputtingBean')
  262. elif (get_ColorSelect_Output_status[i_Output] == 'SO_InputtingBean'):
  263. update_ColorSelect_Output_status_Stand_by(so_Output)
  264. print('修改' + 'SO' + str(so_Output) + '色選機狀態,並發送MQTT訊號。')
  265. send_mqtt()
  266. elif (get_ColorSelect_Output_status[i_Output] == 'SO_OutputtingBean'):
  267. print('修改' + 'SO' + str(so_Output) + '可出豆')
  268. #抓取脫皮機狀態
  269. cur1 = conn.cursor()
  270. sql1 = "SELECT `Peel_Tank_1`,`Peel_Tank_2` FROM `clean_container_status` ORDER BY `sn` DESC LIMIT 1 "
  271. cur1.execute(sql1)
  272. get_Peel_Tank = cur1.fetchone()
  273. # 脫皮狀態1
  274. P1 = get_Peel_Tank[0]
  275. # 脫皮狀態2
  276. P2 = get_Peel_Tank[1]
  277. get_Peel_Tank_status = [P1, P2]
  278. p_Output = 1
  279. for i_Output in range(len(get_Peel_Tank_status)):
  280. p_Output = i_Output + 1
  281. if (get_Peel_Tank_status[i_Output] == 'P_Waiting'):
  282. update_Peel_Tank_status(p_Output)
  283. print('P' + str(p_Output) + ' 修改脫皮機狀態P_Waiting->P_InputtingBean')
  284. elif (get_Peel_Tank_status[i_Output] == 'P_InputtingBean'):
  285. update_Peel_Tank_status_Stand_by(p_Output)
  286. print('修改' + 'P' + str(p_Output) + '脫皮機狀態,並發送MQTT訊號。')
  287. send_mqtt()
  288. elif (get_Peel_Tank_status[i_Output] == 'P_OutputtingBean'):
  289. print('修改' + 'P' + str(p_Output) + '可出豆')
  290. #抓取脫皮出要狀態
  291. cur1 = conn.cursor()
  292. sql1 = "SELECT `Peel_Output_1`,`Peel_Output_2` FROM `clean_container_status` ORDER BY `sn` DESC LIMIT 1 "
  293. cur1.execute(sql1)
  294. get_Peel_Output = cur1.fetchone()
  295. # 脫皮出料狀態1
  296. PO1 = get_Peel_Output[0]
  297. # 脫皮出料狀態2
  298. PO2 = get_Peel_Output[1]
  299. get_Peel_Output_status = [PO1, PO2]
  300. po_Output = 1
  301. for i_Output in range(len(get_Peel_Output_status)):
  302. po_Output = i_Output + 1
  303. if (get_Peel_Output_status[i_Output] == 'PO_Waiting'):
  304. update_Peel_Output_status(po_Output)
  305. print('PO' + str(po_Output) + ' 修改脫皮機出料狀態PO_Waiting->PO_InputtingBean')
  306. elif (get_Peel_Output_status[i_Output] == 'PO_InputtingBean'):
  307. update_Peel_Output_statusstatus_Stand_by(po_Output)
  308. print('修改' + 'PO' + str(po_Output) + '脫皮機出料狀態,並發送MQTT訊號。')
  309. send_mqtt()
  310. elif (get_Peel_Output_status[i_Output] == 'PO_Standby'):
  311. print('P' + str(po_Output) + ' 待命中')
  312. elif (get_Peel_Output_status[i_Output] == 'PO_Warning'):
  313. print('P' + str(po_Output) + ' 警告')
  314. elif (get_Peel_Tank_status[i_Output] == 'P_Standby'):
  315. print('P' + str(p_Output) + ' 待命中')
  316. elif (get_Peel_Tank_status[i_Output] == 'P_Warning'):
  317. print('P' + str(p_Output) + ' 警告')
  318. elif (get_ColorSelect_Output_status[i_Output] == 'SO_Standby'):
  319. print('SO' + str(so_Output) + ' 待命中')
  320. elif (get_ColorSelect_Output_status[i_Output] == 'SO_Warning'):
  321. print('SO' + str(so_Output) + ' 警告')
  322. elif (get_ColorSelect_Tank_status[i_Output] == 'S_Standby'):
  323. print('S' + str(s_Tank) + ' 待命中')
  324. elif (get_ColorSelect_Tank_status[i_Output] == 'S_Warning'):
  325. print('S' + str(s_Tank) + ' 警告')
  326. elif (clean_Tank_status[i_tank] == 'C_Standby'):
  327. print('C' + str(d_tank) + ' ' + clean_Tank_status[i_tank] + ' 待命')
  328. elif (clean_Tank_status[i_tank] == 'C_Warning'):
  329. print('C' + str(d_tank) + ' ' + clean_Tank_status[i_tank] + ' 警告')
  330. elif (clean_Tank_status[i_tank] == 'C_InputtingBean_Pause'):
  331. print('C' + str(c_tank) + ' ' + clean_Tank_status[i_tank] + ' 入豆暫停')
  332. elif (clean_Tank_status[i_tank] == 'C_InputtingBean_Finish'):
  333. print('C' + str(c_tank) + ' ' + clean_Tank_status[i_tank] + ' 入豆完成')
  334. elif (clean_Tank_status[i_tank] == 'C_RecycleWaterIn1'):
  335. print('C' + str(d_tank) + ' ' + clean_Tank_status[i_tank] + ' 乾燥中')
  336. elif (clean_Tank_status[i_tank] == 'C_Wash1'):
  337. print('C' + str(d_tank) + ' ' + clean_Tank_status[i_tank] + ' 清洗中')
  338. elif (clean_Tank_status[i_tank] == 'C_SewageOut1'):
  339. print('C' + str(d_tank) + ' ' + clean_Tank_status[i_tank] + ' 清洗中')
  340. elif (clean_Tank_status[i_tank] == 'C_RecycleWaterIn2'):
  341. print('C' + str(d_tank) + ' ' + clean_Tank_status[i_tank] + ' 乾燥中')
  342. elif (clean_Tank_status[i_tank] == 'C_Wash2'):
  343. print('C' + str(d_tank) + ' ' + clean_Tank_status[i_tank] + ' 清洗中')
  344. elif (clean_Tank_status[i_tank] == 'C_RecycleWaterOut2'):
  345. print('C' + str(d_tank) + ' ' + clean_Tank_status[i_tank] + ' 清洗中')
  346. elif (clean_Tank_status[i_tank] == 'C_RecycleWaterIn3'):
  347. print('C' + str(d_tank) + ' ' + clean_Tank_status[i_tank] + ' 乾燥中')
  348. elif (clean_Tank_status[i_tank] == 'C_Wash3'):
  349. print('C' + str(d_tank) + ' ' + clean_Tank_status[i_tank] + ' 清洗中')
  350. elif (clean_Tank_status[i_tank] == 'C_RecycleWaterOut3'):
  351. print('C' + str(d_tank) + ' ' + clean_Tank_status[i_tank] + ' 乾燥中')
  352. elif(get_Clean_Input_status_N[i] == 'CI_Standby'):
  353. print('CI' + str(f) + ' ' + get_Clean_Input_status_N[i] + '待命')
  354. elif(get_Clean_Input_status_N[i] == 'CI_Warning'):
  355. print('CI' + str(f) + ' ' + get_Clean_Input_status_N[i] + '警告')