blob: d8484c29aeccbfbd88df8123a3ea6c965b9684d6 [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);
37 for (const auto& [type, dwellTime, direction, value] : params)
38 {
39 std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010040 if (logToJournal)
41 {
42 actions.emplace_back(std::make_unique<action::LogToJournal>(
43 numeric::toType(type), value));
44 }
45 if (logToRedfish)
46 {
47 actions.emplace_back(std::make_unique<action::LogToRedfish>(
48 numeric::toType(type), value));
49 }
50 if (updateReport)
51 {
52 actions.emplace_back(std::make_unique<action::UpdateReport>(
53 reportManager, reportNames));
54 }
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010055
56 thresholds.emplace_back(std::make_shared<NumericThreshold>(
57 bus->get_io_context(), sensors, sensorNames, std::move(actions),
58 std::chrono::milliseconds(dwellTime),
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010059 numeric::toDirection(direction), value));
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010060 }
61
62 return std::make_unique<Trigger>(
63 bus->get_io_context(), objServer, name, isDiscrete, logToJournal,
64 logToRedfish, updateReport, sensorPaths, reportNames, thresholdParams,
65 std::move(thresholds), triggerManager);
66}
67
68std::pair<std::vector<std::shared_ptr<interfaces::Sensor>>,
69 std::vector<std::string>>
70 TriggerFactory::getSensors(
71 boost::asio::yield_context& yield,
72 const std::vector<std::pair<sdbusplus::message::object_path,
73 std::string>>& sensorPaths) const
74{
75 auto tree = utils::getSubTreeSensors(yield, bus);
76
77 std::vector<std::shared_ptr<interfaces::Sensor>> sensors;
78 std::vector<std::string> sensorNames;
79 for (const auto& [sensorPath, metadata] : sensorPaths)
80 {
81 auto found = std::find_if(
82 tree.begin(), tree.end(),
83 [&sensorPath](const auto& x) { return x.first == sensorPath; });
84 if (found == tree.end())
85 {
86 throw std::runtime_error("Not found");
87 }
88
89 const auto& service = found->second[0].first;
90 const auto& path = found->first;
91 sensors.emplace_back(sensorCache.makeSensor<Sensor>(
92 service, path, bus->get_io_context(), bus));
93 if (metadata.empty())
94 {
95 sensorNames.emplace_back(sensorPath);
96 }
97 else
98 {
99 sensorNames.emplace_back(metadata);
100 }
101 }
102 return {sensors, sensorNames};
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100103}