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