-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOPSensorData.cc
345 lines (279 loc) · 8.92 KB
/
POPSensorData.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
/**
*
*
* @author Laurent Winkler based on work by Valentin Bourqui
* @date Dec 2014
* @brief POPSensorData class for the POPWIN project. This object represents the data gathered from sensor.
*
*
*/
#include "POPSensorData.h"
#include "fstream"
using namespace std;
/// Return a reference to the data
template<> map<RecordHeader, int>& POPSensorData::RefData() {return dataInt;}
/// Return a reference to the data
template<> map<RecordHeader, double>& POPSensorData::RefData() {return dataDouble;}
/// Return a reference to the data
template<> map<RecordHeader, string>& POPSensorData::RefData() {return dataString;}
/// Return the data
template<> const map<RecordHeader, int>& POPSensorData::GetData() const {return dataInt;}
/// Return the data
template<> const map<RecordHeader, double>& POPSensorData::GetData() const {return dataDouble;}
/// Return the data
template<> const map<RecordHeader, string>& POPSensorData::GetData() const {return dataString;}
RecordHeader::RecordHeader()
{
measurementType = MSR_LOG;
id = 0;
unit = UNT_NONE;
timeStamp = 0;
}
RecordHeader::RecordHeader(unsigned int x_timeStamp, const NotifyMessage& x_msg)
{
// cout << "Create record header id:" << x_msg.id << " units " << x_msg.unit << popcendl;
measurementType = x_msg.measurementType;
id = x_msg.id;
unit = x_msg.unit;
timeStamp = x_timeStamp;
}
void RecordHeader::Serialize(POPBuffer &buf, bool pack)
{
if(pack)
{
int mt = static_cast<int>(measurementType);
buf.Pack(&mt,1);
buf.Pack(&id,1);
int mu = static_cast<int>(unit);
buf.Pack(&mu,1);
int ts = (int)timeStamp;
buf.Pack(&timeStamp,1);
}
else
{
int mt = -1;
buf.UnPack(&mt,1);
measurementType = static_cast<enum MeasurementType>(mt);
buf.UnPack(&id,1);
int mu = -1;
buf.UnPack(&mu,1);
unit = static_cast<enum MeasurementUnit>(mu);
buf.UnPack(&timeStamp,1);
}
}
// Define stream operator for easy printing
ostream& operator<< (ostream& x_stream, const RecordHeader& x_rec)
{
// x_stream << "time: " << x_rec.timeStamp << " id:" << x_rec.id << " type:" << explainMeasurementType(x_rec.measurementType) << " unit:" << explainMeasurementUnit(x_rec.unit);
x_stream << x_rec.timeStamp << ", " << x_rec.id << ", " << explainMeasurementType(x_rec.measurementType) << ", " << explainMeasurementUnit(x_rec.unit);
return x_stream;
}
// ================================================================================================================================================================
template<class T> void serialize(map<RecordHeader, T>& records, POPBuffer &buf, bool pack)
{
if(pack)
{
int size = records.size();
buf.Pack(&size, 1);
for(auto& elem : records)
{
RecordHeader head = elem.first;
head.Serialize(buf, true);
buf.Pack(&((T &)(elem.second)), 1);
}
}
else
{
int size = 0;
buf.UnPack(&size,1);
records.clear();
for(int i=0 ; i < size ; i++)
{
RecordHeader key;
T value{};
key.Serialize(buf, false);
buf.UnPack(&value,1);
records[key] = value;
}
}
}
void POPSensorData::Serialize(POPBuffer &buf, bool pack)
{
serialize(dataDouble, buf, pack);
serialize(dataInt, buf, pack);
serialize(dataString, buf, pack);
// cout << "serialize "<<pack << " " << dataDouble.size() << popcendl;
}
void POPSensorData::Print() const
{
for(auto elem : dataDouble)
{
cout << elem.first << " -- " << elem.second << popcendl;
}
// Print int data
for(auto elem : dataInt)
{
cout << elem.first << " -- " << elem.second << popcendl;
}
// Print string data
for(auto elem : dataString)
{
cout << elem.first << " -- " << elem.second << popcendl;
}
}
void POPSensorData::PrintToFile(ostream& xr_ostream) const
{
for(auto elem : dataDouble)
{
xr_ostream << elem.first << ", " << elem.second << endl;
}
// Print int data
for(auto elem : dataInt)
{
xr_ostream << elem.first << ", " << elem.second << endl;
}
// Print string data
for(auto elem : dataString)
{
xr_ostream << elem.first << ", " << elem.second << endl;
}
}
void POPSensorData::ReadFromFile(istream& xr_istream)
{
string line;
dataInt.clear(); // Note: limitation: for now, only double is read since we cannot really differenciate int and double
dataDouble.clear();
dataString.clear();
// Read line containing titles
assert(getline(xr_istream,line));
// Read each line
while(getline(xr_istream,line))
{
unsigned int timeStamp;
RecordHeader rec;
stringstream lineStream(line);
string cell;
getline(lineStream,cell,',');
rec.timeStamp = atof(cell.c_str());
getline(lineStream,cell,',');
rec.id = atoi(cell.c_str());
getline(lineStream,cell,',');
rec.measurementType = translateMeasurementType(cell.c_str() + 1);
getline(lineStream,cell,',');
rec.unit = translateMeasurementUnit(cell.c_str() + 1);
getline(lineStream,cell,',');
dataDouble[rec] = atof(cell.c_str());
}
}
/// Return all sensor ids in database for the given measurement
set<int> POPSensorData::GroupAllIds(enum MeasurementType x_measurementType) const
{
set<int> res;
for(const auto& elem : dataDouble)
if(x_measurementType == MSR_LOG || elem.first.measurementType == x_measurementType)
res.insert(elem.first.id);
for(const auto& elem : dataInt)
if(x_measurementType == MSR_LOG || elem.first.measurementType == x_measurementType)
res.insert(elem.first.id);
for(const auto& elem : dataString)
if(x_measurementType == MSR_LOG || elem.first.measurementType == x_measurementType)
res.insert(elem.first.id);
// TODO: This is a workaround that suppresses one sensor with wrong measurement
res.erase(158);
return res;
}
/// Return all measurements in database for the sensor
set<enum MeasurementType> POPSensorData::GroupAllMeasurementTypes(int x_id) const
{
set<enum MeasurementType> res;
for(const auto& elem : dataDouble)
if(x_id == 0 || elem.first.id == x_id)
res.insert(elem.first.measurementType);
for(const auto& elem : dataInt)
if(x_id == 0 || elem.first.id == x_id)
res.insert(elem.first.measurementType);
for(const auto& elem : dataString)
if(x_id == 0 || elem.first.id == x_id)
res.insert(elem.first.measurementType);
return res;
}
void POPSensorData::PrintToPlot(const string& x_fileName, enum MeasurementType x_measurementType) const
{
// copy html header to target
stringstream ss1;
ss1 << "cat plots/index.head.html > " << x_fileName;
system(ss1.str().c_str());
ofstream of(x_fileName.c_str(), fstream::app);
unsigned long initialTime = 0;
for(auto id : GroupAllIds())
{
of << "'sensor" << id << "': {\nlabel: 'sensor" << id << "',\ndata: [\n";
for(const auto& elem : dataDouble)
{
if(elem.first.measurementType == x_measurementType && elem.first.id == id)
{
if(initialTime == 0)
initialTime = elem.first.timeStamp;
// cout << "[" << elem.first.measurementType << "," << elem.first.id << "], " << popcendl;
if(elem.first.timeStamp > initialTime)
of << "[" << (elem.first.timeStamp - initialTime) / 24. / 3600000. << "," << elem.second << "], ";
}
}
of << "]},\n";
}
of.close();
stringstream ss2;
ss2 << "cat plots/index.foot.html >> " << x_fileName;
system(ss2.str().c_str());
stringstream ss3;
ss3 << "sed " << " -i 's/###TITLE###/" << explainMeasurementType(x_measurementType) << "/g' " << x_fileName;
system(ss3.str().c_str());
}
void POPSensorData::PrintToPlot(const string& x_fileName, int x_id) const
{
// copy html header to target
stringstream ss1;
ss1 << "cat plots/index.head.html > " << x_fileName;
system(ss1.str().c_str());
ofstream of(x_fileName.c_str(), fstream::app);
unsigned long initialTime = 0;
for(auto mt : GroupAllMeasurementTypes())
{
of << "'" << explainMeasurementType(mt) << "': {\nlabel: '" << explainMeasurementType(mt) << "',\ndata: [\n";
for(const auto& elem : dataDouble)
{
if(elem.first.measurementType == mt && elem.first.id == x_id)
{
if(initialTime == 0)
initialTime = elem.first.timeStamp;
// cout << "[" << elem.first.measurementType << "," << elem.first.id << "], " << popcendl;
if(elem.first.timeStamp > initialTime)
of << "[" << (elem.first.timeStamp - initialTime) / 24. / 3600000. << "," << elem.second << "], ";
}
}
of << "]},\n";
}
of.close();
stringstream ss2;
ss2 << "cat plots/index.foot.html >> " << x_fileName;
system(ss2.str().c_str());
stringstream ss3;
ss3 << "sed " << " -i 's/###TITLE###/sensor" << x_id << "/g' " << x_fileName;
system(ss3.str().c_str());
}
void POPSensorData::Clear()
{
dataString.clear();
dataInt.clear();
dataDouble.clear();
}
int POPSensorData::GetSize() const
{
return dataString.size() + dataInt.size() + dataDouble.size();
}
void POPSensorData::Insert(const POPSensorData& xr_popSensorData)
{
dataDouble.insert(xr_popSensorData.dataDouble.begin(), xr_popSensorData.dataDouble.end());
dataInt.insert(xr_popSensorData.dataInt.begin(), xr_popSensorData.dataInt.end());
dataString.insert(xr_popSensorData.dataString.begin(), xr_popSensorData.dataString.end());
}