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