query.cpp 6.31 KB
Newer Older
Axel Auweter's avatar
Axel Auweter committed
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 * query.cpp
 *
 *  Created on: Feb 19, 2015
 *      Author: Axel Auweter
 */

#include <iostream>
#include <list>
#include <string>
#include <algorithm>

#include <cstdlib>
14
#include <cinttypes>
Axel Auweter's avatar
Axel Auweter committed
15

16
#include <boost/algorithm/string.hpp>
17
#include <boost/regex.hpp>
18
19

#include "dcdbendian.h"
Axel Auweter's avatar
Axel Auweter committed
20
21
#include "query.h"

Axel Auweter's avatar
Axel Auweter committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void DCDBQuery::setLocalTimeEnabled(bool enable) {
  useLocalTime = enable;
}

bool DCDBQuery::getLocalTimeEnabled() {
  return useLocalTime;
}

void DCDBQuery::setRawOutputEnabled(bool enable) {
  useRawOutput = enable;
}

bool DCDBQuery::getRawOutputEnabled() {
  return useRawOutput;
}

Axel Auweter's avatar
Axel Auweter committed
38
39
40
41
42
43
44
45
void DCDBQuery::setFloatOutputEnabled(bool enable) {
  useFloatOutput = enable;
}

bool DCDBQuery::getFloatOutputEnabled() {
  return useFloatOutput;
}

Axel Auweter's avatar
Axel Auweter committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DCDBTimeStamp start, DCDBTimeStamp end)
{
  /* Create a new connection to the database */
  DCDBConnection* connection;
  connection = new DCDBConnection();
  connection->setHostname(hostname);
  if (!connection->connect()) {
      std::cout << "Cannot connect to database." << std::endl;
  }

  /* Initialize the SensorConfig interface */
  SensorConfig sensorConfig(connection);
  SensorDataStore sensorDataStore(connection);

  /* Print the CSV header */
  std::cout << "Sensor,Time,Value" << std::endl;

  /* Iterate over list of sensors requested by the user */
  for (std::list<std::string>::iterator it = sensors.begin(); it != sensors.end(); it++) {
Axel Auweter's avatar
Axel Auweter committed
65
      bool unitConvert = false;
66
67
      bool scale = false;
      double scalingFactor = 1;
root's avatar
root committed
68
      std::string modifierStr;
Axel Auweter's avatar
Axel Auweter committed
69
70
      DCDBUnit baseUnit = DCDBUnit_None, targetUnit = DCDBUnit_None;

71
      /* Check if the sensor was requested in a different unit or with scaling factor */
Axel Auweter's avatar
Axel Auweter committed
72
      if (it->find('/') != std::string::npos) {
73
          modifierStr = it->substr(it->find('/')+1, it->length());
Axel Auweter's avatar
Axel Auweter committed
74

75
76
          /* Remove the modifier from the string */
          *it = it->substr(0, it->find('/'));
Axel Auweter's avatar
Axel Auweter committed
77

78
79
80
81
82
83
84
85
          /* Check what type of modificatino is requested */
          boost::regex e("\\.?[0-9]*", boost::regex::extended);
          if (boost::regex_match(modifierStr, e)) {
              scale = true;
              sscanf(modifierStr.c_str(), "%lf", &scalingFactor);
          }
          else {
              unitConvert = true;
root's avatar
root committed
86
              targetUnit = UnitConv::fromString(modifierStr);
Axel Auweter's avatar
Axel Auweter committed
87

88
89
90
91
              DCDBPublicSensor sen;
              sensorConfig.getPublicSensorByName(sen, it->c_str());
              baseUnit = UnitConv::fromString(sen.unit);
          }
Axel Auweter's avatar
Axel Auweter committed
92
93
      }

Axel Auweter's avatar
Axel Auweter committed
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
      /* Lookup the sensor in the published sensors table */
      std::string pattern;
      switch (sensorConfig.getSensorPattern(pattern, *it)) {
      case SC_OK:
        break;
      case SC_INVALIDSESSION:
        std::cout << "Invalid session." << std::endl;
        return;
      case SC_UNKNOWNSENSOR:
        std::cout << "Unknown sensor: " << *it << std::endl;
        return;
      default:
        std::cout << "Unknown error." << std::endl;
        return;
      }

      /* Expand the pattern into a list of existing sensors in the time range */
      std::list<SensorId> sensorIds;
      switch (sensorConfig.getSensorListForPattern(sensorIds, pattern, start, end)) {
      case SC_OK:
        break;
      case SC_INVALIDPATTERN:
        std::cout << "Invalid pattern." << std::endl;
        return;
      default:
        std::cout << "Unknown error." << std::endl;
        return;
      }

      /* Iterate over the expanded list of sensorIds and output the results in CSV format */
      for (std::list<SensorId>::iterator sit = sensorIds.begin(); sit != sensorIds.end(); sit++) {
          std::list <SensorDataStoreReading> readings;
          sensorDataStore.query(readings, *sit, start, end);

          /* Iterate over the readings */
          for (std::list<SensorDataStoreReading>::iterator rit = readings.begin(); rit != readings.end(); rit++) {
Axel Auweter's avatar
Axel Auweter committed
130
131
132
              double fvalue;
              int64_t ivalue;

Axel Auweter's avatar
Axel Auweter committed
133
134
              SensorDataStoreReading reading = *rit;

Axel Auweter's avatar
Axel Auweter committed
135
136
137
138
139
140
141
142
              /* Assign the reading to local variable */
              if (useFloatOutput) {
                  fvalue = reading.value;
              }
              else {
                  ivalue = reading.value;
              }

Axel Auweter's avatar
Axel Auweter committed
143
144
              /* Convert the unit if requested */
              if (unitConvert) {
Axel Auweter's avatar
Axel Auweter committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
                  if (useFloatOutput) {
                      if (!UnitConv::convert(fvalue, baseUnit, targetUnit)) {
                          std::cerr << "Warning, cannot convert units ("
                              << UnitConv::toString(baseUnit) << " -> "
                              << UnitConv::toString(targetUnit) << ")" << std::endl;
                          unitConvert = false;
                      }
                  }
                  else {
                      if (!UnitConv::convert(ivalue, baseUnit, targetUnit)) {
                          std::cerr << "Warning, cannot convert units ("
                              << UnitConv::toString(baseUnit) << " -> "
                              << UnitConv::toString(targetUnit) << ")" << std::endl;
                          unitConvert = false;
                      }
Axel Auweter's avatar
Axel Auweter committed
160
161
162
                  }
              }

163
164
              /* Scale the value if requested */
              if (scale) {
Axel Auweter's avatar
Axel Auweter committed
165
166
167
168
169
170
                  if (useFloatOutput) {
                      fvalue *= scalingFactor;
                  }
                  else {
                      ivalue *= scalingFactor;
                  }
171
172
              }

Axel Auweter's avatar
Axel Auweter committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
              /* Print the sensor's public name */
              std::cout << *it << ",";

              /* Print the time stamp */
              if (useLocalTime) {
                  reading.timeStamp.convertToLocal();
              }
              if (useRawOutput) {
                  std::cout << reading.timeStamp.getRaw() << ",";
              }
              else {
                  std::cout << reading.timeStamp.getString() << ",";
              }

              /* Print the sensor value */
Axel Auweter's avatar
Axel Auweter committed
188
189
190
191
192
193
              if (useFloatOutput) {
                  std::cout << fvalue << std::endl;
              }
              else {
                  std::cout << ivalue << std::endl;
              }
Axel Auweter's avatar
Axel Auweter committed
194
195
196
197
198
199
200
201
202
203
204
205
          }
      }
  }

  /*
   * Clean up
   */
  connection->disconnect();
  delete connection;

}

Axel Auweter's avatar
Axel Auweter committed
206
DCDBQuery::DCDBQuery()
Axel Auweter's avatar
Axel Auweter committed
207
{
Axel Auweter's avatar
Axel Auweter committed
208
209
210
  connection = nullptr;
  useLocalTime = false;
  useRawOutput = false;
Axel Auweter's avatar
Axel Auweter committed
211
  useFloatOutput = false;
Axel Auweter's avatar
Axel Auweter committed
212
}