spaceManage.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 pickUpPark(self, dict):
  54. with self.lock:
  55. cursor = self.conn.cursor()
  56. SQL = "select primary_key from vehicle where car_number='%s'" % (dict["car_number"])
  57. cursor.execute(SQL)
  58. self.conn.commit()
  59. results = cursor.fetchall()
  60. if len(results) == 1:
  61. key = ''.join(results[0])
  62. table = msg.pick_table()
  63. table.primary_key = key
  64. self.g_sender.publish("command_ex", "user_command_port", tf.MessageToString(table, as_utf8=True))
  65. QMessageBox.question(None, '提示', '取车消息发送成功!',
  66. QMessageBox.Ok) # "退出"代表的是弹出框的标题,"你确认退出.."表示弹出框的内容
  67. else:
  68. QMessageBox.warning(None, '警告', '查询结果有误,请检查数据库!',
  69. QMessageBox.Ok) # "退出"代表的是弹出框的标题,"你确认退出.."表示弹出框的内容
  70. cursor.close()
  71. def run(self):
  72. while self.isClose is not True:
  73. try:
  74. self.conn.ping() # 采用连接对象的ping()函数检测连接状态
  75. # print('connect MySql-OK statu=' + str(self.statu))
  76. self.statu = True
  77. except:
  78. self.statu = False
  79. # print('connect MySql-ERROR statu=' + str(self.statu))
  80. self.connect()
  81. time.sleep(0.5)
  82. continue
  83. with self.lock:
  84. # 获取一个光标
  85. cursor = self.conn.cursor()
  86. SQL = "select * from space"
  87. t_a_unit_park_dict = {}
  88. t_b_unit_park_dict = {}
  89. t_c_unit_park_dict = {}
  90. results = cursor.execute(SQL)
  91. self.conn.commit()
  92. # 结果数量大于0
  93. if results > 0:
  94. parkspace = cursor.fetchall()
  95. column = [index[0] for index in cursor.description] # 列名
  96. for row, i in zip(parkspace, range(results)):
  97. if row[5] == 1:
  98. t_a_unit_park_dict[i % 78] = dict(zip(column, row))
  99. if row[5] == 2:
  100. t_b_unit_park_dict[i % 78] = dict(zip(column, row))
  101. if row[5] == 3:
  102. t_c_unit_park_dict[i % 78] = dict(zip(column, row))
  103. if self.a_unit_park_dict != t_a_unit_park_dict:
  104. self.a_unit_park_dict = t_a_unit_park_dict
  105. self.isUpdate_A = True
  106. elif self.b_unit_park_dict != t_b_unit_park_dict:
  107. self.b_unit_park_dict = t_b_unit_park_dict
  108. self.isUpdate_B = True
  109. elif self.c_unit_park_dict != t_c_unit_park_dict:
  110. self.c_unit_park_dict = t_c_unit_park_dict
  111. self.isUpdate_C = True
  112. SQL = "select * from command_queue where type =2 and (statu=1 or statu=2)"
  113. t_command_dict = {}
  114. results = cursor.execute(SQL)
  115. self.conn.commit()
  116. if results > 0:
  117. command_queue = cursor.fetchall()
  118. column = [index[0] for index in cursor.description] # 列名
  119. t_command_dict = [dict(zip(column, row)) for row in command_queue] # row是数据库返回的一条一条记录,其中的每一天和column写成字典,最后就是字典数组
  120. if self.command_queue_dict != t_command_dict:
  121. self.command_queue_dict = t_command_dict
  122. self.commandIsUpdate = True
  123. # 关闭光标
  124. cursor.close()
  125. # print("------------------------------")
  126. time.sleep(0.5)