1214.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import socket, threading
  2. import pymysql
  3. import time
  4. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5. s.bind(('192.168.51.102', 1))
  6. s.listen(5)
  7. c, a = s.accept()
  8. print('server start at: %s:%s' % a)
  9. print('wait for connection...')
  10. def update_Ferment_Input_status(f,r):
  11. con = pymysql.connect(host='60.250.156.230', user='user', password='Gold@53743001', db='CoffeeManage',
  12. charset='utf8')
  13. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  14. insert = con.cursor()
  15. insert_status = "INSERT INTO `ferment_container_status`(`Ferment_Input_1`,`Ferment_Input_2`," \
  16. "`Ferment_Tank_1`,`Ferment_Tank_2`, `Ferment_Tank_3`, `Ferment_Tank_4`, `Ferment_Tank_5`," \
  17. "`Ferment_Tank_6`, `Ferment_Tank_7`,`Ferment_Tank_8`, `Ferment_Tank_9`, `Ferment_Tank_10`," \
  18. "`Ferment_Tank_11`, `Ferment_Tank_12`, `Ferment_Output_1`,`Ferment_Output_2`)" \
  19. "SELECT `Ferment_Input_1`, `Ferment_Input_2`," \
  20. "`Ferment_Tank_1`,`Ferment_Tank_2`, `Ferment_Tank_3`, `Ferment_Tank_4`, `Ferment_Tank_5`," \
  21. "`Ferment_Tank_6`, `Ferment_Tank_7`,`Ferment_Tank_8`, `Ferment_Tank_9`, `Ferment_Tank_10`," \
  22. "`Ferment_Tank_11`, `Ferment_Tank_12`, `Ferment_Output_1`,`Ferment_Output_2`" \
  23. "FROM ferment_container_status ORDER BY `datetime` DESC LIMIT 1"
  24. insert.execute(insert_status)
  25. con.commit()
  26. time.sleep(1)
  27. update_status = con.cursor()
  28. Ferment_name = 'Ferment_Tank_'
  29. re = "UPDATE `ferment_container_status` SET " + Ferment_name + str(f) + "='"+r+"' ORDER BY `datetime` DESC LIMIT 1 "
  30. update_status.execute(re)
  31. con.commit()
  32. class A(threading.Thread):
  33. def __init__(self):
  34. threading.Thread.__init__(self)
  35. def run(self):
  36. while True:
  37. b = c.recv(1024)
  38. msg = b.decode(encoding='utf8')
  39. print('send: ' + msg)
  40. m = b.decode(encoding='utf8').split('\n')
  41. for j in range(0, (len(b.decode().split('\n')) - 1)):
  42. if m[j].split('= ')[0] == "M1_SonicESMUS07":
  43. con = pymysql.connect(host='60.250.156.230', user='user', password='Gold@53743001',
  44. db='CoffeeManage',
  45. charset='utf8')
  46. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  47. insert = con.cursor()
  48. insert_status = "INSERT INTO `ferment_tank_UltraSonic`(`datetime`,`tank_num`,`UltraSonic`) VALUES ('%s', '%s', '%s')" % (
  49. datetime, 'F1', m[j].split('= ')[1])
  50. insert.execute(insert_status)
  51. con.commit()
  52. if m[j].split('= ')[0] == "M4_PH":
  53. con = pymysql.connect(host='60.250.156.230', user='user', password='Gold@53743001',
  54. db='CoffeeManage',
  55. charset='utf8')
  56. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  57. insert = con.cursor()
  58. insert_status = "INSERT INTO `ferment_tank_PH`(`datetime`,`tank_num`,`PH`) VALUES ('%s', '%s', '%s')" % (
  59. datetime, 'F1', m[j].split('= ')[1])
  60. insert.execute(insert_status)
  61. con.commit()
  62. if m[j].split('= ')[0] == "M17_SHT11":
  63. con = pymysql.connect(host='60.250.156.230', user='user', password='Gold@53743001',
  64. db='CoffeeManage',
  65. charset='utf8')
  66. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  67. insert = con.cursor()
  68. insert_status = "INSERT INTO `ferment_tank_SHT11`(`datetime`,`tank_num`,`SHT11_Temp`,`SHT11_Humidity`) VALUES ('%s', '%s', '%s', '%s')" % (
  69. datetime, 'F1', m[j].split('= ')[1], 60)
  70. insert.execute(insert_status)
  71. con.commit()
  72. if m[j].split('= ')[0] == "M19_WATERLEVEL":
  73. con = pymysql.connect(host='60.250.156.230', user='user', password='Gold@53743001',
  74. db='CoffeeManage',
  75. charset='utf8')
  76. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  77. insert = con.cursor()
  78. insert_status = "INSERT INTO `ferment_tank_WaterLevel`(`datetime`,`tank_num`,`WaterLevel`) VALUES ('%s', '%s', '%s')" % (
  79. datetime, 'F1', m[j].split('= ')[1])
  80. insert.execute(insert_status)
  81. con.commit()
  82. if b.decode().split('\n')[j] == "F_InputtingBean":
  83. update_Ferment_Input_status(1, 'F_InputtingBean')
  84. if b.decode().split('\n')[j] == "F_Fermenting":
  85. update_Ferment_Input_status(1, 'F_Fermenting')
  86. if b.decode().split('\n')[j] == "F_OutputtingBean":
  87. update_Ferment_Input_status(1, 'F_OutputtingBean')
  88. if b.decode().split('\n')[j] == "F_InputtingBean_Finish":
  89. update_Ferment_Input_status(1, 'F_InputtingBean_Finish')
  90. if b.decode().split('\n')[j] == "F_Fermenting_Finish":
  91. update_Ferment_Input_status(1, 'F_Fermenting_Finish')
  92. if b.decode().split('\n')[j] == "F_Waiting":
  93. update_Ferment_Input_status(1, 'F_Waiting')
  94. e = A()
  95. e.start()
  96. while True:
  97. d = input('STATUS::')
  98. c.send(d.encode(encoding='utf8'))