DatabaseSearchPickCmd.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import time
  2. import pymysql as psql
  3. import message_pb2 as message
  4. import threading
  5. class DBSeacherPickCmd():
  6. def __init__(self,ip,port,database,user,password):
  7. self.ip=ip
  8. self.port=port
  9. self.database=database
  10. self.user=user
  11. self.password=password
  12. self._close=False
  13. self._opendoor_callback=None
  14. self.conn=psql.connect(host=ip,
  15. port=port,
  16. database=database,
  17. charset="utf8",
  18. user=user,
  19. passwd=password)
  20. self._lock = threading.Lock()
  21. def close(self):
  22. self._close=True
  23. self.join()
  24. self.conn.close()
  25. def search_pickcmd(self):
  26. self.conn.begin()
  27. cursor=self.conn.cursor()
  28. SQL="select * from command_queue where statu=2 " #寻找已到出口的取车指令
  29. self._lock.acquire()
  30. cursor.execute(SQL)
  31. self._lock.release()
  32. pick_cmds=cursor.fetchall()
  33. cursor.close()
  34. self.conn.commit()
  35. return pick_cmds
  36. def delete_cmd(self,id):
  37. self.conn.begin()
  38. cursor=self.conn.cursor()
  39. SQL1="select * from command_queue for update"
  40. self._lock.acquire()
  41. cursor.execute(SQL1)
  42. self._lock.release()
  43. SQL2="delete from command_queue where statu=2 and export_id=%d"%id
  44. self._lock.acquire()
  45. cursor.execute(SQL2)
  46. self._lock.release()
  47. cursor.close()
  48. self.conn.commit()
  49. '''
  50. def run(self):
  51. while self._close==False:
  52. cmds=self.search_pickcmd()
  53. for cmd in cmds:
  54. if len(cmd)>=9:
  55. table=message.pick_table()
  56. table.car_number=cmd[0]
  57. table.primary_key=cmd[1]
  58. table.unit=cmd[2]
  59. table.queue_id=cmd[3]
  60. table.type=cmd[4]
  61. table.statu=cmd[5]
  62. table.export_id=cmd[8]
  63. if not table.export_id==None:
  64. self._opendoor_callback(table.export_id,table)
  65. time.sleep(1)
  66. '''