ctrlX Data Layer API for Python  1.6.0
The ctrlX Data Layer API allows access to the ctrlX Data Layer with Python
client.py
1 """
2 class Client
3 """
4 import ctypes
5 import typing
6 from enum import Enum
7 
8 import datalayer
9 import datalayer.clib
11 from datalayer.clib import userData_c_void_p
12 from datalayer.clib_client import C_DLR_CLIENT, C_DLR_CLIENT_RESPONSE
13 from datalayer.variant import Result, Variant, VariantRef
14 from datalayer.converter import Converter
15 
16 ResponseCallback = typing.Callable[[
17  Result, typing.Optional[Variant], datalayer.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: typing.Optional[ctypes._CFuncPtr] = None
31 
32  def set_ptr(self, ptr):
33  """
34  setter CallbackPtr
35  """
36  self._ptr = ptr
37 
38  def get_ptr(self):
39  """
40  getter CallbackPtr
41  """
42  return self._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 = False
68  self.__client: C_DLR_CLIENT = c_client
69  self.__token = None
70  self.__ptrs: typing.List[_CallbackPtr] = []
71  self.__subs: typing.List[datalayer.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.close()
85 
86  def close(self):
87  """
88  closes the client instance
89  """
90  if self.__closed:
91  return
92  self.__closed = True
93  self.__close_all_subs()
94  self.__subs.clear()
95 
96  datalayer.clib.libcomm_datalayer.DLR_clientDelete(self.__client)
97  self.__ptrs.clear()
98  del self.__subs
99  del self.__ptrs
100  self.__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
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: datalayer.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(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(datalayer.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 datalayer.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.encode('utf-8')
168  datalayer.clib.libcomm_datalayer.DLR_clientSetAuthToken(
169  self.__client, self.__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 = datalayer.clib.libcomm_datalayer.DLR_clientGetAuthToken(
177  self.__client)
178  self.__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(datalayer.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(datalayer.clib.libcomm_datalayer.DLR_clientCreateSync(
199  self.__client, b_address, data.get_handle(), self.__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(datalayer.clib.libcomm_datalayer.DLR_clientRemoveSync(
210  self.__client, b_address, self.__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(datalayer.clib.libcomm_datalayer.DLR_clientBrowseSync(
223  self.__client, b_address, data.get_handle(), self.__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_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(datalayer.clib.libcomm_datalayer.DLR_clientReadSync(
248  self.__client, b_address, args.get_handle(), self.__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(datalayer.clib.libcomm_datalayer.DLR_clientWriteSync(
262  self.__client, b_address, data.get_handle(), self.__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(datalayer.clib.libcomm_datalayer.DLR_clientMetadataSync(
276  self.__client, b_address, data.get_handle(), self.__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(datalayer.clib.libcomm_datalayer.DLR_clientPingASync(
287  self.__client, self.__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(datalayer.clib.libcomm_datalayer.DLR_clientCreateASync(
301  self.__client, b_address, data.get_handle(),
302  self.__token, self.__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(datalayer.clib.libcomm_datalayer.DLR_clientRemoveASync(self.__client,
316  b_address,
317  self.__token,
318  self.__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(datalayer.clib.libcomm_datalayer.DLR_clientBrowseASync(
334  self.__client, b_address, self.__token, self.__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_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(datalayer.clib.libcomm_datalayer.DLR_clientReadASync(self.__client,
360  b_address,
361  args.get_handle(),
362  self.__token,
363  self.__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(datalayer.clib.libcomm_datalayer.DLR_clientWriteASync(self.__client,
380  b_address,
381  data.get_handle(),
382  self.__token,
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(datalayer.clib.libcomm_datalayer.DLR_clientMetadataASync(self.__client,
398  b_address,
399  self.__token,
400  self.__create_callback(
401  cb),
402  userdata))
403 
404  def _unregister_sync(self, sub: datalayer.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: datalayer.subscription.ResponseNotifyCallback,
411  userdata: userData_c_void_p = None):
412  """
413  Setup 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: datalayer.subscription.ResponseNotifyCallback,
428  cb: ResponseCallback,
429  userdata: userData_c_void_p = None):
430  """
431  Setup 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  indet: int):
454  """
455  This function reads a values as a JSON string
456  @param[in] converter Reference to the converter (see System json_converter())
457  @param[in] address Address of the node to read
458  @param[in] indentStep Indentation length for json string
459  @returns tuple (Result, Variant)
460  @return <Result>, status of function call,
461  @return <Variant>, Generated JSON as Variant (string)
462  """
463  b_address = address.encode('utf-8')
464  data = Variant()
465  result = Result(datalayer.clib.libcomm_datalayer.DLR_clientReadJsonSync(
466  self.__client, conv.get_handle(), b_address, data.get_handle(), indet, self.__token))
467  return result, data
468 
469  def write_json_sync(self,
470  conv: Converter,
471  address: str,
472  json: str):
473  """
474  This function writes a JSON value
475  @param[in] converter Reference to the converter (see System json_converter())
476  @param[in] address Address of the node to write
477  @param[out] json JSON value to write
478  @param[in,out] error Error of conversion as variant string
479  @return result status of the function
480  @returns tuple (Result, Variant)
481  @return <Result>, status of function call,
482  @return <Variant>, Error of conversion as variant string
483  """
484  b_address = address.encode('utf-8')
485  b_json = json.encode('utf-8')
486  error = Variant()
487  result = Result(datalayer.clib.libcomm_datalayer.DLR_clientWriteJsonSync(
488  self.__client, conv.get_handle(), b_address, b_json, error.get_handle(), self.__token))
489  return result, error
datalayer.client.Client.create_sync
def create_sync(self, str address, Variant data)
Create an object.
Definition: client.py:204
datalayer.client.Client.close
def close(self)
closes the client instance
Definition: client.py:91
datalayer.client.Client.__init__
def __init__(self, C_DLR_CLIENT c_client)
Definition: client.py:68
datalayer.subscription.Subscription
Subscription.
Definition: subscription.py:119
datalayer.client._CallbackPtr._ptr
_ptr
Definition: client.py:38
datalayer.client.Client.read_json_sync
def read_json_sync(self, Converter conv, str address, int indet)
This function reads a values as a JSON string.
Definition: client.py:471
datalayer.client._CallbackPtr.__init__
def __init__(self)
init _CallbackPtr
Definition: client.py:31
datalayer.client._CallbackPtr.set_ptr
def set_ptr(self, ptr)
setter CallbackPtr
Definition: client.py:37
datalayer.client.Client.write_sync
def write_sync(self, str address, Variant data)
Write an object.
Definition: client.py:267
datalayer.variant.Result
Definition: variant.py:19
datalayer.client.Client.metadata_sync
def metadata_sync(self, str address)
Read metadata of an object.
Definition: client.py:280
datalayer.client._CallbackPtr
Callback wrapper.
Definition: client.py:25
datalayer.variant
Definition: variant.py:1
datalayer.client.Client.browse_sync
def browse_sync(self, str address)
Browse an object.
Definition: client.py:227
datalayer.variant.Variant
Variant is a container for a many types of data.
Definition: variant.py:131
datalayer.client.Client.__enter__
def __enter__(self)
use the python context manager
Definition: client.py:79
datalayer.client._CallbackPtr.get_ptr
def get_ptr(self)
getter CallbackPtr
Definition: client.py:43
datalayer.client.Client.get_handle
def get_handle(self)
handle value of Client
Definition: client.py:107
datalayer.client.Client.browse_async
Result browse_async(self, str address, ResponseCallback cb, userData_c_void_p userdata=None)
Browse an object.
Definition: client.py:337
datalayer.client.Client.create_subscription_async
def create_subscription_async(self, Variant prop, datalayer.subscription.ResponseNotifyCallback cnb, ResponseCallback cb, userData_c_void_p userdata=None)
Setup a subscription.
Definition: client.py:442
datalayer.client.Client.__closed
__closed
Definition: client.py:69
datalayer.client.Client.remove_sync
Result remove_sync(self, str address)
Remove an object.
Definition: client.py:215
datalayer.client.Client.write_json_sync
def write_json_sync(self, Converter conv, str address, str json)
This function writes a JSON value.
Definition: client.py:492
datalayer.converter
Definition: converter.py:1
datalayer.client.Client.remove_async
Result remove_async(self, str address, ResponseCallback cb, userData_c_void_p userdata=None)
Remove an object.
Definition: client.py:319
datalayer.clib
Definition: clib.py:1
datalayer.client.Client.read_async
Result read_async(self, str address, ResponseCallback cb, userData_c_void_p userdata=None)
Read an object.
Definition: client.py:351
datalayer.client.Client.get_auth_token
str get_auth_token(self)
returns persistent security access token for authentication
Definition: client.py:183
datalayer.client.TimeoutSetting
Settings of different timeout values.
Definition: client.py:50
datalayer.client.Client.read_sync_args
def read_sync_args(self, str address, Variant args)
Read an object.
Definition: client.py:253
datalayer.client.Client.read_sync
def read_sync(self, str address)
Read an object.
Definition: client.py:241
datalayer.client.Client.get_token
def get_token(self)
internal token
Definition: client.py:113
datalayer.client.Client.__close_all_subs
def __close_all_subs(self)
__close_all_subs
Definition: client.py:456
datalayer.subscription
Definition: subscription.py:1
datalayer.client.Client.create_async
Result create_async(self, str address, Variant data, ResponseCallback cb, userData_c_void_p userdata=None)
Create an object.
Definition: client.py:305
datalayer.client.Client.create_subscription_sync
def create_subscription_sync(self, Variant prop, datalayer.subscription.ResponseNotifyCallback cnb, userData_c_void_p userdata=None)
Setup a subscription.
Definition: client.py:426
datalayer.subscription_async.SubscriptionAsync
SubscriptionAsync.
Definition: subscription_async.py:21
datalayer.client.Client.__create_callback
def __create_callback(self, ResponseCallback cb)
callback management
Definition: client.py:121
datalayer.client.Client.is_connected
bool is_connected(self)
returns whether provider is connected
Definition: client.py:165
datalayer.client.Client.ping_async
Result ping_async(self, ResponseCallback cb, userData_c_void_p userdata=None)
Ping the next hop.
Definition: client.py:293
datalayer.client.Client.write_async
Result write_async(self, str address, Variant data, ResponseCallback cb, userData_c_void_p userdata=None)
Write an object.
Definition: client.py:383
datalayer.client.Client.read_async_args
Result read_async_args(self, str address, Variant args, ResponseCallback cb, userData_c_void_p userdata=None)
Read an object.
Definition: client.py:364
datalayer.client.Client.set_timeout
Result set_timeout(self, TimeoutSetting timeout, int value)
Set client timeout value.
Definition: client.py:157
datalayer.client.Client.ping_sync
Result ping_sync(self)
Ping the next hop.
Definition: client.py:193
datalayer.client.Client.set_auth_token
def set_auth_token(self, str token)
Set persistent security access token for authentication as JWT payload.
Definition: client.py:172
datalayer.subscription_sync.SubscriptionSync
SubscriptionSync.
Definition: subscription_sync.py:19
datalayer.client.Client.metadata_async
Result metadata_async(self, str address, ResponseCallback cb, userData_c_void_p userdata=None)
Read metadata of an object.
Definition: client.py:402
datalayer.client.Client.__exit__
def __exit__(self, exc_type, exc_val, exc_tb)
use the python context manager
Definition: client.py:85
datalayer.variant.VariantRef
Definition: variant.py:732
datalayer.client.Client
Client interface for accessing data from the system.
Definition: client.py:61
datalayer.client.Client.__token
__token
Definition: client.py:71