blob: a2073b7b370ae76a5b5a32317eeff27116725261 [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)
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
52 if (thresholdValue)
53 {
54 if (dwell && value != thresholdValue)
55 {
56 timer.cancel();
57 dwell = false;
58 }
59 else if (value == thresholdValue)
60 {
Szymon Dompke94f71c52021-12-10 07:16:33 +010061 startTimer(details, timestamp, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010062 }
63 }
64}
65
Szymon Dompke94f71c52021-12-10 07:16:33 +010066void DiscreteThreshold::startTimer(DiscreteThreshold::ThresholdDetail& details,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010067 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010068{
Szymon Dompke94f71c52021-12-10 07:16:33 +010069 const auto& sensorName = details.sensorName;
70 auto& dwell = details.dwell;
71 auto& timer = details.timer;
72
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000073 if (dwellTime == Milliseconds::zero())
Szymon Dompkef763c9e2021-03-12 09:19:22 +010074 {
75 commit(sensorName, timestamp, value);
76 }
77 else
78 {
79 dwell = true;
80 timer.expires_after(dwellTime);
Szymon Dompke94f71c52021-12-10 07:16:33 +010081 timer.async_wait([this, &sensorName, &dwell, timestamp,
Szymon Dompkef763c9e2021-03-12 09:19:22 +010082 value](const boost::system::error_code ec) {
83 if (ec)
84 {
85 phosphor::logging::log<phosphor::logging::level::DEBUG>(
86 "Timer has been canceled");
87 return;
88 }
Szymon Dompkef763c9e2021-03-12 09:19:22 +010089 commit(sensorName, timestamp, value);
90 dwell = false;
91 });
92 }
93}
94
95void DiscreteThreshold::commit(const std::string& sensorName,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010096 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010097{
98 for (const auto& action : actions)
99 {
100 action->commit(sensorName, timestamp, value);
101 }
102}
Szymon Dompke94f71c52021-12-10 07:16:33 +0100103
104LabeledThresholdParam DiscreteThreshold::getThresholdParam() const
105{
106 return discrete::LabeledThresholdParam(name, severity, dwellTime.count(),
107 std::to_string(thresholdValue));
108}