ctrlX Data Layer API for Python  3.3.0
The ctrlX Data Layer API allows access to the ctrlX Data Layer with Python
All Classes Functions Pages
client.py
1 """
2 class Client
3 """
4 import ctypes
5 import typing
6 from enum import Enum
7 
8 import ctrlxdatalayer
11 from ctrlxdatalayer.clib import userData_c_void_p
12 from ctrlxdatalayer.clib_client import C_DLR_CLIENT, C_DLR_CLIENT_RESPONSE
13 from ctrlxdatalayer.converter import Converter
14 from ctrlxdatalayer.variant import Result, Variant, VariantRef
15 
16 ResponseCallback = typing.Callable[[
17  Result, typing.Optional[Variant], ctrlxdatalayer.clib.userData_c_void_p], None]
18 
19 
20 class _CallbackPtr:
21  """
22  Callback wrapper
23  """
24  __slots__ = ['_ptr']
25 
26  def __init__(self):
27  """
28  init _CallbackPtr
29  """
30  self._ptr_ptr: typing.Optional[ctypes._CFuncPtr] = None
31 
32  def set_ptr(self, ptr):
33  """
34  setter CallbackPtr
35  """
36  self._ptr_ptr = ptr
37 
38  def get_ptr(self):
39  """
40  getter CallbackPtr
41  """
42  return self._ptr_ptr
43 
44 
45 class TimeoutSetting(Enum):
46  """
47  Settings of different timeout values
48  """
49  IDLE = 0
50  PING = 1
51  Reconnect = 2
52 
53 
54 class Client:
55  """
56  Client interface for accessing data from the system
57 
58  Hint: see python context manager for instance handling
59  """
60  # problem with weakref.ref
61  #__slots__ = ['__closed', '__client', '__token', '__ptrs', '__subs']
62 
63  def __init__(self, c_client: C_DLR_CLIENT):
64  """
65  @param[in] client Reference to the client
66  """
67  self.__closed__closed = False
68  self.__client: C_DLR_CLIENT = c_client
69  self.__token__token = None
70  self.__ptrs: typing.List[_CallbackPtr] = []
71  self.__subs: typing.List[ctrlxdatalayer.subscription.Subscription] = [
72  ]
73 
74  def __enter__(self):
75  """
76  use the python context manager
77  """
78  return self
79 
80  def __exit__(self, exc_type, exc_val, exc_tb):
81  """
82  use the python context manager
83  """
84  self.closeclose()
85 
86  def close(self):
87  """
88  closes the client instance
89  """
90  if self.__closed__closed:
91  return
92  self.__closed__closed = True
93  self.__close_all_subs__close_all_subs()
94  self.__subs.clear()
95 
96  ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientDelete(self.__client)
97  self.__ptrs.clear()
98  del self.__subs
99  del self.__ptrs
100  self.__token__token = None
101 
102  def get_handle(self):
103  """
104  handle value of Client
105  """
106  return self.__client
107 
108  def get_token(self):
109  """
110  internal token
111  """
112  return self.__token__token
113 
114  def __create_callback(self, cb: ResponseCallback):
115  """
116  callback management
117  """
118  cb_ptr = _CallbackPtr()
119  self.__ptrs.append(cb_ptr)
120 
121  def _cb(c_result: ctrlxdatalayer.clib.C_DLR_RESULT,
122  c_data: ctypes.c_void_p, c_userdata: ctypes.c_void_p):
123  """
124  datalayer calls this function
125  """
126  if c_data is None:
127  cb(Result(c_result), None, c_userdata)
128  else:
129  data = VariantRef(c_data)
130  cb(Result(c_result), data, c_userdata)
131  cb_ptr.set_ptr(None) # remove cyclic dependency
132  self.__ptrs.remove(cb_ptr)
133 
134  cb_ptr.set_ptr(C_DLR_CLIENT_RESPONSE(_cb))
135  return cb_ptr.get_ptr()
136 
137  def _test_callback(self, cb: ResponseCallback):
138  """
139  internal use
140  """
141  return self.__create_callback__create_callback(cb)
142 
143  def set_timeout(self, timeout: TimeoutSetting, value: int) -> Result:
144  """
145  Set client timeout value
146  @param[in] timeout Timeout to set (see DLR_TIMEOUT_SETTING)
147  @param[in] value Value to set
148  @returns <Result>, status of function call
149  """
150  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientSetTimeout(
151  self.__client, timeout.value, value))
152 
153  def is_connected(self) -> bool:
154  """
155  returns whether provider is connected
156  @returns <bool> status of connection
157  """
158  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientIsConnected(self.__client)
159 
160  def set_auth_token(self, token: str):
161  """
162  Set persistent security access token for authentication as JWT payload
163  @param[in] token Security access &token for authentication
164  """
165  if token is None:
166  raise TypeError('token is invalid')
167  self.__token__token = token.encode('utf-8')
168  ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientSetAuthToken(
169  self.__client, self.__token__token)
170 
171  def get_auth_token(self) -> str:
172  """
173  returns persistent security access token for authentication
174  @returns <str> security access token for authentication
175  """
176  token = ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientGetAuthToken(
177  self.__client)
178  self.__token__token = token
179  return token.decode('utf-8')
180 
181  def ping_sync(self) -> Result:
182  """
183  Ping the next hop. This function is synchronous: It will wait for the answer.
184  @returns <Result> status of function call
185  """
186  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientPingSync(self.__client))
187 
188  def create_sync(self, address: str, data: Variant):
189  """
190  Create an object. This function is synchronous: It will wait for the answer.
191  @param[in] address Address of the node to create object in
192  @param[in] variant Data of the object
193  @returns tuple (Result, Variant)
194  @return <Result>, status of function call
195  @return <Variant>, variant result of write
196  """
197  b_address = address.encode('utf-8')
198  result = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientCreateSync(
199  self.__client, b_address, data.get_handle(), self.__token__token))
200  return result, data
201 
202  def remove_sync(self, address: str) -> Result:
203  """
204  Remove an object. This function is synchronous: It will wait for the answer.
205  @param[in] address Address of the node to remove
206  @returns <Result> status of function call
207  """
208  b_address = address.encode('utf-8')
209  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientRemoveSync(
210  self.__client, b_address, self.__token__token))
211 
212  def browse_sync(self, address: str):
213  """
214  Browse an object. This function is synchronous: It will wait for the answer.
215  @param[in] address Address of the node to browse
216  @returns tuple (Result, Variant)
217  @return <Result>, status of function call,
218  @return <Variant>, Children of the node. Data will be provided as Variant array of strings.
219  """
220  b_address = address.encode('utf-8')
221  data = Variant()
222  result = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientBrowseSync(
223  self.__client, b_address, data.get_handle(), self.__token__token))
224  return result, data
225 
226  def read_sync(self, address: str):
227  """
228  Read an object. This function is synchronous: It will wait for the answer.
229  @param[in] address Address of the node to read
230  @returns tuple (Result, Variant)
231  @return <Result>, status of function call,
232  @return <Variant>, Data of the node
233  """
234  data = Variant()
235  return self.read_sync_argsread_sync_args(address, data)
236 
237  def read_sync_args(self, address: str, args: Variant):
238  """
239  Read an object. This function is synchronous: It will wait for the answer.
240  @param[in] address Address of the node to read
241  @param[in,out] args Read arguments data of the node
242  @returns tuple (Result, Variant)
243  @return <Result>, status of function call,
244  @return <Variant>, Data of the node
245  """
246  b_address = address.encode('utf-8')
247  result = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientReadSync(
248  self.__client, b_address, args.get_handle(), self.__token__token))
249  return result, args
250 
251  def write_sync(self, address: str, data: Variant):
252  """
253  Write an object. This function is synchronous: It will wait for the answer.
254  @param[in] address Address of the node to write
255  @param[in] variant New data of the node
256  @returns tuple (Result, Variant)
257  @return <Result>, status of function call,
258  @return <Variant>, result of write
259  """
260  b_address = address.encode('utf-8')
261  result = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientWriteSync(
262  self.__client, b_address, data.get_handle(), self.__token__token))
263  return result, data
264 
265  def metadata_sync(self, address: str):
266  """
267  Read metadata of an object. This function is synchronous: It will wait for the answer.
268  @param[in] address Address of the node to read metadata of
269  @returns tuple (Result, Variant)
270  @return <Result>, status of function call,
271  @return <Variant>, Metadata of the node. Data will be provided as Variant flatbuffers with metadata.fbs data type.
272  """
273  b_address = address.encode('utf-8')
274  data = Variant()
275  result = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientMetadataSync(
276  self.__client, b_address, data.get_handle(), self.__token__token))
277  return result, data
278 
279  def ping_async(self, cb: ResponseCallback, userdata: userData_c_void_p = None) -> Result:
280  """
281  Ping the next hop. This function is asynchronous. It will return immediately. Callback will be called if function call is finished.
282  @param[in] callback Callback to call when function is finished
283  @param[in] userdata User data - will be returned in callback as userdata. You can use this userdata to identify your request
284  @returns <Result> status of function call
285  """
286  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientPingASync(
287  self.__client, self.__create_callback__create_callback(cb), userdata))
288 
289  def create_async(self, address: str, data: Variant,
290  cb: ResponseCallback, userdata: userData_c_void_p = None) -> Result:
291  """
292  Create an object. This function is asynchronous. It will return immediately. Callback will be called if function call is finished. Result data may be provided in callback function.
293  @param[in] address Address of the node to create object in
294  @param[in] data Data of the object
295  @param[in] callback Callback to call when function is finished
296  @param[in] userdata User data - will be returned in callback as userdata. You can use this userdata to identify your request
297  @returns <Result> status of function call
298  """
299  b_address = address.encode('utf-8')
300  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientCreateASync(
301  self.__client, b_address, data.get_handle(),
302  self.__token__token, self.__create_callback__create_callback(cb), userdata))
303 
304  def remove_async(self, address: str,
305  cb: ResponseCallback,
306  userdata: userData_c_void_p = None) -> Result:
307  """
308  Remove an object. This function is asynchronous. It will return immediately. Callback will be called if function call is finished. Result data may be provided in callback function.
309  @param[in] address Address of the node to remove
310  @param[in] callback Callback to call when function is finished
311  @param[in] userdata User data - will be returned in callback as userdata. You can use this userdata to identify your request
312  @returns <Result> status of function call
313  """
314  b_address = address.encode('utf-8')
315  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientRemoveASync(self.__client,
316  b_address,
317  self.__token__token,
318  self.__create_callback__create_callback(
319  cb),
320  userdata))
321 
322  def browse_async(self, address: str,
323  cb: ResponseCallback,
324  userdata: userData_c_void_p = None) -> Result:
325  """
326  Browse an object. This function is asynchronous. It will return immediately. Callback will be called if function call is finished. Result data may be provided in callback function.
327  @param[in] address Address of the node to browse
328  @param[in] callback Callback to call when function is finished
329  @param[in] userdata User data - will be returned in callback as userdata. You can use this userdata to identify your request
330  @returns <Result> status of function call
331  """
332  b_address = address.encode('utf-8')
333  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientBrowseASync(
334  self.__client, b_address, self.__token__token, self.__create_callback__create_callback(cb), userdata))
335 
336  def read_async(self, address: str,
337  cb: ResponseCallback, userdata: userData_c_void_p = None) -> Result:
338  """
339  Read an object. This function is asynchronous. It will return immediately. Callback will be called if function call is finished. Result data may be provided in callback function.
340  @param[in] address Address of the node to read
341  @param[in] callback Callback to call when function is finished
342  @param[in] userdata User data - will be returned in callback as userdata. You can use this userdata to identify your request
343  @returns <Result>, status of function call
344  """
345  with Variant() as args:
346  return self.read_async_argsread_async_args(address, args, cb, userdata)
347 
348  def read_async_args(self, address: str, args: Variant,
349  cb: ResponseCallback, userdata: userData_c_void_p = None) -> Result:
350  """
351  Read an object. This function is asynchronous. It will return immediately. Callback will be called if function call is finished. Result data may be provided in callback function.
352  @param[in] address Address of the node to read
353  @param[in] args Read arguments data of the node
354  @param[in] callback Callback to call when function is finished
355  @param[in] userdata User data - will be returned in callback as userdata. You can use this userdata to identify your request
356  @returns <Result>, status of function call
357  """
358  b_address = address.encode('utf-8')
359  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientReadASync(self.__client,
360  b_address,
361  args.get_handle(),
362  self.__token__token,
363  self.__create_callback__create_callback(
364  cb),
365  userdata))
366 
367  def write_async(self, address: str,
368  data: Variant, cb: ResponseCallback,
369  userdata: userData_c_void_p = None) -> Result:
370  """
371  Write an object. This function is synchronous: It will wait for the answer.
372  @param[in] address Address of the node to read metadata
373  @param[in] data Data of the object
374  @param[in] callback Callback to call when function is finished
375  @param[in] userdata User data - will be returned in callback as userdata. You can use this userdata to identify your request
376  @returns <Result>, status of function call
377  """
378  b_address = address.encode('utf-8')
379  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientWriteASync(self.__client,
380  b_address,
381  data.get_handle(),
382  self.__token__token,
383  self.__create_callback__create_callback(
384  cb),
385  userdata))
386 
387  def metadata_async(self, address: str,
388  cb: ResponseCallback, userdata: userData_c_void_p = None) -> Result:
389  """
390  Read metadata of an object. This function is asynchronous. It will return immediately. Callback will be called if function call is finished. Result data may be provided in callback function.
391  @param[in] address Address of the node to read metadata
392  @param[in] callback Callback to call when function is finished
393  @param[in] userdata User data - will be returned in callback as userdata. You can use this userdata to identify your request
394  @returns <Result>, status of function call
395  """
396  b_address = address.encode('utf-8')
397  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientMetadataASync(self.__client,
398  b_address,
399  self.__token__token,
400  self.__create_callback__create_callback(
401  cb),
402  userdata))
403 
404  def _unregister_sync(self, sub: ctrlxdatalayer.subscription.Subscription):
405  """ _unregister_sync """
406  if sub in self.__subs:
407  self.__subs.remove(sub)
408 
409  def create_subscription_sync(self, prop: Variant,
410  cnb: ctrlxdatalayer.subscription.ResponseNotifyCallback,
411  userdata: userData_c_void_p = None):
412  """
413  Set up a subscription
414  @param[in] ruleset Variant that describe ruleset of subscription as subscription.fbs
415  @param[in] publishCallback Callback to call when new data is available
416  @param[in] userdata User data - will be returned in publishCallback as userdata. You can use this userdata to identify your subscription
417  @result <Result>, status of function cal
418  """
420  r = sub._create(prop, cnb, userdata)
421  if r == Result.OK:
422  self.__subs.append(sub)
423  return r, sub
424 
425  def create_subscription_async(self,
426  prop: Variant,
427  cnb: ctrlxdatalayer.subscription.ResponseNotifyCallback,
428  cb: ResponseCallback,
429  userdata: userData_c_void_p = None):
430  """
431  Set up a subscription
432  @param[in] ruleset Variant that describe ruleset of subscription as subscription.fbs
433  @param[in] publishCallback Callback to call when new data is available
434  @param[in] userdata User data - will be returned in publishCallback as userdata. You can use this userdata to identify your subscription
435  @result <Result>, status of function cal
436  """
438  r = sub._create(prop, cnb, cb, userdata)
439  if r == Result.OK:
440  self.__subs.append(sub)
441  return r, sub
442 
443  def __close_all_subs(self):
444  """ __close_all_subs """
445  subs = self.__subs.copy()
446  self.__subs.clear()
447  for x in subs:
448  x.on_close()
449 
450  def read_json_sync(self,
451  conv: Converter,
452  address: str,
453  indent: int,
454  data: Variant = None):
455  """
456  This function reads a values as a JSON string
457  @param[in] converter Reference to the converter (see System json_converter())
458  @param[in] address Address of the node to read
459  @param[in] indentStep Indentation length for json string
460  @param[in] json Generated JSON as Variant (string)
461  @returns tuple (Result, Variant)
462  @return <Result>, status of function call,
463  @return <Variant>, Generated JSON as Variant (string)
464  """
465  b_address = address.encode('utf-8')
466  if data is None:
467  data = Variant()
468 
469  result = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientReadJsonSync(
470  self.__client, conv.get_handle(), b_address, data.get_handle(), indent, self.__token__token))
471  return result, data
472 
473  def write_json_sync(self,
474  conv: Converter,
475  address: str,
476  json: str):
477  """
478  This function writes a JSON value
479  @param[in] converter Reference to the converter (see System json_converter())
480  @param[in] address Address of the node to write
481  @param[in] json JSON value to write
482  @param[in,out] error Error of conversion as variant string
483  @return result status of the function
484  @returns tuple (Result, Variant)
485  @return <Result>, status of function call,
486  @return <Variant>, Error of conversion as variant string
487  """
488  b_address = address.encode('utf-8')
489  b_json = json.encode('utf-8')
490  error = Variant()
491  result = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_clientWriteJsonSync(
492  self.__client, conv.get_handle(), b_address, b_json, error.get_handle(), self.__token__token))
493  return result, error
494 
495  def create_bulk(self):
496  """
497  Setup a bulk object
498 
499  Returns:
500  Bulk: Bulk object
501  """
502  bulk = ctrlxdatalayer.bulk.Bulk(self)
503  return bulk
Client interface for accessing data from the system.
Definition: client.py:61
Result read_async_args(self, str address, Variant args, ResponseCallback cb, userData_c_void_p userdata=None)
Read an object.
Definition: client.py:365
def read_sync_args(self, str address, Variant args)
Read an object.
Definition: client.py:253
def __exit__(self, exc_type, exc_val, exc_tb)
use the python context manager
Definition: client.py:85
Result create_async(self, str address, Variant data, ResponseCallback cb, userData_c_void_p userdata=None)
Create an object.
Definition: client.py:306
Result browse_async(self, str address, ResponseCallback cb, userData_c_void_p userdata=None)
Browse an object.
Definition: client.py:339
def __enter__(self)
use the python context manager
Definition: client.py:79
Result remove_async(self, str address, ResponseCallback cb, userData_c_void_p userdata=None)
Remove an object.
Definition: client.py:321
Result set_timeout(self, TimeoutSetting timeout, int value)
Set client timeout value.
Definition: client.py:157
def get_token(self)
internal token
Definition: client.py:113
def __init__(self, C_DLR_CLIENT c_client)
Definition: client.py:68
def create_sync(self, str address, Variant data)
Create an object.
Definition: client.py:204
def create_subscription_async(self, Variant prop, ctrlxdatalayer.subscription.ResponseNotifyCallback cnb, ResponseCallback cb, userData_c_void_p userdata=None)
Set up a subscription.
Definition: client.py:446
Result ping_async(self, ResponseCallback cb, userData_c_void_p userdata=None)
Ping the next hop.
Definition: client.py:293
def read_sync(self, str address)
Read an object.
Definition: client.py:241
def close(self)
closes the client instance
Definition: client.py:91
def write_sync(self, str address, Variant data)
Write an object.
Definition: client.py:267
Result ping_sync(self)
Ping the next hop.
Definition: client.py:193
def get_handle(self)
handle value of Client
Definition: client.py:107
def read_json_sync(self, Converter conv, str address, int indent, Variant data=None)
This function reads a values as a JSON string.
Definition: client.py:476
def create_bulk(self)
Setup a bulk object.
Definition: client.py:513
str get_auth_token(self)
returns persistent security access token for authentication
Definition: client.py:183
def set_auth_token(self, str token)
Set persistent security access token for authentication as JWT payload.
Definition: client.py:172
bool is_connected(self)
returns whether provider is connected
Definition: client.py:165
def write_json_sync(self, Converter conv, str address, str json)
This function writes a JSON value.
Definition: client.py:499
Result metadata_async(self, str address, ResponseCallback cb, userData_c_void_p userdata=None)
Read metadata of an object.
Definition: client.py:403
def metadata_sync(self, str address)
Read metadata of an object.
Definition: client.py:280
Result write_async(self, str address, Variant data, ResponseCallback cb, userData_c_void_p userdata=None)
Write an object.
Definition: client.py:385
def create_subscription_sync(self, Variant prop, ctrlxdatalayer.subscription.ResponseNotifyCallback cnb, userData_c_void_p userdata=None)
Set up a subscription.
Definition: client.py:428
def __create_callback(self, ResponseCallback cb)
Definition: client.py:121
Result read_async(self, str address, ResponseCallback cb, userData_c_void_p userdata=None)
Read an object.
Definition: client.py:352
Result remove_sync(self, str address)
Remove an object.
Definition: client.py:215
def browse_sync(self, str address)
Browse an object.
Definition: client.py:227
Settings of different timeout values.
Definition: client.py:50
def set_ptr(self, ptr)
setter CallbackPtr
Definition: client.py:37
def get_ptr(self)
getter CallbackPtr
Definition: client.py:43
def __init__(self)
init _CallbackPtr
Definition: client.py:31
Variant is a container for a many types of data.
Definition: variant.py:150