-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDistributor.cpp
268 lines (232 loc) · 7.83 KB
/
Distributor.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
/**
* @file Distributor.cpp
* @Author BeeeOn team
* @date
* @brief
*/
#include <unistd.h>
#include "Distributor.h"
using namespace std;
using Poco::AutoPtr;
using Poco::FastMutex;
using Poco::Logger;
using Poco::Util::IniFileConfiguration;
void Distributor::run() {
log.information("Distributor Thread running.");
if (mq.get() != nullptr){
mq->setAgg(&agg);
}
while(!quit_global_flag) {
if (pending_lock->tryLock(1000)) {
if (!pending_msgs.empty()) {
log.information("Items in queue (" + toStringFromInt(pending_msgs.size()) + "):");
if (geek_mode_enabled) {
log.information("Thread function of Distributor is sending data to Geek named pipe.");
sendToNamedPipe(pending_msgs.front());
}
// TODO Other ways of sending
if (mq)
sendDataToMQTT(pending_msgs.front());
pending_msgs.pop_front();
pending_lock->unlock();
}
else
pending_lock->unlock();
}
else {
log.error("Cannot lock pending queue in thread function.");
}
usleep(DIST_THREAD_SLEEP);
}
if (remove(geek_mode_pipe.c_str()) != 0) {
log.error("Can't remove geek pipe file: " + (string)strerror(errno));
}
}
Distributor::Distributor(Aggregator &_agg, shared_ptr<MosqClient> _mq) :
geek_mode_enabled(false),
agg(_agg),
mq(_mq),
log(Poco::Logger::get("Adaapp-DIST"))
{
history_lock.reset(new FastMutex);
pending_lock.reset(new FastMutex);
pending_msgs.clear();
history.clear();
AutoPtr<IniFileConfiguration> cfg;
try {
cfg = new IniFileConfiguration(CONFIG_FILE);
geek_mode_enabled = cfg->getBool("distributor.geek_mode_enabled", false);
if (geek_mode_enabled) {
geek_mode_pipe = cfg->getString("distributor.geek_mode_path", "/tmp/geek_mode");
log.information("Geek mode enabled -> destination: " + geek_mode_pipe);
initGeekMode();
}
}
catch (Poco::Exception& ex) {
log.error("Exception with config file reading:\n" + ex.displayText());
}
}
Distributor::~Distributor() {
}
void Distributor::addNewMessage(IOTMessage msg) {
// Send only data messages (silently discard others)
if (msg.state != "data") {
log.information("This is not message with sensor's data - ignoring! (state = " + msg.state + ")");
return;
}
if (findIfExists(msg)) {
log.information("This message is already in history queue - won't add that.");
return;
}
// Add message to the send queue
if (pending_lock->tryLock(2000)) {
pending_msgs.push_back(msg);
pending_lock->unlock();
}
else {
log.warning("Cannot add item to pending queue!");
}
// Add message to the history
if (history_lock->tryLock(2000)) {
history.insert(std::pair<unsigned long int, IOTMessage>(msg.time, msg));
log.information("Inserted to the history, now contains " + toStringFromInt(history.size()) + " items.");
history_lock->unlock();
}
else {
log.warning("Cannot add item to history!");
}
}
bool Distributor::findIfExists(IOTMessage m1) {
for (auto it = history.find(m1.time); it != history.end(); it++) {
IOTMessage m2 = it->second;
if (m1.fw_version != m2.fw_version) return false;
if (m1.tt_version != m2.tt_version) return false;
if (m1.protocol_version != m2.protocol_version) return false;
if (m1.state != m2.state) return false;
if (m1.debug != m2.debug) return false;
//if (m1.device.battery != m2.device.battery) return false;
if (m1.device.euid != m2.device.euid) return false;
if (m1.device.device_id != m2.device.device_id) return false;
if (m1.device.pairs != m2.device.pairs) return false;
//if (m1.device.rssi != m2.device.rssi) return false;
if (m1.device.version != m2.device.version) return false;
if (m1.device.values != m2.device.values) return false;
return true;
}
return false;
}
std::string Distributor::convertToCSV(IOTMessage msg, bool full_format) {
std::string ret;
ret += "time;" + toStringFromLongInt(msg.time) + ";";
ret += "euid;" + toStringFromLongInt(msg.device.euid) + ";";
ret += "device_id;" + toStringFromLongInt(msg.device.device_id) + ";";
if (full_format) {
ret += "state;" + msg.state + ";";
ret += "fw_version;" + msg.fw_version + ";";
ret += "protocol_version;" + msg.protocol_version + ";";
ret += "dev_version;" + toStringFromInt(msg.device.version) + ";";
ret += "tt_version;" + toStringFromLongInt(msg.tt_version) + ";";
ret += "valid;" + (msg.valid ? string("yes") : string("no")) + ";";
}
//ret += "battery;" + toStringFromInt(msg.device.battery) + ";";
//ret += "rssi;" + toStringFromInt(msg.device.rssi) + ";";
ret += "pairs;" + toStringFromInt(msg.device.pairs) + "\n";
for (auto i : msg.device.values) { // module_id, value
ret += "module_id;" + toStringFromHex(i.mid) + ";";
ret += "value;" + toStringFromFloat(i.value);
if (i.status) {
ret += "\n";
}
else {
ret += ";status;unavailable\n";
}
}
return ret;
}
std::string Distributor::convertToXML(IOTMessage msg) {
std::string ret;
unique_ptr<XMLTool> xml(new XMLTool(ServerMessage(msg)));
if (msg.state == "register")
ret = xml->createXML(INIT);
else
ret = xml->createXML(A_TO_S);
return ret;
}
std::string Distributor::convertToPlainText(IOTMessage msg) {
std::string ret;
ret += "time:" + toStringFromLongInt(msg.time) + "\n";
ret += "euid:" + toStringFromLongInt(msg.device.euid) + "\n";
ret += "device_id:" + toStringFromLongInt(msg.device.device_id) + "\n";
ret += "state:" + msg.state + "\n";
ret += "fw_version:" + msg.fw_version + "\n";
ret += "protocol_version:" + msg.protocol_version + "\n";
ret += "dev_version:" + toStringFromInt(msg.device.version) + "\n";
//ret += "battery:" + toStringFromInt(msg.device.battery) + "\n";
//ret += "rssi:" + toStringFromInt(msg.device.rssi) + "\n";
ret += "sensor_values:\n";
for (auto i : msg.device.values) { // type, offset, value
ret += "\tmodule_id:" + toStringFromHex(i.mid) + ", ";
ret += "value:" + toStringFromFloat(i.value);
if (i.status) {
ret += "\n";
}
else {
ret += ";status;unavailable\n";
}
}
return ret;
}
void Distributor::initGeekMode() {
remove(geek_mode_pipe.c_str());
geek_pipe_fd = mkfifo(geek_mode_pipe.c_str(), S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
if (geek_pipe_fd < 0) {
log.error("Can't create geek pipe: " + (string)strerror(errno));
}
}
void Distributor::sendToNamedPipe(IOTMessage msg) {
sendToNamedPipe(convertToCSV(msg, false));
}
void Distributor::sendToNamedPipe(std::string msg) {
if (geek_mode_enabled) {
geek_pipe_fd = open(geek_mode_pipe.c_str(), O_WRONLY | O_NONBLOCK);
int error_num = errno;
if (geek_pipe_fd < 0) {
if (error_num == ENXIO) { // nobody read
}
else if (error_num == ENOENT || error_num == EISDIR) {
log.error("Geek pipe re-initialization: " + (string)strerror(error_num));
initGeekMode(); // re-create - file doesn't exist OR Is a directory
}
else {
log.error("Can't open geek pipe file: " + (string)strerror(error_num));
}
} else {
write(geek_pipe_fd, msg.c_str(), msg.length());
}
close(geek_pipe_fd);
}
}
void Distributor::sendMessageToMQTT(IOTMessage msg) {
sendMessageToMQTT(convertToCSV(msg, true));
}
void Distributor::sendMessageToMQTT(std::string msg) {
if (mq)
mq->send_message(msg);
}
void Distributor::sendMessageToMQTT(std::string msg, std::string topic, int qos) {
log.information("Distributing message to topic \"" + topic + "\" with QoS " + std::to_string(qos));
if (mq) {
bool retval = mq->send_message(msg, topic, qos);
log.information("Sending message to MQTT " + std::string(retval ? "succeed!" : "failed!"));
}
}
bool Distributor::sendDataToMQTT(IOTMessage msg, int qos) {
log.information("Sendind data to Mosquitto.");
int ret = 0;
if (mq) {
ret += mq->send_message(convertToCSV(msg, true), MOSQ_TOPIC_DATA, qos);
ret += mq->send_message(convertToCSV(msg, false), MOSQ_TOPIC_DATA+"/csv", qos);
ret += mq->send_message(convertToXML(msg), MOSQ_TOPIC_DATA+"/xml", qos);
}
return (ret == 3);
}