blob: bf372c2cf67af5469f95c548a884f86664a59ad9 [file] [log] [blame]
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01001#include "trigger_manager.hpp"
2
3TriggerManager::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, Jozef1477fe62021-01-02 11:56:10 +010013 boost::asio::yield_context& yield, const std::string& name,
14 bool isDiscrete, bool logToJournal, bool logToRedfish,
15 bool updateReport,
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010016 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, Jozef1477fe62021-01-02 11:56:10 +010038 yield, name, isDiscrete, logToJournal, logToRedfish,
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010039 updateReport, sensors, reportNames, thresholds, *this));
40 return triggers.back()->getPath();
41 });
42 });
43}
44
45void 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}