aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2017-09-29 13:51:43 -0700
committerMartin Braun <martin.braun@ettus.com>2017-12-22 15:04:02 -0800
commit21605b4e8b58053f4fa91a0a5136261da33e3bfb (patch)
tree551c8313dcff1047598bcf4a0f00cf0bdd9323e2 /host/lib
parentaeaea4936011665e2bbad66e1fdf4628e2b940f2 (diff)
downloaduhd-21605b4e8b58053f4fa91a0a5136261da33e3bfb.tar.gz
uhd-21605b4e8b58053f4fa91a0a5136261da33e3bfb.tar.bz2
uhd-21605b4e8b58053f4fa91a0a5136261da33e3bfb.zip
types: sensor_value_t can now be created from map
- Adds unit tests for sensor_value_t also
Diffstat (limited to 'host/lib')
-rw-r--r--host/lib/types/sensors.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/host/lib/types/sensors.cpp b/host/lib/types/sensors.cpp
index 4bffc1bd9..d015cf666 100644
--- a/host/lib/types/sensors.cpp
+++ b/host/lib/types/sensors.cpp
@@ -57,6 +57,57 @@ sensor_value_t::sensor_value_t(
/* NOP */
}
+static sensor_value_t::data_type_t _string_to_type(
+ const std::string &type_str
+) {
+ if (type_str == "STRING") {
+ return sensor_value_t::STRING;
+ } else if (type_str == "REALNUM") {
+ return sensor_value_t::REALNUM;
+ } else if (type_str == "INTEGER") {
+ return sensor_value_t::INTEGER;
+ } else if (type_str == "BOOLEAN") {
+ return sensor_value_t::BOOLEAN;
+ } else {
+ throw uhd::value_error(
+ std::string("Invalid sensor value type: ") + type_str
+ );
+ }
+}
+
+sensor_value_t::sensor_value_t(
+ const std::map<std::string, std::string> &sensor_dict
+):
+ name(sensor_dict.at("name")),
+ value(sensor_dict.at("value")),
+ unit(sensor_dict.at("unit")),
+ type(_string_to_type(sensor_dict.at("type")))
+{
+ UHD_ASSERT_THROW(not name.empty());
+ UHD_ASSERT_THROW(not value.empty());
+ try {
+ if (type == INTEGER) {
+ to_int();
+ } else if (type == REALNUM) {
+ to_real();
+ }
+ }
+ catch (const std::invalid_argument&) {
+ throw uhd::value_error(str(
+ boost::format("Could not convert sensor value `%s' to type `%s'")
+ % value
+ % sensor_dict.at("type")
+ ));
+ }
+ catch (const std::out_of_range&) {
+ throw uhd::value_error(str(
+ boost::format("Could not convert sensor value `%s' to type `%s'")
+ % value
+ % sensor_dict.at("type")
+ ));
+ }
+}
+
sensor_value_t::sensor_value_t(const sensor_value_t& source)
{
*this = source;