blob: cc164e4fd89272ea8191d511a1a61483198d26f8 [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](
13 const std::string& name, bool isDiscrete, bool logToJournal,
14 bool logToRedfish, bool updateReport,
15 const std::vector<std::pair<sdbusplus::message::object_path,
16 std::string>>& sensors,
17 const std::vector<std::string>& reportNames,
18 const TriggerThresholdParams& thresholds) {
19 if (triggers.size() >= maxTriggers)
20 {
21 throw sdbusplus::exception::SdBusError(
22 static_cast<int>(std::errc::too_many_files_open),
23 "Reached maximal trigger count");
24 }
25
26 for (const auto& trigger : triggers)
27 {
28 if (trigger->getName() == name)
29 {
30 throw sdbusplus::exception::SdBusError(
31 static_cast<int>(std::errc::file_exists),
32 "Duplicate trigger");
33 }
34 }
35
36 triggers.emplace_back(triggerFactory->make(
37 name, isDiscrete, logToJournal, logToRedfish,
38 updateReport, sensors, reportNames, thresholds, *this));
39 return triggers.back()->getPath();
40 });
41 });
42}
43
44void TriggerManager::removeTrigger(const interfaces::Trigger* trigger)
45{
46 triggers.erase(
47 std::remove_if(triggers.begin(), triggers.end(),
48 [trigger](const auto& x) { return trigger == x.get(); }),
49 triggers.end());
50}