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