-
Notifications
You must be signed in to change notification settings - Fork 5
/
dataminer.cpp
117 lines (91 loc) · 2.64 KB
/
dataminer.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
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
#include <boost/chrono.hpp>
#include <boost/filesystem.hpp>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <string>
#include "httpreader.h"
#include "xml2json.h"
namespace fs = boost::filesystem;
static int poll_count = 0;
static string last_sequence = "";
void poll(HttpReader &reader, string &outputLocation)
{
poll_count++;
string xmlData = reader.read();
if (xmlData.length() == 0)
return;
// convert xml to json
xml2json x;
x.setNumericFields({"sequence", "@@data"});
try {
x.process(xmlData);
}
catch (exception & e)
{
cerr << e.what() << endl;
return;
}
#if DEBUG
string json = x.getJSON(true);
cout << json << endl;
std::cout << "========== { " << poll_count << " } ==========" << std::endl;
#else
// don't output if data has not changed
string sequence = x.getJSON_data("MTConnectStreams.Header.<xmlattr>.lastSequence");
if (sequence.compare(last_sequence) == 0)
{
std::cout << "========== { " << poll_count << ": [SKIPPED] last sequence=" << last_sequence << " } ==========" << std::endl;
return;
}
last_sequence = sequence;
time_t now = time(0);
char timestamp[20] = "";
strftime (timestamp, 20, "%Y%m%d-%H%M%S", localtime(&now));
char outFilename[80];
sprintf(outFilename, "MTComponentStreams-%s.log", timestamp);
fs::path dir (outputLocation);
fs::path file (outFilename);
fs::path fullFilename = dir / file;
x.outputJSON(fullFilename.string());
std::cout << "========== { " << poll_count << ": " << outFilename << " } ==========" << std::endl;
#endif
}
int main(int argc, char** argv)
{
// Check command line arguments.
if (argc != 3)
{
std::cerr <<
"Usage: dataminer <output location> <uri>\n" <<
"Example:\n" <<
" dataminer /tmp https://smstestbed.nist.gov/vds/current\n";
return -1;
}
string outputLocation = argv[1];
string uri = argv[2];
if (!boost::filesystem::exists(outputLocation))
{
std::cerr <<
"Location " << outputLocation << " does not exist!\n";
return -1;
}
HttpReader reader(uri);
if (!reader.connect())
{
std::cerr <<
"Cannot connect to MT agent from [" << uri << "]!\n";
return -1;
}
while (true)
{
poll(reader, outputLocation);
boost::this_thread::sleep_for( boost::chrono::seconds(60) );
}
reader.close();
cout << "DONE" << std::endl;
return 0;
}