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