blob: 1bb250f66c45658cb75cdd1457267da3f4492c9f [file] [log] [blame]
Szymon Dompkef763c9e2021-03-12 09:19:22 +01001#include "discrete_threshold.hpp"
2
Szymon Dompkeaa572362022-03-23 16:31:24 +01003#include "utils/conversion_trigger.hpp"
4
Szymon Dompkef763c9e2021-03-12 09:19:22 +01005#include <phosphor-logging/log.hpp>
6
7DiscreteThreshold::DiscreteThreshold(
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +00008 boost::asio::io_context& ioc, Sensors sensorsIn,
Szymon Dompkef763c9e2021-03-12 09:19:22 +01009 std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn,
Szymon Dompkeaa572362022-03-23 16:31:24 +010010 Milliseconds dwellTimeIn, const std::string& thresholdValueIn,
Szymon Dompke94f71c52021-12-10 07:16:33 +010011 const std::string& nameIn, const discrete::Severity severityIn) :
Szymon Dompkef763c9e2021-03-12 09:19:22 +010012 ioc(ioc),
Szymon Dompke94f71c52021-12-10 07:16:33 +010013 actions(std::move(actionsIn)), dwellTime(dwellTimeIn),
Szymon Dompkeaa572362022-03-23 16:31:24 +010014 thresholdValue(thresholdValueIn),
15 numericThresholdValue(utils::stodStrict(thresholdValue)), name(nameIn),
16 severity(severityIn)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010017{
Szymon Dompke94f71c52021-12-10 07:16:33 +010018 for (const auto& sensor : sensorsIn)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010019 {
Szymon Dompke94f71c52021-12-10 07:16:33 +010020 sensorDetails.emplace(sensor, makeDetails(sensor->getName()));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010021 }
22}
23
24void DiscreteThreshold::initialize()
25{
Szymon Dompke94f71c52021-12-10 07:16:33 +010026 ThresholdOperations().initialize(this);
27}
28
29void DiscreteThreshold::updateSensors(Sensors newSensors)
30{
31 ThresholdOperations().updateSensors(this, std::move(newSensors));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010032}
33
34DiscreteThreshold::ThresholdDetail&
Szymon Dompke94f71c52021-12-10 07:16:33 +010035 DiscreteThreshold::getDetails(const interfaces::Sensor& sensor)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010036{
Szymon Dompke94f71c52021-12-10 07:16:33 +010037 return ThresholdOperations().getDetails(this, sensor);
38}
39
40std::shared_ptr<DiscreteThreshold::ThresholdDetail>
41 DiscreteThreshold::makeDetails(const std::string& sensorName)
42{
43 return std::make_shared<ThresholdDetail>(sensorName, false, ioc);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010044}
45
46void DiscreteThreshold::sensorUpdated(interfaces::Sensor& sensor,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010047 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010048{
Szymon Dompke94f71c52021-12-10 07:16:33 +010049 auto& details = getDetails(sensor);
50 auto& [sensorName, dwell, timer] = details;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010051
Szymon Dompkeaa572362022-03-23 16:31:24 +010052 if (dwell && value != numericThresholdValue)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010053 {
Szymon Dompkeaa572362022-03-23 16:31:24 +010054 timer.cancel();
55 dwell = false;
56 }
57 else if (value == numericThresholdValue)
58 {
59 startTimer(details, timestamp, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010060 }
61}
62
Szymon Dompke94f71c52021-12-10 07:16:33 +010063void DiscreteThreshold::startTimer(DiscreteThreshold::ThresholdDetail& details,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010064 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010065{
Szymon Dompke94f71c52021-12-10 07:16:33 +010066 const auto& sensorName = details.sensorName;
67 auto& dwell = details.dwell;
68 auto& timer = details.timer;
69
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000070 if (dwellTime == Milliseconds::zero())
Szymon Dompkef763c9e2021-03-12 09:19:22 +010071 {
72 commit(sensorName, timestamp, value);
73 }
74 else
75 {
76 dwell = true;
77 timer.expires_after(dwellTime);
Szymon Dompke94f71c52021-12-10 07:16:33 +010078 timer.async_wait([this, &sensorName, &dwell, timestamp,
Szymon Dompkef763c9e2021-03-12 09:19:22 +010079 value](const boost::system::error_code ec) {
80 if (ec)
81 {
82 phosphor::logging::log<phosphor::logging::level::DEBUG>(
83 "Timer has been canceled");
84 return;
85 }
Szymon Dompkef763c9e2021-03-12 09:19:22 +010086 commit(sensorName, timestamp, value);
87 dwell = false;
88 });
89 }
90}
91
92void DiscreteThreshold::commit(const std::string& sensorName,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010093 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010094{
95 for (const auto& action : actions)
96 {
97 action->commit(sensorName, timestamp, value);
98 }
99}
Szymon Dompke94f71c52021-12-10 07:16:33 +0100100
101LabeledThresholdParam DiscreteThreshold::getThresholdParam() const
102{
103 return discrete::LabeledThresholdParam(name, severity, dwellTime.count(),
Szymon Dompkeaa572362022-03-23 16:31:24 +0100104 thresholdValue);
Szymon Dompke94f71c52021-12-10 07:16:33 +0100105}