blob: 91958787ecbecae04c88cf979a760d30e1e48939 [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) :
Szymon Dompkef763c9e2021-03-12 09:19:22 +010015 ioc(ioc),
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020016 triggerId(triggerIdIn), actions(std::move(actionsIn)),
17 dwellTime(dwellTimeIn), thresholdValue(thresholdValueIn),
18 numericThresholdValue(utils::stodStrict(thresholdValue)),
19 severity(severityIn), name(getNonEmptyName(nameIn)),
20 clock(std::move(clockIn))
Szymon Dompkef763c9e2021-03-12 09:19:22 +010021{
Szymon Dompke94f71c52021-12-10 07:16:33 +010022 for (const auto& sensor : sensorsIn)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010023 {
Szymon Dompke94f71c52021-12-10 07:16:33 +010024 sensorDetails.emplace(sensor, makeDetails(sensor->getName()));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010025 }
26}
27
28void DiscreteThreshold::initialize()
29{
Krzysztof Grobelny41fa80d2022-06-09 13:27:16 +020030 ThresholdOperations::initialize(this);
Szymon Dompke94f71c52021-12-10 07:16:33 +010031}
32
33void DiscreteThreshold::updateSensors(Sensors newSensors)
34{
Krzysztof Grobelny41fa80d2022-06-09 13:27:16 +020035 ThresholdOperations::updateSensors(this, std::move(newSensors));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010036}
37
38DiscreteThreshold::ThresholdDetail&
Szymon Dompke94f71c52021-12-10 07:16:33 +010039 DiscreteThreshold::getDetails(const interfaces::Sensor& sensor)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010040{
Krzysztof Grobelny41fa80d2022-06-09 13:27:16 +020041 return ThresholdOperations::getDetails(this, sensor);
Szymon Dompke94f71c52021-12-10 07:16:33 +010042}
43
44std::shared_ptr<DiscreteThreshold::ThresholdDetail>
45 DiscreteThreshold::makeDetails(const std::string& sensorName)
46{
Szymon Dompke162b9762022-06-07 12:45:48 +020047 return std::make_shared<ThresholdDetail>(sensorName, ioc);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010048}
49
50void DiscreteThreshold::sensorUpdated(interfaces::Sensor& sensor,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010051 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010052{
Szymon Dompke94f71c52021-12-10 07:16:33 +010053 auto& details = getDetails(sensor);
Szymon Dompke162b9762022-06-07 12:45:48 +020054 auto& dwell = details.dwell;
55 auto& timer = details.timer;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010056
Szymon Dompkeaa572362022-03-23 16:31:24 +010057 if (dwell && value != numericThresholdValue)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010058 {
Szymon Dompkeaa572362022-03-23 16:31:24 +010059 timer.cancel();
60 dwell = false;
61 }
62 else if (value == numericThresholdValue)
63 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020064 startTimer(details, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010065 }
66}
67
Szymon Dompke94f71c52021-12-10 07:16:33 +010068void DiscreteThreshold::startTimer(DiscreteThreshold::ThresholdDetail& details,
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020069 double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010070{
Szymon Dompke162b9762022-06-07 12:45:48 +020071 auto& sensorName = details.getSensorName();
Szymon Dompke94f71c52021-12-10 07:16:33 +010072 auto& dwell = details.dwell;
73 auto& timer = details.timer;
74
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000075 if (dwellTime == Milliseconds::zero())
Szymon Dompkef763c9e2021-03-12 09:19:22 +010076 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020077 commit(sensorName, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010078 }
79 else
80 {
81 dwell = true;
82 timer.expires_after(dwellTime);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020083 timer.async_wait([this, &sensorName, &dwell,
Szymon Dompkef763c9e2021-03-12 09:19:22 +010084 value](const boost::system::error_code ec) {
85 if (ec)
86 {
87 phosphor::logging::log<phosphor::logging::level::DEBUG>(
88 "Timer has been canceled");
89 return;
90 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020091 commit(sensorName, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010092 dwell = false;
93 });
94 }
95}
96
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020097void DiscreteThreshold::commit(const std::string& sensorName, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010098{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020099 Milliseconds timestamp = clock->systemTimestamp();
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100100 for (const auto& action : actions)
101 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200102 action->commit(triggerId, std::cref(name), sensorName, timestamp,
103 thresholdValue);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100104 }
105}
Szymon Dompke94f71c52021-12-10 07:16:33 +0100106
107LabeledThresholdParam DiscreteThreshold::getThresholdParam() const
108{
109 return discrete::LabeledThresholdParam(name, severity, dwellTime.count(),
Szymon Dompkeaa572362022-03-23 16:31:24 +0100110 thresholdValue);
Szymon Dompke94f71c52021-12-10 07:16:33 +0100111}
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200112
113std::string DiscreteThreshold::getNonEmptyName(const std::string& nameIn) const
114{
115 if (nameIn.empty())
116 {
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000117 return std::string(utils::toShortEnum(utils::enumToString(severity))) +
118 " condition";
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200119 }
120 return nameIn;
121}