Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016-2020 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
Václav Kubernát | 069d3a9 | 2021-11-14 12:37:46 +0100 | [diff] [blame] | 8 | #include <chrono> |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 9 | #include <utility> |
| 10 | #include "IETFHardware.h" |
| 11 | #include "utils/log.h" |
| 12 | #include "utils/time.h" |
| 13 | |
| 14 | using namespace std::literals; |
| 15 | |
| 16 | namespace { |
| 17 | |
Tomáš Pecka | 83b62e1 | 2020-12-16 14:50:49 +0100 | [diff] [blame] | 18 | static const std::string ietfHardwareStatePrefix = "/ietf-hardware:hardware"; |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 19 | |
| 20 | /** @brief Constructs a full XPath for a specific component */ |
| 21 | std::string xpathForComponent(const std::string& componentName) |
| 22 | { |
| 23 | return ietfHardwareStatePrefix + "/component[name='" + componentName + "']/"; |
| 24 | } |
| 25 | |
| 26 | /** @brief Prefix all properties from values DataTree with a component name (calculated from @p componentName) and push them into the DataTree */ |
| 27 | void addComponent(velia::ietf_hardware::DataTree& res, const std::string& componentName, const std::optional<std::string>& parent, const velia::ietf_hardware::DataTree& values) |
| 28 | { |
| 29 | auto componentPrefix = xpathForComponent(componentName); |
| 30 | |
| 31 | if (parent) { |
| 32 | res[componentPrefix + "parent"] = *parent; |
| 33 | } |
| 34 | for (const auto& [k, v] : values) { |
| 35 | res[componentPrefix + k] = v; |
| 36 | } |
Tomáš Pecka | 7eb0c42 | 2023-04-21 15:36:33 +0200 | [diff] [blame] | 37 | |
| 38 | res[componentPrefix + "state/oper-state"] = "enabled"; |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Tomáš Pecka | 655062c | 2023-12-11 15:37:58 +0100 | [diff] [blame] | 41 | void writeSensorValue(velia::ietf_hardware::DataTree& res, const std::string& componentName, const std::string& value, const std::string& operStatus) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 42 | { |
| 43 | const auto componentPrefix = xpathForComponent(componentName); |
| 44 | res[componentPrefix + "sensor-data/value"] = value; |
Tomáš Pecka | 655062c | 2023-12-11 15:37:58 +0100 | [diff] [blame] | 45 | res[componentPrefix + "sensor-data/oper-status"] = operStatus; |
| 46 | } |
| 47 | |
| 48 | /** @brief Write a sensor-data @p value for a component @p componentName and push it into the @p res DataTree */ |
Tomáš Pecka | cd7f9cc | 2023-12-11 15:52:48 +0100 | [diff] [blame^] | 49 | void addSensorValue(velia::Log log, velia::ietf_hardware::DataTree& res, const std::string& componentName, int64_t value) |
Tomáš Pecka | 655062c | 2023-12-11 15:37:58 +0100 | [diff] [blame] | 50 | { |
Tomáš Pecka | cd7f9cc | 2023-12-11 15:52:48 +0100 | [diff] [blame^] | 51 | static constexpr const int64_t YANG_SENSOR_VALUE_MIN = -1'000'000'000; |
| 52 | static constexpr const int64_t YANG_SENSOR_VALUE_MAX = 1'000'000'000; |
| 53 | |
| 54 | // FIXME: Valid value range depends on sensor-type as well, see ietf-hardware's sensor-value type description |
| 55 | |
| 56 | if (value <= YANG_SENSOR_VALUE_MIN) { |
| 57 | log->error("Sensor's '{}' value '{}' underflow. Setting sensor as nonoperational.", componentName, value); |
| 58 | writeSensorValue(res, componentName, std::to_string(YANG_SENSOR_VALUE_MIN), "nonoperational"); |
| 59 | } else if (value >= YANG_SENSOR_VALUE_MAX) { |
| 60 | log->error("Sensor's '{}' value '{}' overflow. Setting sensor as nonoperational.", componentName, value); |
| 61 | writeSensorValue(res, componentName, std::to_string(YANG_SENSOR_VALUE_MAX), "nonoperational"); |
| 62 | } else { |
| 63 | writeSensorValue(res, componentName, std::to_string(value), "ok"); |
| 64 | } |
Tomáš Pecka | 655062c | 2023-12-11 15:37:58 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** @brief Write a sensor-data @p value for a component @p componentName and push it into the @p res DataTree */ |
Tomáš Pecka | cd7f9cc | 2023-12-11 15:52:48 +0100 | [diff] [blame^] | 68 | void addSensorValue(velia::Log, velia::ietf_hardware::DataTree& res, const std::string& componentName, const std::string& value) |
Tomáš Pecka | 655062c | 2023-12-11 15:37:58 +0100 | [diff] [blame] | 69 | { |
| 70 | // TODO: Perhaps we should check if the string value is conforming to sensor-value type |
| 71 | writeSensorValue(res, componentName, value, "ok"); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | namespace velia::ietf_hardware { |
| 76 | |
Tomáš Pecka | 0d8d8ee | 2023-05-10 12:22:58 +0200 | [diff] [blame] | 77 | IETFHardware::IETFHardware() |
| 78 | : m_log(spdlog::get("hardware")) |
| 79 | { |
| 80 | } |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 81 | |
| 82 | IETFHardware::~IETFHardware() = default; |
| 83 | |
Tomáš Pecka | 0d8d8ee | 2023-05-10 12:22:58 +0200 | [diff] [blame] | 84 | /** |
| 85 | * Calls individual registered data readers and processes information obtained from them. |
| 86 | * The sensor values are then passed to threshold watchers to detect if the new value violated a threshold. |
| 87 | * This function does not raise any alarms. It only returns the data tree *and* any changes in the threshold |
| 88 | * crossings (as a mapping from sensor XPath to threshold watcher State (@see Watcher)). |
| 89 | **/ |
| 90 | HardwareInfo IETFHardware::process() |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 91 | { |
Tomáš Pecka | 0d8d8ee | 2023-05-10 12:22:58 +0200 | [diff] [blame] | 92 | DataTree dataTree; |
| 93 | std::map<std::string, State> alarms; |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 94 | |
| 95 | for (auto& dataReader : m_callbacks) { |
Tomáš Pecka | 0d8d8ee | 2023-05-10 12:22:58 +0200 | [diff] [blame] | 96 | dataTree.merge(dataReader()); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Tomáš Pecka | 0d8d8ee | 2023-05-10 12:22:58 +0200 | [diff] [blame] | 99 | for (auto& [sensorXPath, thresholdsWatcher] : m_thresholdsWatchers) { |
| 100 | std::optional<int64_t> newValue; |
| 101 | |
| 102 | if (auto it = dataTree.find(sensorXPath); it != dataTree.end()) { |
| 103 | newValue = std::stoll(it->second); |
| 104 | } else { |
| 105 | newValue = std::nullopt; |
| 106 | } |
| 107 | |
| 108 | if (auto newState = thresholdsWatcher.update(newValue)) { |
| 109 | m_log->debug("threshold: {} {}", sensorXPath, *newState); |
| 110 | alarms.emplace(sensorXPath, *newState); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | dataTree[ietfHardwareStatePrefix + "/last-change"] = velia::utils::yangTimeFormat(std::chrono::system_clock::now()); |
| 115 | |
| 116 | return {dataTree, alarms}; |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Tomáš Pecka | d2322ad | 2023-05-11 16:20:38 +0200 | [diff] [blame] | 119 | std::vector<std::string> IETFHardware::sensorsXPaths() const |
| 120 | { |
| 121 | std::vector<std::string> res; |
| 122 | |
| 123 | for (const auto& [sensorXPath, thresholds] : m_thresholdsWatchers) { |
| 124 | res.emplace_back(sensorXPath); |
| 125 | } |
| 126 | |
| 127 | return res; |
| 128 | } |
| 129 | |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 130 | /** @brief A namespace containing predefined data readers for IETFHardware class. |
| 131 | * @see IETFHardware for more information |
| 132 | */ |
| 133 | namespace data_reader { |
| 134 | |
| 135 | DataReader::DataReader(std::string componentName, std::optional<std::string> parent) |
| 136 | : m_componentName(std::move(componentName)) |
| 137 | , m_parent(std::move(parent)) |
Tomáš Pecka | cd7f9cc | 2023-12-11 15:52:48 +0100 | [diff] [blame^] | 138 | , m_log(spdlog::get("hardware")) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 139 | { |
| 140 | } |
| 141 | |
| 142 | /** @brief Constructs a component without any sensor-data and provide only the static data @p dataTree passed via constructor. |
| 143 | * @param componentName the name of the component in the resulting tree |
| 144 | * @param parent The component in YANG model has a link to parent. Specify who is the parent. |
| 145 | * @param dataTree static data to insert into the resulting tree. The dataTree keys should only contain the YANG node name, not full XPath. The full XPath is constructed from @componentName and the map key. |
| 146 | */ |
| 147 | StaticData::StaticData(std::string componentName, std::optional<std::string> parent, DataTree dataTree) |
| 148 | : DataReader(std::move(componentName), std::move(parent)) |
| 149 | { |
| 150 | addComponent(m_staticData, |
| 151 | m_componentName, |
| 152 | m_parent, |
| 153 | dataTree); |
| 154 | } |
| 155 | |
| 156 | DataTree StaticData::operator()() const { return m_staticData; } |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 157 | ThresholdsBySensorPath StaticData::thresholds() const { return {}; } |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 158 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 159 | Fans::Fans(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, unsigned fanChannelsCount, Thresholds<int64_t> thresholds) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 160 | : DataReader(std::move(componentName), std::move(parent)) |
| 161 | , m_hwmon(std::move(hwmon)) |
| 162 | , m_fanChannelsCount(fanChannelsCount) |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 163 | , m_thresholds(std::move(thresholds)) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 164 | { |
| 165 | // fans |
| 166 | addComponent(m_staticData, |
| 167 | m_componentName, |
| 168 | m_parent, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 169 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 170 | {"class", "iana-hardware:module"}, // FIXME: Read (or pass via constructor) additional properties (mfg, model, ...). They should be in the fans' tray EEPROM. |
| 171 | }); |
| 172 | |
| 173 | for (unsigned i = 1; i <= m_fanChannelsCount; i++) { |
| 174 | // fans -> fan_i |
| 175 | addComponent(m_staticData, |
| 176 | m_componentName + ":fan" + std::to_string(i), |
| 177 | m_componentName, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 178 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 179 | {"class", "iana-hardware:fan"}, |
| 180 | }); |
| 181 | |
| 182 | // fans -> fan_i -> sensor-data |
| 183 | addComponent(m_staticData, |
| 184 | m_componentName + ":fan" + std::to_string(i) + ":rpm", |
| 185 | m_componentName + ":fan" + std::to_string(i), |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 186 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 187 | {"class", "iana-hardware:sensor"}, |
| 188 | {"sensor-data/value-type", "rpm"}, |
| 189 | {"sensor-data/value-scale", "units"}, |
| 190 | {"sensor-data/value-precision", "0"}, |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 191 | }); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | DataTree Fans::operator()() const |
| 196 | { |
| 197 | DataTree res(m_staticData); |
| 198 | |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 199 | for (unsigned i = 1; i <= m_fanChannelsCount; i++) { |
| 200 | const auto sensorComponentName = m_componentName + ":fan" + std::to_string(i) + ":rpm"; |
| 201 | const auto attribute = "fan"s + std::to_string(i) + "_input"; |
| 202 | |
Tomáš Pecka | cd7f9cc | 2023-12-11 15:52:48 +0100 | [diff] [blame^] | 203 | addSensorValue(m_log, res, sensorComponentName, m_hwmon->attribute(attribute)); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | return res; |
| 207 | } |
| 208 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 209 | ThresholdsBySensorPath Fans::thresholds() const |
| 210 | { |
| 211 | ThresholdsBySensorPath res; |
| 212 | |
| 213 | for (unsigned i = 1; i <= m_fanChannelsCount; i++) { |
| 214 | res.emplace(xpathForComponent(m_componentName + ":fan" + std::to_string(i) + ":rpm") + "sensor-data/value", m_thresholds); |
| 215 | } |
| 216 | |
| 217 | return res; |
| 218 | } |
| 219 | |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 220 | std::string getSysfsFilename(const SensorType type, int sysfsChannelNr) |
| 221 | { |
| 222 | switch (type) { |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 223 | case SensorType::Temperature: |
| 224 | return "temp"s + std::to_string(sysfsChannelNr) + "_input"; |
| 225 | case SensorType::Current: |
| 226 | return "curr"s + std::to_string(sysfsChannelNr) + "_input"; |
| 227 | case SensorType::Power: |
| 228 | return "power"s + std::to_string(sysfsChannelNr) + "_input"; |
| 229 | case SensorType::VoltageAC: |
| 230 | case SensorType::VoltageDC: |
| 231 | return "in"s + std::to_string(sysfsChannelNr) + "_input"; |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | __builtin_unreachable(); |
| 235 | } |
| 236 | |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 237 | template <SensorType TYPE> |
| 238 | const DataTree sysfsStaticData; |
| 239 | template <> |
| 240 | const DataTree sysfsStaticData<SensorType::Temperature> = { |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 241 | {"class", "iana-hardware:sensor"}, |
| 242 | {"sensor-data/value-type", "celsius"}, |
| 243 | {"sensor-data/value-scale", "milli"}, |
| 244 | {"sensor-data/value-precision", "0"}, |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 245 | }; |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 246 | template <> |
| 247 | const DataTree sysfsStaticData<SensorType::Current> = { |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 248 | {"class", "iana-hardware:sensor"}, |
| 249 | {"sensor-data/value-type", "amperes"}, |
| 250 | {"sensor-data/value-scale", "milli"}, |
| 251 | {"sensor-data/value-precision", "0"}, |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 252 | }; |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 253 | template <> |
| 254 | const DataTree sysfsStaticData<SensorType::Power> = { |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 255 | {"class", "iana-hardware:sensor"}, |
| 256 | {"sensor-data/value-type", "watts"}, |
| 257 | {"sensor-data/value-scale", "micro"}, |
| 258 | {"sensor-data/value-precision", "0"}, |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 259 | }; |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 260 | template <> |
| 261 | const DataTree sysfsStaticData<SensorType::VoltageAC> = { |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 262 | {"class", "iana-hardware:sensor"}, |
| 263 | {"sensor-data/value-type", "volts-AC"}, |
Tomáš Pecka | 7632360 | 2022-05-11 09:33:59 +0200 | [diff] [blame] | 264 | {"sensor-data/value-scale", "milli"}, |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 265 | {"sensor-data/value-precision", "0"}, |
Tomáš Pecka | 5f07eea | 2023-12-11 15:23:01 +0100 | [diff] [blame] | 266 | }; |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 267 | template <> |
| 268 | const DataTree sysfsStaticData<SensorType::VoltageDC> = { |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 269 | {"class", "iana-hardware:sensor"}, |
| 270 | {"sensor-data/value-type", "volts-DC"}, |
Tomáš Pecka | 7632360 | 2022-05-11 09:33:59 +0200 | [diff] [blame] | 271 | {"sensor-data/value-scale", "milli"}, |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 272 | {"sensor-data/value-precision", "0"}, |
Tomáš Pecka | 5f07eea | 2023-12-11 15:23:01 +0100 | [diff] [blame] | 273 | }; |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 274 | |
| 275 | template <SensorType TYPE> |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 276 | SysfsValue<TYPE>::SysfsValue(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, int sysfsChannelNr, Thresholds<int64_t> thresholds) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 277 | : DataReader(std::move(componentName), std::move(parent)) |
| 278 | , m_hwmon(std::move(hwmon)) |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 279 | , m_sysfsFile(getSysfsFilename(TYPE, sysfsChannelNr)) |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 280 | , m_thresholds(std::move(thresholds)) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 281 | { |
| 282 | addComponent(m_staticData, |
| 283 | m_componentName, |
| 284 | m_parent, |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 285 | sysfsStaticData<TYPE>); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 288 | template <SensorType TYPE> |
| 289 | DataTree SysfsValue<TYPE>::operator()() const |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 290 | { |
| 291 | DataTree res(m_staticData); |
| 292 | |
Václav Kubernát | 0dee6b9 | 2021-04-13 09:14:04 +0200 | [diff] [blame] | 293 | int64_t sensorValue = m_hwmon->attribute(m_sysfsFile); |
Tomáš Pecka | cd7f9cc | 2023-12-11 15:52:48 +0100 | [diff] [blame^] | 294 | addSensorValue(m_log, res, m_componentName, sensorValue); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 295 | |
| 296 | return res; |
| 297 | } |
| 298 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 299 | template <SensorType TYPE> |
| 300 | ThresholdsBySensorPath SysfsValue<TYPE>::thresholds() const |
| 301 | { |
| 302 | return {{xpathForComponent(m_componentName) + "sensor-data/value", m_thresholds}}; |
| 303 | } |
| 304 | |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 305 | template struct SysfsValue<SensorType::Current>; |
| 306 | template struct SysfsValue<SensorType::Power>; |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 307 | template struct SysfsValue<SensorType::Temperature>; |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 308 | template struct SysfsValue<SensorType::VoltageAC>; |
| 309 | template struct SysfsValue<SensorType::VoltageDC>; |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 310 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 311 | EMMC::EMMC(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::EMMC> emmc, Thresholds<int64_t> thresholds) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 312 | : DataReader(std::move(componentName), std::move(parent)) |
| 313 | , m_emmc(std::move(emmc)) |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 314 | , m_thresholds(std::move(thresholds)) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 315 | { |
| 316 | auto emmcAttrs = m_emmc->attributes(); |
| 317 | |
| 318 | // date is specified in MM/YYYY format (source: kernel core/mmc.c) and mfg-date is unfortunately of type yang:date-and-time |
| 319 | std::string mfgDate = emmcAttrs.at("date"); |
Tomáš Pecka | f1cba74 | 2023-05-03 16:26:37 +0200 | [diff] [blame] | 320 | std::chrono::year_month_day calendarDate( |
| 321 | std::chrono::year(std::stoi(mfgDate.substr(3, 4))), |
| 322 | std::chrono::month(std::stoi(mfgDate.substr(0, 2))), |
| 323 | std::chrono::day(1)); |
| 324 | mfgDate = velia::utils::yangTimeFormat(std::chrono::sys_days{calendarDate}); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 325 | |
| 326 | addComponent(m_staticData, |
| 327 | m_componentName, |
| 328 | m_parent, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 329 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 330 | {"class", "iana-hardware:module"}, |
| 331 | {"mfg-date", mfgDate}, |
| 332 | {"serial-num", emmcAttrs.at("serial")}, |
| 333 | {"model-name", emmcAttrs.at("name")}, |
| 334 | }); |
| 335 | |
| 336 | addComponent(m_staticData, |
| 337 | m_componentName + ":lifetime", |
| 338 | m_componentName, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 339 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 340 | {"class", "iana-hardware:sensor"}, |
| 341 | {"sensor-data/value-type", "other"}, |
| 342 | {"sensor-data/value-scale", "units"}, |
| 343 | {"sensor-data/value-precision", "0"}, |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 344 | {"sensor-data/units-display", "percent"s}, |
| 345 | }); |
| 346 | } |
| 347 | |
| 348 | DataTree EMMC::operator()() const |
| 349 | { |
| 350 | DataTree res(m_staticData); |
| 351 | |
| 352 | auto emmcAttrs = m_emmc->attributes(); |
Tomáš Pecka | cd7f9cc | 2023-12-11 15:52:48 +0100 | [diff] [blame^] | 353 | addSensorValue(m_log, res, m_componentName + ":lifetime", emmcAttrs.at("life_time")); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 354 | |
| 355 | return res; |
| 356 | } |
Tomáš Pecka | 2a4c9f6 | 2023-03-26 10:54:57 +0200 | [diff] [blame] | 357 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 358 | ThresholdsBySensorPath EMMC::thresholds() const |
Tomáš Pecka | 2a4c9f6 | 2023-03-26 10:54:57 +0200 | [diff] [blame] | 359 | { |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 360 | return {{xpathForComponent(m_componentName + ":lifetime") + "sensor-data/value", m_thresholds}}; |
Tomáš Pecka | 2a4c9f6 | 2023-03-26 10:54:57 +0200 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | DataTree Group::operator()() const |
| 364 | { |
| 365 | DataTree res; |
| 366 | for (const auto& reader : m_readers) { |
| 367 | res.merge(reader()); |
| 368 | } |
| 369 | return res; |
| 370 | } |
| 371 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 372 | ThresholdsBySensorPath Group::thresholds() const |
| 373 | { |
| 374 | return m_thresholds; |
| 375 | } |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 376 | } |
| 377 | } |