-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshutter.c
More file actions
108 lines (89 loc) · 2.6 KB
/
shutter.c
File metadata and controls
108 lines (89 loc) · 2.6 KB
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
#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 "shutter-config.h"
#include "sensors.h"
#include "utils.h"
#include "mqtt.h"
#include "mcp.h"
static void summer(struct tm *now, potd_t *potd) {
if (sensor->tout >= UINT16_MAX || sensor->lumi == UINT16_MAX) {
xlog("SHUTTER Warning no sensor data");
return;
}
int hot = sensor->tout >= potd->temp && sensor->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, sensor->tout, sensor->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, sensor->tout, sensor->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 (sensor->tout >= UINT16_MAX || sensor->lumi == UINT16_MAX) {
xlog("SHUTTER Warning no sensor data");
return;
}
int dn = now->tm_hour > 12 && sensor->lumi < potd->lumi && sensor->tout <= potd->temp;
int up = now->tm_hour < 12 && sensor->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 && dn) {
xlog("SHUTTER trigger %s DOWN %s at temp=%.1f lumi=%d", potd->name, s->name, sensor->tout, sensor->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, sensor->tout, sensor->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;
}
while (1) {
LOCALTIME
if (SUMMER.months[now->tm_mon])
summer(now, &SUMMER);
else if (WINTER.months[now->tm_mon])
winter(now, &WINTER);
sleep(60);
}
}
static int init() {
return 0;
}
static void stop() {
}
MCP_REGISTER(shutter, 6, &init, &stop, &loop);