Nyx Node
Loading...
Searching...
No Matches
indi_switch.c
1/* NyxNode
2 * Author: Jérôme ODIER <jerome.odier@lpsc.in2p3.fr>
3 * SPDX-License-Identifier: GPL-2.0-only (Mongoose backend) or GPL-3.0+
4 */
5
6/*--------------------------------------------------------------------------------------------------------------------*/
7
8#include <stdio.h>
9
10#include "../nyx_node_internal.h"
11
12/*--------------------------------------------------------------------------------------------------------------------*/
13/* PROP */
14/*--------------------------------------------------------------------------------------------------------------------*/
15
16nyx_dict_t *nyx_switch_prop_new(STR_t name, STR_t label, nyx_onoff_t value)
17{
18 if(label == NULL || label[0] == '\0')
19 {
20 label = name;
21 }
22
23 /*----------------------------------------------------------------------------------------------------------------*/
24
25 nyx_dict_t *result = nyx_dict_new();
26
27 /*----------------------------------------------------------------------------------------------------------------*/
28
29 nyx_dict_set_string_unref(result, "<>", "defSwitch", false);
30
31 /*----------------------------------------------------------------------------------------------------------------*/
32
33 nyx_dict_set_string_unref(result, "@name", nyx_string_dup(name), true);
34 nyx_dict_set_string_unref(result, "@label", nyx_string_dup(label), true);
35
36 /*----------------------------------------------------------------------------------------------------------------*/
37
38 nyx_dict_set_string_unref(result, "$", nyx_onoff_to_str(value), false);
39
40 /*----------------------------------------------------------------------------------------------------------------*/
41
42 return result;
43}
44
45/*--------------------------------------------------------------------------------------------------------------------*/
46/* PROP SETTER & GETTER */
47/*--------------------------------------------------------------------------------------------------------------------*/
48
50{
51 return nyx_dict_set_string(prop, "$", nyx_onoff_to_str(value), false);
52}
53
54/*--------------------------------------------------------------------------------------------------------------------*/
55
57{
58 return nyx_str_to_onoff(nyx_dict_get_string(prop, "$"));
59}
60
61/*--------------------------------------------------------------------------------------------------------------------*/
62/* VECTOR */
63/*--------------------------------------------------------------------------------------------------------------------*/
64
65nyx_dict_t *nyx_switch_vector_new(
66 STR_t device,
67 STR_t name,
68 nyx_state_t state,
69 nyx_perm_t perm,
70 nyx_rule_t rule,
71 nyx_dict_t *props[],
72 const nyx_opts_t *opts
73) {
74 nyx_dict_t *result = nyx_dict_new();
75
76 nyx_list_t *children = nyx_list_new();
77
78 /*----------------------------------------------------------------------------------------------------------------*/
79
80 nyx_dict_set_string_unref(result, "<>", "defSwitchVector", false);
81
82 /*----------------------------------------------------------------------------------------------------------------*/
83
84 nyx_dict_set_string_unref(result, "@client", "unknown", false);
85 nyx_dict_set_string_unref(result, "@device", nyx_string_dup(device), true);
86 nyx_dict_set_string_unref(result, "@name", nyx_string_dup(name), true);
87
88 nyx_dict_set_string_unref(result, "@state", nyx_state_to_str(state), false);
89 nyx_dict_set_string_unref(result, "@perm", nyx_perm_to_str(perm), false);
90 nyx_dict_set_string_unref(result, "@rule", nyx_rule_to_str(rule), false);
91
92 /*----------------------------------------------------------------------------------------------------------------*/
93
94 internal_set_opts(result, opts);
95
96 /*----------------------------------------------------------------------------------------------------------------*/
97
98 nyx_dict_set(result, "children", children);
99
100 if(props) for(; *props != NULL; props++)
101 {
102 nyx_list_push(children, *props);
103
104 nyx_object_unref(*props);
105 }
106
107 nyx_object_unref(children);
108
109 /*----------------------------------------------------------------------------------------------------------------*/
110
111 return result;
112}
113
114/*--------------------------------------------------------------------------------------------------------------------*/
115/* SET VECTOR */
116/*--------------------------------------------------------------------------------------------------------------------*/
117
118nyx_dict_t *nyx_switch_set_vector_new(const nyx_dict_t *vector)
119{
120 return internal_prop_to_set_vector(vector, "setSwitchVector", "oneSwitch");
121}
122
123/*--------------------------------------------------------------------------------------------------------------------*/
Struct describing a JSON dict object.
Struct describing a JSON list object.
#define STR_t
Alias for const char *.
Definition nyx_node.h:71
__NYX_NULLABLE__ str_t nyx_string_dup(__NYX_NULLABLE__ STR_t s)
Similar to libc strdup.
bool nyx_switch_prop_set(const nyx_dict_t *prop, nyx_onoff_t value)
Sets the new value of the provided property object.
Definition indi_switch.c:49
nyx_onoff_t nyx_switch_prop_get(const nyx_dict_t *prop)
Gets the value of the provided property object.
Definition indi_switch.c:56
nyx_perm_t
Vector permission hint.
Definition nyx_node.h:1948
nyx_state_t
Vector state hint.
Definition nyx_node.h:1913
nyx_onoff_t
Switch state.
Definition nyx_node.h:2016
nyx_rule_t
Switch vector rule hint.
Definition nyx_node.h:1982
Struct describing the options for INDI / Nyx vectors.
Definition nyx_node.h:2049