-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
134 lines (124 loc) · 3.77 KB
/
main.cpp
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
/*
* Copyright (C) 2018-2019 Ivan Schréter ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This copyright notice MUST APPEAR in all copies of the software!
*/
#include "enocean_to_hue_bridge.hpp"
#include <iostream>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/wait.h>
#include <syslog.h>
static int64_t timestamp() noexcept
{
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
return tp.tv_nsec / 1000000 + tp.tv_sec * 1000;
}
static void usage(const char* name)
{
std::cerr << "Usage: " << name <<
" <usb300 port> <mapping file> <bridge IP> <API key> <sensor ID> [<bridge IP> <API key> <sensor ID>]...\n";
}
int main(int argc, const char** argv)
{
auto progname = argv[0];
if (argc < 6) {
usage(progname);
return 1;
}
const char* serial_port = argv[1];
const char* config_file = argv[2];
argv += 3;
argc -= 3;
std::deque<hue_sensor_command_posix> bridges;
while (argc >= 3) {
if (bridges.size() == 8) {
std::cerr << "At most 8 bridges are supported\n";
usage(progname);
return 1;
}
struct in_addr bridge_addr;
if (!inet_aton(argv[0], &bridge_addr)) {
std::cerr << "Cannot parse bridge IP address '" << argv[0] << "'\n";
usage(progname);
return 1;
}
int sensor_id;
char* end;
auto id = strtol(argv[2], &end, 10);
if (end == argv[2] || *end || id < 1 || id > 255) {
std::cerr << "Specified sensor ID '" << argv[2] <<
"' is invalid. Expected ID in range [1,255].\n";
usage(progname);
return 1;
}
sensor_id = int(id);
bridges.emplace_back(bridge_addr.s_addr, argv[1], sensor_id);
argv += 3;
argc -= 3;
}
if (argc != 0)
{
std::cerr << "Extraneous argument(s) on the command line\n";
usage(progname);
return 1;
}
setlogmask(LOG_UPTO(LOG_INFO));
openlog("enocean_to_hue", LOG_CONS | LOG_PID | LOG_PERROR, LOG_LOCAL1);
uint32_t respawn_cnt = 0;
for (;;)
{
auto start_time = timestamp();
auto pid = fork();
if (!pid) {
// child process, run the bridge
openlog("enocean_to_hue", LOG_CONS | LOG_PID | LOG_PERROR, LOG_LOCAL1);
try {
enocean_to_hue_bridge bridge(serial_port, bridges, config_file);
bridge.run_poll_loop();
} catch (std::exception& e) {
syslog(LOG_ERR, "EnOcean ERROR: %s", e.what());
}
return 1;
}
// parent process, wait for child
syslog(LOG_INFO, "EnOcean Started child process %d", pid);
int status;
auto cpid = wait(&status);
auto end_time = timestamp();
auto delta = end_time - start_time;
syslog(LOG_ERR, "EnOcean Child process %d exited with status %d after %lld ms", cpid, status, delta);
if (pid != cpid) {
syslog(LOG_ERR, "EnOcean Wrong PID %d of terminated process, expected %d", cpid, pid);
return 1;
}
if (delta >= 60000) {
respawn_cnt = 0;
continue; // restart immediately, it ran long enough
} else if (delta < 5000) {
// short runtime, assume wrong configuration
++respawn_cnt;
}
// sleep before respawn
if (respawn_cnt < 10)
sleep(1);
else if (respawn_cnt < 30)
sleep(30);
else
sleep(60);
}
}