Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 1 | #include "trigger_factory.hpp" |
| 2 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 3 | #include "discrete_threshold.hpp" |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 4 | #include "numeric_threshold.hpp" |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 5 | #include "on_change_threshold.hpp" |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 6 | #include "sensor.hpp" |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 7 | #include "trigger.hpp" |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 8 | #include "trigger_actions.hpp" |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 9 | #include "utils/dbus_mapper.hpp" |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 10 | #include "utils/transform.hpp" |
| 11 | |
| 12 | namespace ts = utils::tstring; |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 13 | |
| 14 | TriggerFactory::TriggerFactory( |
| 15 | std::shared_ptr<sdbusplus::asio::connection> bus, |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 16 | std::shared_ptr<sdbusplus::asio::object_server> objServer, |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 17 | SensorCache& sensorCache, interfaces::ReportManager& reportManager) : |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 18 | bus(std::move(bus)), |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 19 | objServer(std::move(objServer)), sensorCache(sensorCache), |
| 20 | reportManager(reportManager) |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 21 | {} |
| 22 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 23 | std::unique_ptr<interfaces::Trigger> TriggerFactory::make( |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 24 | const std::string& id, const std::string& name, |
| 25 | const std::vector<std::string>& triggerActionsIn, |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 26 | const std::vector<std::string>& reportNames, |
| 27 | interfaces::TriggerManager& triggerManager, |
| 28 | interfaces::JsonStorage& triggerStorage, |
| 29 | const LabeledTriggerThresholdParams& labeledThresholdParams, |
| 30 | const std::vector<LabeledSensorInfo>& labeledSensorsInfo) const |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 31 | { |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 32 | const auto& [sensors, sensorNames] = getSensors(labeledSensorsInfo); |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 33 | std::vector<TriggerAction> triggerActions; |
| 34 | std::transform(triggerActionsIn.begin(), triggerActionsIn.end(), |
| 35 | std::back_inserter(triggerActions), |
| 36 | [](auto& triggerActionStr) { |
| 37 | return stringToTriggerAction(triggerActionStr); |
| 38 | }); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 39 | std::vector<std::shared_ptr<interfaces::Threshold>> thresholds; |
| 40 | |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 41 | if (isTriggerThresholdDiscrete(labeledThresholdParams)) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 42 | { |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 43 | const auto& labeledDiscreteThresholdParams = |
| 44 | std::get<std::vector<discrete::LabeledThresholdParam>>( |
| 45 | labeledThresholdParams); |
| 46 | for (const auto& labeledThresholdParam : labeledDiscreteThresholdParams) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 47 | { |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 48 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actions; |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 49 | |
| 50 | std::string thresholdName = |
| 51 | labeledThresholdParam.at_label<ts::UserId>(); |
| 52 | discrete::Severity severity = |
| 53 | labeledThresholdParam.at_label<ts::Severity>(); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 54 | auto dwellTime = |
| 55 | Milliseconds(labeledThresholdParam.at_label<ts::DwellTime>()); |
Szymon Dompke | 9f34679 | 2021-07-14 21:07:11 +0200 | [diff] [blame] | 56 | std::string thresholdValue = |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 57 | labeledThresholdParam.at_label<ts::ThresholdValue>(); |
| 58 | |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 59 | action::discrete::fillActions(actions, triggerActions, severity, |
| 60 | reportManager, reportNames); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 61 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 62 | thresholds.emplace_back(std::make_shared<DiscreteThreshold>( |
| 63 | bus->get_io_context(), sensors, sensorNames, std::move(actions), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 64 | Milliseconds(dwellTime), std::stod(thresholdValue), |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 65 | thresholdName)); |
| 66 | } |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 67 | if (labeledDiscreteThresholdParams.empty()) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 68 | { |
| 69 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actions; |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 70 | action::discrete::onChange::fillActions(actions, triggerActions, |
| 71 | reportManager, reportNames); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 72 | |
| 73 | thresholds.emplace_back(std::make_shared<OnChangeThreshold>( |
| 74 | sensors, sensorNames, std::move(actions))); |
| 75 | } |
| 76 | } |
| 77 | else |
| 78 | { |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 79 | const auto& labeledNumericThresholdParams = |
| 80 | std::get<std::vector<numeric::LabeledThresholdParam>>( |
| 81 | labeledThresholdParams); |
| 82 | |
| 83 | for (const auto& labeledThresholdParam : labeledNumericThresholdParams) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 84 | { |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 85 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actions; |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 86 | auto type = labeledThresholdParam.at_label<ts::Type>(); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 87 | auto dwellTime = |
| 88 | Milliseconds(labeledThresholdParam.at_label<ts::DwellTime>()); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 89 | auto direction = labeledThresholdParam.at_label<ts::Direction>(); |
| 90 | auto thresholdValue = |
| 91 | double{labeledThresholdParam.at_label<ts::ThresholdValue>()}; |
| 92 | |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 93 | action::numeric::fillActions(actions, triggerActions, type, |
| 94 | thresholdValue, reportManager, |
| 95 | reportNames); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 96 | |
| 97 | thresholds.emplace_back(std::make_shared<NumericThreshold>( |
| 98 | bus->get_io_context(), sensors, sensorNames, std::move(actions), |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 99 | dwellTime, direction, thresholdValue)); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 100 | } |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | return std::make_unique<Trigger>( |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 104 | bus->get_io_context(), objServer, id, name, triggerActionsIn, |
| 105 | reportNames, labeledSensorsInfo, labeledThresholdParams, |
| 106 | std::move(thresholds), triggerManager, triggerStorage); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 109 | std::pair<Sensors, std::vector<std::string>> TriggerFactory::getSensors( |
| 110 | const std::vector<LabeledSensorInfo>& labeledSensorsInfo) const |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 111 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 112 | Sensors sensors; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 113 | std::vector<std::string> sensorNames; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 114 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 115 | for (const auto& labeledSensorInfo : labeledSensorsInfo) |
| 116 | { |
| 117 | const auto& service = labeledSensorInfo.at_label<ts::Service>(); |
| 118 | const auto& sensorPath = labeledSensorInfo.at_label<ts::SensorPath>(); |
| 119 | const auto& metadata = labeledSensorInfo.at_label<ts::SensorMetadata>(); |
| 120 | |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 121 | sensors.emplace_back(sensorCache.makeSensor<Sensor>( |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 122 | service, sensorPath, bus->get_io_context(), bus)); |
| 123 | |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 124 | if (metadata.empty()) |
| 125 | { |
| 126 | sensorNames.emplace_back(sensorPath); |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | sensorNames.emplace_back(metadata); |
| 131 | } |
| 132 | } |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 133 | |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 134 | return {sensors, sensorNames}; |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 135 | } |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 136 | |
| 137 | std::vector<LabeledSensorInfo> |
| 138 | TriggerFactory::getLabeledSensorsInfo(boost::asio::yield_context& yield, |
| 139 | const SensorsInfo& sensorsInfo) const |
| 140 | { |
| 141 | auto tree = utils::getSubTreeSensors(yield, bus); |
| 142 | |
| 143 | return utils::transform(sensorsInfo, [&tree](const auto& item) { |
| 144 | const auto& [sensorPath, metadata] = item; |
| 145 | auto found = std::find_if( |
| 146 | tree.begin(), tree.end(), |
| 147 | [&sensorPath](const auto& x) { return x.first == sensorPath; }); |
| 148 | |
| 149 | if (tree.end() != found) |
| 150 | { |
| 151 | const auto& [service, ifaces] = found->second.front(); |
| 152 | return LabeledSensorInfo(service, sensorPath, metadata); |
| 153 | } |
| 154 | throw std::runtime_error("Not found"); |
| 155 | }); |
| 156 | } |