blob: f333e6acf4d40858111220a473683e1b921bde18 [file] [log] [blame]
James Feist8fd8a582018-11-16 11:10:46 -08001#pragma once
2
3#include <Thresholds.hpp>
4#include <sdbusplus/asio/object_server.hpp>
5
James Feist1169eb42018-10-31 10:08:47 -07006constexpr size_t sensorFailedPollTimeMs = 5000;
James Feist8fd8a582018-11-16 11:10:46 -08007struct Sensor
8{
James Feistce3fca42018-11-21 12:58:24 -08009 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 Feistdc6c55f2018-10-31 12:53:20 -070013 name(name),
James Feistce3fca42018-11-21 12:58:24 -080014 path(path), thresholds(std::move(thresholdData)),
15 configurationPath(configurationPath), maxValue(max), minValue(min)
James Feistdc6c55f2018-10-31 12:53:20 -070016 {
17 }
James Feist8fd8a582018-11-16 11:10:46 -080018 virtual ~Sensor() = default;
James Feistce3fca42018-11-21 12:58:24 -080019 virtual void checkThresholds(void) = 0;
James Feistdc6c55f2018-10-31 12:53:20 -070020 std::string name;
21 std::string path;
James Feistce3fca42018-11-21 12:58:24 -080022 std::string configurationPath;
23 std::string objectType;
24 double maxValue;
25 double minValue;
James Feist8fd8a582018-11-16 11:10:46 -080026 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 Thomaiyar87219222018-11-06 20:25:38 +053031 double overriddenValue = std::numeric_limits<double>::quiet_NaN();
32 bool internalSet = false;
James Feistce3fca42018-11-21 12:58:24 -080033
34 int setSensorValue(const double &newValue, double &oldValue)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053035 {
36 if (internalSet)
37 {
38 internalSet = false;
39 oldValue = newValue;
40 return 1;
41 }
42 overriddenValue = newValue;
43 return 1;
44 }
James Feistce3fca42018-11-21 12:58:24 -080045
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 Feist8fd8a582018-11-16 11:10:46 -0800137};