forked from BeeeOn/gateway-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
327 lines (270 loc) · 9.66 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* @file main.cpp
* @Author BeeeOn team
* @date
* @brief
*/
#include <Poco/Net/NetSSL.h>
#include "main.h"
using namespace std;
using namespace Poco::Net;
using Poco::AutoPtr;
using Poco::Logger;
using Poco::Runnable;
using Poco::Util::IniFileConfiguration;
bool quit_global_flag;
void mosq_thread (shared_ptr<MosqClient> mq) {
while (!quit_global_flag) {
int rc = mq->loop();
if (rc != MOSQ_ERR_SUCCESS) {
//cout << "MQTT: Trying to reconnect (" << rc << ")" << endl;
//TODO this whole code must be moved to mosquittoclient.cpp
mq->reconnect();
usleep(500000);
}
}
}
void killMe(int) {
printf("User pressed Ctrl+C\n");
quit_global_flag = true;
}
int main (int, char**) {
quit_global_flag = false;
signal(SIGINT, killMe);
signal(SIGTERM, killMe);
#ifdef LEDS_ENABLED
LEDControl::initLED(LED_LIME);
LEDControl::initLED(LED_PAN);
LEDControl::setLED(LED_LIME, false);
LEDControl::setLED(LED_PAN, false);
sleep(1);
LEDControl::blinkLED(LED_LIME, 3);
#endif
long long int adapter_id = 0x0;
bool mod_pan = false;
bool mod_virtual_sensor = false;
bool mod_pressure_sensor = false;
bool mod_mqtt = false;
bool mod_vpt = false;
bool mod_openhab = false;
AutoPtr<IniFileConfiguration> cfg;
AutoPtr<IniFileConfiguration> cfg_pan;
AutoPtr<IniFileConfiguration> cfg_virtual_sensor;
AutoPtr<IniFileConfiguration> cfg_pressure_sensor;
AutoPtr<IniFileConfiguration> cfg_vpt;
AutoPtr<IniFileConfiguration> cfg_mqtt;
AutoPtr<IniFileConfiguration> cfg_hab;
Poco::Thread agg_thread("Aggregator");
Poco::Thread vsm_thread("VSM");
Poco::Thread srv_thread("TCP");
Poco::Thread vpt_thread("VPT");
Poco::Thread hab_thread("HAB");
Logger& log = Poco::Logger::get("Adaapp-MAIN"); // get logging utility
log.setLevel("trace"); // set default lowest level
cerr << "Loading AdaApp.ini from " << CONFIG_FILE << endl;
/* Load main config file */
try {
cfg = new IniFileConfiguration(CONFIG_FILE);
}
catch (Poco::Exception& ex) {
cerr << "Error: Exception with config file reading:" << (string)ex.displayText() << endl;
return EXIT_FAILURE;
}
setLoggingLevel(log, cfg); /* Set logging level from configuration file*/
setLoggingChannel(log, cfg); /* Set where to log (console, file, etc.)*/
cerr << "Loading modules configurations from " << MODULES_DIR << endl;
try {
cfg_pan = new IniFileConfiguration(string(MODULES_DIR)+string(MOD_PAN)+".ini");
mod_pan = cfg_pan->getBool(string(MOD_PAN)+".enabled", false);
}
catch (...) { }
try {
cfg_virtual_sensor = new IniFileConfiguration(string(MODULES_DIR)+string(MOD_VIRTUAL_SENSOR)+".ini");
mod_virtual_sensor = cfg_virtual_sensor->getBool(string(MOD_VIRTUAL_SENSOR)+".enabled", false);
}
catch (...) { }
try {
cfg_pressure_sensor = new IniFileConfiguration(string(MODULES_DIR)+string(MOD_PRESSURE_SENSOR)+".ini");
mod_pressure_sensor = cfg_pressure_sensor->getBool(string(MOD_PRESSURE_SENSOR)+".enabled", false);
}
catch (...) { }
try {
cfg_vpt = new IniFileConfiguration(string(MODULES_DIR)+string(MOD_VPT_SENSOR)+".ini");
mod_vpt = cfg_vpt->getBool(string(MOD_VPT_SENSOR)+".enabled", false);
}
catch (...) { }
try { // OpenHAB
cfg_hab = new IniFileConfiguration(string(MODULES_DIR)+string(MOD_OPENHAB)+".ini");
mod_openhab = cfg_hab->getBool(string(MOD_OPENHAB)+".enabled", false);
}
catch (...) { }
try {
cfg_mqtt = new IniFileConfiguration(string(MODULES_DIR)+string(MOD_MQTT)+".ini");
mod_mqtt = cfg_mqtt->getBool(string(MOD_MQTT)+".enabled", false);
}
catch (...) { }
#ifndef PC_PLATFORM
EEPROM_ITEMS eeprom = parseEEPROM();
for (auto item : eeprom.items) {
if (item.first == 0x01) {
adapter_id = item.second;
break;
}
}
#endif
// Try to find adapter_id in configuration file
// TODO this is temporal solution for PC platform
if (adapter_id <= 0)
adapter_id = toNumFromString(cfg->getString("adapter.id", "0x0"));
// Fail if there is no valid adapter_id
if (adapter_id <= 0) {
log.fatal("Error: Adapter ID is not a valid number - quit! (ID = " + toStringFromLongHex(adapter_id) + ")");
return EXIT_FAILURE;
}
/* Print loaded modules */
log.information("Starting Adapter (ID: " + toStringFromLongHex(adapter_id) + ", FW_VERSION: " + FW_VERSION + ") with these modules:");
log.information(MOD_PAN + ": " + ((mod_pan) ? " ON" : "OFF"));
log.information(MOD_PRESSURE_SENSOR + ": " + ((mod_pressure_sensor) ? " ON" : "OFF"));
log.information(MOD_VIRTUAL_SENSOR + ": " + ((mod_virtual_sensor) ? " ON" : "OFF"));
log.information(MOD_MQTT + ": " + ((mod_mqtt) ? " ON" : "OFF"));
log.information(MOD_VPT_SENSOR + ": " + ((mod_vpt) ? " ON" : "OFF"));
log.information(MOD_OPENHAB + ": " + ((mod_openhab) ? " ON" : "OFF"));
srand(time(0));
/* Create default "header" for messages which are sent to server - these parameters are seldomly changed during runtime */
IOTMessage msg;
msg.adapter_id = toStringFromLongHex(adapter_id);
msg.fw_version = FW_VERSION;
msg.protocol_version = cfg->getString("versions.protocol", "0.1");
msg.debug = false;
msg.priority = MSG_PRIO_SENSOR;
msg.valid = true;
msg.offset = 0;
msg.tt_version = 0;
string IP_addr_out = cfg->getString("server.ip");
int port_out = cfg->getInt("server.port");
/* SSL stuff - Lukas Koszegy */
Poco::SharedPtr<PrivateKeyPassphraseHandler> pConsoleHandler = new KeyConsoleHandler(true);
Poco::SharedPtr<InvalidCertificateHandler> pInvalidCertHandler = new ConsoleCertificateHandler(true);
Context::Ptr pContext = NULL;
try {
pContext = new Context(Context::CLIENT_USE, cfg->getString("ssl.key", ""), cfg->getString("ssl.certificate", ""), cfg->getString("ssl.calocation", "./"), (Context::VerificationMode) cfg->getInt("ssl.verify_level", Context::VERIFY_RELAXED), 9, false, "ALL:ADH:!LOW:!EXP:!MD5:@STRENGTH");
} catch (Poco::Exception& ex) {
log.fatal("Creating SSL failed! Please check cerficate file and configuration! (" + (string)ex.displayText() + ")");
return (EXIT_FAILURE);
}
SSLManager::instance().initializeClient(0, pInvalidCertHandler, pContext);
initializeSSL();
/* Module initialization, start threads, wait in loop and test for termination of the app */
try {
shared_ptr<MosqClient> mosq;
shared_ptr<PanInterface> pan;
shared_ptr<VPTSensor> vptsensor;
shared_ptr<OpenHAB> hab;
shared_ptr<PressureSensor> psm;
shared_ptr<VirtualSensorModule> vsm;
std::thread mq_t;
if (mod_mqtt) {
log.information("Starting MQTT module.");
std::string mq_client_id = cfg_mqtt->getString("mqtt.client_id", "AdaApp");
std::string mq_main_topic = cfg_mqtt->getString("mqtt.main_topic", "BeeeOn/#");
std::string mq_host_addr = cfg_mqtt->getString("mqtt.host_addr", "localhost");
int mq_port = cfg_mqtt->getInt("mqtt.port", 1883);
log.information("Starting Mosquitto Client.");
mosq.reset(new MosqClient(mq_client_id, mq_main_topic, "AdaApp", mq_host_addr, mq_port));
mq_t = std::thread(mosq_thread, mosq);
}
/* Mandatory module for sending and receiving data */
shared_ptr<Aggregator> agg (new Aggregator(msg, mosq));
msg.state = "register";
msg.time = time(NULL);
/* Mandatory component for receiving asynchronous messages from server */
shared_ptr<IOTReceiver> receiver (new IOTReceiver(agg, IP_addr_out, port_out, msg, adapter_id));
receiver->keepaliveInit(cfg);
agg->setTCP(receiver);
// BeeeOn PAN coordinator module
if (mod_pan) {
log.information("Creating PAN module.");
pan.reset(new PanInterface(msg, agg));
pan->set_pan(pan); // XXX Function name should be same
agg->setPAN(pan);
}
if (mod_vpt) {
log.information("Creating VPT module.");
vptsensor.reset(new VPTSensor(msg, agg, adapter_id));
agg->setVPT(vptsensor);
}
if (mod_openhab && mod_mqtt) {
log.information("Creating OpenHAB module.");
hab.reset(new OpenHAB(msg, agg));
agg->setHAB(hab);
}
if (mod_pressure_sensor) {
log.information("Creating PressureSensors module.");
psm.reset(new PressureSensor(msg, agg));
agg->setPSM(psm);
}
if (mod_virtual_sensor) {
log.information("Creating VirtualSensors module.");
vsm.reset(new VirtualSensorModule(msg, agg));
agg->setVSM(vsm);
}
agg_thread.start(*agg.get());
srv_thread.start(*receiver.get());
if (mod_vpt) {
log.information("Starting VPT module.");
vpt_thread.start(*vptsensor.get());
}
if (mod_openhab && mod_mqtt) {
log.information("Starting OpenHAB module.");
hab_thread.start(*hab.get());
}
if (mod_pressure_sensor) {
log.information("Starting PressureSensors module.");
psm.get()->start();
}
if (mod_virtual_sensor) {
log.information("Starting VirtualSensors module.");
vsm_thread.start(*vsm.get());
}
/* "Endless" loop waiting for SIGINT/SIGTERM */
while (!quit_global_flag) {
sleep(1);
}
log.information("Stopping modules...");
if (mod_virtual_sensor) {
log.information("Stopping Virtual Sensor module...");
vsm_thread.join();
}
if (mod_pressure_sensor) {
log.information("Stopping Pressure Sensor module...");
psm.get()->stop();
}
if (mod_vpt) {
log.information("Stopping VPT Sensor module...");
vpt_thread.join();
}
if (mod_openhab && mod_mqtt) {
log.information("Stopping OpenHAB module...");
hab_thread.join();
}
if (mod_mqtt) {
log.information("Stopping MQTT...");
mq_t.join();
}
log.information("Stopping server...");
srv_thread.join();
log.information("Stopping aggregator...");
agg_thread.join();
uninitializeSSL();
}
catch (Poco::Exception& ex) {
log.fatal("Error: Poco exception!" + (string)ex.displayText());
return (EXIT_FAILURE);
}
catch (std::exception& ex) {
log.fatal("Error: Standard expcetion!\n" + (string)ex.what());
return (EXIT_FAILURE);
}
log.information("Adapter finished");
return 0;
}