-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
259 lines (217 loc) · 6.93 KB
/
main.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
/**
*
*
* @author Laurent Winkler based on work by Valentin Bourqui
* @date Dec 2014
* @brief Main executable of the POPWIN project: The user can select interactively which commands to send to the gateway sensor
*
*
*/
#include <unistd.h>
#include <termios.h>
#include <map>
#include <fstream>
#include "POPSensor.ph"
#include <time.h>
#define LOGNAME_FORMAT "%Y%m%d_%H:%M:%S"
#define LOGNAME_SIZE 18
using namespace std;
// -------------------------------------------------------------------------------- //
// COMMANDS
// -------------------------------------------------------------------------------- //
typedef void (*Command)(POPSensor& xr_popSensor);
/// Blink all leds 3 times
void blinkLeds(POPSensor& xr_popSensor)
{
static const int usecs = 500 * 1000;
for(int led = 0 ; led < 3 ; led++)
{
for(int i = 0 ; i < 2*3 ; i++)
{
xr_popSensor.Broadcast(PUB_LED, led);
usleep(usecs);
}
}
// Toggle all leds
// xr_popSensor.Broadcast(PUB_LED, 3);
}
/// Ask temperature and acceleration readings
void askSensorReadings(POPSensor& xr_popSensor)
{
// Note: for the moment, only temperature is available
static const int usecs = 100 * 1000;
for(int i = 0 ; i < 10 ; i++)
{
xr_popSensor.Broadcast(PUB_COMMAND, 1);
usleep(usecs);
}
}
/// Send a custom command
void customCommand(POPSensor& xr_popSensor)
{
// Note: for the moment, only temperature is available
static const int usecs = 100 * 1000;
cout << "Input command number (0 to get the list of commands):" << popcendl;
int num = getchar() - '0';
while(num == '\n' - '0')
num = getchar() - '0';
// cout << "send command " << num << popcendl;
xr_popSensor.Broadcast(PUB_COMMAND, num);
}
/// Ask the remote to generate test data
void generateTestData(POPSensor& xr_popSensor)
{
for(int i = 0 ; i < 5 ; i++)
{
// Each command generates 10 sanples of data (types: double, int, string)
xr_popSensor.Broadcast(PUB_COMMAND, 2);
usleep(0.3 * 1000000);
xr_popSensor.Broadcast(PUB_COMMAND, 3);
usleep(0.3 * 1000000);
xr_popSensor.Broadcast(PUB_COMMAND, 4);
// Wait a bit to avoid overloading the mote
usleep(0.3 * 1000000);
}
}
/// Test the different communication messages
void testCommunication(POPSensor& xr_popSensor)
{
cout<<"Send a test notification to sensors"<<popcendl;
xr_popSensor.Notify(MSR_LOG, UNT_NONE, "test_notification");
usleep(0.3 * 1000000);
cout<<"Send a test subsciption to sensors"<<popcendl;
xr_popSensor.Subscribe(MSR_VIBRATION, TYPE_DOUBLE);
usleep(0.3 * 1000000);
cout<<"Send a test publication to sensors (should send the command list)"<<popcendl;
xr_popSensor.Broadcast(PUB_COMMAND, 0);
}
/// Ask the remote to generate test data
void clearData(POPSensor& xr_popSensor)
{
// Each command generates 10 sanples of data (types: double, int, string)
xr_popSensor.Clear();
}
/// Ask the remote to generate test data
void printData(POPSensor& xr_popSensor)
{
POPSensorData data(xr_popSensor.Gather());
data.Print();
}
/// Ask the remote to generate test data
void printToFile(POPSensor& xr_popSensor)
{
POPSensorData data(xr_popSensor.Gather());
ofstream of;
char name[LOGNAME_SIZE];
time_t now = time(0);
strftime(name, sizeof(name), LOGNAME_FORMAT, localtime(&now));
of.open("out/data_" + std::string(name) + ".csv");
of << "Time, Id, Measurement, Unit, data\n";
data.PrintToFile(of);
of.close();
}
/// Display stats (used in reduce)
template<typename T> void displayStats(const std::string& x_label, enum MeasurementType x_mtype, const POPSensorData& x_data)
{
cout << x_label
<< "\tnb:" << x_data.Reduce<T>(x_mtype, POPSensorData::POPReduceF::size)
<< "\tmin:" << x_data.Reduce<T>(x_mtype, POPSensorData::POPReduceF::min)
<< "\tmax:" << x_data.Reduce<T>(x_mtype, POPSensorData::POPReduceF::max)
<< "\tsum:" << x_data.Reduce<T>(x_mtype, POPSensorData::POPReduceF::sum)
<< "\tavg:" << x_data.Reduce<T>(x_mtype, POPSensorData::POPReduceF::aver)
<< "\tstdev:" << x_data.Reduce<T>(x_mtype, POPSensorData::POPReduceF::stdev)
<< popcendl;
}
/// Apply reduce to data
void reduce(POPSensor& xr_popSensor)
{
POPSensorData data(xr_popSensor.Gather());
displayStats<double>("Temperature (double)", MSR_TEMPERATURE, data);
displayStats<double>("Humidity (double) ", MSR_HUMIDITY, data);
displayStats<double>("Light (double) ", MSR_LIGHT, data);
displayStats<double>("Infrared (double) ", MSR_INFRARED, data);
displayStats<double>("Test data (double) ", MSR_TEST, data);
displayStats<int> ("Test data (int) ", MSR_TEST, data);
}
int main(int argc, char** argv)
{
if(argc != 2)
{
cout << "usage: popcrun <obj.map> ./main <resource.json>" << popcendl;
exit(0);
}
// Input a list of commands
map<char, Command> commands;
cout << popcendl;
cout << "=======================================================================================" <<popcendl;
cout << " POPWin demo: type a command to interact with your sensors " <<popcendl;
cout << "=======================================================================================" <<popcendl;
cout << " q: Quit" <<popcendl;
cout << " a: Ask the sensor to read temperature" <<popcendl;
commands['a'] = askSensorReadings;
cout << " b: Make leds blink on sensor" <<popcendl;
commands['b'] = blinkLeds;
cout << " c + <nb>: Send a custom command to sensor. 'c0' should list the available commands." <<popcendl;
commands['c'] = customCommand;
cout << " g: Generate test data" <<popcendl;
commands['g'] = generateTestData;
cout << " e: Clear stored data" <<popcendl;
commands['e'] = clearData;
cout << " p: Print stored data" <<popcendl;
commands['p'] = printData;
cout << " P: Print to file" <<popcendl;
commands['P'] = printToFile;
cout << " t: Test communication" <<popcendl;
commands['t'] = testCommunication;
cout << " r: Apply reduce to data" <<popcendl;
commands['r'] = reduce;
cout << "---------------------------------------------------------------------------------------" <<popcendl;
cout << popcendl;
try
{
POPSensor popSensor("localhost", argv[1],0);
//cout<<"Ask to send the list of commands"<<popcendl;
//popSensor.Broadcast(PUB_COMMAND, 0);
char c = '\n';
while(true)
{
cout << "Enter selection:" << popcendl;
while(c == '\n')
c = getchar();
if(c == 'q')
break;
try
{
auto cmd = commands.find(c);
if(cmd != commands.end())
{
// here we call the command associated with the letter
(*cmd->second)(popSensor);
c = '\n';
}
else
{
cout << "No command for " << c << popcendl;
c = '\n';
}
}
catch(POPException &e)
{
cout<<"Cannot execute command. Received exception: " << e.what() << popcendl;
c = '\n';
}
}
cout<<"End of popwin main"<<popcendl;
}
catch(std::exception &e)
{
cerr<<"Exception caught in popwin main loop: " << e.what() << popcendl;
return 1;
}
catch(...)
{
cerr<<"Exception caught in popwin main loop" << popcendl;
return 1;
}
return 0;
}