spaceManage.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import threading
  2. import time
  3. from PyQt5.QtWidgets import QMessageBox
  4. import pymysql
  5. import google.protobuf.text_format as tf
  6. import message.message_pb2 as msg
  7. class ParkManage(threading.Thread):
  8. def __init__(self, ip, port, database, user, password, sender):
  9. threading.Thread.__init__(self)
  10. self.conn = None
  11. self.ip = ip
  12. self.port = port
  13. self.database = database
  14. self.user = user
  15. self.password = password
  16. self.lock = threading.Lock()
  17. self.command_queue_dict = None
  18. self.commandIsUpdate = False
  19. self.a_unit_park_dict = {}
  20. self.b_unit_park_dict = {}
  21. self.c_unit_park_dict = {}
  22. self.isClose = False
  23. self.statu = False
  24. self.isUpdate_A = False
  25. self.isUpdate_B = False
  26. self.isUpdate_C = False
  27. self.g_sender = sender
  28. def connect(self):
  29. try:
  30. self.conn = pymysql.connect(host=self.ip, port=self.port, database=self.database, charset="utf8",
  31. user=self.user, passwd=self.password)
  32. return True
  33. except:
  34. return False
  35. def updatePark(self, dict, statu):
  36. self.lock.acquire()
  37. cursor = self.conn.cursor()
  38. SQL = "update space set statu=%d where id=%d" % (statu, dict["id"])
  39. cursor.execute(SQL)
  40. print(SQL)
  41. self.conn.commit()
  42. cursor.close()
  43. self.lock.release()
  44. def clearPark(self, dict):
  45. self.lock.acquire()
  46. cursor = self.conn.cursor()
  47. SQL = "update space set car_number=NULL where id=%d" % (dict["id"])
  48. cursor.execute(SQL)
  49. print(SQL)
  50. self.conn.commit()
  51. cursor.close()
  52. self.lock.release()
  53. def processDelete(self, dict):
  54. self.lock.acquire()
  55. cursor = self.conn.cursor()
  56. SQL = "delete from command_queue where car_number='%s';" % (dict["car_number"])
  57. cursor.execute(SQL)
  58. print(SQL)
  59. self.conn.commit()
  60. cursor.close()
  61. self.lock.release()
  62. def pickUpPark(self, dict):
  63. with self.lock:
  64. cursor = self.conn.cursor()
  65. SQL = "select primary_key from vehicle where car_number='%s'" % (dict["car_number"])
  66. cursor.execute(SQL)
  67. self.conn.commit()
  68. results = cursor.fetchall()
  69. if len(results) == 1:
  70. key = ''.join(results[0])
  71. table = msg.pick_table()
  72. table.primary_key = key
  73. self.g_sender.publish("command_ex", "user_command_port", tf.MessageToString(table, as_utf8=True))
  74. QMessageBox.question(None, '提示', '取车消息发送成功!',
  75. QMessageBox.Ok) # "退出"代表的是弹出框的标题,"你确认退出.."表示弹出框的内容
  76. else:
  77. QMessageBox.warning(None, '警告', '查询结果有误,请检查数据库!',
  78. QMessageBox.Ok) # "退出"代表的是弹出框的标题,"你确认退出.."表示弹出框的内容
  79. cursor.close()
  80. def close(self):
  81. self.isClose = True
  82. def run(self):
  83. while self.isClose is not True:
  84. try:
  85. self.conn.ping() # 采用连接对象的ping()函数检测连接状态
  86. # print('connect MySql-OK statu=' + str(self.statu))
  87. self.statu = True
  88. except:
  89. self.statu = False
  90. # print('connect MySql-ERROR statu=' + str(self.statu))
  91. self.connect()
  92. time.sleep(0.5)
  93. continue
  94. with self.lock:
  95. # 获取一个光标
  96. cursor = self.conn.cursor()
  97. SQL = "select * from space"
  98. t_a_unit_park_dict = {}
  99. t_b_unit_park_dict = {}
  100. t_c_unit_park_dict = {}
  101. results = cursor.execute(SQL)
  102. self.conn.commit()
  103. # 结果数量大于0
  104. if results > 0:
  105. parkspace = cursor.fetchall()
  106. column = [index[0] for index in cursor.description] # 列名
  107. for row, i in zip(parkspace, range(results)):
  108. if row[5] == 1:
  109. t_a_unit_park_dict[i % 78] = dict(zip(column, row))
  110. if row[5] == 2:
  111. t_b_unit_park_dict[i % 78] = dict(zip(column, row))
  112. if row[5] == 3:
  113. t_c_unit_park_dict[i % 78] = dict(zip(column, row))
  114. if self.a_unit_park_dict != t_a_unit_park_dict:
  115. self.a_unit_park_dict = t_a_unit_park_dict
  116. self.isUpdate_A = True
  117. elif self.b_unit_park_dict != t_b_unit_park_dict:
  118. self.b_unit_park_dict = t_b_unit_park_dict
  119. self.isUpdate_B = True
  120. elif self.c_unit_park_dict != t_c_unit_park_dict:
  121. self.c_unit_park_dict = t_c_unit_park_dict
  122. self.isUpdate_C = True
  123. # SQL = "select * from command_queue where type =2 and (statu=1 or statu=2)"
  124. SQL = "select * from command_queue"
  125. t_command_dict = {}
  126. results = cursor.execute(SQL)
  127. self.conn.commit()
  128. if results > 0:
  129. command_queue = cursor.fetchall()
  130. column = [index[0] for index in cursor.description] # 列名
  131. t_command_dict = [dict(zip(column, row)) for row in command_queue] # row是数据库返回的一条一条记录,其中的每一天和column写成字典,最后就是字典数组
  132. if self.command_queue_dict != t_command_dict:
  133. self.command_queue_dict = t_command_dict
  134. self.commandIsUpdate = True
  135. # 关闭光标
  136. cursor.close()
  137. # print("------------------------------")
  138. time.sleep(0.5)