db_operation.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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_in_unit_and_type(self, unit,type):
  17. sql = "select * from command_queue WHERE unit=%s and type=%s ORDER BY statu DESC"
  18. return self._db.selectall(sql, (unit,type))
  19. def query_command_all(self):
  20. sql = "select * from command_queue"
  21. return self._db.selectall(sql)
  22. def query_space_in_unit(self,unit):
  23. sql = "select * from space WHERE unit=%s"
  24. return self._db.selectall(sql,unit)
  25. def query_space_all(self):
  26. sql = "select * from space"
  27. return self._db.selectall(sql)
  28. def update_space_status(self,space_id,statu):
  29. sql = "update space set statu=%s where id=%s"
  30. return self._db.update(sql,(statu,space_id))
  31. def clear_space_data(self,space_id):
  32. sql = "update space set car_number=NULL where id=%s"
  33. return self._db.update(sql,space_id)
  34. def query_vehicle_primary_key(self,car_number):
  35. sql = "select primary_key from vehicle where car_number=%s"
  36. return self._db.selectall(sql,car_number)
  37. def delete_command(self,car_number):
  38. sql = "delete from command_queue where car_number=%s"
  39. return self._db.delete(sql,car_number)
  40. def get_space_dict(self):
  41. return self._space_dict
  42. def get_command_dict(self):
  43. return self._command_dict
  44. def get_pick_command_dict(self):
  45. return self._command_dict
  46. def run(self):
  47. while not self._isClose:
  48. for unit in range(1,4):
  49. res = self.query_space_in_unit(unit)
  50. if len(res) > 0 and self._space_dict[unit] != res:
  51. self._space_dict[unit] = res
  52. for unit in range(1,4):
  53. res = self.query_command_in_unit(unit)
  54. if len(res) > 0 and self._command_dict[unit] != res:
  55. self._command_dict[unit] = res
  56. time.sleep(0.001)