Nyx Node
Loading...
Searching...
No Matches
indi_stream.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 bind
14from .. import json
15
16from . import enums
17from . import utils
18
19########################################################################################################################
20
21## @defgroup STREAM_MESSAGE_PY Nyx Stream Message
22# @brief Nyx Stream Message.
23
24########################################################################################################################
25
26@utils.nyx_property(
27 'name',
28 '@name',
29)
30@utils.nyx_property(
31 'label',
32 '@label',
33)
35 """!
36 @ingroup STREAM_MESSAGE_PY
37 @brief Nyx Stream property.
38 """
39
40 ####################################################################################################################
41
42 def __init__(self, name: str, label: str | None = None):
43 """!
44 @brief Allocates a new Nyx Stream property.
45
46 @param name Property name.
47 @param label Property label.
48
49 @note If the property name ends with `.b`, the payload is automatically Base64-encoded.
50 @note If the property name ends with `.z`, the payload is automatically ZLib-compressed.
51 """
52
53 super().__init__(bind.lib.nyx_stream_prop_new(
54 bind.as_bytes(name, allow_none = False),
55 bind.as_bytes(label, allow_none = True),
56 ))
57
58########################################################################################################################
59
60@utils.nyx_property(
61 'device',
62 '@device',
63)
64@utils.nyx_property(
65 'name',
66 '@name',
67)
68@utils.nyx_property(
69 'state',
70 '@state',
71 kind = enums.NyxState,
72 getter = enums.NyxState.to_int,
73 setter = enums.NyxState.to_str,
74)
76 """!
77 @ingroup STREAM_MESSAGE_PY
78 @brief Nyx Stream vector.
79 """
80
81 ####################################################################################################################
82
83 def __init__(self, device: str, name: str, state: enums.NyxState | int | str, props: typing.Iterable[NyxStreamProp], **opts: typing.Any):
84 """!
85 @brief Allocates a new Nyx Stream vector.
86
87 @param device Device name.
88 @param name Vector name.
89 @param state Vector state.
90 @param props Properties.
91 @param opts Options (group, label, hints, timeout, message).
92 """
93
94 ################################################################################################################
95
96 super().__init__(bind.lib.nyx_stream_vector_new(
97 bind.as_bytes(device, allow_none = False),
98 bind.as_bytes(name, allow_none = False),
99 enums.NyxState.to_int(state),
100 bind.nyx_dict_p(),
101 bind.as_opts(opts),
102 ))
104 ################################################################################################################
105
106 # noinspection PyTypeChecker
107 children: json.NyxList = self['children']
108
109 for prop in props:
110
111 if not isinstance(prop, NyxStreamProp):
112
113 raise TypeError('Expected NyxStreamProp')
114
115 children.append(prop)
116
117 ####################################################################################################################
118
119 def stream_pub(self, field_values: typing.Sequence[bytes]) -> bool:
120 """!
121 @brief If Nyx Stream is enabled, publishes an entry to a stream.
122
123 @param field_values Field payloads, one per field.
124 @return @c True if the provided fields match the vector content, @c False otherwise.
125
126 @note Field payloads may contain arbitrary binary data.
127 """
128
129 ################################################################################################################
130
131 field_values = [bind.as_bytes(value, allow_none = False) for value in field_values]
132
133 ################################################################################################################
135 cast = lambda value: ctypes.cast(ctypes.c_char_p(value), bind.c_void_p)
136
137 ################################################################################################################
138
139 n_fields = len(field_values)
140
141 field_sizes = (bind.c_size_t * n_fields)(
142 *(len(value) for value in field_values),
143 )
144
145 field_buffs = (bind.c_void_p * n_fields)(
146 *(cast(value) for value in field_values),
147 )
148
149 ################################################################################################################
150
151 return bool(bind.lib.nyx_stream_pub(
152 self.ptr,
153 n_fields,
154 field_sizes,
155 field_buffs,
156 ))
157
158########################################################################################################################
159
160__all__ = [name for name in globals() if name.lower().startswith('nyx')]
161
162########################################################################################################################
JSON dict object.
Definition json_dict.py:22
JSON list object.
Definition json_list.py:22
Vector state hint.
Definition enums.py:24
bool stream_pub(self, typing.Sequence[bytes] field_values)
If Nyx Stream is enabled, publishes an entry to a stream.
__init__(self, str name, str|None label=None)
Allocates a new Nyx Stream property.
__init__(self, str device, str name, enums.NyxState|int|str state, typing.Iterable[NyxStreamProp] props, **typing.Any opts)
Allocates a new Nyx Stream vector.