summaryrefslogtreecommitdiff
path: root/cethcan/light.c
blob: e3b20a9d30e56f38257bd359042c4ce6bfab3b32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "cethcan.h"

struct value {
	uint8_t val;
	time_t change, valid;
};

struct light {
	struct light *next;
	struct can_user *u;

	char *name;
	bool aggregate;

	unsigned logical_addr;
	size_t slave_count;
	struct light **slaves;

	struct value set, actual;
};

static struct light *lights = NULL, **plights = &lights;

struct light *light_find(const char *name)
{
	struct light *l;
	for (l = lights; l; l = l->next)
		if (!strcmp(l->name, name))
			break;
	return l;
}

int light_set(struct light *l, unsigned value)
{
	if (l->aggregate) {
		int ec = 0;
		for (size_t i = 0; i < l->slave_count; i++)
			ec |= light_set(l->slaves[i], value);
		return ec;
	}

	struct can_message msg;
	msg.daddr = CANA_LIGHT_F(0, l->logical_addr);
	msg.dlc = 1;
	msg.bytes[0] = value;
	can_broadcast(l->u, &msg);

	return 0;
}

unsigned light_getset(struct light *l)
{
	if (l->aggregate) {
		unsigned long long sum = 0;
		size_t count = 0;

		for (size_t i = 0; i < l->slave_count; i++) {
			sum += light_getset(l->slaves[i]);
			count++;
		}

		if (count)
			return sum / count;
		return 0;
	}

	return l->set.val;
}

unsigned light_getact(struct light *l)
{
	if (l->aggregate) {
		unsigned long long sum = 0;
		size_t count = 0;

		for (size_t i = 0; i < l->slave_count; i++) {
			sum += light_getact(l->slaves[i]);
			count++;
		}

		if (count)
			return sum / count;
		return 0;
	}

	return l->actual.val;
}

static void light_json_handler(void *arg, json_t *json, enum json_subtype type)
{
	struct light *l = arg;
	struct light *p = l->aggregate ? l->slaves[0] : l;
	json_t *lobj = json_object();

	json_object_set_new(lobj, "klass", json_string("light"));
	json_object_set_new(lobj, "addr", json_integer(p->logical_addr));

	json_object_set_new(lobj, "actual", json_integer(light_getact(l)));
	if (type != JSON_LONGPOLL)
		json_object_set_new(lobj, "actual_ts", json_integer(p->actual.valid));
	json_object_set_new(lobj, "actual_tschg", json_integer(p->actual.change));

	json_object_set_new(lobj, "set", json_integer(light_getset(l)));
	if (type != JSON_LONGPOLL)
		json_object_set_new(lobj, "set_ts", json_integer(p->set.valid));
	json_object_set_new(lobj, "set_tschg", json_integer(p->set.change));

	json_object_set_new(json, l->name, lobj);
}

static void light_can_handler(void *arg, struct can_message *msg)
{
	struct light *l = arg;
	struct value *v = NULL;
	unsigned laddr;
	uint8_t dval;

	if (l->aggregate)
		return;

	if ((msg->daddr & CANA_PROTOCOL) == CANA_LIGHT)
		v = &l->set;
	if ((msg->daddr & CANA_PROTOCOL) == CANA_SENSOR)
		v = &l->actual;
	if (!v)
		return;

	laddr = msg->daddr & 0xfff;
	if (l->logical_addr < laddr)
		return;
	if (l->logical_addr - laddr >= msg->dlc)
		return;
	dval = msg->bytes[l->logical_addr - laddr];

	time(&v->valid);
	if (dval != v->val || v->change == 0) {
		v->val = dval;
		time(&v->change);
		lprintf("%s: set %02x", l->u->name, dval);

		json_bump_longpoll();
	}
}

int light_init_conf(json_t *config)
{
	struct light *l;
	bool aggregate;

	if (!json_is_object(config)) {
		lprintf("light config must be an object/dictionary");
		return 1;
	}
	if (json_is_integer(json_object_get(config, "addr"))) {
		aggregate = false;
	} else if (json_is_array(json_object_get(config, "slaves"))) {
		aggregate = true;
	} else {
		lprintf("light config must have an 'addr' key or 'saves'.");
		return 1;
	}

	if (!json_is_string(json_object_get(config, "name"))) {
		lprintf("light config must have a 'name' key");
		return 1;
	}
	
	l = calloc(sizeof(*l), 1);
	l->name = strdup(json_string_value(json_object_get(config, "name")));
	l->aggregate = aggregate;

	if (aggregate) {
		json_t *slaves = json_object_get(config, "slaves");
		l->slave_count = json_array_size(slaves);
		if (!l->slave_count) {
			lprintf("Slave array must not be empty.");
			return 1;
		}

		l->slaves = calloc(l->slave_count, sizeof(*l->slaves));
		for (size_t i = 0; i < json_array_size(slaves); i++) {
			const char *name = json_string_value(
				json_array_get(slaves, i)
			);
			l->slaves[i] = light_find(name);
			if (!l->slaves[i]) {
				lprintf("Unknown slave '%s'", name);
				return 1;
			}
		}
	} else {
		l->logical_addr = json_integer_value(
				json_object_get(config, "addr")
		);
	}

	l->u = can_register_alloc(l, light_can_handler, "light[%s]", l->name);
	l->u->json = light_json_handler;

	*plights = l;
	plights = &l->next;
	return 0;
}