Python Client  0.0.0.0
Python wrapper for Corelink DLL
corelink_recv_stream.py
Go to the documentation of this file.
1 """"
2  @file corelink_recv_stream.py
3  Handler class for the receiver streams.
4  A container for the stream id and necessary data to call associated functions.
5 """
6 import ctypes
7 import corelink_const
8 import corelink_stream_data
9 
10 # pylint: disable=global-statement
11 # global declaration is essential
12 
13 RECV_CALLBACK_TYPE = ctypes.CFUNCTYPE(None,(ctypes.c_int),
14  (ctypes.c_int),ctypes.
15  POINTER(ctypes.c_char),
16  (ctypes.c_int),
17  (ctypes.c_int),
18  ctypes.c_void_p)
19 CALLBACK_TYPE = ctypes.CFUNCTYPE(None,(ctypes.c_int),(ctypes.c_int),ctypes.c_wchar_p)
20 
21 RECV_CALLBACK_JSON_TYPE = ctypes.CFUNCTYPE(None,(ctypes.c_int),
22  (ctypes.c_int),
23  ctypes.POINTER(ctypes.c_char),
24  (ctypes.c_int),
25  (ctypes.c_int),
26  ctypes.c_void_p)
27 CALLBACK_JSON_TYPE = ctypes.CFUNCTYPE(None,(ctypes.c_int),
28  (ctypes.c_int),
29  ctypes.c_wchar_p,
30  ctypes.c_wchar_p)
31 # pylint: disable=too-many-arguments
32 # Six is reasonable in this case.
33 def recv_callback_json(recv_id,send_id,msg,json_len,msg_len,callback):
34  """
35  Recv callback for Json
36  """
37  if callback is None:
38  return
39  global CALLBACK_JSON_TYPE
40  callback = ctypes.cast(callback, CALLBACK_JSON_TYPE)
41  json_data = msg[:json_len].decode("utf-8")
42  msg_data= msg[json_len:json_len+msg_len].decode("utf-8")
43  callback(recv_id, send_id, msg_data,json_data)
44 
45 def recv_callback(recv_id,send_id,msg,json_len,msg_len,callback):
46  """
47  recv call back
48  """
49  if callback is None:
50  return
51  global CALLBACK_TYPE
52  msg_data= msg[json_len:json_len+msg_len].decode("utf-8")
53  callback = ctypes.cast(callback, CALLBACK_TYPE)
54  callback(recv_id, send_id, msg_data)
55 
56 class RecvStream:
57  """
58  recv stream
59  """
60  def __init__(self,*arg):
61  if len(arg)==3:
62  self.update(arg[0],arg[1],arg[2])
63  if len(arg)==1:
64  if isinstance(arg[0],corelink_stream_data.StreamData):
65  stream_data = arg[0]
66  if(stream_data.state & corelink_const.STATE_CODES['STREAM_STATE_RECV']) >0:
67  self.update(stream_data.stream_id,stream_data.state,stream_data.stream_ref)
68  else:
69  self.update(-1,corelink_const.STATE_CODES['STREAM_STATE_NONE'],-1)
70  if isinstance(arg[0],RecvStream):
71  rhs = arg[0]
72  self.update(rhs.stream_id,rhs.state,rhs.stream_ref)
73  self.receive_callback = None
74  self.callback = None
75 
76  def update(self,stream_id,state,stream_ref):
77  """
78  update parameters
79  """
80  self.stream_id = stream_id
81  self.state = state
82  self.stream_ref = stream_ref
83  self.func_c = None
84  self.temp_f = None
85 
86  def set_on_receive(self):
87  """
88  set on receive
89  """
90  corelink_const.LIB.setOnRecv.restype = ctypes.c_void_p
91  _ = corelink_const.LIB.setOnRecv((ctypes.c_int(self.state)),
92  (ctypes.c_int(self.stream_ref)),
93  ctypes.byref(ctypes.c_int(self.stream_id)),None,None)
94 
95  def set_on_receive_msg(self,func):
96  """
97  set on receive message
98  """
99  global RECV_CALLBACK_TYPE,CALLBACK_TYPE
100  self.receive_callback = RECV_CALLBACK_TYPE(recv_callback)
101  callback = CALLBACK_TYPE(func)
102  self.callback = ctypes.cast(callback,ctypes.c_void_p)
103  corelink_const.LIB.setOnRecv.restype = ctypes.c_void_p
104  _ = corelink_const.LIB.setOnRecv((ctypes.c_int(self.state)),
105  (ctypes.c_int(self.stream_ref)),
106  ctypes.byref(ctypes.c_int(self.stream_id)),
107  self.receive_callback,self.callback)
108 
109  def set_on_receive_msg_json(self,func):
110  """
111  set on receive msg json
112  """
113  global RECV_CALLBACK_JSON_TYPE,CALLBACK_JSON_TYPE
114  self.receive_callback = RECV_CALLBACK_JSON_TYPE(recv_callback_json)
115  callback = CALLBACK_JSON_TYPE(func)
116  self.callback = ctypes.cast(callback,ctypes.c_void_p)
117  corelink_const.LIB.setOnRecv.restype = ctypes.c_void_p
118  _ = corelink_const.LIB.setOnRecv((ctypes.c_int(self.state)),
119  (ctypes.c_int(self.stream_ref)),
120  ctypes.byref(ctypes.c_int(self.stream_id)),
121  self.receive_callback,self.callback)
122 
123  @staticmethod
124  def get_receiver_handler(stream_id):
125  """
126  get receiver handler
127  """
128  corelink_const.LIB.getStreamState.argtypes = [ctypes.POINTER(ctypes.c_int)]
129  corelink_const.LIB.getStreamState.restype = ctypes.c_int
130  state = corelink_const.LIB.getStreamState(ctypes.byref(ctypes.c_int(stream_id)))
131 
132  corelink_const.LIB.getStreamRef.argtypes = [ctypes.POINTER(ctypes.c_int)]
133  corelink_const.LIB.getStreamRef.restype = ctypes.c_int
134  ref = corelink_const.LIB.getStreamRef(ctypes.byref(ctypes.c_int(stream_id)))
135  if(ref <0 or (state & corelink_const.STATE_CODES['STREAM_STATE_RECV'])==0 ):
136  return RecvStream()
137  return RecvStream(stream_id,state,ref)