ctrlX Data Layer API for Python  3.3.0
The ctrlX Data Layer API allows access to the ctrlX Data Layer with Python
provider_node.py
1 """
2 Class Provider Node
3 """
4 import ctypes
5 import typing
6 
7 import ctrlxdatalayer
8 from ctrlxdatalayer.clib import C_DLR_RESULT
9 from ctrlxdatalayer.clib_provider_node import (
10  C_DLR_PROVIDER_NODE_CALLBACK, C_DLR_PROVIDER_NODE_CALLBACKDATA,
11  C_DLR_PROVIDER_NODE_CALLBACKS, C_DLR_PROVIDER_NODE_FUNCTION,
12  C_DLR_PROVIDER_NODE_FUNCTION_DATA, C_DLR_PROVIDER_SUBSCRIPTION_FUNCTION,
13  C_DLR_SUBSCRIPTION, C_DLR_VARIANT, address_c_char_p, userData_c_void_p)
14 from ctrlxdatalayer.provider_subscription import ProviderSubscription
15 from ctrlxdatalayer.variant import Result, Variant, VariantRef
16 
17 NodeCallback = typing.Callable[[Result, typing.Optional[Variant]], None]
18 NodeFunction = typing.Callable[[userData_c_void_p, str, NodeCallback], None]
19 NodeFunctionData = typing.Callable[[
20  userData_c_void_p, str, Variant, NodeCallback], None]
21 NodeFunctionSubscription = typing.Callable[[
22  userData_c_void_p, ProviderSubscription, str], None]
23 
24 class _CallbackPtr:
25  """
26  _CallbackPtr helper
27  """
28 
29  __slots__ = ['_ptr']
30 
31  def __init__(self):
32  """
33  init _CallbackPtr
34  """
35  self._ptr_ptr: typing.Optional[ctypes._CFuncPtr] = None
36 
37  def set_ptr(self, ptr):
38  """
39  setter _CallbackPtr
40  """
41  self._ptr_ptr = ptr
42 
43  def get_ptr(self):
44  """
45  getter _CallbackPtr
46  """
47  return self._ptr_ptr
48 
49 
51  """
52  Provider Node callbacks interface
53  """
54  __slots__ = ['on_create', 'on_remove', 'on_browse',
55  'on_read', 'on_write', 'on_metadata',
56  'on_subscribe', 'on_unsubscribe']
57 
58  def __init__(self,
59  on_create: NodeFunctionData,
60  on_remove: NodeFunction,
61  on_browse: NodeFunction,
62  on_read: NodeFunctionData,
63  on_write: NodeFunctionData,
64  on_metadata: NodeFunction,
65  on_subscribe: NodeFunctionSubscription = None,
66  on_unsubscribe: NodeFunctionSubscription = None):
67  """
68  init ProviderNodeCallbacks
69  """
70  self.on_createon_create = on_create
71  self.on_removeon_remove = on_remove
72  self.on_browseon_browse = on_browse
73  self.on_readon_read = on_read
74  self.on_writeon_write = on_write
75  self.on_metadataon_metadata = on_metadata
76  self.on_subscribeon_subscribe = on_subscribe
77  self.on_unsubscribeon_unsubscribe = on_unsubscribe
78 
79 
80 class ProviderNode:
81  """
82  Provider node interface for providing data to the system
83 
84  Hint: see python context manager for instance handling
85  """
86  __slots__ = ['__ptrs', '__c_cbs', '__closed', '__provider_node']
87 
88  def __init__(self, cbs: ProviderNodeCallbacks, userdata: userData_c_void_p = None):
89  """
90  init ProviderNode
91  """
92  self.__ptrs: typing.List[_CallbackPtr] = []
93  if cbs.on_subscribe is None:
94  self.__c_cbs__c_cbs = C_DLR_PROVIDER_NODE_CALLBACKS(
95  userdata,
96  self.__create_function_data__create_function_data(cbs.on_create),
97  self.__create_function__create_function(cbs.on_remove),
98  self.__create_function__create_function(cbs.on_browse),
99  self.__create_function_data__create_function_data(cbs.on_read),
100  self.__create_function_data__create_function_data(cbs.on_write),
101  self.__create_function__create_function(cbs.on_metadata),
102  )
103  else:
104  self.__c_cbs__c_cbs = C_DLR_PROVIDER_NODE_CALLBACKS(
105  userdata,
106  self.__create_function_data__create_function_data(cbs.on_create),
107  self.__create_function__create_function(cbs.on_remove),
108  self.__create_function__create_function(cbs.on_browse),
109  self.__create_function_data__create_function_data(cbs.on_read),
110  self.__create_function_data__create_function_data(cbs.on_write),
111  self.__create_function__create_function(cbs.on_metadata),
112  self.__create_function_subscribe__create_function_subscribe(cbs.on_subscribe),
113  self.__create_function_subscribe__create_function_subscribe(cbs.on_unsubscribe)
114  )
115 
116  self.__closed__closed = False
117  self.__provider_node__provider_node = ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerNodeCreate(
118  self.__c_cbs__c_cbs)
119 
120  def __enter__(self):
121  """
122  use the python context manager
123  """
124  return self
125 
126  def __exit__(self, exc_type, exc_val, exc_tb):
127  """
128  use the python context manager
129  """
130  self.closeclose()
131 
132  def __del__(self):
133  """
134  __del__
135  """
136  self.closeclose()
137 
138  def close(self):
139  """
140  closes the node instance
141  """
142  if self.__closed__closed:
143  return
144  self.__closed__closed = True
145  ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerNodeDelete(
146  self.__provider_node__provider_node)
147  self.__ptrs.clear()
148  self.__c_cbs__c_cbs = None
149 
150  def get_handle(self):
151  """
152  handle value of ProviderNode
153  """
154  return self.__provider_node__provider_node
155 
156  def __create_callback(self,
157  c_cb: C_DLR_PROVIDER_NODE_CALLBACK,
158  c_cbdata: C_DLR_PROVIDER_NODE_CALLBACKDATA) -> NodeCallback:
159  """
160  create callback
161  """
162  def cb(result: Result, data: typing.Optional[Variant]):
163  """ cb """
164  if data is None:
165  c_cb(c_cbdata, result.value, None)
166  else:
167  c_cb(c_cbdata, result.value, data.get_handle())
168  return cb
169 
170  def __create_function(self, func: NodeFunction):
171  """
172  create callback management
173  """
174  cb_ptr = _CallbackPtr()
175  self.__ptrs.append(cb_ptr)
176 
177  def _func(c_userdata: userData_c_void_p,
178  c_address: address_c_char_p,
179  c_cb: C_DLR_PROVIDER_NODE_CALLBACK,
180  c_cbdata: C_DLR_PROVIDER_NODE_CALLBACKDATA) -> C_DLR_RESULT:
181  """
182  datalayer calls this function
183  """
184  address = c_address.decode('utf-8')
185  cb = self.__create_callback__create_callback(c_cb, c_cbdata)
186  func(c_userdata, address, cb)
187  return Result.OK.value
188  cb_ptr.set_ptr(C_DLR_PROVIDER_NODE_FUNCTION(_func))
189  return cb_ptr.get_ptr()
190 
191  def __create_function_data(self, func: NodeFunctionData):
192  """
193  create callback management
194  """
195  cb_ptr = _CallbackPtr()
196  self.__ptrs.append(cb_ptr)
197 
198  def _func(c_userdata: userData_c_void_p,
199  c_address: address_c_char_p,
200  c_data: C_DLR_VARIANT,
201  c_cb: C_DLR_PROVIDER_NODE_CALLBACK,
202  c_cbdata: C_DLR_PROVIDER_NODE_CALLBACKDATA) -> C_DLR_RESULT:
203  """
204  datalayer calls this function
205  """
206  address = c_address.decode('utf-8')
207  data = VariantRef(c_data)
208  cb = self.__create_callback__create_callback(c_cb, c_cbdata)
209  func(c_userdata, address, data, cb)
210  return Result.OK.value
211  cb_ptr.set_ptr(C_DLR_PROVIDER_NODE_FUNCTION_DATA(_func))
212  return cb_ptr.get_ptr()
213 
214  def __create_function_subscribe(self, func: NodeFunctionSubscription):
215  """
216  create callback managment
217  """
218  cb_ptr = _CallbackPtr()
219  self.__ptrs.append(cb_ptr)
220 
221  def _func(c_userdata: userData_c_void_p,
222  c_subscribe: C_DLR_SUBSCRIPTION,
223  c_address: address_c_char_p) -> C_DLR_RESULT:
224  """
225  datalayer calls this function
226  """
227  if c_address is None:
228  return Result.OK.value
229  if func is None:
230  return Result.OK.value
231  address = c_address.decode('utf-8')
232  sub = ProviderSubscription(c_subscribe)
233  func(c_userdata, sub, address)
234  return Result.OK.value
235  cb_ptr.set_ptr(C_DLR_PROVIDER_SUBSCRIPTION_FUNCTION(_func))
236  return cb_ptr.get_ptr()
237 
238  def _test_function(self, func: NodeFunction):
239  """
240  internal use
241  """
242  return self.__create_function__create_function(func)
243 
244  def _test_function_data(self, func: NodeFunctionData):
245  """
246  internal use
247  """
248  return self.__create_function_data__create_function_data(func)
Provider Node callbacks interface.
def __init__(self, NodeFunctionData on_create, NodeFunction on_remove, NodeFunction on_browse, NodeFunctionData on_read, NodeFunctionData on_write, NodeFunction on_metadata, NodeFunctionSubscription on_subscribe=None, NodeFunctionSubscription on_unsubscribe=None)
init ProviderNodeCallbacks
Provider node interface for providing data to the system.
def __exit__(self, exc_type, exc_val, exc_tb)
use the python context manager
def __create_function_subscribe(self, NodeFunctionSubscription func)
def __enter__(self)
use the python context manager
def __create_function_data(self, NodeFunctionData func)
def close(self)
closes the node instance
def get_handle(self)
handle value of ProviderNode
def __create_function(self, NodeFunction func)
NodeCallback __create_callback(self, C_DLR_PROVIDER_NODE_CALLBACK c_cb, C_DLR_PROVIDER_NODE_CALLBACKDATA c_cbdata)
def __init__(self, ProviderNodeCallbacks cbs, userData_c_void_p userdata=None)
init ProviderNode
def set_ptr(self, ptr)
setter _CallbackPtr
def get_ptr(self)
getter _CallbackPtr
def __init__(self)
init _CallbackPtr