dry_logic.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_dry():
  12. insert = conn.cursor()
  13. insert_status = "INSERT INTO `dry_container_status`(`Dry_Input_1`,`Dry_Input_2`," \
  14. "`Dry_Tank_1`,`Dry_Tank_2`, `Dry_Tank_3`, `Dry_Tank_4`, `Dry_Tank_5`," \
  15. "`Dry_Tank_6`, `Dry_Tank_7`,`Dry_Tank_8`, `Dry_Tank_9`, `Dry_Tank_10`," \
  16. "`Dry_Tank_11`, `Dry_Tank_12`, `Dry_Output_1`,`Dry_Output_2`)" \
  17. "SELECT `Dry_Input_1`,`Dry_Input_2`," \
  18. "`Dry_Tank_1`,`Dry_Tank_2`, `Dry_Tank_3`, `Dry_Tank_4`, `Dry_Tank_5`," \
  19. "`Dry_Tank_6`, `Dry_Tank_7`,`Dry_Tank_8`, `Dry_Tank_9`, `Dry_Tank_10`," \
  20. "`Dry_Tank_11`, `Dry_Tank_12`, `Dry_Output_1`,`Dry_Output_2`" \
  21. "FROM dry_container_status ORDER BY `datetime` DESC LIMIT 1"
  22. insert.execute(insert_status)
  23. conn.commit()
  24. def update_Dry_Input_status(f):
  25. insert_data_dry()
  26. update_status = conn.cursor()
  27. Dry_name = 'dry_Input_'
  28. re = "UPDATE `dry_container_status` SET " + Dry_name + str(f) + " = 'DI_InputtingBean' ORDER BY `datetime` DESC LIMIT 1 "
  29. update_status.execute(re)
  30. conn.commit()
  31. def update_dry_Input_status(f):
  32. insert_data_dry()
  33. update_status_dry = conn.cursor()
  34. dry_name = 'Dry_Input_'
  35. re_fer = "UPDATE `dry_container_status` SET "+dry_name+str(f)+"= 'DI_Stand_by' " \
  36. "ORDER BY `datetime` DESC LIMIT 1 "
  37. update_status_dry.execute(re_fer)
  38. conn.commit()
  39. def update_Ferment_Output_status(p):
  40. insert = conn.cursor()
  41. insert_status = "INSERT INTO `ferment_container_status`(`Ferment_Input_1`,`Ferment_Input_2`," \
  42. "`Ferment_Tank_1`,`Ferment_Tank_2`, `Ferment_Tank_3`, `Ferment_Tank_4`, `Ferment_Tank_5`," \
  43. "`Ferment_Tank_6`, `Ferment_Tank_7`,`Ferment_Tank_8`, `Ferment_Tank_9`, `Ferment_Tank_10`," \
  44. "`Ferment_Tank_11`, `Ferment_Tank_12`, `Ferment_Output_1`,`Ferment_Output_2`)" \
  45. "SELECT `Ferment_Input_1`, `Ferment_Input_2`," \
  46. "`Ferment_Tank_1`,`Ferment_Tank_2`, `Ferment_Tank_3`, `Ferment_Tank_4`, `Ferment_Tank_5`," \
  47. "`Ferment_Tank_6`, `Ferment_Tank_7`,`Ferment_Tank_8`, `Ferment_Tank_9`, `Ferment_Tank_10`," \
  48. "`Ferment_Tank_11`, `Ferment_Tank_12`, `Ferment_Output_1`,`Ferment_Output_2`" \
  49. "FROM ferment_container_status ORDER BY `datetime` DESC LIMIT 1"
  50. insert.execute(insert_status)
  51. conn.commit()
  52. update_status_Ferment = conn.cursor()
  53. Ferment_name = 'Ferment_Output_'
  54. re_ferment = "UPDATE `ferment_container_status` SET " + Ferment_name + str(p) + "= 'FO_Stand_by' " \
  55. "ORDER BY `datetime` DESC LIMIT 1 "
  56. update_status_Ferment.execute(re_ferment)
  57. conn.commit()
  58. #修改乾燥桶槽狀態:入豆中 D_Waiting改成D_InputtingBean
  59. def update_Dry_Tank_status1(d_tank):
  60. insert_data_dry()
  61. update_status_dry_Tank = conn.cursor()
  62. Dry_Tank_name = 'Dry_Tank_'
  63. re_dry_Tank = "UPDATE `dry_container_status` SET "+Dry_Tank_name+str(d_tank)+"= 'D_InputtingBean' " \
  64. "ORDER BY `datetime` DESC LIMIT 1 "
  65. update_status_dry_Tank.execute(re_dry_Tank)
  66. conn.commit()
  67. #修改乾燥桶槽狀態:D_InputtingBean改成D_Stand_by
  68. def update_Dry_Tank_status(d_tank):
  69. insert_data_dry()
  70. update_status_dry_Tank = conn.cursor()
  71. Dry_Tank_name = 'Dry_Tank_'
  72. re_fer_Tank = "UPDATE `dry_container_status` SET "+Dry_Tank_name+str(d_tank)+"= 'D_Stand_by' " \
  73. "ORDER BY `datetime` DESC LIMIT 1 "
  74. update_status_dry_Tank.execute(re_fer_Tank)
  75. conn.commit()
  76. #修改發酵出料儲豆槽狀態:入豆中 FO_Waiting改成FO_InputtingBean
  77. def update_Dry_Output_status(d_Output):
  78. insert_data_dry()
  79. update_status_dry = conn.cursor()
  80. Dry_name = 'Dry_Output_'
  81. re_dry = "UPDATE `dry_container_status` SET "+Dry_name+str(d_Output)+"= 'DO_InputtingBean' " \
  82. "ORDER BY `datetime` DESC LIMIT 1 "
  83. update_status_dry.execute(re_dry)
  84. conn.commit()
  85. def update_Dry_Output_status_Stand_by(d_Output):
  86. insert_data_dry()
  87. update_status_dry = conn.cursor()
  88. Dry_Output_name = 'Dry_Output_'
  89. re_dry = "UPDATE `dry_container_status` SET "+Dry_Output_name+str(d_Output)+"= 'DO_Stand_by' " \
  90. "ORDER BY `datetime` DESC LIMIT 1 "
  91. update_status_dry.execute(re_dry)
  92. conn.commit()
  93. #-----------------------------------------------------------------------
  94. #傳送MQTT訊號
  95. def send_mqtt():
  96. # 連線設定
  97. # 初始化地端程式
  98. client = mqtt.Client()
  99. # 設置日期時間的格式
  100. ISOTIMEFORMAT = '%m/%d %H:%M:%S'
  101. t = datetime.datetime.now().strftime(ISOTIMEFORMAT)
  102. # 設定登入帳號密碼
  103. client.username_pw_set("aisky-server","aisky")
  104. # 設定連線資訊(IP, Port, 連線時間)
  105. client.connect("60.250.156.234", 1883, 60)
  106. payload = {"command": "F_InputtingBean"}
  107. print(json.dumps(payload),t)
  108. #要發布的主題和內容
  109. #自行測試用
  110. client.publish("AISKY/Coffee/MK-G/b8:27:eb:7e:24:78", json.dumps(payload))
  111. #正式測試用
  112. #client.publish("AISKY/Coffee/MK-G/b8:27:eb:b4:59:3e", json.dumps(payload))
  113. #time.sleep(5)
  114. #while True:
  115. conn = pymysql.connect(
  116. host='127.0.0.1',
  117. user='root',
  118. password='g53743001',
  119. db='coffee',
  120. charset='utf8'
  121. )
  122. cur1 = conn.cursor()
  123. sql1 = "SELECT `Dry_Input_1`,`Dry_Input_2` FROM `dry_container_status` ORDER BY `sn` DESC LIMIT 1 "
  124. cur1.execute(sql1)
  125. get_Dry_Input = cur1.fetchone()
  126. # 乾燥入料儲豆槽1
  127. DI1 = get_Dry_Input[0]
  128. # 乾燥入料儲豆槽2
  129. DI2 = get_Dry_Input[1]
  130. get_Dry_Input_status_N = [DI1, DI2]
  131. f = 1
  132. for i in range(len(get_Dry_Input_status_N)):
  133. f = i + 1
  134. cur = conn.cursor()
  135. sql = "SELECT `Ferment_Output_1`,`Ferment_Output_2` FROM `ferment_container_status` ORDER BY `sn` DESC LIMIT 1"
  136. cur.execute(sql)
  137. get_Ferment_Output_status = cur.fetchone()
  138. # 發酵出料儲豆槽1
  139. FO1 = get_Ferment_Output_status[0]
  140. # 發酵出料儲豆槽2
  141. FO2 = get_Ferment_Output_status[1]
  142. get_Ferment_Output_status_N = [FO1, FO2]
  143. p = 1
  144. if(get_Dry_Input_status_N[i] == 'DI_Waiting'):
  145. N = 'DI' + str(f)
  146. print(N + ' ' + get_Dry_Input_status_N[i] + ' 空桶等待')
  147. update_Dry_Input_status(f)
  148. time.sleep(1)
  149. elif(get_Dry_Input_status_N[i] == 'DI_InputtingBean'):
  150. print('DI' + str(f) + ' ' + get_Dry_Input_status_N[i] + ' 入豆中')
  151. for b in range(len(get_Ferment_Output_status_N)):
  152. p = b + 1
  153. if (get_Ferment_Output_status_N[b] == 'FO_InputtingBean'):
  154. print('FO' + str(p) + ' ' + get_Ferment_Output_status_N[b] + ' 入豆中')
  155. elif (get_Ferment_Output_status_N[b] == 'FO_Waiting'):
  156. print('FO' + str(p) + ' ' + get_Ferment_Output_status_N[b] + ' 空桶等待')
  157. elif (get_Ferment_Output_status_N[b] == 'FO_Stand_by'):
  158. print('FO' + str(p) + '待命')
  159. elif (get_Ferment_Output_status_N[b] == 'FO_OutputtingBean'):
  160. print('FO' + str(p) + ' ' + get_Ferment_Output_status_N[b] + ' 可出豆')
  161. send_mqtt()
  162. print('發送MQTT訊號通知', 'FO' + str(p) + ' 可出豆')
  163. # 修改乾燥入料槽狀態
  164. update_dry_Input_status(f)
  165. # 修改發酵出料狀態
  166. update_Ferment_Output_status(p)
  167. break
  168. elif(get_Dry_Input_status_N[i] == 'DI_OutputtingBean'):
  169. print('DI' + str(f) + ' ' + get_Dry_Input_status_N[i] + ' 可出豆')
  170. cur1 = conn.cursor()
  171. sql1 = "SELECT `Dry_Tank_1`, `Dry_Tank_2`, `Dry_Tank_3`, `Dry_Tank_4`, `Dry_Tank_5`, `Dry_Tank_6`, " \
  172. "`Dry_Tank_7`, `Dry_Tank_8`, `Dry_Tank_9`, `Dry_Tank_10`, `Dry_Tank_11`, `Dry_Tank_12` " \
  173. "FROM `dry_container_status` ORDER BY `sn` DESC LIMIT 1 "
  174. cur1.execute(sql1)
  175. get_dry_Tank = cur1.fetchone()
  176. # print(get_Ferment_Tank)
  177. # 乾燥槽1-12號
  178. D1 = get_dry_Tank[0]
  179. D2 = get_dry_Tank[1]
  180. D3 = get_dry_Tank[2]
  181. D4 = get_dry_Tank[3]
  182. D5 = get_dry_Tank[4]
  183. D6 = get_dry_Tank[5]
  184. D7 = get_dry_Tank[6]
  185. D8 = get_dry_Tank[7]
  186. D9 = get_dry_Tank[8]
  187. D10 = get_dry_Tank[9]
  188. D11 = get_dry_Tank[10]
  189. D12 = get_dry_Tank[11]
  190. dry_Tank_status = [F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12]
  191. d_tank = 1
  192. for i_tank in range(len(dry_Tank_status)):
  193. d_tank = i_tank + 1
  194. if (dry_Tank_status[i_tank] == 'D_Waiting'):
  195. update_Dry_Tank_status1(d_tank)
  196. print('D' + str(d_tank) + '修改發酵桶槽狀態D_Waiting->D_InputtingBean')
  197. elif (dry_Tank_status[i_tank] == 'D_InputtingBean'):
  198. # 修改發酵桶槽狀態F_InputtingBean->F_Standby
  199. update_Dry_Tank_status(d_tank)
  200. print('D' + str(d_tank) + ' ' + dry_Tank_status[i_tank] + ' 入豆中')
  201. send_mqtt()
  202. break
  203. elif (dry_Tank_status[i_tank] == 'D_OutputtingBean'):
  204. cur1 = conn.cursor()
  205. sql1 = "SELECT `Dry_Output_1`, `Dry_Output_2` FROM `dry_container_status` ORDER BY `sn` DESC LIMIT 1 "
  206. cur1.execute(sql1)
  207. get_dry_Output = cur1.fetchone()
  208. # 乾燥出料儲豆槽1
  209. DO1 = get_dry_Output[0]
  210. # 乾燥出料儲豆槽2
  211. DO2 = get_dry_Output[1]
  212. get_dry_Output_status = [DO1, DO2]
  213. d_Output = 1
  214. for i_Output in range(len(get_dry_Output_status)):
  215. d_Output = i_Output + 1
  216. if (get_dry_Output_status[i_Output] == 'DO_Waiting'):
  217. update_Dry_Output_status(d_Output)
  218. print('DO' + str(d_Output) + ' 修改發酵桶槽狀態DO_Waiting->DO_InputtingBean')
  219. elif (get_dry_Output_status[i_Output] == 'DO_InputtingBean'):
  220. update_Dry_Output_status_Stand_by(d_Output)
  221. print('修改' + 'DO' + str(d_Output) + '出料槽狀態,並發送MQTT訊號。')
  222. send_mqtt()
  223. elif (get_dry_Output_status[i_Output] == 'DO_OutputtingBean'):
  224. print('修改' + 'DO' + str(d_Output) + '可出豆')
  225. elif (get_dry_Output_status[i_Output] == 'DO_Standby'):
  226. print('DO' + str(d_Output) + ' 待命中')
  227. elif (get_dry_Output_status[i_Output] == 'DO_Warning'):
  228. print('DO' + str(d_Output) + ' 待命中')
  229. elif (dry_Tank_status[i_tank] == 'D_Standby'):
  230. print('D' + str(d_tank) + ' ' + dry_Tank_status[i_tank] + ' 待命')
  231. elif (dry_Tank_status[i_tank] == 'D_Warning'):
  232. print('D' + str(d_tank) + ' ' + dry_Tank_status[i_tank] + ' 警告')
  233. elif (dry_Tank_status[i_tank] == 'D_Drying'):
  234. print('D' + str(d_tank) + ' ' + dry_Tank_status[i_tank] + ' 乾燥中')
  235. elif (dry_Tank_status[i_tank] == 'D_Cleaning'):
  236. print('D' + str(d_tank) + ' ' + dry_Tank_status[i_tank] + ' 清洗中')
  237. elif (dry_Tank_status[i_tank] == 'D_InputtingBean_Pause'):
  238. print('D' + str(d_tank) + ' ' + dry_Tank_status[i_tank] + ' 入豆暫停')
  239. elif (dry_Tank_status[i_tank] == 'D_InputtingBean_Finish'):
  240. print('D' + str(d_tank) + ' ' + dry_Tank_status[i_tank] + ' 入豆完成')
  241. elif(get_Dry_Input_status_N[i] == 'DI_Standby'):
  242. print('DI' + str(f) + ' ' + get_Ferment_Output_status_N[i] + '待命')
  243. elif(get_Dry_Input_status_N[i] == 'DI_Warning'):
  244. print('DI' + str(f) + ' ' + get_Ferment_Output_status_N[i] + '警告')