ctrlX Data Layer API for Python  3.3.0
The ctrlX Data Layer API allows access to the ctrlX Data Layer with Python
subscription.py
1 """
2 Class Subscription
3 """
4 import datetime
5 import enum
6 import typing
7 
8 import comm.datalayer.NotifyInfo
9 import comm.datalayer.SubscriptionProperties
10 import flatbuffers
11 
12 import ctrlxdatalayer
13 from ctrlxdatalayer.clib_variant import C_DLR_VARIANT
14 from ctrlxdatalayer.variant import Result, Variant, VariantRef, VariantType
15 
16 
17 class NotifyType(enum.Enum):
18  """
19  NotifyType
20 
21  """
22  DATA = 0
23  BROWSE = 1
24  METADATA = 2
25 
26 
27 class NotifyItem:
28  """
29  NotifyItem
30  """
31 
32  __slots__ = ['__data', '__info']
33 
34  def __init__(self, data: C_DLR_VARIANT, info: C_DLR_VARIANT):
35  """
36  @param[in] data of the notify item
37  @param[in] containing notify_info.fbs
38  """
39  self.__data__data = VariantRef(data)
40  i = VariantRef(info)
41  if i.get_type() != VariantType.FLATBUFFERS:
42  return
43  b = i.get_flatbuffers()
44  self.__info__info = comm.datalayer.NotifyInfo.NotifyInfo.GetRootAsNotifyInfo(
45  b, 0)
46 
47  def get_data(self) -> Variant:
48  """
49  data of the notify item
50 
51  @returns <Variant>
52  """
53  return self.__data__data
54 
55  def get_address(self) -> str:
56  """
57  Node address
58 
59  @returns <str>
60  """
61  return self.__info__info.Node().decode("utf-8")
62 
63  def get_type(self) -> NotifyType:
64  """
65  Notify type
66 
67  @returns <NotifyType>
68  """
69  return NotifyType(self.__info__info.NotifyType())
70 
71  def get_timestamp(self) -> int:
72  """
73  uint64; // Filetime: Contains a 64-bit value representing the number of
74  100-nanosecond intervals since January 1, 1601 (UTC).
75 
76  @returns <int>
77  """
78  return self.__info__info.Timestamp()
79 
80 
81 # http://support.microsoft.com/kb/167296
82 # How To Convert a UNIX time_t to a Win32 FILETIME or SYSTEMTIME
83 EPOCH_AS_FILETIME = 116444736000000000 # January 1, 1970, as MS file time
84 HUNDREDS_OF_NANOSECONDS = 10000000
85 
86 
87 def to_datetime(filetime: int) -> datetime.datetime:
88  """Converts a Microsoft filetime number to a Python datetime. The new
89  datetime object is time zone-naive but is equivalent to tzinfo=utc.
90  >>> filetime_to_dt(116444736000000000)
91  datetime.datetime(1970, 1, 1, 0, 0)
92  >>> filetime_to_dt(128930364000000000)
93  datetime.datetime(2009, 7, 25, 23, 0)
94  """
95  return datetime.datetime.fromtimestamp(
96  (filetime - EPOCH_AS_FILETIME) / HUNDREDS_OF_NANOSECONDS, tz=datetime.timezone.utc)
97 
98 
99 ResponseNotifyCallback = typing.Callable[[
100  Result, typing.List[NotifyItem], ctrlxdatalayer.clib.userData_c_void_p], None]
101 """
102  ResponseNotifyCallback
103  This callback delivers a vector with the updated nodes of a subscription.
104  It is usually called in the interval given by the publishInterval
105  which has been set by the creation of the subscription.
106  The callback may not contain all nodes of the subscription.I.e.when a node did not change.
107  The callback may contain a node multiple times.I.e.when the node did
108  change multiple times since the last callback.
109  The sorting order of the items in the vector is undefined.
110  @param[in] status Notify status
111  @param[in] items Notify data
112  @param[in] userdata Same userdata as in create subscription given
113  Result != OK the list of NotifyItem is None
114 """
115 
116 
117 class Subscription:
118  """
119  Subscription
120  """
121 
122  def id(self) -> str:
123  """
124  Subscription ID
125  """
126  pass
127 
128  def on_close(self):
129  """
130  notification of the close
131  """
132  pass
133 
134 
135 def create_properties(ident: str, publish_interval: int = 1000,
136  keepalive_interval: int = 60000, error_interval: int = 10000) -> Variant:
137  """
138  create_properties
139  @returns <Variant> Variant that describe ruleset of subscription
140  """
141  builder = flatbuffers.Builder(1024)
142  # Hint: CreateString must be done beforehand
143  idl = builder.CreateString(ident)
144  comm.datalayer.SubscriptionProperties.SubscriptionPropertiesStart(
145  builder)
146  comm.datalayer.SubscriptionProperties.SubscriptionPropertiesAddId(
147  builder, idl)
148  comm.datalayer.SubscriptionProperties.SubscriptionPropertiesAddPublishInterval(
149  builder, publish_interval)
150  comm.datalayer.SubscriptionProperties.SubscriptionPropertiesAddKeepaliveInterval(
151  builder, keepalive_interval)
152  comm.datalayer.SubscriptionProperties.SubscriptionPropertiesAddErrorInterval(
153  builder, error_interval)
154  prop = comm.datalayer.SubscriptionProperties.SubscriptionPropertiesEnd(
155  builder)
156  builder.Finish(prop)
157  v = Variant()
158  v.set_flatbuffers(builder.Output())
159  return v
160 
161 
162 def get_id(prop: Variant) -> str:
163  """ get_id """
164  if prop.get_type() != VariantType.FLATBUFFERS:
165  return ""
166  b = prop.get_flatbuffers()
167  p = comm.datalayer.SubscriptionProperties.SubscriptionProperties.GetRootAsSubscriptionProperties(
168  b, 0)
169  return p.Id().decode("utf-8")
str get_address(self)
Node address.
Definition: subscription.py:60
NotifyType get_type(self)
Notify type.
Definition: subscription.py:68
def __init__(self, C_DLR_VARIANT data, C_DLR_VARIANT info)
Definition: subscription.py:38
Variant get_data(self)
data of the notify item
Definition: subscription.py:52
def on_close(self)
notification of the close