Skip to content

Commit

Permalink
Added test for sensor_descs.
Browse files Browse the repository at this point in the history
Added details documtation about requirements of sensor_desc.
  • Loading branch information
crowbar27 committed Mar 9, 2023
1 parent 9130d33 commit 8412e64
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,12 @@ Second, in order to be eligible for the automated enumeration by the sensor util
* a template specialisation of `visus::power_overwhelming::detail::sensor_desc` must be provided in [sensor_desc.h](power_overwhelming/src/sensor_desc.h), which provides means to serialise and deserialise sensors,
* the class must be added to the `sensor_list` template at the bottom of [sensor_desc.h](power_overwhelming/src/sensor_desc.h).

The specialisation of `visus::power_overwhelming::detail::sensor_desc` must fulfil the following contract:
* It must have a member `static constexpr const char *type_name` specifing the unique name of the sensor, which can be declared using the `POWER_OVERWHELMING_DECLARE_SENSOR_NAME` macro.
* It must have a member `static constexpr bool intrinsic_async` specifying whether the sensor can run asynchronously without emulating it by starting a sampler thread that regularly polls the sensor.
* It must have a method `static inline nlohmann::json serialise(const value_type& value)` which serialises the given sensor into a JSON representation.
* It must have a method `static inline value_type deserialise(const nlohmann::json& value)` which restores a sensor from a given JSON representation.
* If the sensor can serialise all of its instances more efficiently than creating an instance of it and converting these instances to JSON, it can implement a method `static inline nlohmann::json serialise_all(void)` which serialises all sensors into a JSON array. The library will prefer this method if it is provided.

## Acknowledgments
This work was partially funded by Deutsche Forschungsgemeinschaft (DFG) as part of [SFB/Transregio 161](https://www.sfbtrr161.de) (project ID 251654672).
37 changes: 31 additions & 6 deletions power_overwhelming/src/sensor_desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@
#include "tinkerforge_sensor_impl.h"


/// <summary>
/// Declares the static constant <c>type_name</c> for the sensor with the
/// given name.
/// </summary>
#define POWER_OVERWHELMING_DECLARE_SENSOR_NAME(name)\
static constexpr const char *type_name = #name

/// <summary>
/// Declares the static constant <c>intrinsic_async</c> specifying whether a
/// sensor is intrinsically asynchronous and should not be sampled by polling if
/// used in a <see cref="visus::power_overwhelming::collector" />.
/// </summary>
#define POWER_OVERWHELMING_DECLARE_INTRINSIC_ASYNC(value)\
static constexpr bool intrinsic_async = (value)


namespace visus {
namespace power_overwhelming {
namespace detail {
Expand Down Expand Up @@ -102,7 +118,8 @@ namespace detail {
/// </summary>
template<> struct sensor_desc<adl_sensor> final
: detail::sensor_desc_base<sensor_desc<adl_sensor>> {
static constexpr const char *type_name = "adl_sensor";
POWER_OVERWHELMING_DECLARE_SENSOR_NAME(adl_sensor);
POWER_OVERWHELMING_DECLARE_INTRINSIC_ASYNC(false);

static inline value_type deserialise(const nlohmann::json& value) {
auto source0 = value[json_field_source].get<std::string>();
Expand Down Expand Up @@ -131,7 +148,8 @@ namespace detail {
/// </summary>
template<> struct sensor_desc<emi_sensor> final
: detail::sensor_desc_base<sensor_desc<emi_sensor>> {
static constexpr const char *type_name = "emi_sensor";
POWER_OVERWHELMING_DECLARE_SENSOR_NAME(emi_sensor);
POWER_OVERWHELMING_DECLARE_INTRINSIC_ASYNC(false);

static inline value_type deserialise(const nlohmann::json& value) {
#if defined(_WIN32)
Expand Down Expand Up @@ -189,7 +207,8 @@ namespace detail {
/// </summary>
template<> struct sensor_desc<hmc8015_sensor> final
: detail::sensor_desc_base<sensor_desc<hmc8015_sensor>> {
static constexpr const char *type_name = "hmc8015_sensor";
POWER_OVERWHELMING_DECLARE_SENSOR_NAME(hmc8015_sensor);
POWER_OVERWHELMING_DECLARE_INTRINSIC_ASYNC(false);

static inline value_type deserialise(const nlohmann::json& value) {
auto path = value[json_field_path].get<std::string>();
Expand All @@ -215,7 +234,8 @@ namespace detail {
/// </summary>
template<> struct sensor_desc<nvml_sensor> final
: detail::sensor_desc_base<sensor_desc<nvml_sensor>> {
static constexpr const char *type_name = "nvml_sensor";
POWER_OVERWHELMING_DECLARE_SENSOR_NAME(nvml_sensor);
POWER_OVERWHELMING_DECLARE_INTRINSIC_ASYNC(false);

static inline value_type deserialise(const nlohmann::json& value) {
auto guid = value[json_field_dev_guid].get<std::string>();
Expand All @@ -239,7 +259,8 @@ namespace detail {
/// </summary>
template<> struct sensor_desc<rtb_sensor> final
: detail::sensor_desc_base<sensor_desc<rtb_sensor>> {
static constexpr const char *type_name = "rtb_sensor";
POWER_OVERWHELMING_DECLARE_SENSOR_NAME(rtb_sensor);
POWER_OVERWHELMING_DECLARE_INTRINSIC_ASYNC(false);

static inline value_type deserialise(const nlohmann::json& value) {
auto path = value[json_field_path].get<std::string>();
Expand All @@ -265,7 +286,8 @@ namespace detail {
/// </summary>
template<> struct sensor_desc<tinkerforge_sensor> final
: detail::sensor_desc_base<sensor_desc<tinkerforge_sensor>> {
static constexpr const char *type_name = "tinkerforge_sensor";
POWER_OVERWHELMING_DECLARE_SENSOR_NAME(tinkerforge_sensor);
POWER_OVERWHELMING_DECLARE_INTRINSIC_ASYNC(true);

static inline value_type deserialise(const nlohmann::json& value) {
const auto dit = value.find(json_field_description);
Expand Down Expand Up @@ -348,6 +370,9 @@ namespace detail {
};


#undef POWER_OVERWHELMING_DECLARE_SENSOR_NAME
#undef POWER_OVERWHELMING_DECLARE_INTRINSIC_ASYNC

/// <summary>
/// A type list of all known sensors, which allows for compile-time
/// enumeration of known sensor types.
Expand Down
1 change: 1 addition & 0 deletions test/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
#include <timestamp.h>
#include <nvml_exception.h>
#include <nvml_scope.h>
#include <sensor_desc.h>
#include <setup_api.h>
60 changes: 60 additions & 0 deletions test/sensor_desc_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// <copyright file="sensor_desc.cpp" company="Visualisierungsinstitut der Universität Stuttgart">
// Copyright © 2023 Visualisierungsinstitut der Universität Stuttgart. Alle Rechte vorbehalten.
// </copyright>
// <author>Christoph Müller</author>

#include "pch.h"
#include "CppUnitTest.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;


namespace visus {
namespace power_overwhelming {
namespace test {

TEST_CLASS(sensor_desc) {

public:

TEST_METHOD(test_adl_sensor) {
typedef detail::sensor_desc<adl_sensor> desc_type;
Assert::AreEqual("adl_sensor", desc_type::type_name, L"adl_sensor type name", LINE_INFO());
Assert::IsFalse(desc_type::intrinsic_async, L"adl_sensor not intrinsically asynchronous", LINE_INFO());
}

TEST_METHOD(test_emi_sensor) {
typedef detail::sensor_desc<emi_sensor> desc_type;
Assert::AreEqual("emi_sensor", desc_type::type_name, L"emi_sensor type name", LINE_INFO());
Assert::IsFalse(desc_type::intrinsic_async, L"emi_sensor not intrinsically asynchronous", LINE_INFO());
}


TEST_METHOD(test_hmc8015_sensor) {
typedef detail::sensor_desc<hmc8015_sensor> desc_type;
Assert::AreEqual("hmc8015_sensor", desc_type::type_name, L"hmc8015_sensor type name", LINE_INFO());
Assert::IsFalse(desc_type::intrinsic_async, L"hmc8015_sensor not intrinsically asynchronous", LINE_INFO());
}

TEST_METHOD(test_nvml_sensor) {
typedef detail::sensor_desc<nvml_sensor> desc_type;
Assert::AreEqual("nvml_sensor", desc_type::type_name, L"nvml_sensor type name", LINE_INFO());
Assert::IsFalse(desc_type::intrinsic_async, L"nvml_sensor not intrinsically asynchronous", LINE_INFO());
}

TEST_METHOD(test_rtb_sensor) {
typedef detail::sensor_desc<rtb_sensor> desc_type;
Assert::AreEqual("rtb_sensor", desc_type::type_name, L"rtb_sensor type name", LINE_INFO());
Assert::IsFalse(desc_type::intrinsic_async, L"rtb_sensor not intrinsically asynchronous", LINE_INFO());
}

TEST_METHOD(test_tinkerforge_sensor) {
typedef detail::sensor_desc<tinkerforge_sensor> desc_type;
Assert::AreEqual("tinkerforge_sensor", desc_type::type_name, L"tinkerforge_sensor type name", LINE_INFO());
Assert::IsTrue(desc_type::intrinsic_async, L"tinkerforge_sensor intrinsically asynchronous", LINE_INFO());
}

};
} /* namespace test */
} /* namespace power_overwhelming */
} /* namespace visus */

0 comments on commit 8412e64

Please sign in to comment.