-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebcam.c
91 lines (74 loc) · 1.8 KB
/
webcam.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "utils.h"
#include "webcam.h"
#include "mcp.h"
#define LUMI sensors->bh1750_raw2
static int webcam_on;
static void webcam_start() {
system(WEBCAM_START);
webcam_on = 1;
xlog("executed %s", WEBCAM_START);
}
static void start_reset() {
system(WEBCAM_START_RESET);
webcam_on = 1;
xlog("executed %s", WEBCAM_START_RESET);
}
static void webcam_stop() {
system(WEBCAM_STOP);
webcam_on = 0;
xlog("executed %s", WEBCAM_STOP);
}
static void stop_timelapse() {
system(WEBCAM_STOP_TIMELAPSE);
webcam_on = 0;
xlog("executed %s", WEBCAM_STOP_TIMELAPSE);
}
static void webcam() {
if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) {
xlog("Error setting pthread_setcancelstate");
return;
}
// wait till network & nfs available
sleep(15);
// state unknown (e.g. system startup) --> check need for switching on
if (LUMI > WEBCAM_SUNRISE)
webcam_start();
else
webcam_stop();
while (1) {
time_t now_ts = time(NULL);
struct tm *ltstatic = localtime(&now_ts);
int afternoon = ltstatic->tm_hour < 12 ? 0 : 1;
if (afternoon && webcam_on) {
// evening and on --> check if need to switch off
// xlog("awaiting WEBCAM_SUNDOWN at %i", value);
if (LUMI < WEBCAM_SUNDOWN) {
xlog("reached WEBCAM_SUNDOWN at bh1750_raw2=%d", LUMI);
stop_timelapse();
}
}
if (!afternoon && !webcam_on) {
// morning and off --> check if need to switch on
// xlog("awaiting WEBCAM_SUNRISE at %i", value);
if (LUMI > WEBCAM_SUNRISE) {
xlog("reached WEBCAM_SUNRISE at bh1750_raw2=%d", LUMI);
start_reset();
}
}
sleep(60);
}
}
static int init() {
webcam_on = 0;
return 0;
}
static void stop() {
webcam_stop();
}
MCP_REGISTER(webcam, 6, &init, &stop, &webcam);