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" |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 9 | #include "utils/clock.hpp" |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 10 | #include "utils/dbus_mapper.hpp" |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 11 | #include "utils/transform.hpp" |
| 12 | |
| 13 | namespace ts = utils::tstring; |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 14 | |
| 15 | TriggerFactory::TriggerFactory( |
| 16 | std::shared_ptr<sdbusplus::asio::connection> bus, |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 17 | std::shared_ptr<sdbusplus::asio::object_server> objServer, |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 18 | SensorCache& sensorCache) : |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 19 | bus(std::move(bus)), |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 20 | objServer(std::move(objServer)), sensorCache(sensorCache) |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 21 | {} |
| 22 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 23 | void TriggerFactory::updateDiscreteThresholds( |
| 24 | std::vector<std::shared_ptr<interfaces::Threshold>>& currentThresholds, |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 25 | const std::string& triggerId, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 26 | const std::vector<TriggerAction>& triggerActions, |
| 27 | const std::shared_ptr<std::vector<std::string>>& reportIds, |
| 28 | const Sensors& sensors, |
| 29 | const std::vector<discrete::LabeledThresholdParam>& newParams) const |
| 30 | { |
| 31 | auto oldThresholds = currentThresholds; |
| 32 | std::vector<std::shared_ptr<interfaces::Threshold>> newThresholds; |
| 33 | |
| 34 | bool isCurrentOnChange = false; |
| 35 | if (oldThresholds.size() == 1 && |
| 36 | std::holds_alternative<std::monostate>( |
| 37 | oldThresholds.back()->getThresholdParam())) |
| 38 | { |
| 39 | isCurrentOnChange = true; |
| 40 | } |
| 41 | |
| 42 | newThresholds.reserve(newParams.size()); |
| 43 | |
| 44 | if (!isCurrentOnChange) |
| 45 | { |
| 46 | for (const auto& labeledThresholdParam : newParams) |
| 47 | { |
| 48 | auto paramChecker = [labeledThresholdParam](auto threshold) { |
| 49 | return labeledThresholdParam == |
| 50 | std::get<discrete::LabeledThresholdParam>( |
| 51 | threshold->getThresholdParam()); |
| 52 | }; |
| 53 | if (auto existing = std::find_if(oldThresholds.begin(), |
| 54 | oldThresholds.end(), paramChecker); |
| 55 | existing != oldThresholds.end()) |
| 56 | { |
| 57 | newThresholds.emplace_back(*existing); |
| 58 | oldThresholds.erase(existing); |
| 59 | continue; |
| 60 | } |
| 61 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 62 | makeDiscreteThreshold(newThresholds, triggerId, triggerActions, |
| 63 | reportIds, sensors, labeledThresholdParam); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | for (const auto& labeledThresholdParam : newParams) |
| 69 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 70 | makeDiscreteThreshold(newThresholds, triggerId, triggerActions, |
| 71 | reportIds, sensors, labeledThresholdParam); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | if (newParams.empty()) |
| 75 | { |
| 76 | if (isCurrentOnChange) |
| 77 | { |
| 78 | newThresholds.emplace_back(*oldThresholds.begin()); |
| 79 | } |
| 80 | else |
| 81 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 82 | makeOnChangeThreshold(newThresholds, triggerId, triggerActions, |
| 83 | reportIds, sensors); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | currentThresholds = std::move(newThresholds); |
| 87 | } |
| 88 | |
| 89 | void TriggerFactory::updateNumericThresholds( |
| 90 | std::vector<std::shared_ptr<interfaces::Threshold>>& currentThresholds, |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 91 | const std::string& triggerId, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 92 | const std::vector<TriggerAction>& triggerActions, |
| 93 | const std::shared_ptr<std::vector<std::string>>& reportIds, |
| 94 | const Sensors& sensors, |
| 95 | const std::vector<numeric::LabeledThresholdParam>& newParams) const |
| 96 | { |
| 97 | auto oldThresholds = currentThresholds; |
| 98 | std::vector<std::shared_ptr<interfaces::Threshold>> newThresholds; |
| 99 | |
| 100 | newThresholds.reserve(newParams.size()); |
| 101 | |
| 102 | for (const auto& labeledThresholdParam : newParams) |
| 103 | { |
| 104 | auto paramChecker = [labeledThresholdParam](auto threshold) { |
| 105 | return labeledThresholdParam == |
| 106 | std::get<numeric::LabeledThresholdParam>( |
| 107 | threshold->getThresholdParam()); |
| 108 | }; |
| 109 | if (auto existing = std::find_if(oldThresholds.begin(), |
| 110 | oldThresholds.end(), paramChecker); |
| 111 | existing != oldThresholds.end()) |
| 112 | { |
| 113 | newThresholds.emplace_back(*existing); |
| 114 | oldThresholds.erase(existing); |
| 115 | continue; |
| 116 | } |
| 117 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 118 | makeNumericThreshold(newThresholds, triggerId, triggerActions, |
| 119 | reportIds, sensors, labeledThresholdParam); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 120 | } |
| 121 | currentThresholds = std::move(newThresholds); |
| 122 | } |
| 123 | |
| 124 | void TriggerFactory::updateThresholds( |
| 125 | std::vector<std::shared_ptr<interfaces::Threshold>>& currentThresholds, |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 126 | const std::string& triggerId, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 127 | const std::vector<TriggerAction>& triggerActions, |
| 128 | const std::shared_ptr<std::vector<std::string>>& reportIds, |
| 129 | const Sensors& sensors, |
| 130 | const LabeledTriggerThresholdParams& newParams) const |
| 131 | { |
| 132 | if (isTriggerThresholdDiscrete(newParams)) |
| 133 | { |
| 134 | const auto& labeledDiscreteThresholdParams = |
| 135 | std::get<std::vector<discrete::LabeledThresholdParam>>(newParams); |
| 136 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 137 | updateDiscreteThresholds(currentThresholds, triggerId, triggerActions, |
| 138 | reportIds, sensors, |
| 139 | labeledDiscreteThresholdParams); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 140 | } |
| 141 | else |
| 142 | { |
| 143 | const auto& labeledNumericThresholdParams = |
| 144 | std::get<std::vector<numeric::LabeledThresholdParam>>(newParams); |
| 145 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 146 | updateNumericThresholds(currentThresholds, triggerId, triggerActions, |
| 147 | reportIds, sensors, |
| 148 | labeledNumericThresholdParams); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | void TriggerFactory::makeDiscreteThreshold( |
| 153 | std::vector<std::shared_ptr<interfaces::Threshold>>& thresholds, |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 154 | const std::string& triggerId, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 155 | const std::vector<TriggerAction>& triggerActions, |
| 156 | const std::shared_ptr<std::vector<std::string>>& reportIds, |
| 157 | const Sensors& sensors, |
| 158 | const discrete::LabeledThresholdParam& thresholdParam) const |
| 159 | { |
| 160 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actions; |
| 161 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 162 | const std::string& thresholdName = thresholdParam.at_label<ts::UserId>(); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 163 | discrete::Severity severity = thresholdParam.at_label<ts::Severity>(); |
| 164 | auto dwellTime = Milliseconds(thresholdParam.at_label<ts::DwellTime>()); |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 165 | const std::string& thresholdValue = |
| 166 | thresholdParam.at_label<ts::ThresholdValue>(); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 167 | |
| 168 | action::discrete::fillActions(actions, triggerActions, severity, |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 169 | bus->get_io_context(), reportIds); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 170 | |
| 171 | thresholds.emplace_back(std::make_shared<DiscreteThreshold>( |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 172 | bus->get_io_context(), triggerId, sensors, std::move(actions), |
| 173 | Milliseconds(dwellTime), thresholdValue, thresholdName, severity, |
| 174 | std::make_unique<Clock>())); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void TriggerFactory::makeNumericThreshold( |
| 178 | std::vector<std::shared_ptr<interfaces::Threshold>>& thresholds, |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 179 | const std::string& triggerId, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 180 | const std::vector<TriggerAction>& triggerActions, |
| 181 | const std::shared_ptr<std::vector<std::string>>& reportIds, |
| 182 | const Sensors& sensors, |
| 183 | const numeric::LabeledThresholdParam& thresholdParam) const |
| 184 | { |
| 185 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actions; |
| 186 | |
| 187 | auto type = thresholdParam.at_label<ts::Type>(); |
| 188 | auto dwellTime = Milliseconds(thresholdParam.at_label<ts::DwellTime>()); |
| 189 | auto direction = thresholdParam.at_label<ts::Direction>(); |
| 190 | auto thresholdValue = double{thresholdParam.at_label<ts::ThresholdValue>()}; |
| 191 | |
| 192 | action::numeric::fillActions(actions, triggerActions, type, thresholdValue, |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 193 | bus->get_io_context(), reportIds); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 194 | |
| 195 | thresholds.emplace_back(std::make_shared<NumericThreshold>( |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 196 | bus->get_io_context(), triggerId, sensors, std::move(actions), |
| 197 | dwellTime, direction, thresholdValue, type, std::make_unique<Clock>())); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void TriggerFactory::makeOnChangeThreshold( |
| 201 | std::vector<std::shared_ptr<interfaces::Threshold>>& thresholds, |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 202 | const std::string& triggerId, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 203 | const std::vector<TriggerAction>& triggerActions, |
| 204 | const std::shared_ptr<std::vector<std::string>>& reportIds, |
| 205 | const Sensors& sensors) const |
| 206 | { |
| 207 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actions; |
| 208 | |
| 209 | action::discrete::onChange::fillActions(actions, triggerActions, |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 210 | bus->get_io_context(), reportIds); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 211 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 212 | thresholds.emplace_back(std::make_shared<OnChangeThreshold>( |
| 213 | triggerId, sensors, std::move(actions), std::make_unique<Clock>())); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 216 | std::unique_ptr<interfaces::Trigger> TriggerFactory::make( |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 217 | const std::string& idIn, const std::string& name, |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 218 | const std::vector<std::string>& triggerActionsIn, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 219 | const std::vector<std::string>& reportIdsIn, |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 220 | interfaces::TriggerManager& triggerManager, |
| 221 | interfaces::JsonStorage& triggerStorage, |
| 222 | const LabeledTriggerThresholdParams& labeledThresholdParams, |
| 223 | const std::vector<LabeledSensorInfo>& labeledSensorsInfo) const |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 224 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 225 | const auto& sensors = getSensors(labeledSensorsInfo); |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 226 | auto triggerActions = utils::transform(triggerActionsIn, |
| 227 | [](const auto& triggerActionStr) { |
| 228 | return toTriggerAction(triggerActionStr); |
| 229 | }); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 230 | std::vector<std::shared_ptr<interfaces::Threshold>> thresholds; |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 231 | auto id = std::make_unique<const std::string>(idIn); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 232 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 233 | auto reportIds = std::make_shared<std::vector<std::string>>(reportIdsIn); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 234 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 235 | updateThresholds(thresholds, *id, triggerActions, reportIds, sensors, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 236 | labeledThresholdParams); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 237 | |
| 238 | return std::make_unique<Trigger>( |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 239 | bus->get_io_context(), objServer, std::move(id), name, triggerActions, |
| 240 | reportIds, std::move(thresholds), triggerManager, triggerStorage, *this, |
| 241 | sensors); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 244 | Sensors TriggerFactory::getSensors( |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 245 | const std::vector<LabeledSensorInfo>& labeledSensorsInfo) const |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 246 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 247 | Sensors sensors; |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 248 | updateSensors(sensors, labeledSensorsInfo); |
| 249 | return sensors; |
| 250 | } |
| 251 | |
| 252 | void TriggerFactory::updateSensors( |
| 253 | Sensors& currentSensors, |
| 254 | const std::vector<LabeledSensorInfo>& labeledSensorsInfo) const |
| 255 | { |
| 256 | Sensors oldSensors = currentSensors; |
| 257 | Sensors newSensors; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 258 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 259 | for (const auto& labeledSensorInfo : labeledSensorsInfo) |
| 260 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 261 | auto existing = std::find_if(oldSensors.begin(), oldSensors.end(), |
| 262 | [labeledSensorInfo](auto sensor) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 263 | return labeledSensorInfo == sensor->getLabeledSensorInfo(); |
| 264 | }); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 265 | |
| 266 | if (existing != oldSensors.end()) |
| 267 | { |
| 268 | newSensors.emplace_back(*existing); |
| 269 | oldSensors.erase(existing); |
| 270 | continue; |
| 271 | } |
| 272 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 273 | const auto& service = labeledSensorInfo.at_label<ts::Service>(); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 274 | const auto& sensorPath = labeledSensorInfo.at_label<ts::Path>(); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 275 | const auto& metadata = labeledSensorInfo.at_label<ts::Metadata>(); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 276 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 277 | newSensors.emplace_back(sensorCache.makeSensor<Sensor>( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 278 | service, sensorPath, metadata, bus->get_io_context(), bus)); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 279 | } |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 280 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 281 | currentSensors = std::move(newSensors); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 282 | } |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 283 | |
| 284 | std::vector<LabeledSensorInfo> |
| 285 | TriggerFactory::getLabeledSensorsInfo(boost::asio::yield_context& yield, |
| 286 | const SensorsInfo& sensorsInfo) const |
| 287 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 288 | if (sensorsInfo.empty()) |
| 289 | { |
| 290 | return {}; |
| 291 | } |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 292 | auto tree = utils::getSubTreeSensors(yield, bus); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 293 | return parseSensorTree(tree, sensorsInfo); |
| 294 | } |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 295 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 296 | std::vector<LabeledSensorInfo> |
| 297 | TriggerFactory::getLabeledSensorsInfo(const SensorsInfo& sensorsInfo) const |
| 298 | { |
| 299 | if (sensorsInfo.empty()) |
| 300 | { |
| 301 | return {}; |
| 302 | } |
| 303 | auto tree = utils::getSubTreeSensors(bus); |
| 304 | return parseSensorTree(tree, sensorsInfo); |
| 305 | } |
| 306 | |
| 307 | std::vector<LabeledSensorInfo> |
| 308 | TriggerFactory::parseSensorTree(const std::vector<utils::SensorTree>& tree, |
| 309 | const SensorsInfo& sensorsInfo) |
| 310 | { |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 311 | return utils::transform(sensorsInfo, [&tree](const auto& item) { |
| 312 | const auto& [sensorPath, metadata] = item; |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 313 | auto found = std::find_if( |
| 314 | tree.begin(), tree.end(), |
| 315 | [path = sensorPath](const auto& x) { return x.first == path; }); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 316 | |
| 317 | if (tree.end() != found) |
| 318 | { |
| 319 | const auto& [service, ifaces] = found->second.front(); |
| 320 | return LabeledSensorInfo(service, sensorPath, metadata); |
| 321 | } |
| 322 | throw std::runtime_error("Not found"); |
| 323 | }); |
| 324 | } |