diff options
Diffstat (limited to 'host')
-rw-r--r-- | host/include/uhd/types/sensors.hpp | 3 | ||||
-rw-r--r-- | host/lib/types/sensors.cpp | 27 | ||||
-rw-r--r-- | host/tests/sensors_test.cpp | 7 |
3 files changed, 37 insertions, 0 deletions
diff --git a/host/include/uhd/types/sensors.hpp b/host/include/uhd/types/sensors.hpp index 6b9c4f137..4f1b64534 100644 --- a/host/include/uhd/types/sensors.hpp +++ b/host/include/uhd/types/sensors.hpp @@ -110,6 +110,9 @@ namespace uhd{ //! convert the sensor value to real number double to_real(void) const; + //! convert the sensor value to sensor_map_t + sensor_map_t to_map(void) const; + //! The name of the sensor value std::string name; diff --git a/host/lib/types/sensors.cpp b/host/lib/types/sensors.cpp index e9f7ab506..982e02f05 100644 --- a/host/lib/types/sensors.cpp +++ b/host/lib/types/sensors.cpp @@ -76,6 +76,24 @@ static sensor_value_t::data_type_t _string_to_type( } } +static std::string _type_to_string( + const sensor_value_t::data_type_t &type +) { + if (type == sensor_value_t::STRING) { + return "STRING"; + } else if (type == sensor_value_t::REALNUM) { + return "REALNUM"; + } else if (type == sensor_value_t::INTEGER) { + return "INTEGER"; + } else if (type == sensor_value_t::BOOLEAN) { + return "BOOLEAN"; + } else { + throw uhd::value_error( + std::string("Invalid raw sensor value type.") + ); + } +} + sensor_value_t::sensor_value_t( const std::map<std::string, std::string> &sensor_dict ): @@ -139,6 +157,15 @@ double sensor_value_t::to_real(void) const{ return std::stod(value); } +sensor_value_t::sensor_map_t sensor_value_t::to_map(void) const{ + sensor_map_t ret_map; + ret_map["name"] = name; + ret_map["value"] = value; + ret_map["unit"] = unit; + ret_map["type"] = _type_to_string(type); + return ret_map; +} + sensor_value_t& sensor_value_t::operator=(const sensor_value_t& rhs) { this->name = rhs.name; diff --git a/host/tests/sensors_test.cpp b/host/tests/sensors_test.cpp index e15269e2d..2136ca13b 100644 --- a/host/tests/sensors_test.cpp +++ b/host/tests/sensors_test.cpp @@ -91,5 +91,12 @@ BOOST_AUTO_TEST_CASE(test_sensor_string) { auto sensor_str2 = sensor_value_t(sensor_map); BOOST_CHECK_EQUAL(sensor_str2.unit, sens_units); + + sensor_value_t sensor_str3(sensor_str2.to_map()); + BOOST_CHECK_EQUAL(sensor_str2.name, sensor_str3.name); + BOOST_CHECK_EQUAL(sensor_str2.value, sensor_str3.value); + BOOST_CHECK_EQUAL(sensor_str2.unit, sensor_str3.unit); + BOOST_CHECK_EQUAL(sensor_str2.type, sensor_str3.type); + } |