blob: 0d98c28c442cbbc752d45181fde36dd5800daa3c [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"
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +00004#include "utils/to_short_enum.hpp"
Szymon Dompkeaa572362022-03-23 16:31:24 +01005
Szymon Dompkef763c9e2021-03-12 09:19:22 +01006#include <phosphor-logging/log.hpp>
7
8DiscreteThreshold::DiscreteThreshold(
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +02009 boost::asio::io_context& ioc, const std::string& triggerIdIn,
10 Sensors sensorsIn,
Szymon Dompkef763c9e2021-03-12 09:19:22 +010011 std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn,
Szymon Dompkeaa572362022-03-23 16:31:24 +010012 Milliseconds dwellTimeIn, const std::string& thresholdValueIn,
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020013 const std::string& nameIn, const discrete::Severity severityIn,
14 std::unique_ptr<interfaces::Clock> clockIn) :
Patrick Williamsf535cad2024-08-16 15:21:20 -040015 ioc(ioc), triggerId(triggerIdIn), actions(std::move(actionsIn)),
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020016 dwellTime(dwellTimeIn), thresholdValue(thresholdValueIn),
17 numericThresholdValue(utils::stodStrict(thresholdValue)),
18 severity(severityIn), name(getNonEmptyName(nameIn)),
19 clock(std::move(clockIn))
Szymon Dompkef763c9e2021-03-12 09:19:22 +010020{
Szymon Dompke94f71c52021-12-10 07:16:33 +010021 for (const auto& sensor : sensorsIn)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010022 {
Szymon Dompke94f71c52021-12-10 07:16:33 +010023 sensorDetails.emplace(sensor, makeDetails(sensor->getName()));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010024 }
25}
26
27void DiscreteThreshold::initialize()
28{
Krzysztof Grobelny41fa80d2022-06-09 13:27:16 +020029 ThresholdOperations::initialize(this);
Szymon Dompke94f71c52021-12-10 07:16:33 +010030}
31
32void DiscreteThreshold::updateSensors(Sensors newSensors)
33{
Krzysztof Grobelny41fa80d2022-06-09 13:27:16 +020034 ThresholdOperations::updateSensors(this, std::move(newSensors));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010035}
36
37DiscreteThreshold::ThresholdDetail&
Szymon Dompke94f71c52021-12-10 07:16:33 +010038 DiscreteThreshold::getDetails(const interfaces::Sensor& sensor)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010039{
Krzysztof Grobelny41fa80d2022-06-09 13:27:16 +020040 return ThresholdOperations::getDetails(this, sensor);
Szymon Dompke94f71c52021-12-10 07:16:33 +010041}
42
43std::shared_ptr<DiscreteThreshold::ThresholdDetail>
44 DiscreteThreshold::makeDetails(const std::string& sensorName)
45{
Szymon Dompke162b9762022-06-07 12:45:48 +020046 return std::make_shared<ThresholdDetail>(sensorName, ioc);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010047}
48
49void DiscreteThreshold::sensorUpdated(interfaces::Sensor& sensor,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010050 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010051{
Szymon Dompke94f71c52021-12-10 07:16:33 +010052 auto& details = getDetails(sensor);
Szymon Dompke162b9762022-06-07 12:45:48 +020053 auto& dwell = details.dwell;
54 auto& timer = details.timer;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010055
Szymon Dompkeaa572362022-03-23 16:31:24 +010056 if (dwell && value != numericThresholdValue)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010057 {
Szymon Dompkeaa572362022-03-23 16:31:24 +010058 timer.cancel();
59 dwell = false;
60 }
61 else if (value == numericThresholdValue)
62 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020063 startTimer(details, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010064 }
65}
66
Szymon Dompke94f71c52021-12-10 07:16:33 +010067void DiscreteThreshold::startTimer(DiscreteThreshold::ThresholdDetail& details,
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020068 double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010069{
Szymon Dompke162b9762022-06-07 12:45:48 +020070 auto& sensorName = details.getSensorName();
Szymon Dompke94f71c52021-12-10 07:16:33 +010071 auto& dwell = details.dwell;
72 auto& timer = details.timer;
73
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000074 if (dwellTime == Milliseconds::zero())
Szymon Dompkef763c9e2021-03-12 09:19:22 +010075 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020076 commit(sensorName, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010077 }
78 else
79 {
80 dwell = true;
81 timer.expires_after(dwellTime);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020082 timer.async_wait([this, &sensorName, &dwell,
Szymon Dompkef763c9e2021-03-12 09:19:22 +010083 value](const boost::system::error_code ec) {
84 if (ec)
85 {
86 phosphor::logging::log<phosphor::logging::level::DEBUG>(
87 "Timer has been canceled");
88 return;
89 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020090 commit(sensorName, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010091 dwell = false;
92 });
93 }
94}
95
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020096void DiscreteThreshold::commit(const std::string& sensorName, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010097{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020098 Milliseconds timestamp = clock->systemTimestamp();
Szymon Dompkef763c9e2021-03-12 09:19:22 +010099 for (const auto& action : actions)
100 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200101 action->commit(triggerId, std::cref(name), sensorName, timestamp,
102 thresholdValue);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100103 }
104}
Szymon Dompke94f71c52021-12-10 07:16:33 +0100105
106LabeledThresholdParam DiscreteThreshold::getThresholdParam() const
107{
108 return discrete::LabeledThresholdParam(name, severity, dwellTime.count(),
Szymon Dompkeaa572362022-03-23 16:31:24 +0100109 thresholdValue);
Szymon Dompke94f71c52021-12-10 07:16:33 +0100110}
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200111
112std::string DiscreteThreshold::getNonEmptyName(const std::string& nameIn) const
113{
114 if (nameIn.empty())
115 {
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000116 return std::string(utils::toShortEnum(utils::enumToString(severity))) +
117 " condition";
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200118 }
119 return nameIn;
120}