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