DatabaseSearch.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import time
  2. import pymysql as psql
  3. import message_pb2 as message
  4. import google.protobuf.text_format as tf
  5. import threading
  6. class DBSeacher(threading.Thread):
  7. def __init__(self,ip,port,database,user,password,unit):
  8. threading.Thread.__init__(self)
  9. self.ip=ip
  10. self.port=port
  11. self.database=database
  12. self.user=user
  13. self.password=password
  14. self.unit=unit
  15. self._close=False
  16. self.conn=psql.connect(host=ip,
  17. port=port,
  18. database=database,
  19. charset="utf8",
  20. user=user,
  21. passwd=password)
  22. self.led_callback=None
  23. def set_ledcallback(self,callback):
  24. self.led_callback=callback
  25. def close(self):
  26. self._close=True
  27. self.join()
  28. self.conn.close()
  29. def search_difficulty(self):
  30. self.conn.begin()
  31. cursor=self.conn.cursor()
  32. SQL="select * from space where height>1.55 and unit=%d and car_number IS NULL and statu=0"%self.unit
  33. suv=cursor.execute(SQL)
  34. SQL="select * from space where height<=1.55 and unit=%d and car_number IS NULL and statu=0"%self.unit
  35. small=cursor.execute(SQL)
  36. SQL="select * from command_queue where statu=0 and type=1 and unit=%d"%self.unit
  37. cursor.execute(SQL)
  38. idel_cmds=cursor.fetchall()
  39. self.conn.commit()
  40. cursor.close()
  41. suv_cmd=0
  42. small_cmd=0
  43. for cmd in idel_cmds:
  44. measure_string=cmd[7]
  45. measure_info=message.measure_info()
  46. tf.Parse(measure_string,measure_info)
  47. if measure_info.height>1.55:
  48. suv_cmd=suv_cmd+1
  49. elif measure_info.height<=1.55:
  50. small_cmd=small_cmd+1
  51. return [small,suv,suv_cmd,small_cmd]
  52. def run(self):
  53. while self._close==False:
  54. space=self.search_difficulty()
  55. if space==None:
  56. print("ERROR Search None")
  57. small,suv,cmd_suv,cmd_small=space
  58. idle_small=small-cmd_small
  59. idle_suv=suv-cmd_suv
  60. if idle_small<0:
  61. idle_suv=suv-cmd_suv-(-idle_small)
  62. idle_small=0
  63. if not self.led_callback==None:
  64. self.led_callback(idle_suv,idle_suv+idle_small)
  65. time.sleep(1)