GrpcClient.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import time
  2. import grpc
  3. import def_pb2 as pb
  4. import def_pb2_grpc as mrpc
  5. import threading
  6. from concurrent.futures import ThreadPoolExecutor
  7. def singleton(cls):
  8. _instance = {}
  9. def inner():
  10. if cls not in _instance:
  11. _instance[cls] = cls()
  12. return _instance[cls]
  13. return inner
  14. @singleton
  15. class GrpcStream(threading.Thread):
  16. def __init__(self):
  17. threading.Thread.__init__(self)
  18. self.threadpool=ThreadPoolExecutor(5)
  19. self.exit_=False
  20. self.features_=None
  21. self.measures_=None
  22. self.imagesCallBack=None
  23. self.cloudCallBack=None
  24. self.MeasureDataCallBack=None
  25. def SetDataCallBack(self,ImagesCallback,CloudsCallBack,MeasureDataCallback):
  26. self.imagesCallBack=ImagesCallback
  27. self.cloudCallBack=CloudsCallBack
  28. self.MeasureDataCallBack=MeasureDataCallback
  29. def GrabImage(self,ip,port,id): # id:1-4,其他表示所有
  30. connectstr='%s:%d'%(ip,port)
  31. self.channel=grpc.insecure_channel(connectstr)
  32. self.stub = mrpc.StreamServerStub(self.channel)
  33. cmd=pb.RequestCmd()
  34. cmd.Id=id
  35. resImage=self.stub.GetImage(cmd)
  36. if self.imagesCallBack is not None:
  37. self.imagesCallBack(resImage)
  38. def GrabCloud(self,ip,port,id): # id:1-4,其他表示所有
  39. connectstr='%s:%d'%(ip,port)
  40. self.channel=grpc.insecure_channel(connectstr)
  41. self.stub = mrpc.StreamServerStub(self.channel)
  42. cmd=pb.RequestCmd()
  43. cmd.Id=id
  44. resCloud=self.stub.GetCloud(cmd)
  45. if self.cloudCallBack is not None:
  46. self.cloudCallBack(resCloud)
  47. def ChangeCameraParameter(self,ip,port,id,parameter:pb.CoordinateTransformation3D):
  48. connectstr='%s:%d'%(ip,port)
  49. self.channel=grpc.insecure_channel(connectstr)
  50. self.stub = mrpc.StreamServerStub(self.channel)
  51. cmd=pb.RequestCmd()
  52. cmd.Id=id
  53. cmd.params.CopyFrom(parameter)
  54. res=self.stub.setTofTransformation(cmd)
  55. return res
  56. def SaveCameraParameter(self,ip,port,id,parameter:pb.CoordinateTransformation3D):
  57. connectstr='%s:%d'%(ip,port)
  58. self.channel=grpc.insecure_channel(connectstr)
  59. self.stub = mrpc.StreamServerStub(self.channel)
  60. cmd=pb.RequestCmd()
  61. cmd.Id=id
  62. cmd.params.CopyFrom(parameter)
  63. res=self.stub.saveTofTransformation(cmd)
  64. return res
  65. def OpenDataStream(self,ip,port):
  66. connectstr='%s:%d'%(ip,port)
  67. channel=grpc.insecure_channel(connectstr)
  68. stub = mrpc.StreamServerStub(channel)
  69. cmd=pb.RequestCmd()
  70. self.measures_ = stub.OpenMeasureDataStream(cmd)
  71. def CloseDataStream(self,ip,port):
  72. connectstr='%s:%d'%(ip,port)
  73. channel=grpc.insecure_channel(connectstr)
  74. stub = mrpc.StreamServerStub(channel)
  75. cmd=pb.RequestCmd()
  76. stub.CloseMeasureDataStream(cmd)
  77. def OpenImageStream(self,ip,port):
  78. connectstr='%s:%d'%(ip,port)
  79. channel=grpc.insecure_channel(connectstr)
  80. stub = mrpc.StreamServerStub(channel)
  81. cmd=pb.RequestCmd()
  82. self.features_ = stub.OpenImageStream(cmd)
  83. def CloseImageStream(self,ip,port):
  84. connectstr='%s:%d'%(ip,port)
  85. channel=grpc.insecure_channel(connectstr)
  86. stub = mrpc.StreamServerStub(channel)
  87. cmd=pb.RequestCmd()
  88. stub.CloseImageStream(cmd)
  89. def loopImageStream(self):
  90. while self.exit_ == False:
  91. time.sleep(0.001)
  92. if self.features_ is not None:
  93. try:
  94. for feature in self.features_: #流式返回的结果
  95. if self.imagesCallBack is not None:
  96. self.imagesCallBack(feature)
  97. except Exception as e:
  98. self.features_=None
  99. def loopDataStream(self):
  100. while self.exit_ == False:
  101. time.sleep(0.001)
  102. if self.measures_ is not None:
  103. try:
  104. for measure in self.measures_:
  105. if self.MeasureDataCallBack is not None:
  106. self.MeasureDataCallBack(measure)
  107. except Exception as e:
  108. if self.MeasureDataCallBack is not None:
  109. info=pb.MeasureInfo()
  110. info.error=str(e)
  111. self.MeasureDataCallBack(info)
  112. self.measures_=None
  113. def run(self):
  114. self.threadpool.submit(self.loopImageStream)
  115. self.threadpool.submit(self.loopDataStream)
  116. print(" close ")
  117. def close(self):
  118. self.exit_=True
  119. self.threadpool.shutdown()
  120. self.join()