db_operation.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import threading
  2. import time
  3. import mysqlhelper
  4. class DBOperation(threading.Thread):
  5. def __init__(self):
  6. threading.Thread.__init__(self)
  7. self._space_dict = {1:{},2:{},3:{}}
  8. self._command_dict = {1:{},2:{},3:{}}
  9. self._db = mysqlhelper.MySqLHelper()
  10. self._isClose = False
  11. def close(self):
  12. self._isClose = True
  13. def query_command_in_unit(self, unit):
  14. sql = "select * from command_queue WHERE unit=%s"
  15. return self._db.selectall(sql, unit)
  16. def query_command_all(self):
  17. sql = "select * from command_queue"
  18. return self._db.selectall(sql)
  19. def query_space_in_unit(self,unit):
  20. sql = "select * from space WHERE unit=%s"
  21. return self._db.selectall(sql,unit)
  22. def query_space_all(self):
  23. sql = "select * from space"
  24. return self._db.selectall(sql)
  25. def update_space_status(self,space_id,statu):
  26. sql = "update space set statu=%s where id=%s"
  27. return self._db.update(sql,(statu,space_id))
  28. def clear_space_data(self,space_id):
  29. sql = "update space set car_number=NULL where id=%s"
  30. return self._db.update(sql,space_id)
  31. def query_vehicle_primary_key(self,car_number):
  32. sql = "select primary_key from vehicle where car_number=%s"
  33. return self._db.selectall(sql,car_number)
  34. def delete_command(self,car_number):
  35. sql = "delete from command_queue where car_number=%s"
  36. return self._db.delete(sql,car_number)
  37. def get_space_dict(self):
  38. return self._space_dict
  39. def get_command_dict(self):
  40. return self._command_dict
  41. def run(self):
  42. while not self._isClose:
  43. for unit in range(1,4):
  44. res = self.query_space_in_unit(unit)
  45. if len(res) > 0 and self._space_dict[unit] != res:
  46. self._space_dict[unit] = res
  47. for unit in range(1,4):
  48. res = self.query_command_in_unit(unit)
  49. if self._command_dict[unit] != res:
  50. self._command_dict[unit] = res
  51. time.sleep(0.001)
  52. # class ParkManage(threading.Thread):
  53. # def __init__(self, ip, port, database, user, password, sender):
  54. # threading.Thread.__init__(self)
  55. # self.conn = None
  56. # self.ip = ip
  57. # self.port = port
  58. # self.database = database
  59. # self.user = user
  60. # self.password = password
  61. #
  62. #
  63. # self.lock = threading.Lock()
  64. #
  65. # self.a_unit_park_list = {}
  66. # self.b_unit_park_list = {}
  67. # self.c_unit_park_list = {}
  68. #
  69. # self.command_queue_dict = {}
  70. # self.a_command_queue_list = {}
  71. # self.b_command_queue_list = {}
  72. # self.c_command_queue_list = {}
  73. #
  74. #
  75. # self.my_sql_str = queue.Queue()
  76. #
  77. #
  78. #
  79. # self.isClose = False
  80. # self.g_sender = sender
  81. #
  82. # self.db_statu_callback = None
  83. # self.command_update_callback = None
  84. # self.space_update_callback = None
  85. #
  86. # def setDbStatuCallback(self, callback):
  87. # self.db_statu_callback = callback
  88. # def setCommandUpdateCallback(self,callback):
  89. # self.command_update_callback = callback
  90. #
  91. # def setSpaceUpdateCallback(self, callback):
  92. # self.space_update_callback = callback
  93. # def connect(self):
  94. # try:
  95. # self.conn = pymysql.connect(host=self.ip, port=self.port, database=self.database, charset="utf8",
  96. # user=self.user, passwd=self.password)
  97. # return True
  98. # except:
  99. # return False
  100. # def putSql(self,SQL):
  101. # self.my_sql_str.put(SQL)
  102. #
  103. # def updatePark(self, dict, statu):
  104. # self.lock.acquire()
  105. #
  106. # cursor = self.conn.cursor()
  107. # SQL = "update space set statu=%d where id=%d" % (statu, dict["id"])
  108. # cursor.execute(SQL)
  109. # print(SQL)
  110. # self.conn.commit()
  111. # cursor.close()
  112. # self.lock.release()
  113. #
  114. # def clearPark(self, dict):
  115. # self.lock.acquire()
  116. #
  117. # cursor = self.conn.cursor()
  118. # SQL = "update space set car_number=NULL where id=%d" % (dict["id"])
  119. # cursor.execute(SQL)
  120. # print(SQL)
  121. # self.conn.commit()
  122. # cursor.close()
  123. # self.lock.release()
  124. #
  125. # def processDelete(self, dict):
  126. # self.lock.acquire()
  127. # cursor = self.conn.cursor()
  128. # SQL = "delete from command_queue where car_number='%s';" % (dict["car_number"])
  129. # cursor.execute(SQL)
  130. # print(SQL)
  131. # self.conn.commit()
  132. # cursor.close()
  133. # self.lock.release()
  134. #
  135. # def pickUpPark(self, dict):
  136. # self.lock.acquire()
  137. #
  138. # cursor = self.conn.cursor()
  139. # SQL = "select primary_key from vehicle where car_number='%s'" % (dict["car_number"])
  140. # cursor.execute(SQL)
  141. # self.conn.commit()
  142. # results = cursor.fetchall()
  143. # cursor.close()
  144. #
  145. # self.lock.release()
  146. #
  147. # if len(results) == 1:
  148. # key = ''.join(results[0])
  149. # table = msg.pick_table()
  150. # table.primary_key = key
  151. # self.g_sender.publish("command_ex", "user_command_port", tf.MessageToString(table, as_utf8=True))
  152. # QMessageBox.question(None, '提示', '取车消息发送成功!',
  153. # QMessageBox.Ok) # "退出"代表的是弹出框的标题,"你确认退出.."表示弹出框的内容
  154. #
  155. # else:
  156. # QMessageBox.warning(None, '警告', '查询结果有误,请检查数据库!',
  157. # QMessageBox.Ok) # "退出"代表的是弹出框的标题,"你确认退出.."表示弹出框的内容
  158. # def close(self):
  159. # self.isClose = True
  160. # def run(self):
  161. # while self.isClose is not True:
  162. # try:
  163. # self.conn.ping() # 采用连接对象的ping()函数检测连接状态
  164. # self.db_statu_callback(True)
  165. # except:
  166. # self.db_statu_callback(False)
  167. # self.connect()
  168. # time.sleep(0.5)
  169. # continue
  170. # # 获取一个光标
  171. # cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
  172. # SQL = "select * from space"
  173. # t_a_unit_park_list = []
  174. # t_b_unit_park_list = []
  175. # t_c_unit_park_list = []
  176. # results = cursor.execute(SQL)
  177. #
  178. #
  179. # # 结果数量大于0
  180. # if results > 0:
  181. # parkspace = cursor.fetchall()
  182. # for space in parkspace:
  183. # if space["unit"] == 1:
  184. # t_a_unit_park_list.append(space)
  185. # if space["unit"] == 2:
  186. # t_b_unit_park_list.append(space)
  187. # if space["unit"] == 3:
  188. # t_c_unit_park_list.append(space)
  189. #
  190. # if self.a_unit_park_list != t_a_unit_park_list:
  191. # self.a_unit_park_list = t_a_unit_park_list
  192. # self.space_update_callback(self.a_unit_park_list, 'A')
  193. #
  194. # if self.b_unit_park_list != t_b_unit_park_list:
  195. # self.b_unit_park_list = t_b_unit_park_list
  196. # self.space_update_callback(self.b_unit_park_list, 'B')
  197. #
  198. # if self.c_unit_park_list != t_c_unit_park_list:
  199. # self.c_unit_park_list = t_c_unit_park_list
  200. # self.space_update_callback(self.c_unit_park_list, 'C')
  201. #
  202. # SQL = "select * from command_queue"
  203. # t_a_command_list = []
  204. # t_b_command_list = []
  205. # t_c_command_list = []
  206. # results = cursor.execute(SQL)
  207. # if results > 0:
  208. # command_queue = cursor.fetchall()
  209. # self.command_queue_dict = command_queue
  210. # for command in command_queue:
  211. # if command["unit"] == 1:
  212. # t_a_command_list.append(command)
  213. # if command["unit"] == 2:
  214. # t_a_command_list.append(command)
  215. # if command["unit"] == 3:
  216. # t_a_command_list.append(command)
  217. #
  218. # if self.a_command_queue_list != t_a_command_list:
  219. # self.a_command_queue_list = t_a_command_list
  220. # self.command_update_callback(self.a_command_queue_list, 'A')
  221. #
  222. # elif self.b_command_queue_list != t_b_command_list:
  223. # self.b_command_queue_list = t_b_command_list
  224. # self.command_update_callback(self.b_command_queue_list, 'B')
  225. #
  226. # elif self.c_command_queue_list != t_c_command_list:
  227. # self.c_command_queue_list = t_c_command_list
  228. # self.command_update_callback(self.c_command_queue_list, 'C')
  229. #
  230. # if self.my_sql_str.qsize() > 0:
  231. # SQL = self.my_sql_str.get(False)
  232. # cursor.execute(SQL)
  233. # self.conn.commit()
  234. #
  235. # # 关闭光标
  236. # cursor.close()
  237. #
  238. # # print("------------------------------")
  239. # time.sleep(0.001)