-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGamaSenseIt.cpp
453 lines (378 loc) · 10.9 KB
/
GamaSenseIt.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include "GamaSenseIt.h"
using namespace std;
GamaSenseIT::GamaSenseIT(SX1272 &loraConnection)
{
string id = MQTT_TOPIC;
gatewayName = new char[id.length()+1];
strcpy(gatewayName,id.c_str());
this->loraConnector = loraConnection;
this->username= MQTT_USER_NAME;
this->password= MQTT_PASSWORD;
brokerAddress = ADDRESS;
useBroker = false;
saveInFile = false;
fileName = DEFAULT_FILE_NAME;
loraMode=LORAMODE;
DEFAULT_CHANNEL = CH_10_868;
}
void GamaSenseIT::setupLora()
{
int e;
// Print a start message
printf("loraConnector module and Raspberry Pi: receive packets with ACK\n");
// Power ON the module
e = loraConnector.ON();
delay(1000);
printf("Setting power ON: state %d\n", e);
// Set transmission mode
e = loraConnector.setMode(loraMode);
printf("Setting Mode: state %d\n", e);
// Set header
// e = loraConnector.setHeaderON();
// printf("Setting Header ON: state %d\n", e);
loraConnector._enableCarrierSense=true;
printf("Setting Header ON: state %d\n", e);
// Select frequency channel
e = loraConnector.setChannel(DEFAULT_CHANNEL);
printf("Setting Channel: state %d\n", e);
// Set CRC
//e = loraConnector.setCRC_ON();
//printf("Setting CRC ON: state %d\n", e);
#ifdef PABOOST
loraConnector._needPABOOST=true;
// previous way for setting output power
// powerLevel='x';
printf("with PA BOOST");
#else
printf("no PA BOOST");
// previous way for setting output power
// powerLevel='M';
#endif
e = loraConnector.setPowerDBM((uint8_t)MAX_DBM);
printf("Setting Power: state %d\n", e);
printf("select power in DBM : %d\n", MAX_DBM);
// Set the node address
e = loraConnector.setNodeAddress(1);
printf("Setting Node address: state %d\n", e);
// Print a success message
printf("loraConnector successfully configured\n\n");
}
bool GamaSenseIT::containPrefix(string s, string prefix)
{
for(int i=0; i<prefix.size() && i < s.size(); i++)
{
if(s[i] != prefix[i])
{
return false;
}
}
return true;
}
void GamaSenseIT::sendToSensor(string data,int receiverAddress)
{
int e;
string prefix = GAMA_SENS_IT_MESSAGE_HEADER;
string toSend = prefix + data;
char dtToSend[toSend.size()+1];//as 1 char space for null is also required
strcpy(dtToSend, toSend.c_str());
#ifdef WITH_ACK
int n_retry=NB_RETRIES;
do {
e = loraConnector.sendPacketTimeoutACKRetries(receiverAddress, dtToSend);
n_retry--;
if (n_retry == 0)
{
cout<<"Abort message to "<<receiverAddress<<endl;
cout<<"contents "<<dtToSend<<endl;
}
} while (e && n_retry);
#else
e = loraConnector.sendPacketTimeout(receiverAddress, dtToSend);
#endif
}
string GamaSenseIT::extractData(string message)
{
string prefix = GAMA_SENS_IT_MESSAGE_HEADER;
string result=message.substr(prefix.size());
return result;
}
void GamaSenseIT::waitAndReceiveMessage(string& message, int& source)
{
int e;
boolean cc = true;
int sender = 0;
string prefix = GAMA_SENS_IT_MESSAGE_HEADER;
string receivedMessage = "";
cout<<"waiting message."<<endl;
do
{
e = 0;
string tmpReceivedMessage = "";
#ifdef WITH_ACK
e = loraConnector.receivePacketTimeoutACK(10000);
#else
e = loraConnector.receivePacketTimeout(10000);
#endif
if ( e == 0 )
{
sender =loraConnector.packet_received.src;
for (unsigned int i = 0; i < loraConnector.packet_received.length; i++)
{
tmpReceivedMessage += (char)loraConnector.packet_received.data[i];
}
cout<<"pre contents : "<<tmpReceivedMessage<<endl;
if(containPrefix(tmpReceivedMessage,prefix))
{
receivedMessage = extractData(tmpReceivedMessage);
cc = false;
}
}
else {
// cout<<".";
delay(100);
}
}while(cc);
message =receivedMessage;
source = sender;
cout<<"receiving message from : "<<source<<endl;
cout<<"contents : "<<message<<endl;
//return 0;
}
unsigned long GamaSenseIT::getdate()
{
time_t timer;
time(&timer);
unsigned long tt = timer;
return tt;
}
void GamaSenseIT::sendDate(int receiverAddress)
{
unsigned long mdate =getdate();
stringstream ss;
ss << mdate;
string sdate = ss.str();
string data = GAMA_SENS_IT_MESSAGE_UPDATE_DATE_COMMAND;
data = data + sdate;
sendToSensor(data,receiverAddress);
}
int GamaSenseIT::messageCommand(string message)
{
int found = message.find(GAMA_SENS_IT_MESSAGE_CAPTURE_COMMAND);
if(found!=std::string::npos)
return CAPTURE_COMMAND;
found = message.find(GAMA_SENS_IT_MESSAGE_UPDATE_DATE_COMMAND);
if(found!=std::string::npos)
return DATE_UPDATE_COMMAND;
found = message.find(GAMA_SENS_IT_MESSAGE_REGISTER_COMMAND);
if(found!=std::string::npos)
return REGISTER_COMMAND;
return -1;
}
int GamaSenseIT::offsetMessageContent(string message)
{
switch(messageCommand(message))
{
case CAPTURE_COMMAND : { return strlen(GAMA_SENS_IT_MESSAGE_CAPTURE_COMMAND);}
case DATE_UPDATE_COMMAND : { return strlen(GAMA_SENS_IT_MESSAGE_UPDATE_DATE_COMMAND);}
case REGISTER_COMMAND : { return strlen(GAMA_SENS_IT_MESSAGE_REGISTER_COMMAND);}
}
return -1;
}
string GamaSenseIT::messageContents(string message)
{
string tailString = message.substr(offsetMessageContent(message));
return tailString;
}
int GamaSenseIT::sendToBrocker(string message, string sender, string mid, unsigned long sensorDate)
{
string dte = to_string(sensorDate);
string data ="";
data.append(dte);
data.append(";");
data.append(sender);
data.append(";");
data.append(mid);
data.append(";");
data.append(message);
char msg[data.size() ];
strcpy(msg, data.c_str());
int rc;
pubmsg.payload = msg;
pubmsg.payloadlen = strlen(msg);
pubmsg.qos = QOS;
pubmsg.retained = 0;
MQTTClient_publishMessage(client, gatewayName, &pubmsg, &token);
printf("NOT Waiting for up to %d seconds for publication of %s\n"
"on topic %s for client with ClientID: %s\n",
(int)(TIMEOUT/1000), msg, gatewayName, gatewayName);
//rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Message with delivery token %d delivered\n", token);
return rc;
}
int GamaSenseIT::computeCaptureCommand(string message, int senderAddress)
{
string datePrefix = GAMA_SENS_IT_SENDER_NAME;
string MIDPrefix = GAMA_SENS_IT_MESSAGE_ID;
string valuePrefix = GAMA_SENS_IT_MESSAGE_VALUE;
int dateFound = message.find(GAMA_SENS_IT_SENDER_NAME);
if(dateFound==std::string::npos)
return -1;
cout <<" pos name "<<dateFound<<endl;
int midFound = message.find(GAMA_SENS_IT_MESSAGE_ID);
if(midFound==std::string::npos)
return -1;
int dataFound = message.find(GAMA_SENS_IT_MESSAGE_VALUE);
if(dataFound==std::string::npos)
return -1;
int dateIndex =dateFound + datePrefix.size();
int dateSize =midFound - dateIndex ;
string sensorName = message.substr(dateIndex,dateSize);
int midIndex =midFound + MIDPrefix.size();
int midSize =dataFound - midIndex ;
string mid = message.substr(midIndex,midSize);
int dataIndex =dataFound + valuePrefix.size();
string data = message.substr(dataIndex);
unsigned long sensorDate = time(NULL);
if(saveInFile == true)
{
(*outFile) << sensorDate << ";"<<sensorName<<";"<<mid<<";"<<data<<endl;
outFile->flush();
}
int sending = 0;
if(useBroker)
{
setupMQTT();
sendToBrocker(data, sensorName, mid, sensorDate);
closeMQTT();
}
return 1;
}
void GamaSenseIT::computeMessage(string message, int senderAddress)
{
int command = messageCommand(message);
switch(command)
{
case CAPTURE_COMMAND : {
computeCaptureCommand(message,senderAddress);
break;
}
}
}
void GamaSenseIT::setupMQTT()
{
/*char tcpAddress[pro.length()+port.length()+1+address.length()];
strcpy(tcpAddress,pro.c_str());
strcat(tcpAddress,address.c_str());
strcat(tcpAddress,port.c_str());
*/
string address = this->brokerAddress;
string clientID = this->username;
char tcpAddress[1+address.length()];
strcpy(tcpAddress,address.c_str());
//strcat(tcpAddress,address.c_str());
//strcat(tcpAddress,port.c_str());
int rc;
char id_c[clientID.length()+1];
strcpy(id_c,clientID.c_str());
MQTTClient_create(&client, tcpAddress, id_c,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 4000;
conn_opts.cleansession = 1;
conn_opts.username =(this->username).c_str();
conn_opts.password =(this->password).c_str();
cout<<"username "<<(this->username).c_str()<<endl;
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(-1);
}
}
void GamaSenseIT::closeMQTT()
{
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
}
void GamaSenseIT::setupOutFile(string outf)
{
outFile = new ofstream();
outFile->open(outf.c_str());
}
void GamaSenseIT::analyseParameter(string cmd, string value)
{
if (cmd.compare("-broker") == 0)
{
useBroker = true;
if (value.compare("autoconf") == 0)
{
cout<<"****************************************************"<<endl;
cout<<"* using default broker configuration *"<<endl;
cout<<"* broker: vmpams.ird.fr:1935/gamaSenseIt *"<<endl;
cout<<"* login: gamasenseit *"<<endl;
cout<<"* password: gamasenseit *"<<endl;
cout<<"* topic: gamasenseit *"<<endl;
cout<<"****************************************************"<<endl;
}
else
{
brokerAddress = value;
}
}
if (cmd.compare("-username") == 0)
{
this->username =value;
cout<<"username :"<<this->username<<endl;
}
if (cmd.compare("-password") == 0)
{
this->password =value;
cout<<"password :"<<this->password<<endl;
}
if (cmd.compare("-topicname") == 0)
{
gatewayName = new char[value.length()+1];
strcpy(gatewayName, value.c_str());
cout<<"topicname :"<<this->gatewayName<<endl;
}
if (cmd.compare("-file") == 0)
{
saveInFile = true;
fileName = value;
}
}
void GamaSenseIT::setup()
{
setupLora();
if(useBroker == true)
{
setupMQTT();
closeMQTT();
}
if(saveInFile == true)
{
setupOutFile(fileName);
}
}
void GamaSenseIT::loop()
{
while(true)
{
string msg = "";
int source = -1;
waitAndReceiveMessage(msg,source);
computeMessage(msg,source);
}
}
int main(int argc, char *argv[])
{
GamaSenseIT* gamaSenseIt;
gamaSenseIt = new GamaSenseIT(sx1272);
for(int i = 1; i+1<argc; i=i+2 )
{
string cmd(argv[i]);
string val(argv[i+1]);
gamaSenseIt->analyseParameter(cmd,val);
}
gamaSenseIt->setup();
gamaSenseIt->loop();
return 0;
}