Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 1 | #include "trigger_manager.hpp" |
| 2 | |
| 3 | TriggerManager::TriggerManager( |
| 4 | std::unique_ptr<interfaces::TriggerFactory> triggerFactoryIn, |
| 5 | const std::shared_ptr<sdbusplus::asio::object_server>& objServer) : |
| 6 | triggerFactory(std::move(triggerFactoryIn)) |
| 7 | { |
| 8 | managerIface = objServer->add_unique_interface( |
| 9 | triggerManagerPath, triggerManagerIfaceName, [this](auto& iface) { |
| 10 | iface.register_method( |
| 11 | "AddTrigger", |
| 12 | [this]( |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 13 | boost::asio::yield_context& yield, const std::string& name, |
| 14 | bool isDiscrete, bool logToJournal, bool logToRedfish, |
| 15 | bool updateReport, |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 16 | const std::vector<std::pair<sdbusplus::message::object_path, |
| 17 | std::string>>& sensors, |
| 18 | const std::vector<std::string>& reportNames, |
| 19 | const TriggerThresholdParams& thresholds) { |
| 20 | if (triggers.size() >= maxTriggers) |
| 21 | { |
| 22 | throw sdbusplus::exception::SdBusError( |
| 23 | static_cast<int>(std::errc::too_many_files_open), |
| 24 | "Reached maximal trigger count"); |
| 25 | } |
| 26 | |
| 27 | for (const auto& trigger : triggers) |
| 28 | { |
| 29 | if (trigger->getName() == name) |
| 30 | { |
| 31 | throw sdbusplus::exception::SdBusError( |
| 32 | static_cast<int>(std::errc::file_exists), |
| 33 | "Duplicate trigger"); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | triggers.emplace_back(triggerFactory->make( |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 38 | yield, name, isDiscrete, logToJournal, logToRedfish, |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 39 | updateReport, sensors, reportNames, thresholds, *this)); |
| 40 | return triggers.back()->getPath(); |
| 41 | }); |
| 42 | }); |
| 43 | } |
| 44 | |
| 45 | void TriggerManager::removeTrigger(const interfaces::Trigger* trigger) |
| 46 | { |
| 47 | triggers.erase( |
| 48 | std::remove_if(triggers.begin(), triggers.end(), |
| 49 | [trigger](const auto& x) { return trigger == x.get(); }), |
| 50 | triggers.end()); |
| 51 | } |