Nyx Node
Loading...
Searching...
No Matches
indi_blob.py
1# -*- coding: utf-8 -*-
2########################################################################################################################
3# NyxNode
4# Author: Jérôme ODIER <jerome.odier@lpsc.in2p3.fr>
5# SPDX-License-Identifier: GPL-3.0+
6########################################################################################################################
7
8import ctypes
9import typing
10
11########################################################################################################################
12
13from .. import obj
14from .. import bind
15from .. import json
16
17from . import enums
18from . import utils
19
20########################################################################################################################
21
22## @defgroup BLOB_MESSAGE_PY Nyx BLOB Message
23# @brief Nyx / INDI BLOB Message.
24
25########################################################################################################################
26
27@utils.nyx_property(
28 'name',
29 '@name',
30)
31@utils.nyx_property(
32 'label',
33 '@label',
34)
35@utils.nyx_property(
36 'format',
37 '@format',
38)
39@utils.nyx_property(
40 'value',
41 '$',
42 kind = bytes,
43)
45 """!
46 @ingroup BLOB_MESSAGE_PY
47 @brief INDI / Nyx BLOB property.
48 """
49
50 ####################################################################################################################
51
52 def __init__(self, name: str, label: str | None = None, fmt: str | None = None, value: bytes | None = None):
53 """!
54 @brief Allocates a new INDI / Nyx BLOB property.
55
56 @param name Property name.
57 @param label Property label.
58 @param fmt Payload format.
59 @param value Initial payload buffer.
60
61 @note If a format ends with `.z`, the payload is automatically ZLib+Base64-compressed, otherwise, the payload is automatically Base64-encoded.
62 """
63
64 ################################################################################################################
65
66 data = bind.as_bytes(value, allow_none = False)
68 buff = ctypes.cast(bind.lib.nyx_buffer_ndup(data, len(data)), bind.c_void_p)
69
70 ################################################################################################################
71
72 super().__init__(bind.lib.nyx_blob_prop_new(
73 bind.as_bytes(name, allow_none = False),
74 bind.as_bytes(label, allow_none = True),
75 bind.as_bytes(fmt, allow_none = True),
76 len(data),
77 buff,
78 True,
79 ))
80
81 ####################################################################################################################
82
83 @obj.nyx_callback(bind.nyx_callback_buffer_t)
84 def _nyx_callback_func(self, _vector, _prop, size, buff) -> bool:
85
86 return all(self._dispatch_callbacks(ctypes.string_at(buff, size) if buff is not None and size > 0 else b''))
87
88########################################################################################################################
89
90@utils.nyx_property(
91 'device',
92 '@device',
93)
94@utils.nyx_property(
95 'name',
96 '@name',
97)
98@utils.nyx_property(
99 'state',
100 '@state',
101 getter = enums.NyxState.to_int,
102 setter = enums.NyxState.to_str,
103)
104@utils.nyx_property(
105 'perm',
106 '@perm',
107 getter = enums.NyxPerm.to_int,
108 setter = enums.NyxPerm.to_str,
109)
111 """!
112 @ingroup BLOB_MESSAGE_PY
113 @brief INDI / Nyx BLOB vector.
114 """
115
116 ####################################################################################################################
117
118 def __init__(self, device: str, name: str, state: enums.NyxState | int | str, perm: enums.NyxPerm | int | str, props: typing.Iterable[NyxBLOBProp], **opts: typing.Any):
119 """!
120 @brief Allocates a new INDI / Nyx BLOB vector.
121
122 @param device Device name.
123 @param name Vector name.
124 @param state Vector state.
125 @param perm Vector permissions.
126 @param props Properties.
127 @param opts Options (group, label, hints, timeout, message).
128 """
129
130 ################################################################################################################
131
132 super().__init__(bind.lib.nyx_blob_vector_new(
133 bind.as_bytes(device, allow_none = False),
134 bind.as_bytes(name, allow_none = False),
135 enums.NyxState.to_int(state),
136 enums.NyxPerm.to_int(perm),
137 bind.nyx_dict_p(),
138 bind.as_opts(opts),
139 ))
140
141 ################################################################################################################
143 # noinspection PyTypeChecker
144 children: json.NyxList = self['children']
146 for prop in props:
147
148 if not isinstance(prop, NyxBLOBProp):
149
150 raise TypeError('Expected NyxBlobProp')
152 children.append(prop)
153
154 ####################################################################################################################
155
156 @obj.nyx_callback(bind.nyx_callback_vector_t)
157 def _nyx_callback_func(self, _vector: json.json_dict.NyxDict, modified: bool) -> None:
158
159 self._dispatch_callbacks(bool(modified))
160
161########################################################################################################################
162
163__all__ = [name for name in globals() if name.lower().startswith('nyx')]
164
165########################################################################################################################
JSON dict object.
Definition json_dict.py:22
JSON list object.
Definition json_list.py:22
_dispatch_callbacks(self, *args)
Definition obj.py:154
__init__(self, str device, str name, enums.NyxState|int|str state, enums.NyxPerm|int|str perm, typing.Iterable[NyxBLOBProp] props, **typing.Any opts)
Allocates a new INDI / Nyx BLOB vector.
Definition indi_blob.py:130
__init__(self, str name, str|None label=None, str|None fmt=None, bytes|None value=None)
Allocates a new INDI / Nyx BLOB property.
Definition indi_blob.py:52
INDI / Nyx BLOB property.
Definition indi_blob.py:44
INDI / Nyx BLOB vector.
Definition indi_blob.py:122
Vector permission hint.
Definition enums.py:81
Vector state hint.
Definition enums.py:24