query.cpp 6.13 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;
}

46
void DCDBQuery::genOutput(DCDB::SensorDataStoreReading& reading)
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
{
  double fvalue;
  int64_t ivalue;

  /* Assign the reading to local variable */
  if (useFloatOutput) {
      fvalue = reading.value;
  }
  else {
      ivalue = reading.value;
  }

  /* Convert the unit if requested */
  if (unitConvert) {
      if (useFloatOutput) {
          if (!DCDB::UnitConv::convert(fvalue, baseUnit, targetUnit)) {
              std::cerr << "Warning, cannot convert units ("
                  << DCDB::UnitConv::toString(baseUnit) << " -> "
                  << DCDB::UnitConv::toString(targetUnit) << ")" << std::endl;
              unitConvert = false;
          }
      }
      else {
          if (!DCDB::UnitConv::convert(ivalue, baseUnit, targetUnit)) {
              std::cerr << "Warning, cannot convert units ("
                  << DCDB::UnitConv::toString(baseUnit) << " -> "
                  << DCDB::UnitConv::toString(targetUnit) << ")" << std::endl;
              unitConvert = false;
          }
      }
  }

  /* Scale the value if requested */
  if (scale) {
      if (useFloatOutput) {
          fvalue *= scalingFactor;
      }
      else {
          ivalue *= scalingFactor;
      }
  }

  /* Print the sensor's public name */
90
  std::cout << sensorName << ",";
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

  /* 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 */
  if (useFloatOutput) {
      std::cout << fvalue << std::endl;
  }
  else {
      std::cout << ivalue << std::endl;
  }
}

112
113
114
115
116
117
118
119
void DCDBQuery::queryCallback(DCDB::SensorDataStoreReading& reading, void* userData)
{
  DCDBQuery* self = (DCDBQuery*)userData;

  /* Generate output for the reading */
  self->genOutput(reading);
}

120
void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DCDB::TimeStamp start, DCDB::TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
121
122
{
  /* Create a new connection to the database */
123
  connection = new DCDB::Connection();
Axel Auweter's avatar
Axel Auweter committed
124
125
126
  connection->setHostname(hostname);
  if (!connection->connect()) {
      std::cout << "Cannot connect to database." << std::endl;
127
      exit(EXIT_FAILURE);
Axel Auweter's avatar
Axel Auweter committed
128
129
130
  }

  /* Initialize the SensorConfig interface */
131
132
  DCDB::SensorConfig sensorConfig(connection);
  DCDB::SensorDataStore sensorDataStore(connection);
Axel Auweter's avatar
Axel Auweter committed
133
134
135
136
137
138

  /* 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++) {
139
140
141
      unitConvert = false;
      scale = false;
      scalingFactor = 1;
root's avatar
root committed
142
      std::string modifierStr;
143
144
      baseUnit = DCDB::Unit_None;
      targetUnit = DCDB::Unit_None;
Axel Auweter's avatar
Axel Auweter committed
145

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

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

153
154
155
156
157
158
159
160
          /* 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;
161
              targetUnit = DCDB::UnitConv::fromString(modifierStr);
Axel Auweter's avatar
Axel Auweter committed
162

163
              DCDB::PublicSensor sen;
164
              sensorConfig.getPublicSensorByName(sen, it->c_str());
165
              baseUnit = DCDB::UnitConv::fromString(sen.unit);
166
          }
Axel Auweter's avatar
Axel Auweter committed
167
168
      }

Axel Auweter's avatar
Axel Auweter committed
169
      /* Lookup the sensor in the published sensors table */
170
171
      DCDB::PublicSensor publicSensor;
      switch (sensorConfig.getPublicSensorByName(publicSensor, it->c_str())) {
172
      case DCDB::SC_OK:
Axel Auweter's avatar
Axel Auweter committed
173
        break;
174
      case DCDB::SC_INVALIDSESSION:
Axel Auweter's avatar
Axel Auweter committed
175
176
        std::cout << "Invalid session." << std::endl;
        return;
177
      case DCDB::SC_UNKNOWNSENSOR:
Axel Auweter's avatar
Axel Auweter committed
178
179
180
181
182
183
184
        std::cout << "Unknown sensor: " << *it << std::endl;
        return;
      default:
        std::cout << "Unknown error." << std::endl;
        return;
      }

185
186
      /* If this is a virtual sensor... */
      if (publicSensor.is_virtual) {
187
          sensorName = *it;
188
          DCDB::VSensor vSen(connection, publicSensor);
189
          vSen.queryCB(queryCallback, this, start, end);
190
191
192
193
194
195
196
197
198
199
200
201
202
203
      }
      else {
          /* Expand the pattern into a list of existing sensors in the time range */
          std::list<DCDB::SensorId> sensorIds;
          switch (sensorConfig.getSensorListForPattern(sensorIds, publicSensor.pattern, start, end)) {
          case DCDB::SC_OK:
            break;
          case DCDB::SC_INVALIDPATTERN:
            std::cout << "Invalid pattern." << std::endl;
            return;
          default:
            std::cout << "Unknown error." << std::endl;
            return;
          }
Axel Auweter's avatar
Axel Auweter committed
204

205
206
          /* Iterate over the expanded list of sensorIds and output the results in CSV format */
          for (std::list<DCDB::SensorId>::iterator sit = sensorIds.begin(); sit != sensorIds.end(); sit++) {
207
208
              sensorName = *it;
              sensorDataStore.queryCB(queryCallback, this, *sit, start, end);
Axel Auweter's avatar
Axel Auweter committed
209
210
211
212
213
214
215
216
217
218
219
220
          }
      }
  }

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

}

Axel Auweter's avatar
Axel Auweter committed
221
DCDBQuery::DCDBQuery()
Axel Auweter's avatar
Axel Auweter committed
222
{
Axel Auweter's avatar
Axel Auweter committed
223
224
225
  connection = nullptr;
  useLocalTime = false;
  useRawOutput = false;
Axel Auweter's avatar
Axel Auweter committed
226
  useFloatOutput = false;
Axel Auweter's avatar
Axel Auweter committed
227
}