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