blob: d7640f615eee79f520cddd4530e523b934c46e23 [file] [log] [blame]
Szymon Dompkef763c9e2021-03-12 09:19:22 +01001#include "discrete_threshold.hpp"
2
3#include <phosphor-logging/log.hpp>
4
5DiscreteThreshold::DiscreteThreshold(
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +00006 boost::asio::io_context& ioc, Sensors sensorsIn,
Szymon Dompkef763c9e2021-03-12 09:19:22 +01007 std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn,
Krzysztof Grobelnyfbeb5bf2022-01-03 09:41:29 +01008 Milliseconds dwellTimeIn, double thresholdValueIn,
Szymon Dompke94f71c52021-12-10 07:16:33 +01009 const std::string& nameIn, const discrete::Severity severityIn) :
Szymon Dompkef763c9e2021-03-12 09:19:22 +010010 ioc(ioc),
Szymon Dompke94f71c52021-12-10 07:16:33 +010011 actions(std::move(actionsIn)), dwellTime(dwellTimeIn),
12 thresholdValue(thresholdValueIn), name(nameIn), severity(severityIn)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010013{
Szymon Dompke94f71c52021-12-10 07:16:33 +010014 for (const auto& sensor : sensorsIn)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010015 {
Szymon Dompke94f71c52021-12-10 07:16:33 +010016 sensorDetails.emplace(sensor, makeDetails(sensor->getName()));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010017 }
18}
19
20void DiscreteThreshold::initialize()
21{
Szymon Dompke94f71c52021-12-10 07:16:33 +010022 ThresholdOperations().initialize(this);
23}
24
25void DiscreteThreshold::updateSensors(Sensors newSensors)
26{
27 ThresholdOperations().updateSensors(this, std::move(newSensors));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010028}
29
30DiscreteThreshold::ThresholdDetail&
Szymon Dompke94f71c52021-12-10 07:16:33 +010031 DiscreteThreshold::getDetails(const interfaces::Sensor& sensor)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010032{
Szymon Dompke94f71c52021-12-10 07:16:33 +010033 return ThresholdOperations().getDetails(this, sensor);
34}
35
36std::shared_ptr<DiscreteThreshold::ThresholdDetail>
37 DiscreteThreshold::makeDetails(const std::string& sensorName)
38{
39 return std::make_shared<ThresholdDetail>(sensorName, false, ioc);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010040}
41
42void DiscreteThreshold::sensorUpdated(interfaces::Sensor& sensor,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010043 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010044{
Szymon Dompke94f71c52021-12-10 07:16:33 +010045 auto& details = getDetails(sensor);
46 auto& [sensorName, dwell, timer] = details;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010047
48 if (thresholdValue)
49 {
50 if (dwell && value != thresholdValue)
51 {
52 timer.cancel();
53 dwell = false;
54 }
55 else if (value == thresholdValue)
56 {
Szymon Dompke94f71c52021-12-10 07:16:33 +010057 startTimer(details, timestamp, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010058 }
59 }
60}
61
Szymon Dompke94f71c52021-12-10 07:16:33 +010062void DiscreteThreshold::startTimer(DiscreteThreshold::ThresholdDetail& details,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010063 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010064{
Szymon Dompke94f71c52021-12-10 07:16:33 +010065 const auto& sensorName = details.sensorName;
66 auto& dwell = details.dwell;
67 auto& timer = details.timer;
68
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000069 if (dwellTime == Milliseconds::zero())
Szymon Dompkef763c9e2021-03-12 09:19:22 +010070 {
71 commit(sensorName, timestamp, value);
72 }
73 else
74 {
75 dwell = true;
76 timer.expires_after(dwellTime);
Szymon Dompke94f71c52021-12-10 07:16:33 +010077 timer.async_wait([this, &sensorName, &dwell, timestamp,
Szymon Dompkef763c9e2021-03-12 09:19:22 +010078 value](const boost::system::error_code ec) {
79 if (ec)
80 {
81 phosphor::logging::log<phosphor::logging::level::DEBUG>(
82 "Timer has been canceled");
83 return;
84 }
Szymon Dompkef763c9e2021-03-12 09:19:22 +010085 commit(sensorName, timestamp, value);
86 dwell = false;
87 });
88 }
89}
90
91void DiscreteThreshold::commit(const std::string& sensorName,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010092 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010093{
94 for (const auto& action : actions)
95 {
96 action->commit(sensorName, timestamp, value);
97 }
98}
Szymon Dompke94f71c52021-12-10 07:16:33 +010099
100LabeledThresholdParam DiscreteThreshold::getThresholdParam() const
101{
102 return discrete::LabeledThresholdParam(name, severity, dwellTime.count(),
103 std::to_string(thresholdValue));
104}