blob: 7953cd913bcf7e16687d699df8a54965db4789c6 [file] [log] [blame]
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01001#include "trigger_factory.hpp"
2
Wludzik, Jozef1477fe62021-01-02 11:56:10 +01003#include "numeric_threshold.hpp"
4#include "sensor.hpp"
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01005#include "trigger.hpp"
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +01006#include "trigger_actions.hpp"
Wludzik, Jozef1477fe62021-01-02 11:56:10 +01007#include "utils/dbus_mapper.hpp"
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01008
9TriggerFactory::TriggerFactory(
10 std::shared_ptr<sdbusplus::asio::connection> bus,
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010011 std::shared_ptr<sdbusplus::asio::object_server> objServer,
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010012 SensorCache& sensorCache, interfaces::ReportManager& reportManager) :
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010013 bus(std::move(bus)),
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010014 objServer(std::move(objServer)), sensorCache(sensorCache),
15 reportManager(reportManager)
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010016{}
17
18std::unique_ptr<interfaces::Trigger> TriggerFactory::make(
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010019 boost::asio::yield_context& yield, const std::string& name, bool isDiscrete,
20 bool logToJournal, bool logToRedfish, bool updateReport,
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010021 const std::vector<std::pair<sdbusplus::message::object_path, std::string>>&
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010022 sensorPaths,
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010023 const std::vector<std::string>& reportNames,
24 const TriggerThresholdParams& thresholdParams,
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010025 interfaces::TriggerManager& triggerManager) const
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010026{
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010027 if (isDiscrete)
28 {
29 throw std::runtime_error("Not implemented!");
30 }
31
32 auto [sensors, sensorNames] = getSensors(yield, sensorPaths);
33 std::vector<std::shared_ptr<interfaces::Threshold>> thresholds;
34
35 const auto& params =
36 std::get<std::vector<numeric::ThresholdParam>>(thresholdParams);
Wludzik, Jozef9f145912021-02-11 08:54:10 +010037 for (const auto& [typeStr, dwellTime, directionStr, value] : params)
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010038 {
Wludzik, Jozef9f145912021-02-11 08:54:10 +010039 numeric::Type type = numeric::stringToType(typeStr);
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010040 std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010041 if (logToJournal)
42 {
Wludzik, Jozef9f145912021-02-11 08:54:10 +010043 actions.emplace_back(
44 std::make_unique<action::LogToJournal>(type, value));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010045 }
46 if (logToRedfish)
47 {
Wludzik, Jozef9f145912021-02-11 08:54:10 +010048 actions.emplace_back(
49 std::make_unique<action::LogToRedfish>(type, value));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010050 }
51 if (updateReport)
52 {
53 actions.emplace_back(std::make_unique<action::UpdateReport>(
54 reportManager, reportNames));
55 }
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010056
57 thresholds.emplace_back(std::make_shared<NumericThreshold>(
58 bus->get_io_context(), sensors, sensorNames, std::move(actions),
59 std::chrono::milliseconds(dwellTime),
Wludzik, Jozef9f145912021-02-11 08:54:10 +010060 numeric::stringToDirection(directionStr), value));
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010061 }
62
63 return std::make_unique<Trigger>(
64 bus->get_io_context(), objServer, name, isDiscrete, logToJournal,
65 logToRedfish, updateReport, sensorPaths, reportNames, thresholdParams,
66 std::move(thresholds), triggerManager);
67}
68
69std::pair<std::vector<std::shared_ptr<interfaces::Sensor>>,
70 std::vector<std::string>>
71 TriggerFactory::getSensors(
72 boost::asio::yield_context& yield,
73 const std::vector<std::pair<sdbusplus::message::object_path,
74 std::string>>& sensorPaths) const
75{
76 auto tree = utils::getSubTreeSensors(yield, bus);
77
78 std::vector<std::shared_ptr<interfaces::Sensor>> sensors;
79 std::vector<std::string> sensorNames;
80 for (const auto& [sensorPath, metadata] : sensorPaths)
81 {
82 auto found = std::find_if(
83 tree.begin(), tree.end(),
84 [&sensorPath](const auto& x) { return x.first == sensorPath; });
85 if (found == tree.end())
86 {
87 throw std::runtime_error("Not found");
88 }
89
90 const auto& service = found->second[0].first;
91 const auto& path = found->first;
92 sensors.emplace_back(sensorCache.makeSensor<Sensor>(
93 service, path, bus->get_io_context(), bus));
94 if (metadata.empty())
95 {
96 sensorNames.emplace_back(sensorPath);
97 }
98 else
99 {
100 sensorNames.emplace_back(metadata);
101 }
102 }
103 return {sensors, sensorNames};
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100104}