Commit a79dd972 authored by Michael Ott's avatar Michael Ott
Browse files

Convert readings to base unit in grafanaserver

parent b7e056b1
......@@ -26,6 +26,7 @@
//================================================================================
#include "RestAPI.h"
#include "dcdb/unitconv.h"
#include <sstream>
#include <boost/property_tree/json_parser.hpp>
......@@ -243,7 +244,7 @@ void RestAPI::POST_query(endpointArgs) {
//LOG(debug) << "Picking smoother " << bestSmoother << " for sensor " << sensorName << ".";
}
}
//We need the Sensor ID, since smoothed sensors are not published.
DCDB::SensorId sid(sensorName + bestSmoother);
uint16_t startWs=start.getWeekstamp(), endWs=end.getWeekstamp();
......@@ -253,12 +254,14 @@ void RestAPI::POST_query(endpointArgs) {
sid.setRsvd(currWs);
_sensorDataStore->query(results, sid, start, end, DCDB::AGGREGATE_NONE);
}
//Format the output for the response to Grafana.
std::string datapoints = "[";
for (auto& r : results)
datapoints += "[" + std::to_string(r.value * sScale) + ","
+ std::to_string(r.timeStamp.getRaw()/1000000) + "],";
for (auto& r : results) {
double val = (double) r.value;
DCDB::UnitConv::convertToBaseUnit(val, DCDB::UnitConv::fromString(*sm.getUnit()));
datapoints += "[" + std::to_string(val * sScale) + "," + std::to_string(r.timeStamp.getRaw()/1000000) + "],";
}
if(datapoints.back() == ',')
datapoints.pop_back();
datapoints += "]";
......
......@@ -112,6 +112,7 @@ public:
static std::string toString(Unit unit);
static bool convert(int64_t& value, Unit from, Unit to);
static bool convert(double& value, Unit from, Unit to);
static bool convertToBaseUnit(double& value, Unit from);
};
} /* End of namespace DCDB */
......
......@@ -320,3 +320,19 @@ bool UnitConv::convert(double& value, Unit in, Unit out) {
return false;
}
bool UnitConv::convertToBaseUnit(double& value, Unit from) {
Unit to = from;
for (int i=0; i<ConversionTableSize; i++) {
if (conversionTable[i].unit == from) {
to = conversionTable[i].baseUnit;
break;
}
}
if (to != from) {
return convert(value, from, to);
} else {
return true;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment