ctrlX Data Layer API for Python  3.3.0
The ctrlX Data Layer API allows access to the ctrlX Data Layer with Python
variant.py
1 """
2  Variant class
3 """
4 import ctypes
5 import datetime
6 import typing
7 from enum import Enum
8 
9 import ctrlxdatalayer
10 from ctrlxdatalayer.clib_variant import C_DLR_VARIANT
11 
12 FILE_TIME_EPOCH = datetime.datetime(1601, 1, 1)
13 # FILETIME counts 100 nanoseconds intervals = 0.1 microseconds, so 10 of those are 1 microsecond
14 FILE_TIME_MICROSECOND = 10
15 
16 
17 class Result(Enum):
18  """
19  Result(Enum)
20 
21  status of function call
22  """
23  OK = 0
24  OK_NO_CONTENT = 0x00000001
25  FAILED = 0x80000001
26  # application
27  INVALID_ADDRESS = 0x80010001
28  UNSUPPORTED = 0x80010002
29  OUT_OF_MEMORY = 0x80010003
30  LIMIT_MIN = 0x80010004
31  LIMIT_MAX = 0x80010005
32  TYPE_MISMATCH = 0x80010006
33  SIZE_MISMATCH = 0x80010007
34  INVALID_FLOATINGPOINT = 0x80010009
35  INVALID_HANDLE = 0x8001000A
36  INVALID_OPERATION_MODE = 0x8001000B
37  INVALID_CONFIGURATION = 0x8001000C
38  INVALID_VALUE = 0x8001000D
39  SUBMODULE_FAILURE = 0x8001000E
40  TIMEOUT = 0x8001000F
41  ALREADY_EXISTS = 0x80010010
42  CREATION_FAILED = 0x80010011
43  VERSION_MISMATCH = 0x80010012
44  DEPRECATED = 0x80010013
45  PERMISSION_DENIED = 0x80010014
46  NOT_INITIALIZED = 0x80010015
47  MISSING_ARGUMENT = 0x80010016
48  TOO_MANY_ARGUMENTS = 0x80010017
49  RESOURCE_UNAVAILABLE = 0x80010018
50  COMMUNICATION_ERROR = 0x80010019
51  TOO_MANY_OPERATIONS = 0x8001001A
52  WOULD_BLOCK = 0x8001001B
53 
54  # communication
55  COMM_PROTOCOL_ERROR = 0x80020001
56  COMM_INVALID_HEADER = 0x80020002
57  # client
58  CLIENT_NOT_CONNECTED = 0x80030001
59  # provider
60  # broker
61  # realtime related error codes
62  RT_NOT_OPEN = 0x80060001
63  RT_INVALID_OBJECT = 0x80060002
64  RT_WRONG_REVISION = 0x80060003
65  RT_NO_VALID_DATA = 0x80060004
66  RT_MEMORY_LOCKED = 0x80060005
67  RT_INVALID_MEMORY_MAP = 0x80060006
68  RT_INVALID_RETAIN = 0x80060007
69  RT_INVALID_ERROR = 0x80060008
70  RT_MALLOC_FAILED = 0x80060009
71 
72  # security
73  sec_noSEC_NO_TOKEN_token = 0x80070001
74  SEC_INVALID_SESSION = 0x80070002
75  SEC_INVALID_TOKEN_CONTENT = 0x80070003
76  SEC_UNAUTHORIZED = 0x80070004
77  SEC_PAYMENT_REQUIRED = 0x80070005
78 
79  @classmethod
80  def _missing_(cls, value):
81  """
82  _missing_ function
83  """
84  i = 0xFFFFFFFF & value
85  if i == 0x00000001:
86  return cls(i)
87  if i == 0x80000001:
88  return cls(i)
89  if (i >= 0x80010001) and (i <= Result.WOULD_BLOCK.value):
90  return cls(i)
91  if (i >= 0x80020001) and (i <= 0x80020002):
92  return cls(i)
93  if i == 0x80030001:
94  return cls(i)
95  if (i >= 0x80060001) and (i <= Result.RT_MALLOC_FAILED.value):
96  return cls(i)
97  if (i >= 0x80070001) and (i <= Result.SEC_PAYMENT_REQUIRED.value):
98  return cls(i)
99  return value
100 
101 
102 class VariantType(Enum):
103  """
104  VariantType(Enum)
105 
106  type of variant
107  """
108  UNKNON = 0
109  BOOL8 = 1
110  INT8 = 2
111  UINT8 = 3
112  INT16 = 4
113  UINT16 = 5
114  INT32 = 6
115  UINT32 = 7
116  INT64 = 8
117  UINT64 = 9
118  FLOAT32 = 10
119  FLOAT64 = 11
120  STRING = 12
121  ARRAY_BOOL8 = 13
122  ARRAY_INT8 = 14
123  ARRAY_UINT8 = 15
124  ARRAY_INT16 = 16
125  ARRAY_UINT16 = 17
126  ARRAY_INT32 = 18
127  ARRAY_UINT32 = 19
128  ARRAY_INT64 = 20
129  ARRAY_UINT64 = 21
130  ARRAY_FLOAT32 = 22
131  ARRAY_FLOAT64 = 23
132  ARRAY_STRING = 24
133  RAW = 25
134  FLATBUFFERS = 26
135  TIMESTAMP = 27
136  ARRAY_OF_TIMESTAMP = 28
137 
138 
139 class Variant:
140  """
141  Variant is a container for a many types of data.
142 
143  Hint: see python context manager for instance handling
144  """
145 
146  __slots__ = ['_variant', '__closed']
147 
148  def __init__(self, c_variant: C_DLR_VARIANT = None):
149  """
150  generate Variant
151  """
152  self.__closed__closed = False
153  if c_variant is None:
154  self._variant_variant = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantCreate()
155  else:
156  self.__closed__closed = True
157  self._variant_variant = c_variant
158 
159  def __enter__(self):
160  """
161  use the python context manager
162  """
163  return self
164 
165  def __exit__(self, exc_type, exc_val, exc_tb):
166  """
167  use the python context manager
168  """
169  self.closeclose()
170 
171  def __del__(self):
172  """
173  __del__
174  """
175  self.closeclose()
176 
177  def close(self):
178  """
179  closes the variant instance
180  """
181  if self.__closed__closed:
182  return
183  self.__closed__closed = True
184  ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantDelete(self._variant_variant)
185 
186  def get_handle(self):
187  """
188  handle value of variant
189  """
190  return self._variant_variant
191 
192  def get_type(self) -> VariantType:
193  """
194  Returns the type of the variant
195  @returns <VariantType>
196  """
197  return VariantType(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetType(self._variant_variant))
198 
199  def get_data(self) -> bytearray:
200  """
201  Returns the pointer to the data of the variant
202  @returns array of bytes
203  """
204  length = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetSize(
205  self._variant_variant)
206  c_data = ctypes.string_at(
207  ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetData(self._variant_variant), length)
208  return bytearray(c_data)
209 
210  def get_size(self) -> int:
211  """
212  @returns size of the type in bytes
213  """
214  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetSize(self._variant_variant)
215 
216  def get_count(self) -> int:
217  """
218  Returns the count of elements in the variant (scalar data types = 1, array = count of elements in array)
219  @returns count of a type
220  """
221  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetCount(self._variant_variant)
222 
223  def check_convert(self, datatype: VariantType) -> Result:
224  """
225  Checks whether the variant can be converted to another type
226  @returns <Result>, status of function call
227  """
228  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantCheckConvert(
229  self._variant_variant, datatype.value))
230 
231  @staticmethod
232  def copy(c_variant: C_DLR_VARIANT):
233  """
234  copies the content of a variant to another variant
235  @returns tuple (Result, Variant)
236  @return <Result>, status of function call,
237  @return <Variant>, copy of variant
238  """
239  dest = Variant()
240  result = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantCopy(
241  dest._variant, c_variant))
242  return result, dest
243 
244  def clone(self):
245  """
246  clones the content of a variant to another variant
247  @returns tuple (Result, Variant)
248  @return <Result>, status of function call,
249  @return <Variant>, clones of variant
250  """
251  return Variant.copy(self._variant_variant)
252 
253  def get_bool8(self) -> bool:
254  """
255  Returns the value of the variant as a bool (auto convert if possible) otherwise 0
256  @returns [True, False]
257  """
258  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetBOOL8(self._variant_variant)
259 
260  def get_int8(self) -> int:
261  """
262  Returns the value of the variant as an int8 (auto convert if possible) otherwise 0
263  @returns [-128, 127]
264  """
265  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetINT8(self._variant_variant)
266 
267  def get_uint8(self) -> int:
268  """
269  Returns the value of the variant as an uint8 (auto convert if possible) otherwise 0
270  @returns [0, 255]
271  """
272  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetUINT8(self._variant_variant)
273 
274  def get_int16(self) -> int:
275  """
276  Returns the value of the variant as an int16 (auto convert if possible) otherwise 0
277  @returns [-32768, 32767]
278  """
279  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetINT16(self._variant_variant)
280 
281  def get_uint16(self) -> int:
282  """
283  Returns the value of the variant as an uint16 (auto convert if possible) otherwise 0
284  @returns [0, 65.535]
285  """
286  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetUINT16(self._variant_variant)
287 
288  def get_int32(self) -> int:
289  """
290  Returns the value of the variant as an int32 (auto convert if possible) otherwise 0
291  @returns [-2.147.483.648, 2.147.483.647]
292  """
293  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetINT32(self._variant_variant)
294 
295  def get_uint32(self) -> int:
296  """
297  Returns the value of the variant as an Uint32 (auto convert if possible) otherwise 0
298  @returns [0, 4.294.967.295]
299  """
300  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetUINT32(self._variant_variant)
301 
302  def get_int64(self) -> int:
303  """
304  Returns the value of the variant as an int64 (auto convert if possible) otherwise 0
305  @returns [-9.223.372.036.854.775.808, 9.223.372.036.854.775.807]
306  """
307  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetINT64(self._variant_variant)
308 
309  def get_uint64(self) -> int:
310  """
311  Returns the value of the variant as an uint64 (auto convert if possible) otherwise 0
312  @returns [0, 18446744073709551615]
313  """
314  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetUINT64(self._variant_variant)
315 
316  def get_float32(self) -> float:
317  """
318  Returns the value of the variant as a float (auto convert if possible) otherwise 0
319  @returns [1.2E-38, 3.4E+38]
320  """
321  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetFLOAT32(self._variant_variant)
322 
323  def get_float64(self) -> float:
324  """
325  Returns the value of the variant as a double (auto convert if possible) otherwise 0
326  @returns [2.3E-308, 1.7E+308]
327  """
328  return ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetFLOAT64(self._variant_variant)
329 
330  def get_string(self) -> str:
331  """
332  Returns the array of bool8 if the type is an array of bool otherwise null
333  @returns string
334  """
335  if self.check_convertcheck_convert(VariantType.STRING) != Result.OK:
336  return None
337  b = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetSTRING(
338  self._variant_variant)
339  if b is None:
340  return b
341  return b.decode('utf-8')
342 
343  def get_flatbuffers(self) -> bytearray:
344  """
345  Returns the flatbuffers if the type is a flatbuffers otherwise null
346  @returns flatbuffer (bytearray)
347  """
348  if self.check_convertcheck_convert(VariantType.FLATBUFFERS) != Result.OK:
349  return None
350 
351  return self.get_dataget_data()
352 
353  def get_datetime(self) -> datetime.datetime:
354  """datetime object as timestamp (FILETIME) 64 bit 100ns since 1.1.1601 (UTC)
355 
356  Returns:
357  datetime.datetime: datetime object
358  """
359  d = self.get_uint64get_uint64()
360  return Variant.from_filetime(d)
361 
362  def get_array_bool8(self) -> typing.List[bool]:
363  """
364  Returns the array of int8 if the type is an array of int8 otherwise null
365  @returns array of bool8
366  """
367  if self.check_convertcheck_convert(VariantType.ARRAY_BOOL8) != Result.OK:
368  return None
369 
370  c_data = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetArrayOfBOOL8(
371  self._variant_variant)
372  length = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetCount(
373  self._variant_variant)
374  return [c_data[i] for i in range(length)]
375 
376  def get_array_int8(self) -> typing.List[int]:
377  """
378  Returns the array of int8 if the type is an array of int8 otherwise null
379  @returns array of int8
380  """
381  if self.check_convertcheck_convert(VariantType.ARRAY_INT8) != Result.OK:
382  return None
383  c_data = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetArrayOfINT8(
384  self._variant_variant)
385  length = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetCount(
386  self._variant_variant)
387  return [c_data[i] for i in range(length)]
388 
389  def get_array_uint8(self) -> typing.List[int]:
390  """
391  Returns the array of uint8 if the type is an array of uint8 otherwise null
392  @returns array of uint8
393  """
394  if self.check_convertcheck_convert(VariantType.ARRAY_UINT8) != Result.OK:
395  return None
396  c_data = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetArrayOfUINT8(
397  self._variant_variant)
398  length = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetCount(
399  self._variant_variant)
400  return [c_data[i] for i in range(length)]
401 
402  def get_array_int16(self) -> typing.List[int]:
403  """
404  Returns the array of int16 if the type is an array of int16 otherwise null
405  @returns array of int16
406  """
407  if self.check_convertcheck_convert(VariantType.ARRAY_INT16) != Result.OK:
408  return None
409  c_data = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetArrayOfINT16(
410  self._variant_variant)
411  length = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetCount(
412  self._variant_variant)
413  return [c_data[i] for i in range(length)]
414 
415  def get_array_uint16(self) -> typing.List[int]:
416  """
417  Returns the array of uint16 if the type is an array of uint16 otherwise null
418  @returns array of uint16
419  """
420  if self.check_convertcheck_convert(VariantType.ARRAY_UINT16) != Result.OK:
421  return None
422  c_data = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetArrayOfUINT16(
423  self._variant_variant)
424  length = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetCount(
425  self._variant_variant)
426  return [c_data[i] for i in range(length)]
427 
428  def get_array_int32(self) -> typing.List[int]:
429  """
430  Returns the array of int32 if the type is an array of int32 otherwise null
431  @returns array of int32
432  """
433  if self.check_convertcheck_convert(VariantType.ARRAY_INT32) != Result.OK:
434  return None
435  c_data = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetArrayOfINT32(
436  self._variant_variant)
437  length = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetCount(
438  self._variant_variant)
439  return [c_data[i] for i in range(length)]
440 
441  def get_array_uint32(self) -> typing.List[int]:
442  """
443  Returns the array of uint32 if the type is an array of uint32 otherwise null
444  @returns array of uint32
445  """
446  if self.check_convertcheck_convert(VariantType.ARRAY_UINT32) != Result.OK:
447  return None
448  c_data = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetArrayOfUINT32(
449  self._variant_variant)
450  length = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetCount(
451  self._variant_variant)
452  return [c_data[i] for i in range(length)]
453 
454  def get_array_int64(self) -> typing.List[int]:
455  """
456  Returns the array of int64 if the type is an array of int64 otherwise null
457  @returns array of int64
458  """
459  if self.check_convertcheck_convert(VariantType.ARRAY_INT64) != Result.OK:
460  return None
461  c_data = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetArrayOfINT64(
462  self._variant_variant)
463  length = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetCount(
464  self._variant_variant)
465  return [c_data[i] for i in range(length)]
466 
467  def get_array_uint64(self) -> typing.List[int]:
468  """
469  Returns the array of uint64 if the type is an array of uint64 otherwise null
470  @returns array of uint64
471  """
472  if self.check_convertcheck_convert(VariantType.ARRAY_UINT64) != Result.OK and self.check_convertcheck_convert(VariantType.ARRAY_OF_TIMESTAMP) != Result.OK:
473  return None
474 
475  c_data = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetArrayOfUINT64(
476  self._variant_variant)
477  length = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetCount(
478  self._variant_variant)
479  return [c_data[i] for i in range(length)]
480 
481  def get_array_float32(self) -> typing.List[float]:
482  """
483  Returns the array of float if the type is an array of float otherwise null
484  @returns array of float32
485  """
486  if self.check_convertcheck_convert(VariantType.ARRAY_FLOAT32) != Result.OK:
487  return None
488  c_data = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetArrayOfFLOAT32(
489  self._variant_variant)
490  length = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetCount(
491  self._variant_variant)
492  return [c_data[i] for i in range(length)]
493 
494  def get_array_float64(self) -> typing.List[float]:
495  """
496  Returns the array of double if the type is an array of double otherwise null
497  @returns array of float64
498  """
499  if self.check_convertcheck_convert(VariantType.ARRAY_FLOAT64) != Result.OK:
500  return None
501  c_data = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetArrayOfFLOAT64(
502  self._variant_variant)
503  length = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetCount(
504  self._variant_variant)
505  return [c_data[i] for i in range(length)]
506 
507  def get_array_string(self) -> typing.List[str]:
508  """
509  Returns the type of the variant
510  @returns array of strings
511  """
512  if self.check_convertcheck_convert(VariantType.ARRAY_STRING) != Result.OK:
513  return None
514  c_data = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetArrayOfSTRING(
515  self._variant_variant)
516  length = ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantGetCount(
517  self._variant_variant)
518  return [c_data[i].decode('utf-8') for i in range(length)]
519 
520  def get_array_datetime(self) -> typing.List[datetime.datetime]:
521  """datetime objects as timestamp (FILETIME) 64 bit 100ns since 1.1.1601 (UTC)
522 
523  Returns:
524  array of datetime.datetime: datetime object
525  """
526  vals = self.get_array_uint64get_array_uint64()
527  return [Variant.from_filetime(ft) for ft in vals]
528 
529  def set_bool8(self, data: bool) -> Result:
530  """
531  Set a bool value
532  @returns <Result>, status of function call
533  """
534  if self.__closed__closed:
535  return Result.NOT_INITIALIZED
536  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetBOOL8(self._variant_variant, data))
537 
538  def set_int8(self, data: int) -> Result:
539  """
540  Set an int8 value
541  @returns <Result>, status of function call
542  """
543  if self.__closed__closed:
544  return Result.NOT_INITIALIZED
545  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetINT8(self._variant_variant, data))
546 
547  def set_uint8(self, data: int) -> Result:
548  """
549  Set a uint8 value
550  @returns <Result>, status of function call
551  """
552  if self.__closed__closed:
553  return Result.NOT_INITIALIZED
554  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetUINT8(self._variant_variant, data))
555 
556  def set_int16(self, data: int) -> Result:
557  """
558  Set an int16 value
559  @returns <Result>, status of function call
560  """
561  if self.__closed__closed:
562  return Result.NOT_INITIALIZED
563  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetINT16(self._variant_variant, data))
564 
565  def set_uint16(self, data: int) -> Result:
566  """
567  Set a uint16 value
568  @returns <Result>, status of function call
569  """
570  if self.__closed__closed:
571  return Result.NOT_INITIALIZED
572  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetUINT16(self._variant_variant, data))
573 
574  def set_int32(self, data: int) -> Result:
575  """
576  Set an int32 value
577  @returns <Result>, status of function call
578  """
579  if self.__closed__closed:
580  return Result.NOT_INITIALIZED
581  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetINT32(self._variant_variant, data))
582 
583  def set_uint32(self, data: int) -> Result:
584  """
585  Set a uint32 value
586  @returns <Result>, status of function call
587  """
588  if self.__closed__closed:
589  return Result.NOT_INITIALIZED
590  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetUINT32(self._variant_variant, data))
591 
592  def set_int64(self, data: int) -> Result:
593  """
594  Set an int64 value
595  @returns <Result>, status of function call
596  """
597  if self.__closed__closed:
598  return Result.NOT_INITIALIZED
599  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetINT64(self._variant_variant, data))
600 
601  def set_uint64(self, data: int) -> Result:
602  """
603  Set a uint64 value
604  @returns <Result>, status of function call
605  """
606  if self.__closed__closed:
607  return Result.NOT_INITIALIZED
608  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetUINT64(self._variant_variant, data))
609 
610  def set_float32(self, data: float) -> Result:
611  """
612  Set a float value
613  @returns <Result>, status of function call
614  """
615  if self.__closed__closed:
616  return Result.NOT_INITIALIZED
617  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetFLOAT32(self._variant_variant, data))
618 
619  def set_float64(self, data: float) -> Result:
620  """
621  Set a double value
622  @returns <Result>, status of function call
623  """
624  if self.__closed__closed:
625  return Result.NOT_INITIALIZED
626  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetFLOAT64(self._variant_variant, data))
627 
628  def set_string(self, data: str) -> Result:
629  """
630  Set a string
631  @returns <Result>, status of function call
632  """
633  b_data = data.encode('utf-8')
634  if self.__closed__closed:
635  return Result.NOT_INITIALIZED
636  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetSTRING(self._variant_variant, b_data))
637 
638  def set_flatbuffers(self, data: bytearray) -> Result:
639  """
640  Set a flatbuffers
641  @returns <Result>, status of function call
642  """
643  if self.__closed__closed:
644  return Result.NOT_INITIALIZED
645  buf = (ctypes.c_byte * len(data)).from_buffer(data)
646  c_data = ctypes.cast(buf, ctypes.POINTER(ctypes.c_byte))
647  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetFlatbuffers(
648  self._variant_variant, c_data, len(data)))
649  del c_data
650  return r
651 
652  def set_timestamp(self, data: int) -> Result:
653  """
654  Set a timestamp value
655  data <int>: timestamp (FILETIME) 64 bit 100ns since 1.1.1601 (UTC)
656  @returns <Result>, status of function call
657  """
658  if self.__closed__closed:
659  return Result.NOT_INITIALIZED
660  return Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetTimestamp(self._variant_variant, data))
661 
662  def set_datetime(self, dt: datetime) -> Result:
663  """Set a timestamp value as datetime object
664 
665  Args:
666  dt (datetime): datetime object
667 
668  Returns:
669  Result: status of function call
670  """
671  ft = Variant.to_filetime(dt)
672  return self.set_timestampset_timestamp(ft)
673 
674  @staticmethod
675  def from_filetime(filetime) -> datetime:
676  """convert filetime to datetime
677 
678  Args:
679  filetime (int): (FILETIME) 64 bit 100ns since 1.1.1601 (UTC)
680 
681  Returns:
682  datetime: datetime object
683  """
684  #microseconds_since_file_time_epoch = filetime // FILE_TIME_MICROSECOND
685  return FILE_TIME_EPOCH + datetime.timedelta(microseconds=(filetime // FILE_TIME_MICROSECOND))
686 
687  @staticmethod
688  def to_filetime(dt: datetime) -> int:
689  """convert datetime to filetime
690 
691  Args:
692  dt (datetime): datetime to convert
693 
694  Returns:
695  int: (FILETIME) 64 bit 100ns since 1.1.1601 (UTC)
696  """
697  microseconds_since_file_time_epoch = (
698  dt - FILE_TIME_EPOCH) // datetime.timedelta(microseconds=1)
699  return microseconds_since_file_time_epoch * FILE_TIME_MICROSECOND
700 
701  def set_array_bool8(self, data: typing.List[bool]) -> Result:
702  """
703  Set array of bool8
704  @returns <Result>, status of function call
705  """
706  if self.__closed__closed:
707  return Result.NOT_INITIALIZED
708  c_data = (ctypes.c_bool * len(data))(*data)
709  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetARRAY_OF_BOOL8(
710  self._variant_variant, c_data, len(data)))
711  del c_data
712  return r
713 
714  def set_array_int8(self, data: typing.List[int]) -> Result:
715  """
716  Set array of int8
717  @returns <Result>, status of function call
718  """
719  if self.__closed__closed:
720  return Result.NOT_INITIALIZED
721  c_data = (ctypes.c_int8 * len(data))(*data)
722  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetARRAY_OF_INT8(
723  self._variant_variant, c_data, len(data)))
724  del c_data
725  return r
726 
727  def set_array_uint8(self, data: typing.List[int]) -> Result:
728  """
729  Set array of uint8
730  @returns <Result>, status of function call
731  """
732  if self.__closed__closed:
733  return Result.NOT_INITIALIZED
734  c_data = (ctypes.c_uint8 * len(data))(*data)
735  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetARRAY_OF_UINT8(
736  self._variant_variant, c_data, len(data)))
737  del c_data
738  return r
739 
740  def set_array_int16(self, data: typing.List[int]) -> Result:
741  """
742  Set array of int16
743  @returns <Result>, status of function call
744  """
745  if self.__closed__closed:
746  return Result.NOT_INITIALIZED
747  c_data = (ctypes.c_int16 * len(data))(*data)
748  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetARRAY_OF_INT16(
749  self._variant_variant, c_data, len(data)))
750  del c_data
751  return r
752 
753  def set_array_uint16(self, data: typing.List[int]) -> Result:
754  """
755  Set array of uint16
756  @returns <Result>, status of function call
757  """
758  if self.__closed__closed:
759  return Result.NOT_INITIALIZED
760  c_data = (ctypes.c_uint16 * len(data))(*data)
761  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetARRAY_OF_UINT16(
762  self._variant_variant, c_data, len(data)))
763  del c_data
764  return r
765 
766  def set_array_int32(self, data: typing.List[int]) -> Result:
767  """
768  Set array of int32
769  @returns <Result>, status of function call
770  """
771  if self.__closed__closed:
772  return Result.NOT_INITIALIZED
773  c_data = (ctypes.c_int32 * len(data))(*data)
774  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetARRAY_OF_INT32(
775  self._variant_variant, c_data, len(data)))
776  del c_data
777  return r
778 
779  def set_array_uint32(self, data: typing.List[int]) -> Result:
780  """
781  Set array of uint32
782  @returns <Result>, status of function call
783  """
784  if self.__closed__closed:
785  return Result.NOT_INITIALIZED
786  c_data = (ctypes.c_uint32 * len(data))(*data)
787  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetARRAY_OF_UINT32(
788  self._variant_variant, c_data, len(data)))
789  del c_data
790  return r
791 
792  def set_array_int64(self, data: typing.List[int]) -> Result:
793  """
794  Set array of int64
795  @returns <Result>, status of function call
796  """
797  if self.__closed__closed:
798  return Result.NOT_INITIALIZED
799  c_data = (ctypes.c_int64 * len(data))(*data)
800  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetARRAY_OF_INT64(
801  self._variant_variant, c_data, len(data)))
802  del c_data
803  return r
804 
805  def set_array_uint64(self, data: typing.List[int]) -> Result:
806  """
807  Set array of uint64
808  @returns <Result>, status of function call
809  """
810  if self.__closed__closed:
811  return Result.NOT_INITIALIZED
812  c_data = (ctypes.c_uint64 * len(data))(*data)
813  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetARRAY_OF_UINT64(
814  self._variant_variant, c_data, len(data)))
815  del c_data
816  return r
817 
818  def set_array_float32(self, data: typing.List[float]) -> Result:
819  """
820  Set array of float32
821  @returns <Result>, status of function call
822  """
823  if self.__closed__closed:
824  return Result.NOT_INITIALIZED
825  c_data = (ctypes.c_float * len(data))(*data)
826  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetARRAY_OF_FLOAT32(
827  self._variant_variant, c_data, len(data)))
828  del c_data
829  return r
830 
831  def set_array_float64(self, data: typing.List[float]) -> Result:
832  """
833  Set array of float64
834  @returns <Result>, status of function call
835  """
836  if self.__closed__closed:
837  return Result.NOT_INITIALIZED
838  c_data = (ctypes.c_double * len(data))(*data)
839  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetARRAY_OF_FLOAT64(
840  self._variant_variant, c_data, len(data)))
841  del c_data
842  return r
843 
844  def set_array_string(self, data: typing.List[str]) -> Result:
845  """
846  Set array of strings
847  @returns <Result>, status of function call
848  """
849  if self.__closed__closed:
850  return Result.NOT_INITIALIZED
851  c_data = (ctypes.c_char_p * len(data))(*
852  [d.encode('utf-8') for d in data])
853  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetARRAY_OF_STRING(
854  self._variant_variant, c_data, len(data)))
855  del c_data
856  return r
857 
858  def set_array_timestamp(self, data: typing.List[int]) -> Result:
859  """
860  Set array of timestamp (uint64)
861  @returns <Result>, status of function call
862  """
863  if self.__closed__closed:
864  return Result.NOT_INITIALIZED
865  c_data = (ctypes.c_uint64 * len(data))(*data)
866  r = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantSetARRAY_OF_TIMESTAMP(
867  self._variant_variant, c_data, len(data)))
868  del c_data
869  return r
870 
871  def set_array_datetime(self, data: typing.List[datetime.datetime]) -> Result:
872  """
873  Set array of datetime
874  @returns <Result>, status of function call
875  """
876  if self.__closed__closed:
877  return Result.NOT_INITIALIZED
878  return self.set_array_timestampset_array_timestamp([Variant.to_filetime(d) for d in data])
879 
880 
881 def copy(dest: Variant, src: Variant):
882  """
883  copies the content of a variant to another variant
884  @returns tuple (Result, Variant)
885  @return <Result>, status of function call,
886  """
887  result = Result(ctrlxdatalayer.clib.libcomm_datalayer.DLR_variantCopy(
888  dest.get_handle(), src.get_handle()))
889  return result
890 
891 
892 class VariantRef(Variant):
893  """
894  Variant Helper interface,
895  Important: store not an instance of VariantRef, uses the clone/copy function
896  """
897 
898  def __init__(self, c_variant: C_DLR_VARIANT = None):
899  """
900  generate Variant
901  """
902  super().__init__(c_variant)
903 
904  def __del__(self):
905  """
906  __del__
907  """
908  pass
909 
910  def __exit__(self, exc_type, exc_val, exc_tb):
911  """
912  __exit__
913  """
914  pass
def __exit__(self, exc_type, exc_val, exc_tb)
exit
Definition: variant.py:919
def __init__(self, C_DLR_VARIANT c_variant=None)
generate Variant
Definition: variant.py:907
Variant is a container for a many types of data.
Definition: variant.py:150
Result set_array_datetime(self, typing.List[datetime.datetime] data)
Set array of datetime.
Definition: variant.py:881
Result set_array_float64(self, typing.List[float] data)
Set array of float64.
Definition: variant.py:841
def clone(self)
clones the content of a variant to another variant
Definition: variant.py:256
int get_uint8(self)
Returns the value of the variant as an uint8 (auto convert if possible) otherwise 0.
Definition: variant.py:277
Result check_convert(self, VariantType datatype)
Checks whether the variant can be converted to another type.
Definition: variant.py:233
typing.List[int] get_array_uint16(self)
Returns the array of uint16 if the type is an array of uint16 otherwise null.
Definition: variant.py:425
def __exit__(self, exc_type, exc_val, exc_tb)
use the python context manager
Definition: variant.py:174
Result set_uint16(self, int data)
Set a uint16 value.
Definition: variant.py:575
Result set_array_uint8(self, typing.List[int] data)
Set array of uint8.
Definition: variant.py:737
Result set_array_int64(self, typing.List[int] data)
Set array of int64.
Definition: variant.py:802
typing.List[float] get_array_float32(self)
Returns the array of float if the type is an array of float otherwise null.
Definition: variant.py:491
int get_int64(self)
Returns the value of the variant as an int64 (auto convert if possible) otherwise 0.
Definition: variant.py:312
Result set_uint64(self, int data)
Set a uint64 value.
Definition: variant.py:611
Result set_array_int32(self, typing.List[int] data)
Set array of int32.
Definition: variant.py:776
def __enter__(self)
use the python context manager
Definition: variant.py:168
Result set_int16(self, int data)
Set an int16 value.
Definition: variant.py:566
VariantType get_type(self)
Returns the type of the variant.
Definition: variant.py:202
int get_uint64(self)
Returns the value of the variant as an uint64 (auto convert if possible) otherwise 0.
Definition: variant.py:319
Result set_array_int8(self, typing.List[int] data)
Set array of int8.
Definition: variant.py:724
def __init__(self, C_DLR_VARIANT c_variant=None)
generate Variant
Definition: variant.py:157
int get_uint16(self)
Returns the value of the variant as an uint16 (auto convert if possible) otherwise 0.
Definition: variant.py:291
Result set_float32(self, float data)
Set a float value.
Definition: variant.py:620
Result set_array_string(self, typing.List[str] data)
Set array of strings.
Definition: variant.py:854
bytearray get_flatbuffers(self)
Returns the flatbuffers if the type is a flatbuffers otherwise null.
Definition: variant.py:353
int get_count(self)
Returns the count of elements in the variant (scalar data types = 1, array = count of elements in arr...
Definition: variant.py:226
typing.List[datetime.datetime] get_array_datetime(self)
datetime objects as timestamp (FILETIME) 64 bit 100ns since 1.1.1601 (UTC)
Definition: variant.py:531
Result set_array_int16(self, typing.List[int] data)
Set array of int16.
Definition: variant.py:750
Result set_array_uint64(self, typing.List[int] data)
Set array of uint64.
Definition: variant.py:815
Result set_bool8(self, bool data)
Set a bool value.
Definition: variant.py:539
Result set_timestamp(self, int data)
Definition: variant.py:663
float get_float64(self)
Returns the value of the variant as a double (auto convert if possible) otherwise 0.
Definition: variant.py:333
def copy(C_DLR_VARIANT c_variant)
copies the content of a variant to another variant
Definition: variant.py:244
typing.List[int] get_array_int64(self)
Returns the array of int64 if the type is an array of int64 otherwise null.
Definition: variant.py:464
typing.List[int] get_array_uint8(self)
Returns the array of uint8 if the type is an array of uint8 otherwise null.
Definition: variant.py:399
Result set_array_float32(self, typing.List[float] data)
Set array of float32.
Definition: variant.py:828
bool get_bool8(self)
Returns the value of the variant as a bool (auto convert if possible) otherwise 0.
Definition: variant.py:263
typing.List[int] get_array_int8(self)
Returns the array of int8 if the type is an array of int8 otherwise null.
Definition: variant.py:386
int get_int32(self)
Returns the value of the variant as an int32 (auto convert if possible) otherwise 0.
Definition: variant.py:298
int get_int16(self)
Returns the value of the variant as an int16 (auto convert if possible) otherwise 0.
Definition: variant.py:284
datetime from_filetime(filetime)
convert filetime to datetime
Definition: variant.py:689
typing.List[float] get_array_float64(self)
Returns the array of double if the type is an array of double otherwise null.
Definition: variant.py:504
Result set_flatbuffers(self, bytearray data)
Set a flatbuffers.
Definition: variant.py:648
datetime.datetime get_datetime(self)
datetime object as timestamp (FILETIME) 64 bit 100ns since 1.1.1601 (UTC)
Definition: variant.py:364
def close(self)
closes the variant instance
Definition: variant.py:186
Result set_int64(self, int data)
Set an int64 value.
Definition: variant.py:602
int to_filetime(datetime dt)
convert datetime to filetime
Definition: variant.py:702
typing.List[int] get_array_int32(self)
Returns the array of int32 if the type is an array of int32 otherwise null.
Definition: variant.py:438
Result set_uint32(self, int data)
Set a uint32 value.
Definition: variant.py:593
typing.List[bool] get_array_bool8(self)
Returns the array of int8 if the type is an array of int8 otherwise null.
Definition: variant.py:372
typing.List[int] get_array_uint32(self)
Returns the array of uint32 if the type is an array of uint32 otherwise null.
Definition: variant.py:451
typing.List[int] get_array_uint64(self)
Returns the array of uint64 if the type is an array of uint64 otherwise null.
Definition: variant.py:477
Result set_int32(self, int data)
Set an int32 value.
Definition: variant.py:584
def get_handle(self)
handle value of variant
Definition: variant.py:195
Result set_array_uint32(self, typing.List[int] data)
Set array of uint32.
Definition: variant.py:789
Result set_datetime(self, datetime dt)
Set a timestamp value as datetime object.
Definition: variant.py:676
bytearray get_data(self)
Returns the pointer to the data of the variant.
Definition: variant.py:209
Result set_int8(self, int data)
Set an int8 value.
Definition: variant.py:548
Result set_array_uint16(self, typing.List[int] data)
Set array of uint16.
Definition: variant.py:763
Result set_float64(self, float data)
Set a double value.
Definition: variant.py:629
Result set_array_bool8(self, typing.List[bool] data)
Set array of bool8.
Definition: variant.py:711
str get_string(self)
Returns the array of bool8 if the type is an array of bool otherwise null.
Definition: variant.py:340
int get_int8(self)
Returns the value of the variant as an int8 (auto convert if possible) otherwise 0.
Definition: variant.py:270
Result set_uint8(self, int data)
Set a uint8 value.
Definition: variant.py:557
Result set_array_timestamp(self, typing.List[int] data)
Set array of timestamp (uint64)
Definition: variant.py:868
float get_float32(self)
Returns the value of the variant as a float (auto convert if possible) otherwise 0.
Definition: variant.py:326
typing.List[int] get_array_int16(self)
Returns the array of int16 if the type is an array of int16 otherwise null.
Definition: variant.py:412
typing.List[str] get_array_string(self)
Returns the type of the variant.
Definition: variant.py:517
Result set_string(self, str data)
Set a string.
Definition: variant.py:638
int get_uint32(self)
Returns the value of the variant as an Uint32 (auto convert if possible) otherwise 0.
Definition: variant.py:305