-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshutter.c
111 lines (91 loc) · 2.55 KB
/
shutter.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <time.h>
#include "shutter-config.h"
#include "tasmota.h"
#include "utils.h"
#include "mqtt.h"
#include "mcp.h"
static void summer(struct tm *now, potd_t *potd) {
if (TEMP == UINT16_MAX || LUMI == UINT16_MAX) {
xlog("SHUTTER Error no sensor data");
return;
}
int hot = TEMP >= potd->temp && LUMI >= potd->lumi;
// xdebug("SHUTTER %s program temp=%.1f lumi=%d hot=%d", potd->name, temp, lumi, hot);
for (shutter_t **potds = potd->shutters; *potds != NULL; potds++) {
shutter_t *s = *potds;
int down = s->down_from <= now->tm_hour && now->tm_hour < s->down_to;
// down
if (!s->lock_down && down && hot) {
xlog("SHUTTER trigger %s DOWN %s at temp=%.1f lumi=%d", potd->name, s->name, TEMP, LUMI);
tasmota_shutter(s->id, s->down);
s->lock_down = 1;
s->lock_up = 0;
continue;
}
// up
if (!s->lock_up && !down) {
xlog("SHUTTER trigger %s UP %s at temp=%.1f lumi=%d", potd->name, s->name, TEMP, LUMI);
tasmota_shutter(s->id, SHUTTER_UP);
s->lock_up = 1;
s->lock_down = 0;
continue;
}
}
}
static void winter(struct tm *now, potd_t *potd) {
if (TEMP == UINT16_MAX || LUMI == UINT16_MAX) {
xlog("SHUTTER Error no sensor data");
return;
}
int down = now->tm_hour > 12 && LUMI <= potd->lumi && TEMP <= potd->temp;
int up = !down && LUMI >= potd->lumi;
// xdebug("SHUTTER %s program temp=%.1f lumi=%d", potd->name, temp, lumi);
for (shutter_t **potds = potd->shutters; *potds != NULL; potds++) {
shutter_t *s = *potds;
// down
if (!s->lock_down && down) {
xlog("SHUTTER trigger %s DOWN %s at temp=%.1f lumi=%d", potd->name, s->name, TEMP, LUMI);
tasmota_shutter(s->id, SHUTTER_DOWN);
s->lock_down = 1;
s->lock_up = 0;
continue;
}
// up
if (!s->lock_up && up) {
xlog("SHUTTER trigger %s UP %s at temp=%.1f lumi=%d", potd->name, s->name, TEMP, LUMI);
tasmota_shutter(s->id, SHUTTER_UP);
s->lock_up = 1;
s->lock_down = 0;
continue;
}
}
}
static void loop() {
if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) {
xlog("SHUTTER Error setting pthread_setcancelstate");
return;
}
// wait for sensors
sleep(3);
while (1) {
time_t now_ts = time(NULL);
struct tm *ltstatic = localtime(&now_ts);
if (SUMMER.months[ltstatic->tm_mon])
summer(ltstatic, &SUMMER);
else if (WINTER.months[ltstatic->tm_mon])
winter(ltstatic, &WINTER);
sleep(60);
}
}
static int init() {
return 0;
}
static void stop() {
}
MCP_REGISTER(shutter, 6, &init, &stop, &loop);