-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
executable file
·128 lines (93 loc) · 3.64 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
/*
The MIT License (MIT)
Copyright (c) 2017 Wassim Filali
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
___________________________________________________________________________________
dependencies :
- Poco,Boost filesystem : using The Boost Software License 1.0
- bme_280 : with datasheet's associated rights see bme280_server.hpp
- json.hpp MIT License Copyright (c) 2013-2017 Niels Lohmann <http://nlohmann.me>.
___________________________________________________________________________________
start date : 20.01.2017
takeopver into rf_gateway : 13.12.2017
*/
//for usleep() sleep()
#include <unistd.h>
//for printf
#include <stdio.h>
//for stdout
#include <iostream>
//for file
#include <fstream>
//for abs
#include <cmath>
//for exit()
#include <cstdlib>
#include "serial.hpp"
#include "utils.hpp"
#include "mqtt_rf.hpp"
#include "mesh.hpp"
#include <assert.h>
#include <string>
#include <memory>
#include "json.hpp"
using json = nlohmann::json;
#include "log.hpp"
using namespace std;
json read_json(std::string const &filename)
{
//TODO check file existence without boost
std::ifstream data_file(filename);
json result;
data_file >> result;
return result;
}
int main(int argc, const char *argv[])
{
string path(argv[0]);
std::cout << "______________________Main Gateway Init______________________" << std::endl;
json config = read_json(path+"_config.json");
json calib = read_json(path+"_bme280_calibration.json");
Log::config(config["log"]);
Serial stream(config["serial"],calib); // - process serial port stream : - calibrate sensors values
// - provides ready to store measures MAP of Nodes.Sensors.Values,Timestamp
// - If not configured to be used then the .update() polling is neutral
mqtt_rf_c mqtt(config["mqtt_client"],stream); //MQTT client app wrapper, will attempt connection on creation if params provided
//could be retrived from parsing the dongle startup or by querry
mesh::set_source_nodId(config["mqtt_client"]["dongle_node_id"]);
//#2 issue, it is likely that someone else is using the port in parallel
//discard first trash buffer if available right after opening the port
//this discard measure is not enough as ibberish appears still
stream.update();
std::cout << "______________________Main Gateway Loop______________________" << std::endl;
while (1)
{
if(stream.update())
{
NodeMap_t measures = stream.processBuffer();
if(measures.size() != 0)
{
mqtt.publish_measures(measures);
}
}
//run() contains the loop needed to process certain QoS messages and reconnect if connection lost
mqtt.run();
//5 ms : this is an unnneccessary load if the processing grows up
usleep(5000);
}
return 0;
}