blob: 38820228cdeff87591545971678764a26c9cc50c [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,
Cezary Zwolaka4e67612021-02-18 13:16:16 +01005 std::unique_ptr<interfaces::JsonStorage> triggerStorageIn,
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01006 const std::shared_ptr<sdbusplus::asio::object_server>& objServer) :
Cezary Zwolaka4e67612021-02-18 13:16:16 +01007 triggerFactory(std::move(triggerFactoryIn)),
8 triggerStorage(std::move(triggerStorageIn))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01009{
10 managerIface = objServer->add_unique_interface(
11 triggerManagerPath, triggerManagerIfaceName, [this](auto& iface) {
12 iface.register_method(
13 "AddTrigger",
14 [this](
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010015 boost::asio::yield_context& yield, const std::string& name,
16 bool isDiscrete, bool logToJournal, bool logToRedfish,
17 bool updateReport,
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010018 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, Jozef1477fe62021-01-02 11:56:10 +010040 yield, name, isDiscrete, logToJournal, logToRedfish,
Cezary Zwolaka4e67612021-02-18 13:16:16 +010041 updateReport, sensors, reportNames, thresholds, *this,
42 *triggerStorage));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010043 return triggers.back()->getPath();
44 });
45 });
46}
47
48void 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}