-
Notifications
You must be signed in to change notification settings - Fork 0
/
SensorProxy.cc
552 lines (482 loc) · 15.7 KB
/
SensorProxy.cc
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/**
*
*
* @author Laurent Winkler based on work by Valentin Bourqui
* @date Dec 2014
* @brief Proxy class that handles communication with one sensor but resides on the same machine as the gateway
*
*
*/
#include "SensorProxy.ph"
#include "POPSensor.ph"
#include <unistd.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <regex.h>
// Note: check baudrate of USB port with : stty -F /dev/ttyUSB0
#define BAUDRATE B115200
//#define EN_COUTS 1 // enable cout to std out, comment to disable
using namespace std;
/// Constructor
/// @param x_id Id to set to this object (for low-level communication)
/// @param x_url URL on which this parallel object is allocated
/// @param x_device Device name e.g. /dev/ttyUSB0
SensorProxy::SensorProxy(int x_id, const std::string& x_url, const string& x_device)
{
m_id = x_id;
fd_set mask;
speed_t speed = BAUDRATE;
m_fd = open(x_device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY | O_SYNC );
if (m_fd <0) {
throw POPException("Cannot open device", x_device);
}
if (fcntl(m_fd, F_SETFL, 0) < 0) {
throw POPException("Could not set fcntl");
}
struct termios options;
if (tcgetattr(m_fd, &options) < 0) {
throw POPException("Could not get options");
}
cfsetispeed(&options, speed);
cfsetospeed(&options, speed);
/* Enable the receiver and set local mode */
options.c_cflag |= (CLOCAL | CREAD);
/* Mask the character size bits and turn off (odd) parity */
options.c_cflag &= ~(CSIZE|PARENB|PARODD);
/* Select 8 data bits */
options.c_cflag |= CS8;
/* Raw input */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
/* Raw output */
options.c_oflag &= ~OPOST;
if (tcsetattr(m_fd, TCSANOW, &options) < 0) {
throw POPException("could not set options");
}
FD_ZERO(&mask);
FD_SET(m_fd, &mask);
FD_SET(fileno(stdin), &mask);
if(m_fd <= 0)
{
throw POPException("Fail to open connection to sensor", x_device);
}
// m_debugOf.open("debug.txt");
}
/// Destructur
SensorProxy::~SensorProxy()
{
// Destroy object
cout<<"Destroy sensor proxy" << popcendl;
close(m_fd);
// m_debugOf.close();
}
/// Send raw data to the gateway
void SensorProxy::SendRawData(const std::string& x_data)
{
// m_debugOf << x_data; m_debugOf.flush();
string datan = x_data + "\n";
const char* data = datan.c_str();
// printf("Sending %s on %d\n", data, m_fd);
int n = strlen(data);
if (n <= 0) {
throw POPException("could not read");
}
else if (n > 0)
{
for (int i = 0; i < n; i++)
{
if (write(m_fd, &data[i], 1) <= 0)
{
throw POPException("Failed to write data while sending to sensor");
}
else
{
// printf("write successfull\n");
fflush(NULL);
// usleep(6000);
}
}
}
}
/// Read data coming from the gateway
void SensorProxy::ReadData(std::ostream& xr_ostream)
{
while(m_listening.load()) {
char buf[BUFFERSIZE+1];
int n = read(m_fd, buf, sizeof(buf));
// printf("n=%d\n", n);
/*
for(int i = 0; i < n; i++) {
printf("%c", buf[i]);
}
*/
if(n==0)
{
// read(...) is not blocking. Add a sleep to reduce the charge when inactive
usleep(10000); // sleep 10 ms
continue;
}
// note: buffer should be null terminated to be concatenated to string
buf[n] = '\0';
// printf("%s\n", buf);
xr_ostream << buf;
// Carriage return means end of message
if(buf[n-1] == '\n')
break;
}
}
/// Start listening to messages coming from sensors
void SensorProxy::StartListening()
{
m_listening.store(true);
while(m_listening.load())
{
stringstream received;
ReadData(received);
if(received.str().size() != 0) // && received.str() != "(null)")
{
// We need to split the message with the character \n to avoid the case where
// several messages arrive in the same call
std::string msg;
while(std::getline(received,msg,'\n')){
//cout << "handling incoming message: " << msg << popcendl;
HandleIncomingMessage(msg);
}
}
else
{
printf("Received an empty message\n");
}
}
}
/// Stop listening to messages coming from sensors
void SensorProxy::StopListening()
{
m_listening.store(false);
}
/// Return a POPSensorData structure containing the messages received from sensors
POPSensorData SensorProxy::Gather()
{
// cout << "Retrieve " << m_doubleData.size() << " records of type double" <<popcendl;
return m_sensorData;
}
/// Clear the stored messages
void SensorProxy::Clear()
{
m_sensorData.Clear();
// m_subscriptions.clear();
}
/// Handle all incoming messages
void SensorProxy::HandleIncomingMessage(const std::string& x_rawMsg)
{
// cout << "received :" << x_rawMsg << popcendl;
struct timeval time_now;
gettimeofday(&time_now, NULL);
long ms = time_now.tv_sec * 1000 + time_now.tv_usec / 1000;
// cout << "ms" << ms << popcendl;
try
{
switch(getMessageType(x_rawMsg.c_str()))
{
case MSG_PUBLISH:
{
/*struct PublishMessage msg;
memset(&msg, 0, sizeof(msg));
if(unbufferizePublishMessage(&msg, x_rawMsg.c_str(),x_rawMsg.size()) <= 0)
throw POPException("Cannot unbufferize publish message");*/
throw POPException("Publication messages are not handled yet");
}
break;
case MSG_SUBSCRIBE:
{
/*struct SubscribeMessage msg;
memset(&msg, 0, sizeof(msg));
if(unbufferizeSubscribeMessage(&msg, x_rawMsg.c_str()) <= 0)
throw POPException("Cannot unbufferize subscribe message");*/
throw POPException("Subscription messages are not handled yet");
}
break;
case MSG_NOTIFY:
{
struct NotifyMessage msg;
memset(&msg,0,sizeof(msg));
if(unbufferizeNotifyMessage(&msg, x_rawMsg.c_str(), x_rawMsg.size()) <= 0)
throw POPException("Cannot unbufferize notify message");
auto it = m_subscriptions.find(msg.measurementType);
bool subscribed = it != m_subscriptions.end() && it->second;
if(msg.measurementType == MSR_LOG)
{
#ifdef EN_COUTS
cout << "Proxy received log from " << msg.id << " : " << msg.data << popcendl;
#endif
break;
}
else if(!subscribed)
{
// Not subscribed to this type of data
#ifdef EN_COUTS
cout<< "Proxy "<< m_id << ", unstored notification (" << explainMeasurementType(msg.measurementType) << "): '" << msg.data << "'" << popcendl;
#endif
break;
}
#ifdef EN_COUTS
cout<< "Proxy "<< m_id << ", stored notification (" << explainMeasurementType(msg.measurementType) << "): '" << msg.data << "'" << popcendl;
#endif
switch(msg.dataType)
{
case TYPE_DOUBLE:
{
std::pair<RecordHeader, double> pair(RecordHeader(ms, msg), atof(msg.data));
m_sensorData.Insert(pair);
break;
}
case TYPE_INT:
{
std::pair<RecordHeader, int> pair(RecordHeader(ms, msg), atoi(msg.data));
m_sensorData.Insert(pair);
break;
}
case TYPE_STRING:
{
std::pair<RecordHeader, std::string> pair(RecordHeader(ms, msg), std::string(msg.data));
m_sensorData.Insert(pair);
break;
}
default:
printf("Unknown data type %d in %s\n", msg.dataType, x_rawMsg.c_str());
}
}
break;
#ifdef EN_COUTS
default:
// Unknown message: print
cout << "Proxy received raw message: '" << x_rawMsg << "'" << popcendl;
#endif
}
}
catch(std::exception &e)
{
cout << "Error at reception of message '" << x_rawMsg << "': " << e.what() << popcendl;
}
}
/// Send a notification to the gateway
void SensorProxy::Notify(int x_measurementType, int x_measurementUnit, int x_data)
{
enum MeasurementType msgType = static_cast<MeasurementType>(x_measurementType);
// Check publication rights
auto it = m_publications.find(msgType);
bool canPublish = (it != m_publications.end() && it->second);
if(!canPublish)
{
// Not subscribed to this type of data
cout<< "Cannot notify " << explainMeasurementType(msgType) << " OUT" << popcendl;
return;
}
// note: only string messages are implemented. Other types can be trivially implemented
struct NotifyMessage msg;
memset(&msg, 0, sizeof(struct NotifyMessage));
snprintf(msg.data, sizeof(msg.data), "%d", x_data);
msg.measurementType = static_cast<MeasurementType>(x_measurementType);
msg.dataType = TYPE_INT;
msg.unit = static_cast<MeasurementUnit>(x_measurementUnit);
msg.id = m_id;
msg.dataSize = strlen(msg.data);
char buffer[BUFFERSIZE];
if(bufferizeNotifyMessage(&msg, buffer, BUFFERSIZE) <= 0)
throw POPException("Cannot bufferize notify message", buffer);
// cout<< "Sending " << buf << popcendl;
SendRawData(buffer);
}
/// Send a notification to the gateway
void SensorProxy::Notify(int x_measurementType, int x_measurementUnit, double x_data)
{
enum MeasurementType msgType = static_cast<MeasurementType>(x_measurementType);
// Check publication rights
auto it = m_publications.find(msgType);
bool canPublish = (it != m_publications.end() && it->second);
if(!canPublish)
{
// Not subscribed to this type of data
cout<< "Cannot notify " << explainMeasurementType(msgType) << " OUT" << popcendl;
return;
}
// note: only string messages are implemented. Other types can be trivially implemented
struct NotifyMessage msg;
memset(&msg, 0, sizeof(struct NotifyMessage));
snprintf(msg.data, sizeof(msg.data), "%lf", x_data);
msg.measurementType = static_cast<MeasurementType>(x_measurementType);
msg.dataType = TYPE_DOUBLE;
msg.unit = static_cast<MeasurementUnit>(x_measurementUnit);
msg.id = m_id;
msg.dataSize = strlen(msg.data);
char buffer[BUFFERSIZE];
if(bufferizeNotifyMessage(&msg, buffer, BUFFERSIZE) <= 0)
throw POPException("Cannot bufferize notify message", buffer);
// cout<< "Sending " << buf << popcendl;
SendRawData(buffer);
}
/// Send a notification to the gateway
void SensorProxy::Notify(int x_measurementType, int x_measurementUnit, const std::string& x_message)
{
enum MeasurementType msgType = static_cast<MeasurementType>(x_measurementType);
// Check publication rights
auto it = m_publications.find(msgType);
bool canPublish = (it != m_publications.end() && it->second);
if(!canPublish)
{
// Not subscribed to this type of data
cout<< "Cannot notify " << explainMeasurementType(msgType) << " OUT" << popcendl;
return;
}
struct NotifyMessage msg;
memset(&msg, 0, sizeof(struct NotifyMessage));
snprintf(msg.data, sizeof(msg.data), "%s\r\n", x_message.c_str());
msg.measurementType = static_cast<MeasurementType>(x_measurementType);
msg.dataType = TYPE_STRING;
msg.unit = static_cast<MeasurementUnit>(x_measurementUnit);
msg.id = m_id;
msg.dataSize = strlen(msg.data);
char buffer[BUFFERSIZE];
if(bufferizeNotifyMessage(&msg, buffer, BUFFERSIZE) <= 0)
throw POPException("Cannot bufferize notify message", buffer);
// cout<< "Sending " << buf << popcendl;
SendRawData(buffer);
}
/// Send a publication to the gateway
void SensorProxy::Publish(int x_measurementType)
{
m_publications[x_measurementType] = true;
struct PublishMessage msg;
memset(&msg, 0, sizeof(struct PublishMessage));
//sprintf(msg.data, "%d", x_data);
msg.publicationType = static_cast<MeasurementType>(x_measurementType);
//msg.dataType = TYPE_INT;
msg.id = m_id;
//msg.dataSize = strlen(msg.data);
char buffer[BUFFERSIZE];
memset(&buffer, 0, BUFFERSIZE*sizeof(char));
if(bufferizePublishMessage(&msg, buffer, BUFFERSIZE) <= 0)
throw POPException("Cannot bufferize publish message", buffer);
//cout << "Bufferized publish to buffer size: " << strlen(buffer) << " | " << buffer << popcendl;
// cout<< "Sending " << buf << popcendl;
SendRawData(buffer);
}
/// Send a publication to the gateway
/*void SensorProxy::Publish(int x_publicationType, double x_data)
{
struct PublishMessage msg;
memset(&msg, 0, sizeof(struct PublishMessage));
//sprintf(msg.data, "%lf", x_data);
msg.publicationType = static_cast<PublicationType>(x_publicationType);
//msg.dataType = TYPE_DOUBLE;
msg.id = m_id;
//msg.dataSize = strlen(msg.data);
char buffer[BUFFERSIZE];
if(bufferizePublishMessage(&msg, buffer, BUFFERSIZE) <= 0)
throw POPException("Cannot bufferize publish message", buffer);
// cout<< "Sending " << buf << popcendl;
SendRawData(buffer);
}*/
/// Send a publication to the gateway
/*void SensorProxy::Publish(int x_publicationType, const string& x_data)
{
struct PublishMessage msg;
memset(&msg, 0, sizeof(struct PublishMessage));
//sprintf(msg.data, "%s", x_data.c_str());
msg.publicationType = static_cast<PublicationType>(x_publicationType);
//msg.dataType = TYPE_STRING;
// msg.unit = UNT_UNKNOWN;
msg.id = m_id;
//msg.dataSize = strlen(msg.data);
char buffer[BUFFERSIZE];
if(bufferizePublishMessage(&msg, buffer, BUFFERSIZE) <= 0)
throw POPException("Cannot bufferize publish message", buffer);
// cout<< "Sending " << buf << popcendl;
SendRawData(buffer);
}*/
void SensorProxy::UnPublish(int x_measurementType)
{
m_publications[x_measurementType] = false;
struct UnPublishMessage msg;
memset(&msg, 0, sizeof(struct UnPublishMessage));
//sprintf(msg.data, "%d", x_data);
msg.publicationType = static_cast<MeasurementType>(x_measurementType);
//msg.dataType = TYPE_INT;
msg.id = m_id;
//msg.dataSize = strlen(msg.data);
char buffer[BUFFERSIZE];
memset(&buffer, 0, BUFFERSIZE*sizeof(char));
if(bufferizeUnPublishMessage(&msg, buffer, BUFFERSIZE) <= 0)
throw POPException("Cannot bufferize unpublish message", buffer);
//cout << "Bufferized publish to buffer size: " << strlen(buffer) << " | " << buffer << popcendl;
// cout<< "Sending " << buf << popcendl;
SendRawData(buffer);
}
/// Send a subscription to the gateway
void SensorProxy::Subscribe(int x_measurementType, int x_dataType)
{
char buf[BUFFERSIZE];
memset(buf,0,BUFFERSIZE*sizeof(char));
// cout << "subscribe to "<< x_measurementType << endl;
m_subscriptions[x_measurementType] = true;
struct SubscribeMessage msg;
memset(&msg, 0, sizeof(struct SubscribeMessage));
msg.measurementType = static_cast<MeasurementType>(x_measurementType);
msg.dataType = static_cast<DataType>(x_dataType);
msg.id = m_id;
// Bufferize message and send to gateway
if(bufferizeSubscribeMessage(&msg, buf, BUFFERSIZE) <= 0)
throw POPException("Cannot bufferize subscribe message");
// cout<< "Sending " << buf << popcendl;
SendRawData(buf);
}
/// Send a subscription to the gateway
void SensorProxy::UnSubscribe(int x_measurementType, int x_dataType)
{
char buf[BUFFERSIZE];
memset(buf,0,BUFFERSIZE*sizeof(char));
// cout << "subscribe to "<< x_measurementType << endl;
m_subscriptions[x_measurementType] = false;
struct UnSubscribeMessage msg;
memset(&msg, 0, sizeof(struct UnSubscribeMessage));
msg.measurementType = static_cast<MeasurementType>(x_measurementType);
msg.dataType = static_cast<DataType>(x_dataType);
msg.id = m_id;
// Bufferize message and send to gateway
if(bufferizeUnSubscribeMessage(&msg, buf, BUFFERSIZE) <= 0)
throw POPException("Cannot bufferize unsubscribe message");
// cout<< "Sending " << buf << popcendl;
SendRawData(buf);
}
/// Return the size of the stored data
int SensorProxy::GetDataSize()
{
return m_sensorData.GetSize();
}
/// Apply a reduce operation to the stored data {size, min, max, aver, sum, stdev}
double SensorProxy::Reduce(int x_mtype, int x_dataType, int x_fct)
{
switch(x_dataType)
{
case TYPE_INT:
return m_sensorData.Reduce<int>(static_cast<enum MeasurementType>(x_mtype), static_cast<POPSensorData::POPReduceF>(x_fct));
case TYPE_DOUBLE:
return m_sensorData.Reduce<int>(static_cast<enum MeasurementType>(x_mtype), static_cast<POPSensorData::POPReduceF>(x_fct));
default:
throw POPException("No reduce operation for type " + string(explainDataType(static_cast<enum DataType>(x_dataType))));
}
}
/// Send a notification to set the gwID GW as a GW
void SensorProxy::SetAsGateway(int gwID)
{
Notify(MSR_SET_GW, UNT_NONE, gwID);
}
@pack(SensorProxy);