ctrlX Data Layer API for Python  1.6.0
The ctrlX Data Layer API allows access to the ctrlX Data Layer with Python
subscription_sync.py
1 """
2 Class Sync Subscription
3 """
4 import ctypes
5 import typing
6 import weakref
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_NOTIFY_RESPONSE, C_NotifyItem
13 from datalayer.variant import Result, Variant
14 
15 
17  """
18  SubscriptionSync
19  """
20  __slots__ = ['__ptr_notify', '__closed', '__client', '__id']
21 
22  def __init__(self, client: datalayer.client.Client):
23  """
24  @param [in] client Reference to the client
25  """
27  self.__closed = False
28  self.__client = weakref.ref(client)
29  self.__id = ""
30 
31  def __enter__(self):
32  """
33  use the python context manager
34  """
35  return self
36 
37  def __exit__(self, exc_type, exc_val, exc_tb):
38  """
39  use the python context manager
40  """
41  self.close()
42 
43  def on_close(self):
44  """
45  on_close
46  """
47  self.close()
48 
49  def id(self) -> str:
50  """
51  Subscription ID
52 
53  @return <str> id
54  """
55  return self.__id
56 
57  def __create_sub_callback(self, cb: datalayer.subscription.ResponseNotifyCallback):
58  """
59  callback management
60  """
62  self.__ptr_notify = cb_ptr
63 
64  def _cb(status: datalayer.clib.C_DLR_RESULT, items: ctypes.POINTER(C_NotifyItem),
65  count: ctypes.c_uint32, userdata: ctypes.c_void_p):
66  """
67  datalayer calls this function
68  """
69  r = Result(status)
70  if r == Result.OK:
71  notify_items = []
72  for x in range(0, count):
74  items[x].data, items[x].info)
75  notify_items.append(n)
76  cb(r, notify_items, userdata)
77  del notify_items
78  return
79  cb(r, None, userdata)
80 
81  cb_ptr.set_ptr(C_DLR_CLIENT_NOTIFY_RESPONSE(_cb))
82  return cb_ptr.get_ptr()
83 
84  def _test_notify_callback(self, cb: datalayer.subscription.ResponseNotifyCallback):
85  """
86  internal use
87  """
88  return self.__create_sub_callback(cb)
89 
90  def close(self):
91  """
92  closes the client instance
93  """
94  if self.__closed:
95  return
96  self.__closed = True
97  self.unsubscribe_all()
98  self.__ptr_notify = None
99  self.__client = None
100 
101  def _create(self, prop: Variant, cnb: datalayer.subscription.ResponseNotifyCallback,
102  userdata: userData_c_void_p = None) -> Result:
103  """
104  Setup a subscription
105  @param[in] ruleset Variant that describe ruleset of subscription as subscription.fbs
106  @param[in] publishCallback Callback to call when new data is available
107  @param[in] userdata User data - will be returned in publishCallback as userdata. You can use this userdata to identify your subscription
108  @result <Result> status of function cal
109  """
110  r = Result(datalayer.clib.libcomm_datalayer.DLR_clientCreateSubscriptionSync(
111  self.__client().get_handle(), prop.get_handle(),
112  self.__create_sub_callback(cnb), userdata, self.__client().get_token()))
113  if r == Result.OK:
115  return r
116 
117  def subscribe(self, address: str) -> Result:
118  """
119  Adds a node to a subscription id
120  @param[in] address Address of a node, that should be added to the given subscription.
121  @result <Result> status of function call
122  """
123  b_id = self.id().encode('utf-8')
124  b_address = address.encode('utf-8')
125  return Result(datalayer.clib.libcomm_datalayer.DLR_clientSubscribeSync(
126  self.__client().get_handle(), b_id, b_address))
127 
128  def unsubscribe(self, address: str) -> Result:
129  """
130  Removes a node from a subscription id
131  @param[in] address Address of a node, that should be removed to the given subscription.
132  @result <Result> status of function call
133  """
134  b_id = self.id().encode('utf-8')
135  b_address = address.encode('utf-8')
136  return Result(datalayer.clib.libcomm_datalayer.DLR_clientUnsubscribeSync(
137  self.__client().get_handle(), b_id, b_address))
138 
139  def subscribe_multi(self, address: typing.List[str]) -> Result:
140  """
141  Adds a list of nodes to a subscription id
142  @param[in] address List of Addresses of a node, that should be added to the given subscription.
143  @param[in] count Count of addresses.
144  @result <Result> status of function call
145  """
146  b_id = self.id().encode('utf-8')
147  b_address = (ctypes.c_char_p * len(address))(*
148  [d.encode('utf-8') for d in address])
149  return Result(datalayer.clib.libcomm_datalayer.DLR_clientSubscribeMultiSync(
150  self.__client().get_handle(), b_id, b_address, len(address)))
151 
152  def unsubscribe_multi(self, address: typing.List[str]) -> Result:
153  """
154  Removes a set of nodes from a subscription id
155  @param[in] address Set of addresses of nodes, that should be removed to the given subscription.
156  @result <Result> status of function call
157  """
158  b_id = self.id().encode('utf-8')
159  b_address = (ctypes.c_char_p * len(address))(*
160  [d.encode('utf-8') for d in address])
161  return Result(datalayer.clib.libcomm_datalayer.DLR_clientUnsubscribeMultiSync(
162  self.__client().get_handle(), b_id, b_address, len(address)))
163 
164  def unsubscribe_all(self) -> Result:
165  """
166  Removes subscription id completely
167  @result <Result> status of function call
168  """
169  if self.__client is None:
170  return
171  self.__client()._unregister_sync(self)
172  b_id = self.id().encode('utf-8')
173  return Result(datalayer.clib.libcomm_datalayer.DLR_clientUnsubscribeAllSync(
174  self.__client().get_handle(), b_id))
datalayer.subscription.Subscription
Subscription.
Definition: subscription.py:119
datalayer.variant.Result
Definition: variant.py:19
datalayer.subscription_sync.SubscriptionSync.__client
__client
Definition: subscription_sync.py:28
datalayer.client._CallbackPtr
Callback wrapper.
Definition: client.py:25
datalayer.variant
Definition: variant.py:1
datalayer.subscription.Subscription.id
str id(self)
Subscription ID.
Definition: subscription.py:124
datalayer.subscription_sync.SubscriptionSync.id
str id(self)
Subscription ID.
Definition: subscription_sync.py:54
datalayer.clib
Definition: clib.py:1
datalayer.subscription_sync.SubscriptionSync.close
def close(self)
closes the client instance
Definition: subscription_sync.py:99
datalayer.subscription.NotifyItem
NotifyItem.
Definition: subscription.py:30
datalayer.subscription_sync.SubscriptionSync.__exit__
def __exit__(self, exc_type, exc_val, exc_tb)
use the python context manager
Definition: subscription_sync.py:40
datalayer.subscription
Definition: subscription.py:1
datalayer.subscription_sync.SubscriptionSync.unsubscribe_multi
Result unsubscribe_multi(self, typing.List[str] address)
Removes a set of nodes from a subscription id.
Definition: subscription_sync.py:165
datalayer.subscription_sync.SubscriptionSync.__ptr_notify
__ptr_notify
Definition: subscription_sync.py:64
datalayer.subscription_sync.SubscriptionSync.__closed
__closed
Definition: subscription_sync.py:27
datalayer.subscription_sync.SubscriptionSync.__create_sub_callback
def __create_sub_callback(self, datalayer.subscription.ResponseNotifyCallback cb)
callback management
Definition: subscription_sync.py:62
datalayer.subscription_sync.SubscriptionSync.subscribe_multi
Result subscribe_multi(self, typing.List[str] address)
Adds a list of nodes to a subscription id.
Definition: subscription_sync.py:153
datalayer.subscription.get_id
str get_id(Variant prop)
get_id
Definition: subscription.py:162
datalayer.subscription_sync.SubscriptionSync.__id
__id
Definition: subscription_sync.py:29
datalayer.subscription_sync.SubscriptionSync.subscribe
Result subscribe(self, str address)
Adds a node to a subscription id.
Definition: subscription_sync.py:130
datalayer.subscription_sync.SubscriptionSync
SubscriptionSync.
Definition: subscription_sync.py:19
datalayer.subscription_sync.SubscriptionSync.__enter__
def __enter__(self)
use the python context manager
Definition: subscription_sync.py:34
datalayer.subscription_sync.SubscriptionSync.on_close
def on_close(self)
on_close
Definition: subscription_sync.py:46
datalayer.subscription_sync.SubscriptionSync.unsubscribe
Result unsubscribe(self, str address)
Removes a node from a subscription id.
Definition: subscription_sync.py:141
datalayer.client.Client
Client interface for accessing data from the system.
Definition: client.py:61
datalayer.subscription_sync.SubscriptionSync.__init__
def __init__(self, datalayer.client.Client client)
Definition: subscription_sync.py:25
datalayer.subscription_sync.SubscriptionSync.unsubscribe_all
Result unsubscribe_all(self)
Removes subscription id completely.
Definition: subscription_sync.py:176