James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <Thresholds.hpp> |
| 4 | #include <sdbusplus/asio/object_server.hpp> |
| 5 | |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 6 | constexpr size_t sensorFailedPollTimeMs = 5000; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 7 | struct Sensor |
| 8 | { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 9 | Sensor(const std::string &name, const std::string &path, |
| 10 | std::vector<thresholds::Threshold> &&thresholdData, |
| 11 | const std::string &configurationPath, const std::string &objectType, |
| 12 | const double max, const double min) : |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 13 | name(name), |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 14 | path(path), thresholds(std::move(thresholdData)), |
| 15 | configurationPath(configurationPath), maxValue(max), minValue(min) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 16 | { |
| 17 | } |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 18 | virtual ~Sensor() = default; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 19 | virtual void checkThresholds(void) = 0; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 20 | std::string name; |
| 21 | std::string path; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 22 | std::string configurationPath; |
| 23 | std::string objectType; |
| 24 | double maxValue; |
| 25 | double minValue; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 26 | std::vector<thresholds::Threshold> thresholds; |
| 27 | std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface; |
| 28 | std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning; |
| 29 | std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical; |
| 30 | double value = std::numeric_limits<double>::quiet_NaN(); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 31 | double overriddenValue = std::numeric_limits<double>::quiet_NaN(); |
| 32 | bool internalSet = false; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 33 | |
| 34 | int setSensorValue(const double &newValue, double &oldValue) |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 35 | { |
| 36 | if (internalSet) |
| 37 | { |
| 38 | internalSet = false; |
| 39 | oldValue = newValue; |
| 40 | return 1; |
| 41 | } |
| 42 | overriddenValue = newValue; |
| 43 | return 1; |
| 44 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 45 | |
| 46 | void |
| 47 | setInitialProperties(std::shared_ptr<sdbusplus::asio::connection> &conn) |
| 48 | { |
| 49 | sensorInterface->register_property("MaxValue", maxValue); |
| 50 | sensorInterface->register_property("MinValue", minValue); |
| 51 | sensorInterface->register_property( |
| 52 | "Value", value, [&](const double &newValue, double &oldValue) { |
| 53 | return setSensorValue(newValue, oldValue); |
| 54 | }); |
| 55 | |
| 56 | for (auto &threshold : thresholds) |
| 57 | { |
| 58 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 59 | std::string level; |
| 60 | std::string alarm; |
| 61 | if (threshold.level == thresholds::Level::CRITICAL) |
| 62 | { |
| 63 | iface = thresholdInterfaceCritical; |
| 64 | if (threshold.direction == thresholds::Direction::HIGH) |
| 65 | { |
| 66 | level = "CriticalHigh"; |
| 67 | alarm = "CriticalAlarmHigh"; |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | level = "CriticalLow"; |
| 72 | alarm = "CriticalAlarmLow"; |
| 73 | } |
| 74 | } |
| 75 | else if (threshold.level == thresholds::Level::WARNING) |
| 76 | { |
| 77 | iface = thresholdInterfaceWarning; |
| 78 | if (threshold.direction == thresholds::Direction::HIGH) |
| 79 | { |
| 80 | level = "WarningHigh"; |
| 81 | alarm = "WarningAlarmHigh"; |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | level = "WarningLow"; |
| 86 | alarm = "WarningAlarmLow"; |
| 87 | } |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | std::cerr << "Unknown threshold level" << threshold.level |
| 92 | << "\n"; |
| 93 | continue; |
| 94 | } |
| 95 | if (!iface) |
| 96 | { |
| 97 | std::cout << "trying to set uninitialized interface\n"; |
| 98 | continue; |
| 99 | } |
| 100 | iface->register_property( |
| 101 | level, threshold.value, |
| 102 | [&](const double &request, double &oldValue) { |
| 103 | oldValue = request; // todo, just let the config do this? |
| 104 | threshold.value = request; |
| 105 | thresholds::persistThreshold(configurationPath, objectType, |
| 106 | threshold, conn); |
| 107 | return 1; |
| 108 | }); |
| 109 | iface->register_property(alarm, false); |
| 110 | } |
| 111 | if (!sensorInterface->initialize()) |
| 112 | { |
| 113 | std::cerr << "error initializing value interface\n"; |
| 114 | } |
| 115 | if (thresholdInterfaceWarning && |
| 116 | !thresholdInterfaceWarning->initialize()) |
| 117 | { |
| 118 | std::cerr << "error initializing warning threshold interface\n"; |
| 119 | } |
| 120 | |
| 121 | if (thresholdInterfaceCritical && |
| 122 | !thresholdInterfaceCritical->initialize()) |
| 123 | { |
| 124 | std::cerr << "error initializing critical threshold interface\n"; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void updateValue(const double &newValue) |
| 129 | { |
| 130 | // Indicate that it is internal set call |
| 131 | internalSet = true; |
| 132 | sensorInterface->set_property("Value", newValue); |
| 133 | internalSet = false; |
| 134 | value = newValue; |
| 135 | checkThresholds(); |
| 136 | } |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 137 | }; |