Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 1 | #include "utils/conversion_trigger.hpp" |
| 2 | |
| 3 | #include "utils/transform.hpp" |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 4 | #include "utils/tstring.hpp" |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 5 | |
| 6 | #include <sdbusplus/exception.hpp> |
| 7 | |
| 8 | namespace utils |
| 9 | { |
| 10 | namespace ts = utils::tstring; |
| 11 | |
| 12 | LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()( |
| 13 | const std::monostate& arg) const |
| 14 | { |
| 15 | throw sdbusplus::exception::SdBusError( |
| 16 | static_cast<int>(std::errc::invalid_argument), |
| 17 | "Provided threshold parameter is invalid"); |
| 18 | } |
| 19 | |
| 20 | LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()( |
| 21 | const std::vector<numeric::ThresholdParam>& arg) const |
| 22 | { |
| 23 | return utils::transform(arg, [](const auto& thresholdParam) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 24 | const auto& [type, dwellTime, direction, |
| 25 | thresholdValue] = thresholdParam; |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 26 | return numeric::LabeledThresholdParam(numeric::toType(type), dwellTime, |
| 27 | numeric::toDirection(direction), |
| 28 | thresholdValue); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 29 | }); |
| 30 | } |
| 31 | |
| 32 | LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()( |
| 33 | const std::vector<discrete::ThresholdParam>& arg) const |
| 34 | { |
| 35 | return utils::transform(arg, [](const auto& thresholdParam) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 36 | const auto& [userId, severity, dwellTime, |
| 37 | thresholdValue] = thresholdParam; |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 38 | return discrete::LabeledThresholdParam( |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 39 | userId, discrete::toSeverity(severity), dwellTime, thresholdValue); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 40 | }); |
| 41 | } |
| 42 | |
| 43 | TriggerThresholdParams FromLabeledThresholdParamConversion::operator()( |
| 44 | const std::vector<numeric::LabeledThresholdParam>& arg) const |
| 45 | { |
| 46 | return utils::transform( |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 47 | arg, |
| 48 | [](const numeric::LabeledThresholdParam& labeledThresholdParam) { |
| 49 | return numeric::ThresholdParam( |
| 50 | numeric::typeToString(labeledThresholdParam.at_label<ts::Type>()), |
| 51 | labeledThresholdParam.at_label<ts::DwellTime>(), |
| 52 | numeric::directionToString( |
| 53 | labeledThresholdParam.at_label<ts::Direction>()), |
| 54 | labeledThresholdParam.at_label<ts::ThresholdValue>()); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 55 | }); |
| 56 | } |
| 57 | |
| 58 | TriggerThresholdParams FromLabeledThresholdParamConversion::operator()( |
| 59 | const std::vector<discrete::LabeledThresholdParam>& arg) const |
| 60 | { |
| 61 | return utils::transform( |
| 62 | arg, [](const discrete::LabeledThresholdParam& labeledThresholdParam) { |
| 63 | return discrete::ThresholdParam( |
| 64 | labeledThresholdParam.at_label<ts::UserId>(), |
| 65 | discrete::severityToString( |
| 66 | labeledThresholdParam.at_label<ts::Severity>()), |
| 67 | labeledThresholdParam.at_label<ts::DwellTime>(), |
| 68 | labeledThresholdParam.at_label<ts::ThresholdValue>()); |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | SensorsInfo fromLabeledSensorsInfo(const std::vector<LabeledSensorInfo>& infos) |
| 73 | { |
| 74 | return utils::transform(infos, [](const LabeledSensorInfo& val) { |
| 75 | return SensorsInfo::value_type( |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 76 | sdbusplus::message::object_path(val.at_label<ts::Path>()), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 77 | val.at_label<ts::Metadata>()); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 78 | }); |
| 79 | } |
| 80 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 81 | TriggerThresholdParams |
| 82 | fromLabeledThresholdParam(const std::vector<LabeledThresholdParam>& params) |
| 83 | { |
| 84 | namespace ts = utils::tstring; |
Krzysztof Grobelny | 5582455 | 2022-02-18 16:15:31 +0100 | [diff] [blame] | 85 | if (params.empty()) |
| 86 | { |
| 87 | return std::vector<numeric::ThresholdParam>(); |
| 88 | } |
| 89 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 90 | if (isFirstElementOfType<std::monostate>(params)) |
| 91 | { |
| 92 | return std::vector<discrete::ThresholdParam>(); |
| 93 | } |
| 94 | |
| 95 | if (isFirstElementOfType<discrete::LabeledThresholdParam>(params)) |
| 96 | { |
| 97 | return utils::transform(params, [](const auto& param) { |
| 98 | const discrete::LabeledThresholdParam* paramUnpacked = |
| 99 | std::get_if<discrete::LabeledThresholdParam>(¶m); |
| 100 | if (!paramUnpacked) |
| 101 | { |
| 102 | throw std::runtime_error( |
| 103 | "Mixing threshold types is not allowed"); |
| 104 | } |
| 105 | return discrete::ThresholdParam( |
| 106 | paramUnpacked->at_label<ts::UserId>(), |
| 107 | discrete::severityToString( |
| 108 | paramUnpacked->at_label<ts::Severity>()), |
| 109 | paramUnpacked->at_label<ts::DwellTime>(), |
| 110 | paramUnpacked->at_label<ts::ThresholdValue>()); |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | if (isFirstElementOfType<numeric::LabeledThresholdParam>(params)) |
| 115 | { |
| 116 | return utils::transform(params, [](const auto& param) { |
| 117 | const numeric::LabeledThresholdParam* paramUnpacked = |
| 118 | std::get_if<numeric::LabeledThresholdParam>(¶m); |
| 119 | if (!paramUnpacked) |
| 120 | { |
| 121 | throw std::runtime_error( |
| 122 | "Mixing threshold types is not allowed"); |
| 123 | } |
| 124 | return numeric::ThresholdParam( |
| 125 | numeric::typeToString(paramUnpacked->at_label<ts::Type>()), |
| 126 | paramUnpacked->at_label<ts::DwellTime>(), |
| 127 | numeric::directionToString( |
| 128 | paramUnpacked->at_label<ts::Direction>()), |
| 129 | paramUnpacked->at_label<ts::ThresholdValue>()); |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | throw std::runtime_error("Incorrect threshold params"); |
| 134 | } |
| 135 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 136 | nlohmann::json labeledThresholdParamsToJson( |
| 137 | const LabeledTriggerThresholdParams& labeledThresholdParams) |
| 138 | { |
| 139 | return std::visit([](const auto& lt) { return nlohmann::json(lt); }, |
| 140 | labeledThresholdParams); |
| 141 | } |
| 142 | |
Szymon Dompke | aa57236 | 2022-03-23 16:31:24 +0100 | [diff] [blame] | 143 | double stodStrict(const std::string& str) |
| 144 | { |
| 145 | size_t pos = 0; |
| 146 | double result = std::stod(str, &pos); |
| 147 | if (pos < str.length()) |
| 148 | { |
| 149 | throw std::invalid_argument( |
| 150 | "non-numeric characters at the end of string"); |
| 151 | } |
| 152 | return result; |
| 153 | } |
| 154 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 155 | } // namespace utils |