-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlarmPI.cpp
312 lines (254 loc) · 8.67 KB
/
AlarmPI.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//============================================================================
// Name : AlarmPI.cpp
// Author : Horfee
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <algorithm>
#include "AlarmSystemDAO.h"
#include "AlarmSystemDAOSQLite.h"
#include "DAOFactory.h"
#include "httpserver/HTTPServer.h"
#include "httpserver/HTTPFileServlet.h"
#include "json/json.h"
#include "Property.h"
#include "Properties.h"
#include <iomanip>
#include "servlets/AlarmPIModesServlet.h"
#include "servlets/AlarmPIPropertiesServlet.h"
#include "servlets/AlarmPIPhonesServlet.h"
#include "servlets/AlarmPIAssociationsServlet.h"
#include "servlets/AlarmPIDevicesServlet.h"
#include "servlets/AlarmPIActionsServlet.h"
#include "servlets/AlarmPIDetectedDevicesServlet.h"
#include "servlets/AlarmPISystemServlet.h"
#include "servlets/AlarmPISimulateSignalReceivedServlet.h"
#include "servlets/AlarmPIPingServlet.h"
#include "servlets/AlarmPISimulateMessageServlet.h"
#include <syslog.h>
#ifdef RPI
#ifdef WIRINGPI
#include <wiringPi.h>
#else
#include <pigpio.h>
#endif
#endif
#include <unistd.h>
using namespace std;
using namespace alarmpi;
//bool stopAsked = false;
#include <regex>
#include <event2/listener.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <evhttp.h>
#include <vector>
#include <sstream>
#include "Utils.h"
//#include <codecvt>
#include <cstdlib>
#include <string>
#ifndef S_ISDIR
#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
#endif
bool stopAsked = false;
bool rebootAsked = false;
void daemonize(const char* pidfile) {
logMessage(LOG_DEBUG, "Daemonizing");
/* Daemonize - make ourselves a subprocess. */
#ifdef HAVE_DAEMON
if ( daemon( 1, 1 ) < 0 ) {
logMessage( LOG_CRIT, "daemon - %m" );
exit( EXIT_FAILURE );
}
#else /* HAVE_DAEMON */
switch (fork()) {
case 0:
break;
case -1:
logMessage( LOG_CRIT, "fork - %m");
exit(EXIT_FAILURE);
default:
exit(EXIT_SUCCESS);
}
#endif /* HAVE_DAEMON */
int sid = setsid();
if (sid < 0) {
logMessage( LOG_CRIT, "Error on setsid %s", strerror(errno));
exit(EXIT_FAILURE);
}
if (pidfile != NULL) {
/* Write the PID file. */
FILE* pidfp = fopen(pidfile, "w");
if (pidfp == NULL) {
syslog( LOG_CRIT, "%.80s - %m", pidfile);
exit(1);
}
fprintf(pidfp, "%d\n", (int) getpid());
fclose(pidfp);
}
}
bool mustDaemonize = false;
int logLevel = LOG_NOTICE;
void parseArgs(int argc, char *argv[]) {
for(int i = 1; i < argc; i++) {
if ( std::string(argv[i]) == "-d" || std::string(argv[i]) == "-D") {
mustDaemonize = true;
} else if ( argv[i][0] == '-' && (argv[i][1] == 'v' || argv[i][1] == 'V') ) {
logLevel = atoi(argv[i]+2);
}
}
}
int main(int argc, char* argv[]) {
struct sigaction sigIntHandler;
alarmpi::AlarmSystemDAO* dao;
alarmpi::AlarmSystem* alarmSystem;
httpserver::HTTPServer* server;
parseArgs(argc, argv);
setlogmask (LOG_UPTO (logLevel));
openlog (argv[0], LOG_CONS | LOG_PERROR | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
sigIntHandler.sa_handler = [](int s){
stopAsked = true;
};
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGTERM, &sigIntHandler, NULL);
sigaction(SIGKILL, &sigIntHandler, NULL);
sigaction(SIGABRT, &sigIntHandler, NULL);
sigaction(SIGINT, &sigIntHandler, NULL);
sigaction(SIGCONT, &sigIntHandler, NULL);
sigaction(SIGSEGV, &sigIntHandler, NULL);
if ( mustDaemonize ) {
daemonize("/var/run/alarmPI.pid");
}
umask(0);
char cwd[2048] = "/etc/alarmpi/";
if (chdir(cwd) < 0) {
logMessage( LOG_CRIT, "chdir - %m %s", strerror(errno));
exit(EXIT_FAILURE);
}
dao = new alarmpi::AlarmSystemDAOSQLite("alarmsystem.db");
alarmpi::DAOFactory::getInstance()->setDAO(dao);
#ifdef RPI
#ifdef WIRINGPI
if ( wiringPiSetup() == -1 ) {
logMessage( LOG_CRIT, "unable to initialize wiring API");
} else {
logMessage( LOG_NOTICE, "WIRINGPI initialized successfully");
}
#else
int nbTry = 10;
int res;
while ( nbTry-- > 0 && (res = gpioInitialise()) == PI_INIT_FAILED ) {
gpioTerminate();
logMessage( LOG_CRIT, "unable to initialize pigpio API");
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
if ( res == PI_INIT_FAILED ) {
// return 1;
} else {
logMessage( LOG_NOTICE, "PIGPIO initialized successfully");
}
#endif
#endif
alarmSystem = new alarmpi::AlarmSystem();
std::string m = alarmSystem->activeMode();
Property* listeningAddress = alarmSystem->getProperty(PROPERTY_WEBSERVER_ADDR);
Property* listeningPort = alarmSystem->getProperty(PROPERTY_WEBSERVER_PORT);
Property* webserverRoot = alarmSystem->getProperty(PROPERTY_WEBSERVER_ROOT);
if ( listeningAddress == NULL || listeningPort == NULL || webserverRoot == NULL ) {
alarmSystem->setConfigMode(DEFAULT_VALUE_PASSWORD);
if ( listeningAddress == NULL ) {
listeningAddress = new Property(PROPERTY_WEBSERVER_ADDR, PROPERTY_WEBSERVER_ADDR_DESCRIPTION, (std::string)DEFAULT_VALUE_WEBSERVER_ADDR);
alarmSystem->addProperty(listeningAddress);
}
if ( listeningPort == NULL ) {
listeningPort = new Property(PROPERTY_WEBSERVER_PORT, PROPERTY_WEBSERVER_PORT_DESCRIPTION, (int)DEFAULT_VALUE_WEBSERVER_PORT);
alarmSystem->addProperty(listeningPort);
}
if ( webserverRoot == NULL ) {
webserverRoot = new Property(PROPERTY_WEBSERVER_ROOT, PROPERTY_WEBSERVER_ROOT_DESCRIPTION, (std::string)DEFAULT_VALUE_WEBSERVER_ROOT);
alarmSystem->addProperty(webserverRoot);
}
alarmSystem->activateMode(m, DEFAULT_VALUE_PASSWORD);
}
server = new HTTPServer(listeningAddress->getStringValue(), listeningPort->getIntValue());
server->setAllowedMethods(EVHTTP_REQ_OPTIONS | EVHTTP_REQ_GET | EVHTTP_REQ_POST | EVHTTP_REQ_PUT | EVHTTP_REQ_DELETE);
HTTPServlet* servlets[12];
server->setDefaultServlet( servlets[0] = new HTTPFileServlet(webserverRoot->getStringValue()));
server->addServlet("/rest/modes.*", servlets[1] = new AlarmPIModesServlet(alarmSystem));
server->addServlet("/rest/devices.*", servlets[2] = new AlarmPIDevicesServlet(alarmSystem));
server->addServlet("/rest/detecteddevices.*", servlets[3] = new AlarmPIDetectedDevicesServlet(alarmSystem));
server->addServlet("/rest/properties.*", servlets[4] = new AlarmPIPropertiesServlet(alarmSystem));
server->addServlet("/rest/actions.*", servlets[5] = new AlarmPIActionsServlet(alarmSystem));
server->addServlet("/rest/phones.*", servlets[6] = new AlarmPIPhonesServlet(alarmSystem));
server->addServlet("/rest/associations.*", servlets[7] = new AlarmPIAssociationsServlet(alarmSystem));
server->addServlet("/services.*", servlets[8] = new AlarmPISystemServlet(alarmSystem, &stopAsked, &rebootAsked));
server->addServlet("/ping.*", servlets[9] = new AlarmPIPingServlet(alarmSystem));
server->addServlet("/rest/signal.*", servlets[10] = new AlarmPISimulateSignalReceivedServlet(alarmSystem));
server->addServlet("/rest/message.*", servlets[11] = new AlarmPISimulateMessageServlet(alarmSystem));
//server->addServlet("/services/.*", servlets[8] = new ShutdownServiceServlet());
logMessage( LOG_INFO, "Starting Web Server on node %s:%d", listeningAddress->getStringValue().c_str(), listeningPort->getIntValue());
server->start();
class InnerNetworkListener : public NetworkListener {
public:
void onConnectionStateChanged(bool connected) {
server->stop();
server->start();
}
void onClientConnected(bool created) {
}
InnerNetworkListener(httpserver::HTTPServer *server) {
this->server = server;
}
virtual ~InnerNetworkListener() {
}
private:
httpserver::HTTPServer *server;
};
alarmSystem->addListener(new InnerNetworkListener(server));
std::string sysAvailableOn("System available on : \n");
int portNumber = alarmSystem->getProperty(PROPERTY_WEBSERVER_PORT)->getIntValue();
for(std::string s : alarmSystem->ipAddresses()) {
sysAvailableOn += s + ":" + std::to_string(portNumber) + "\n";
}
logMessage( LOG_INFO, sysAvailableOn);
while ( !stopAsked ) {
usleep(500);
}
logMessage( LOG_NOTICE, "Exit from main loop");
delete server;
for(auto it : servlets) {
delete it;
}
logMessage( LOG_NOTICE, "Web server deleted");
delete alarmSystem;
logMessage( LOG_NOTICE, "Alarm system deleted");
alarmpi::DAOFactory::getInstance()->setDAO(NULL);
delete dao;
logMessage( LOG_NOTICE, "DAO deleted");
#ifdef RPI
#ifndef WIRINGPI
gpioTerminate();
#endif
#endif
closelog ();
if ( rebootAsked ) {
return system("reboot now");
}
return 0;
}