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