Rework SysfsTemperature to be more generic

Change-Id: I68fe3bc86b0b92f0ac62a6f41d285da63741b6ed
diff --git a/src/ietf-hardware/Factory.cpp b/src/ietf-hardware/Factory.cpp
index 68eac37..a8f120f 100644
--- a/src/ietf-hardware/Factory.cpp
+++ b/src/ietf-hardware/Factory.cpp
@@ -8,6 +8,11 @@
 std::shared_ptr<IETFHardware> create(const std::string& applianceName)
 {
     auto ietfHardware = std::make_shared<velia::ietf_hardware::IETFHardware>();
+    using velia::ietf_hardware::data_reader::StaticData;
+    using velia::ietf_hardware::data_reader::Fans;
+    using velia::ietf_hardware::data_reader::SysfsValue;
+    using velia::ietf_hardware::data_reader::EMMC;
+    using velia::ietf_hardware::data_reader::SensorType;
 
     if (applianceName == "czechlight-clearfog") {
         auto hwmonFans = std::make_shared<velia::ietf_hardware::sysfs::HWMon>("/sys/bus/i2c/devices/1-002e/hwmon/");
@@ -17,15 +22,14 @@
         auto sysfsTempMII1 = std::make_shared<velia::ietf_hardware::sysfs::HWMon>("/sys/devices/platform/soc/soc:internal-regs/f1072004.mdio/mdio_bus/f1072004.mdio-mii/f1072004.mdio-mii:01/hwmon/");
         auto emmc = std::make_shared<velia::ietf_hardware::sysfs::EMMC>("/sys/block/mmcblk0/device/");
 
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::StaticData("ne", std::nullopt, {{"description", "Czechlight project"s}}));
-
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::StaticData("ne:ctrl", "ne", {{"class", "iana-hardware:module"}}));
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::Fans("ne:fans", "ne", hwmonFans, 4));
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::SysfsTemperature("ne:ctrl:temperature-front", "ne:ctrl", sysfsTempFront, 1));
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::SysfsTemperature("ne:ctrl:temperature-cpu", "ne:ctrl", sysfsTempCpu, 1));
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::SysfsTemperature("ne:ctrl:temperature-internal-0", "ne:ctrl", sysfsTempMII0, 1));
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::SysfsTemperature("ne:ctrl:temperature-internal-1", "ne:ctrl", sysfsTempMII1, 1));
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::EMMC("ne:ctrl:emmc", "ne:ctrl", emmc));
+        ietfHardware->registerDataReader(StaticData("ne", std::nullopt, {{"description", "Czechlight project"s}}));
+        ietfHardware->registerDataReader(StaticData("ne:ctrl", "ne", {{"class", "iana-hardware:module"}}));
+        ietfHardware->registerDataReader(Fans("ne:fans", "ne", hwmonFans, 4));
+        ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-front", "ne:ctrl", sysfsTempFront, 1));
+        ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-cpu", "ne:ctrl", sysfsTempCpu, 1));
+        ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-internal-0", "ne:ctrl", sysfsTempMII0, 1));
+        ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-internal-1", "ne:ctrl", sysfsTempMII1, 1));
+        ietfHardware->registerDataReader(EMMC("ne:ctrl:emmc", "ne:ctrl", emmc));
     } else if (applianceName == "czechlight-clearfog-g2") {
         auto fans = std::make_shared<velia::ietf_hardware::sysfs::HWMon>("/sys/bus/i2c/devices/1-0020/hwmon/");
         auto tempMainBoard = std::make_shared<velia::ietf_hardware::sysfs::HWMon>("/sys/bus/i2c/devices/1-0048/hwmon/");
@@ -39,16 +43,16 @@
          * Publish more properties for ne element. We have an EEPROM at the PCB for storing serial numbers (etc.), but it's so far unused. We could also use U-Boot env variables
          * This will be needed for sdn-roadm-line only. So we should also parse the model from /proc/cmdline here
          */
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::StaticData("ne", std::nullopt, {{"description", "Czechlight project"s}}));
+        ietfHardware->registerDataReader(StaticData("ne", std::nullopt, {{"description", "Czechlight project"s}}));
 
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::StaticData("ne:ctrl", "ne", {{"class", "iana-hardware:module"}}));
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::Fans("ne:fans", "ne", fans, 4));
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::SysfsTemperature("ne:ctrl:temperature-front", "ne:ctrl", tempMainBoard, 1));
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::SysfsTemperature("ne:ctrl:temperature-cpu", "ne:ctrl", tempCpu, 1));
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::SysfsTemperature("ne:ctrl:temperature-rear", "ne:ctrl", tempFans, 1));
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::SysfsTemperature("ne:ctrl:temperature-internal-0", "ne:ctrl", tempMII0, 1));
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::SysfsTemperature("ne:ctrl:temperature-internal-1", "ne:ctrl", tempMII1, 1));
-        ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::EMMC("ne:ctrl:emmc", "ne:ctrl", emmc));
+        ietfHardware->registerDataReader(StaticData("ne:ctrl", "ne", {{"class", "iana-hardware:module"}}));
+        ietfHardware->registerDataReader(Fans("ne:fans", "ne", fans, 4));
+        ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-front", "ne:ctrl", tempMainBoard, 1));
+        ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-cpu", "ne:ctrl", tempCpu, 1));
+        ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-rear", "ne:ctrl", tempFans, 1));
+        ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-internal-0", "ne:ctrl", tempMII0, 1));
+        ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-internal-1", "ne:ctrl", tempMII1, 1));
+        ietfHardware->registerDataReader(EMMC("ne:ctrl:emmc", "ne:ctrl", emmc));
     } else {
         throw std::runtime_error("Unknown appliance '" + applianceName + "'");
     }
diff --git a/src/ietf-hardware/IETFHardware.cpp b/src/ietf-hardware/IETFHardware.cpp
index ba079e9..618995b 100644
--- a/src/ietf-hardware/IETFHardware.cpp
+++ b/src/ietf-hardware/IETFHardware.cpp
@@ -145,33 +145,50 @@
     return res;
 }
 
-SysfsTemperature::SysfsTemperature(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, int sysfsChannelNr)
+std::string getSysfsFilename(const SensorType type, int sysfsChannelNr)
+{
+    switch (type) {
+        case SensorType::Temperature:
+            return "temp"s + std::to_string(sysfsChannelNr) + "_input";
+    }
+
+    __builtin_unreachable();
+}
+
+template <SensorType TYPE> const DataTree sysfsStaticData;
+template <> const DataTree sysfsStaticData<SensorType::Temperature> = {
+    {"class", "iana-hardware:sensor"},
+    {"sensor-data/value-type", "celsius"},
+    {"sensor-data/value-scale", "milli"},
+    {"sensor-data/value-precision", "0"},
+    {"sensor-data/oper-status", "ok"},
+};
+
+template <SensorType TYPE>
+SysfsValue<TYPE>::SysfsValue(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, int sysfsChannelNr)
     : DataReader(std::move(componentName), std::move(parent))
     , m_hwmon(std::move(hwmon))
-    , m_sysfsTemperatureFile("temp"s + std::to_string(sysfsChannelNr) + "_input")
+    , m_sysfsFile(getSysfsFilename(TYPE, sysfsChannelNr))
 {
     addComponent(m_staticData,
                  m_componentName,
                  m_parent,
-                 DataTree {
-                     {"class", "iana-hardware:sensor"},
-                     {"sensor-data/value-type", "celsius"},
-                     {"sensor-data/value-scale", "milli"},
-                     {"sensor-data/value-precision", "0"},
-                     {"sensor-data/oper-status", "ok"},
-                 });
+                 sysfsStaticData<TYPE>);
 }
 
-DataTree SysfsTemperature::operator()() const
+template <SensorType TYPE>
+DataTree SysfsValue<TYPE>::operator()() const
 {
     DataTree res(m_staticData);
 
-    int64_t sensorValue = m_hwmon->attributes().at(m_sysfsTemperatureFile);
+    int64_t sensorValue = m_hwmon->attributes().at(m_sysfsFile);
     addSensorValue(res, m_componentName, std::to_string(sensorValue));
 
     return res;
 }
 
+template struct SysfsValue<SensorType::Temperature>;
+
 EMMC::EMMC(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::EMMC> emmc)
     : DataReader(std::move(componentName), std::move(parent))
     , m_emmc(std::move(emmc))
diff --git a/src/ietf-hardware/IETFHardware.h b/src/ietf-hardware/IETFHardware.h
index b02f204..4708e7e 100644
--- a/src/ietf-hardware/IETFHardware.h
+++ b/src/ietf-hardware/IETFHardware.h
@@ -101,14 +101,19 @@
     DataTree operator()() const;
 };
 
-/** @brief Manages a single temperature sensor, data is provided by a sysfs::HWMon object. */
-struct SysfsTemperature : private DataReader {
+enum class SensorType {
+   Temperature
+};
+
+/** @brief Manages a single value from sysfs, data is provided by a sysfs::HWMon object. */
+template<SensorType TYPE>
+struct SysfsValue : private DataReader {
 private:
     std::shared_ptr<sysfs::HWMon> m_hwmon;
-    std::string m_sysfsTemperatureFile;
+    std::string m_sysfsFile;
 
 public:
-    SysfsTemperature(std::string propertyPrefix, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, int sysfsChannelNr);
+    SysfsValue(std::string propertyPrefix, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, int sysfsChannelNr);
     DataTree operator()() const;
 };
 
diff --git a/tests/hardware_ietf-hardware.cpp b/tests/hardware_ietf-hardware.cpp
index 0ff68a0..9f70663 100644
--- a/tests/hardware_ietf-hardware.cpp
+++ b/tests/hardware_ietf-hardware.cpp
@@ -57,15 +57,20 @@
     attributesEMMC = {{"life_time"s, "40"s}};
     FAKE_EMMC(emmc, attributesEMMC);
 
+    using velia::ietf_hardware::data_reader::SensorType;
+    using velia::ietf_hardware::data_reader::StaticData;
+    using velia::ietf_hardware::data_reader::Fans;
+    using velia::ietf_hardware::data_reader::SysfsValue;
+    using velia::ietf_hardware::data_reader::EMMC;
     // register components into hw state
-    ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::StaticData("ne", std::nullopt, {{"class", "iana-hardware:chassis"}, {"mfg-name", "CESNET"s}}));
-    ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::StaticData("ne:ctrl", "ne", {{"class", "iana-hardware:module"}}));
-    ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::Fans("ne:fans", "ne", fans, 4));
-    ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::SysfsTemperature("ne:ctrl:temperature-front", "ne:ctrl", sysfsTempFront, 1));
-    ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::SysfsTemperature("ne:ctrl:temperature-cpu", "ne:ctrl", sysfsTempCpu, 1));
-    ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::SysfsTemperature("ne:ctrl:temperature-internal-0", "ne:ctrl", sysfsTempMII0, 1));
-    ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::SysfsTemperature("ne:ctrl:temperature-internal-1", "ne:ctrl", sysfsTempMII1, 1));
-    ietfHardware->registerDataReader(velia::ietf_hardware::data_reader::EMMC("ne:ctrl:emmc", "ne:ctrl", emmc));
+    ietfHardware->registerDataReader(StaticData("ne", std::nullopt, {{"class", "iana-hardware:chassis"}, {"mfg-name", "CESNET"s}}));
+    ietfHardware->registerDataReader(StaticData("ne:ctrl", "ne", {{"class", "iana-hardware:module"}}));
+    ietfHardware->registerDataReader(Fans("ne:fans", "ne", fans, 4));
+    ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-front", "ne:ctrl", sysfsTempFront, 1));
+    ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-cpu", "ne:ctrl", sysfsTempCpu, 1));
+    ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-internal-0", "ne:ctrl", sysfsTempMII0, 1));
+    ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-internal-1", "ne:ctrl", sysfsTempMII1, 1));
+    ietfHardware->registerDataReader(EMMC("ne:ctrl:emmc", "ne:ctrl", emmc));
 
     SECTION("Test HardwareState without sysrepo")
     {