ctrlX Data Layer API for Python  3.4.0
The ctrlX Data Layer API allows access to the ctrlX Data Layer with Python
provider.py
1 """
2 Class Provider
3 """
4 # pylint: disable=C0301
5 import typing
6 import ctrlxdatalayer
7 from ctrlxdatalayer.clib_provider import C_DLR_PROVIDER
8 from ctrlxdatalayer.provider_node import ProviderNode
9 from ctrlxdatalayer.variant import Result, Variant
10 
11 
12 class Provider:
13  """
14  Provider interface to manage provider nodes
15  Hint: see python context manager for instance handling
16  """
17 
18  __slots__ = ['__provider', '__closed']
19 
20  def __init__(self, c_provider: C_DLR_PROVIDER):
21  """
22  generate Provider
23  """
24  self.__provider: C_DLR_PROVIDER = c_provider
25  self.__closed__closed = False
26 
27  def __enter__(self):
28  """
29  use the python context manager
30  """
31  return self
32 
33  def __exit__(self, exc_type, exc_val, exc_tb):
34  """
35  use the python context manager
36  """
37  self.closeclose()
38 
39  def close(self):
40  """
41  closes the provider instance
42  """
43  if self.__closed__closed:
44  return
45  self.__closed__closed = True
46  ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerStop(self.__provider)
47  ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerDelete(
48  self.__provider)
49 
50  def register_type(self, address: str, pathname: str) -> Result:
51  """
52  Register a type to the datalayer
53  @param[in] address Address of the node to register (no wildcards allowed)
54  @param[in] pathname Path to flatbuffer bfbs
55  @returns <Result>, status of function call
56  """
57  b_address = address.encode('utf-8')
58  b_pathname = pathname.encode('utf-8')
59  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerRegisterType(self.__provider, b_address, b_pathname))
60 
61  def unregister_type(self, address: str) -> Result:
62  """
63  Unregister a type from the datalayer
64  @param[in] address Address of the node to register (wildcards allowed)
65  @returns <Result>, status of function call
66  """
67  b_address = address.encode('utf-8')
68  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerUnregisterType(self.__provider, b_address))
69 
70  def register_node(self, address: str, node: ProviderNode) -> Result:
71  """
72  Register a node to the datalayer
73  @param[in] address Address of the node to register (wildcards allowed)
74  @param[in] node Node to register
75  @returns <Result>, status of function call
76  """
77  b_address = address.encode('utf-8')
78  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerRegisterNode(self.__provider, b_address, node.get_handle()))
79 
80  def unregister_node(self, address: str) -> Result:
81  """
82  Unregister a node from the datalayer
83  @param[in] address Address of the node to register (wildcards allowed)
84  @returns <Result>, status of function call
85  """
86  b_address = address.encode('utf-8')
87  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerUnregisterNode(self.__provider, b_address))
88 
89  def set_timeout_node(self, node: ProviderNode, timeout_ms: int) -> Result:
90  """
91  Set timeout for a node for asynchron requests (default value is 1000ms)
92  @param[in] node Node to set timeout for
93  @param[in] timeoutMS Timeout in milliseconds for this node
94  @returns <Result>, status of function call
95  """
96  if node is None:
97  return Result.FAILED
98  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerSetTimeoutNode(self.__provider, node.get_handle(), timeout_ms))
99 
100  def start(self) -> Result:
101  """
102  Start the provider
103  @returns <Result>, status of function call
104  """
105  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerStart(self.__provider))
106 
107  def stop(self) -> Result:
108  """
109  Stop the provider
110  @returns <Result>, status of function call
111  """
112  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerStop(self.__provider))
113 
114  def is_connected(self) -> bool:
115  """
116  returns whether provider is connected
117  @returns status of connection
118  """
119  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerIsConnected(self.__provider)
120 
121  def get_token(self) -> Variant:
122  """
123  return the current token of the current request.You can call this function during your onRead, onWrite, ... methods of your ProviderNodes. If there is no current request the method return an empty token
124  @returns <Variant> current token
125  """
126  return Variant(ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerGetToken(self.__provider))
127 
128  def register_type_variant(self, address: str, data: Variant) -> Result:
129  """
130  Register a type to the datalayer
131  @param[in] address Address of the node to register (no wildcards allowed)
132  @param[in] data Variant with flatbuffer type
133  @returns <Result>, status of function call
134  """
135  b_address = address.encode('utf-8')
136  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerRegisterTypeVariant(self.__provider, b_address, data.get_handle()))
137 
138  def get_registered_type(self, address: str):
139  """get_registered_type
140 
141  Get the variant of a registered type
142  @param[in] address Address of the type to get type (no wildcards allowed)
143  result status of function call and Variant to stored type
144  """
145  b_address = address.encode('utf-8')
146  data = Variant()
147  result = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerGetRegisteredType(self.__provider, b_address, data.get_handle()))
148  return result, data
149 
150 
151  def get_registered_node_paths(self):
152  """get_registered_node_paths
153 
154  return status of the function call and the current registered node paths
155  """
156  with Variant() as data:
157  result = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerGetRegisteredNodePaths(self.__provider, data.get_handle()))
158  if result != Result.OK:
159  return result, []
160  return result, data.get_array_string()
161 
162  def get_rejected_node_paths(self):
163  """get_rejected_node_paths
164 
165  return status of the function call and the current rejected node paths
166  """
167  with Variant() as data:
168  result = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerGetRejectedNodePaths(self.__provider, data.get_handle()))
169  if result != Result.OK:
170  return result, []
171  return result, data.get_array_string()
172 
173  def publish_event(self, data: Variant, event_type: Variant) -> Result:
174  """publish_event
175 
176  Publishes an event
177  @param[in] data The payload data of the event. Has to match the type, that is given in the notifyInfo.
178  @param[in] notifyInfo Contains additional info about the event with type event_info.fbs
179  @returns <Result>, status of function call
180  """
181  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_providerPublishEvent(self.__provider, data.get_handle(), event_type.get_handle()))
Variant get_token(self)
return the current token of the current request.You can call this function during your onRead,...
Definition: provider.py:125
Result stop(self)
Stop the provider.
Definition: provider.py:111
Result register_type_variant(self, str address, Variant data)
Register a type to the datalayer.
Definition: provider.py:134
def __exit__(self, exc_type, exc_val, exc_tb)
use the python context manager
Definition: provider.py:36
def get_rejected_node_paths(self)
get_rejected_node_paths
Definition: provider.py:166
Result register_type(self, str address, str pathname)
Register a type to the datalayer.
Definition: provider.py:56
def __enter__(self)
use the python context manager
Definition: provider.py:30
Result unregister_node(self, str address)
Unregister a node from the datalayer.
Definition: provider.py:85
Result start(self)
Start the provider.
Definition: provider.py:104
def close(self)
closes the provider instance
Definition: provider.py:42
def __init__(self, C_DLR_PROVIDER c_provider)
generate Provider
Definition: provider.py:23
def get_registered_type(self, str address)
get_registered_type
Definition: provider.py:144
bool is_connected(self)
returns whether provider is connected
Definition: provider.py:118
Result unregister_type(self, str address)
Unregister a type from the datalayer.
Definition: provider.py:66
def get_registered_node_paths(self)
get_registered_node_paths
Definition: provider.py:155
Result register_node(self, str address, ProviderNode node)
Register a node to the datalayer.
Definition: provider.py:76
Result set_timeout_node(self, ProviderNode node, int timeout_ms)
Set timeout for a node for asynchron requests (default value is 1000ms)
Definition: provider.py:95
Result publish_event(self, Variant data, Variant event_type)
publish_event
Definition: provider.py:180
Variant is a container for a many types of data.
Definition: variant.py:150