blob: f152e7600bac01c12aa3462fec9630109d8ebe26 [file] [log] [blame]
Wludzik, Jozef1477fe62021-01-02 11:56:10 +01001#include "numeric_threshold.hpp"
2
3#include <phosphor-logging/log.hpp>
4
5NumericThreshold::NumericThreshold(
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +00006 boost::asio::io_context& ioc, Sensors sensorsIn,
Wludzik, Jozef1477fe62021-01-02 11:56:10 +01007 std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn,
Szymon Dompke94f71c52021-12-10 07:16:33 +01008 Milliseconds dwellTimeIn, numeric::Direction directionIn,
9 double thresholdValueIn, numeric::Type typeIn) :
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010010 ioc(ioc),
Szymon Dompke94f71c52021-12-10 07:16:33 +010011 actions(std::move(actionsIn)), dwellTime(dwellTimeIn),
12 direction(directionIn), thresholdValue(thresholdValueIn), type(typeIn)
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010013{
Szymon Dompke94f71c52021-12-10 07:16:33 +010014 for (const auto& sensor : sensorsIn)
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010015 {
Szymon Dompke94f71c52021-12-10 07:16:33 +010016 sensorDetails.emplace(sensor, makeDetails(sensor->getName()));
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010017 }
18}
19
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010020void NumericThreshold::initialize()
21{
Szymon Dompke94f71c52021-12-10 07:16:33 +010022 ThresholdOperations().initialize(this);
23}
24
25void NumericThreshold::updateSensors(Sensors newSensors)
26{
27 ThresholdOperations().updateSensors(this, std::move(newSensors));
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010028}
29
30NumericThreshold::ThresholdDetail&
Szymon Dompke94f71c52021-12-10 07:16:33 +010031 NumericThreshold::getDetails(const interfaces::Sensor& sensor)
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010032{
Szymon Dompke94f71c52021-12-10 07:16:33 +010033 return ThresholdOperations().getDetails(this, sensor);
34}
35
36std::shared_ptr<NumericThreshold::ThresholdDetail>
37 NumericThreshold::makeDetails(const std::string& sensorName)
38{
39 return std::make_shared<ThresholdDetail>(sensorName, thresholdValue, false,
40 ioc);
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010041}
42
43void NumericThreshold::sensorUpdated(interfaces::Sensor& sensor,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010044 Milliseconds timestamp)
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010045{}
46
47void NumericThreshold::sensorUpdated(interfaces::Sensor& sensor,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010048 Milliseconds timestamp, double value)
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010049{
Szymon Dompke94f71c52021-12-10 07:16:33 +010050 auto& details = getDetails(sensor);
51 auto& [sensorName, prevValue, dwell, timer] = details;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010052 bool decreasing = thresholdValue < prevValue && thresholdValue > value;
53 bool increasing = thresholdValue > prevValue && thresholdValue < value;
54
55 if (dwell && (increasing || decreasing))
56 {
57 timer.cancel();
58 dwell = false;
59 }
60 if ((direction == numeric::Direction::decreasing && decreasing) ||
61 (direction == numeric::Direction::increasing && increasing) ||
62 (direction == numeric::Direction::either && (increasing || decreasing)))
63 {
Szymon Dompke94f71c52021-12-10 07:16:33 +010064 startTimer(details, timestamp, value);
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010065 }
66
67 prevValue = value;
68}
69
Szymon Dompke94f71c52021-12-10 07:16:33 +010070void NumericThreshold::startTimer(NumericThreshold::ThresholdDetail& details,
71 Milliseconds timestamp, double value)
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010072{
Szymon Dompke94f71c52021-12-10 07:16:33 +010073 const auto& sensorName = details.sensorName;
74 auto& dwell = details.dwell;
75 auto& timer = details.timer;
76
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000077 if (dwellTime == Milliseconds::zero())
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010078 {
79 commit(sensorName, timestamp, value);
80 }
81 else
82 {
83 dwell = true;
84 timer.expires_after(dwellTime);
Szymon Dompke94f71c52021-12-10 07:16:33 +010085 timer.async_wait([this, &sensorName, &dwell, timestamp,
86 value](const boost::system::error_code ec) {
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010087 if (ec)
88 {
89 phosphor::logging::log<phosphor::logging::level::DEBUG>(
90 "Timer has been canceled");
91 return;
92 }
93 commit(sensorName, timestamp, value);
94 dwell = false;
95 });
96 }
97}
98
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010099void NumericThreshold::commit(const std::string& sensorName,
100 Milliseconds timestamp, double value)
Wludzik, Jozef1477fe62021-01-02 11:56:10 +0100101{
102 for (const auto& action : actions)
103 {
104 action->commit(sensorName, timestamp, value);
105 }
106}
Szymon Dompke94f71c52021-12-10 07:16:33 +0100107
108LabeledThresholdParam NumericThreshold::getThresholdParam() const
109{
110 return numeric::LabeledThresholdParam(type, dwellTime.count(), direction,
111 thresholdValue);
112}