blob: a6bf2b12e7bb1bb1c8a970144b5b4076c6480d16 [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 Feistd8705872019-02-08 13:26:09 -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,
James Feistce3fca42018-11-21 12:58:24 -080012 const double max, const double min) :
James Feistdc6c55f2018-10-31 12:53:20 -070013 name(name),
James Feista222ba72019-03-01 15:57:51 -080014 path(path), configurationPath(configurationPath),
15 objectType(objectType), thresholds(std::move(thresholdData)),
16 maxValue(max), minValue(min)
James Feistdc6c55f2018-10-31 12:53:20 -070017 {
18 }
James Feist8fd8a582018-11-16 11:10:46 -080019 virtual ~Sensor() = default;
James Feistce3fca42018-11-21 12:58:24 -080020 virtual void checkThresholds(void) = 0;
James Feistdc6c55f2018-10-31 12:53:20 -070021 std::string name;
22 std::string path;
James Feistce3fca42018-11-21 12:58:24 -080023 std::string configurationPath;
24 std::string objectType;
25 double maxValue;
26 double minValue;
James Feist8fd8a582018-11-16 11:10:46 -080027 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 Feist078f2322019-03-08 11:09:05 -080031 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
James Feist8fd8a582018-11-16 11:10:46 -080032 double value = std::numeric_limits<double>::quiet_NaN();
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053033 double overriddenValue = std::numeric_limits<double>::quiet_NaN();
Richard Marian Thomaiyarb2eb29a2018-12-08 18:26:58 +053034 bool overridenState = false;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053035 bool internalSet = false;
James Feistce3fca42018-11-21 12:58:24 -080036
James Feistd8705872019-02-08 13:26:09 -080037 int setSensorValue(const double& newValue, double& oldValue)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053038 {
39 if (internalSet)
40 {
41 internalSet = false;
42 oldValue = newValue;
43 return 1;
44 }
45 overriddenValue = newValue;
Richard Marian Thomaiyarb2eb29a2018-12-08 18:26:58 +053046 overridenState = true;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053047 return 1;
48 }
James Feistce3fca42018-11-21 12:58:24 -080049
50 void
James Feistd8705872019-02-08 13:26:09 -080051 setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn)
James Feistce3fca42018-11-21 12:58:24 -080052 {
James Feist078f2322019-03-08 11:09:05 -080053 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 Feistce3fca42018-11-21 12:58:24 -080064 sensorInterface->register_property("MaxValue", maxValue);
65 sensorInterface->register_property("MinValue", minValue);
66 sensorInterface->register_property(
James Feistd8705872019-02-08 13:26:09 -080067 "Value", value, [&](const double& newValue, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -080068 return setSensorValue(newValue, oldValue);
69 });
70
James Feistd8705872019-02-08 13:26:09 -080071 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -080072 {
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 Feistd8705872019-02-08 13:26:09 -0800117 [&](const double& request, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800118 oldValue = request; // todo, just let the config do this?
119 threshold.value = request;
120 thresholds::persistThreshold(configurationPath, objectType,
James Feista222ba72019-03-01 15:57:51 -0800121 threshold, conn,
122 thresholds.size());
James Feistce3fca42018-11-21 12:58:24 -0800123 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 Feistd8705872019-02-08 13:26:09 -0800144 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800145 {
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 Feist8fd8a582018-11-16 11:10:46 -0800153};