123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- import time
- import grpc
- import def_pb2 as pb
- import def_pb2_grpc as mrpc
- import threading
- from concurrent.futures import ThreadPoolExecutor
- def singleton(cls):
- _instance = {}
- def inner():
- if cls not in _instance:
- _instance[cls] = cls()
- return _instance[cls]
- return inner
- @singleton
- class GrpcStream(threading.Thread):
- def __init__(self):
- threading.Thread.__init__(self)
- self.threadpool=ThreadPoolExecutor(5)
- self.exit_=False
- self.features_=None
- self.measures_=None
- self.imagesCallBack=None
- self.cloudCallBack=None
- self.MeasureDataCallBack=None
- def SetDataCallBack(self,ImagesCallback,CloudsCallBack,MeasureDataCallback):
- self.imagesCallBack=ImagesCallback
- self.cloudCallBack=CloudsCallBack
- self.MeasureDataCallBack=MeasureDataCallback
- def GrabImage(self,ip,port,id): # id:1-4,其他表示所有
- connectstr='%s:%d'%(ip,port)
- self.channel=grpc.insecure_channel(connectstr)
- self.stub = mrpc.StreamServerStub(self.channel)
- cmd=pb.RequestCmd()
- cmd.Id=id
- resImage=self.stub.GetImage(cmd)
- if self.imagesCallBack is not None:
- self.imagesCallBack(resImage)
- def GrabCloud(self,ip,port,id): # id:1-4,其他表示所有
- connectstr='%s:%d'%(ip,port)
- self.channel=grpc.insecure_channel(connectstr)
- self.stub = mrpc.StreamServerStub(self.channel)
- cmd=pb.RequestCmd()
- cmd.Id=id
- resCloud=self.stub.GetCloud(cmd)
- if self.cloudCallBack is not None:
- self.cloudCallBack(resCloud)
- def ChangeCameraParameter(self,ip,port,id,parameter:pb.CoordinateTransformation3D):
- connectstr='%s:%d'%(ip,port)
- self.channel=grpc.insecure_channel(connectstr)
- self.stub = mrpc.StreamServerStub(self.channel)
- cmd=pb.RequestCmd()
- cmd.Id=id
- cmd.params.CopyFrom(parameter)
- res=self.stub.setTofTransformation(cmd)
- return res
- def SaveCameraParameter(self,ip,port,id,parameter:pb.CoordinateTransformation3D):
- connectstr='%s:%d'%(ip,port)
- self.channel=grpc.insecure_channel(connectstr)
- self.stub = mrpc.StreamServerStub(self.channel)
- cmd=pb.RequestCmd()
- cmd.Id=id
- cmd.params.CopyFrom(parameter)
- res=self.stub.saveTofTransformation(cmd)
- return res
- def OpenDataStream(self,ip,port):
- connectstr='%s:%d'%(ip,port)
- channel=grpc.insecure_channel(connectstr)
- stub = mrpc.StreamServerStub(channel)
- cmd=pb.RequestCmd()
- self.measures_ = stub.OpenMeasureDataStream(cmd)
- def CloseDataStream(self,ip,port):
- connectstr='%s:%d'%(ip,port)
- channel=grpc.insecure_channel(connectstr)
- stub = mrpc.StreamServerStub(channel)
- cmd=pb.RequestCmd()
- stub.CloseMeasureDataStream(cmd)
- def OpenImageStream(self,ip,port):
- connectstr='%s:%d'%(ip,port)
- channel=grpc.insecure_channel(connectstr)
- stub = mrpc.StreamServerStub(channel)
- cmd=pb.RequestCmd()
- self.features_ = stub.OpenImageStream(cmd)
- def CloseImageStream(self,ip,port):
- connectstr='%s:%d'%(ip,port)
- channel=grpc.insecure_channel(connectstr)
- stub = mrpc.StreamServerStub(channel)
- cmd=pb.RequestCmd()
- stub.CloseImageStream(cmd)
- def loopImageStream(self):
- while self.exit_ == False:
- time.sleep(0.001)
- if self.features_ is not None:
- try:
- for feature in self.features_: #流式返回的结果
- if self.imagesCallBack is not None:
- self.imagesCallBack(feature)
- except Exception as e:
- self.features_=None
- def loopDataStream(self):
- while self.exit_ == False:
- time.sleep(0.001)
- if self.measures_ is not None:
- try:
- for measure in self.measures_:
- if self.MeasureDataCallBack is not None:
- self.MeasureDataCallBack(measure)
- except Exception as e:
- if self.MeasureDataCallBack is not None:
- info=pb.MeasureInfo()
- info.error=str(e)
- self.MeasureDataCallBack(info)
- self.measures_=None
- def run(self):
- self.threadpool.submit(self.loopImageStream)
- self.threadpool.submit(self.loopDataStream)
- print(" close ")
- def close(self):
- self.exit_=True
- self.threadpool.shutdown()
- self.join()
|