Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 1 | #include "trigger_factory.hpp" |
| 2 | |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 3 | #include "numeric_threshold.hpp" |
| 4 | #include "sensor.hpp" |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 5 | #include "trigger.hpp" |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 6 | #include "trigger_actions.hpp" |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 7 | #include "utils/dbus_mapper.hpp" |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 8 | |
| 9 | TriggerFactory::TriggerFactory( |
| 10 | std::shared_ptr<sdbusplus::asio::connection> bus, |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 11 | std::shared_ptr<sdbusplus::asio::object_server> objServer, |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 12 | SensorCache& sensorCache, interfaces::ReportManager& reportManager) : |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 13 | bus(std::move(bus)), |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 14 | objServer(std::move(objServer)), sensorCache(sensorCache), |
| 15 | reportManager(reportManager) |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 16 | {} |
| 17 | |
| 18 | std::unique_ptr<interfaces::Trigger> TriggerFactory::make( |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 19 | boost::asio::yield_context& yield, const std::string& name, bool isDiscrete, |
| 20 | bool logToJournal, bool logToRedfish, bool updateReport, |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 21 | const std::vector<std::pair<sdbusplus::message::object_path, std::string>>& |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 22 | sensorPaths, |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 23 | const std::vector<std::string>& reportNames, |
| 24 | const TriggerThresholdParams& thresholdParams, |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 25 | interfaces::TriggerManager& triggerManager) const |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 26 | { |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 27 | 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, Jozef | 9f14591 | 2021-02-11 08:54:10 +0100 | [diff] [blame] | 37 | for (const auto& [typeStr, dwellTime, directionStr, value] : params) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 38 | { |
Wludzik, Jozef | 9f14591 | 2021-02-11 08:54:10 +0100 | [diff] [blame] | 39 | numeric::Type type = numeric::stringToType(typeStr); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 40 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actions; |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 41 | if (logToJournal) |
| 42 | { |
Wludzik, Jozef | 9f14591 | 2021-02-11 08:54:10 +0100 | [diff] [blame] | 43 | actions.emplace_back( |
| 44 | std::make_unique<action::LogToJournal>(type, value)); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 45 | } |
| 46 | if (logToRedfish) |
| 47 | { |
Wludzik, Jozef | 9f14591 | 2021-02-11 08:54:10 +0100 | [diff] [blame] | 48 | actions.emplace_back( |
| 49 | std::make_unique<action::LogToRedfish>(type, value)); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 50 | } |
| 51 | if (updateReport) |
| 52 | { |
| 53 | actions.emplace_back(std::make_unique<action::UpdateReport>( |
| 54 | reportManager, reportNames)); |
| 55 | } |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 56 | |
| 57 | thresholds.emplace_back(std::make_shared<NumericThreshold>( |
| 58 | bus->get_io_context(), sensors, sensorNames, std::move(actions), |
| 59 | std::chrono::milliseconds(dwellTime), |
Wludzik, Jozef | 9f14591 | 2021-02-11 08:54:10 +0100 | [diff] [blame] | 60 | numeric::stringToDirection(directionStr), value)); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 61 | } |
| 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 | |
| 69 | std::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, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 104 | } |