-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxmas.c
179 lines (147 loc) · 3.77 KB
/
xmas.c
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <time.h>
#include "tasmota.h"
#include "flamingo.h"
#include "utils.h"
#include "xmas.h"
#include "mcp.h"
#define LUMI sensors->bh1750_lux
// TODO define channel status for each remote control unit
static char channel_status[128];
static int power = -1;
void xmas_on() {
#ifndef PICAM
for (int i = 0; i < ARRAY_SIZE(devices); i++) {
const xmas_t *device = &devices[i];
tasmota_power(device->id, device->relay, 1);
}
#endif
}
void xmas_off() {
#ifndef PICAM
for (int i = 0; i < ARRAY_SIZE(devices); i++) {
const xmas_t *device = &devices[i];
tasmota_power(device->id, device->relay, 0);
}
#endif
}
static void xmas_on_flamingo(const xmas_timing_t *timing) {
int index = timing->channel - 'A';
if (!channel_status[index]) {
xlog("flamingo_send_FA500 %d %c 1", timing->remote, timing->channel);
flamingo_send_FA500(timing->remote, timing->channel, 1, -1);
channel_status[index] = 1;
}
}
static void xmas_off_flamingo(const xmas_timing_t *timing) {
int index = timing->channel - 'A';
if (channel_status[index]) {
xlog("flamingo_send_FA500 %d %c 0", timing->remote, timing->channel);
flamingo_send_FA500(timing->remote, timing->channel, 0, -1);
channel_status[index] = 0;
}
}
static void on_sundown(const xmas_timing_t *timing) {
xlog("XMAS reached SUNDOWN at %d", LUMI);
xmas_on();
xmas_on_flamingo(timing);
power = 1;
}
static void on_morning(const xmas_timing_t *timing) {
xlog("XMAS reached ON time frame");
xmas_on();
xmas_on_flamingo(timing);
power = 1;
}
static void off_sunrise(const xmas_timing_t *timing) {
xlog("XMAS reached SUNRISE at %d", LUMI);
xmas_off();
xmas_off_flamingo(timing);
power = 0;
}
static void off_evening(const xmas_timing_t *timing) {
xlog("XMAS reached OFF time frame");
xmas_off();
xmas_off_flamingo(timing);
power = 0;
}
static int process(int h, int m, const xmas_timing_t *timing) {
int afternoon = h < 12 ? 0 : 1;
int curr = h * 60 + m;
int from = timing->on_h * 60 + timing->on_m;
int to = timing->off_h * 60 + timing->off_m;
if (from <= curr && curr <= to) {
// ON time frame
// already on
if (power == 1)
return 0;
if (afternoon) {
// evening: check if sundown is reached an switch on
if (LUMI < SUNDOWN)
on_sundown(timing);
else
// xlog("in ON time, waiting for XMAS_SUNDOWN(%d:%d) ", lumi, XMAS_SUNDOWN);
return 0;
} else
// morning: switch on
on_morning(timing);
} else {
// OFF time frame
// already off
if (power == 0)
return 0;
if (!afternoon)
// morning: check if sunrise is reached an switch off
if (LUMI > SUNRISE)
off_sunrise(timing);
else
// xlog("in OFF time, waiting for XMAS_SUNRISE(%d:%d)", lumi, XMAS_SUNRISE);
return 0;
else
// evening: switch off
off_evening(timing);
}
return 0;
}
static void xmas() {
if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) {
xlog("XMAS Error setting pthread_setcancelstate");
return;
}
// elevate realtime priority for flamingo 433MHz transmit
if (elevate_realtime(1) < 0) {
xlog("XMAS Error elevating realtime");
return;
}
sleep(3); // wait for sensors
if (LUMI == UINT16_MAX) {
xlog("XMAS Error no sensor data");
return;
}
while (1) {
time_t now_ts = time(NULL);
struct tm *ltstatic = localtime(&now_ts);
int h = ltstatic->tm_hour, m = ltstatic->tm_min, wday = ltstatic->tm_wday;
for (int i = 0; i < ARRAY_SIZE(timings); i++) {
const xmas_timing_t *timing = &timings[i];
if (!timing->active)
continue;
if (wday != timing->wday)
continue;
// xlog("processing timing[%i]", i);
process(h, m, timing);
}
sleep(60);
}
}
static int init() {
return 0;
}
static void stop() {
}
MCP_REGISTER(xmas, 6, &init, &stop, &xmas);