blob: d577f763555b7f2d7e424e7b2546408168146675 [file] [log] [blame]
Cezary Zwolak4416fce2021-03-17 03:21:06 +01001#include "utils/conversion_trigger.hpp"
2
3#include "utils/transform.hpp"
Szymon Dompke94f71c52021-12-10 07:16:33 +01004#include "utils/tstring.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +01005
6#include <sdbusplus/exception.hpp>
7
8namespace utils
9{
10namespace ts = utils::tstring;
11
12LabeledTriggerThresholdParams 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
20LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()(
21 const std::vector<numeric::ThresholdParam>& arg) const
22{
23 return utils::transform(arg, [](const auto& thresholdParam) {
Patrick Williams3a1c2972023-05-10 07:51:04 -050024 const auto& [type, dwellTime, direction,
25 thresholdValue] = thresholdParam;
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010026 return numeric::LabeledThresholdParam(numeric::toType(type), dwellTime,
27 numeric::toDirection(direction),
28 thresholdValue);
Cezary Zwolak4416fce2021-03-17 03:21:06 +010029 });
30}
31
32LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()(
33 const std::vector<discrete::ThresholdParam>& arg) const
34{
35 return utils::transform(arg, [](const auto& thresholdParam) {
Patrick Williams3a1c2972023-05-10 07:51:04 -050036 const auto& [userId, severity, dwellTime,
37 thresholdValue] = thresholdParam;
Cezary Zwolak4416fce2021-03-17 03:21:06 +010038 return discrete::LabeledThresholdParam(
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000039 userId, utils::toSeverity(severity), dwellTime, thresholdValue);
Cezary Zwolak4416fce2021-03-17 03:21:06 +010040 });
41}
42
43TriggerThresholdParams FromLabeledThresholdParamConversion::operator()(
44 const std::vector<numeric::LabeledThresholdParam>& arg) const
45{
46 return utils::transform(
Patrick Williamsc7935fa2023-10-20 11:19:30 -050047 arg, [](const numeric::LabeledThresholdParam& labeledThresholdParam) {
Patrick Williams3a1c2972023-05-10 07:51:04 -050048 return numeric::ThresholdParam(
49 numeric::typeToString(labeledThresholdParam.at_label<ts::Type>()),
50 labeledThresholdParam.at_label<ts::DwellTime>(),
51 numeric::directionToString(
52 labeledThresholdParam.at_label<ts::Direction>()),
53 labeledThresholdParam.at_label<ts::ThresholdValue>());
Patrick Williamsc7935fa2023-10-20 11:19:30 -050054 });
Cezary Zwolak4416fce2021-03-17 03:21:06 +010055}
56
57TriggerThresholdParams FromLabeledThresholdParamConversion::operator()(
58 const std::vector<discrete::LabeledThresholdParam>& arg) const
59{
60 return utils::transform(
Patrick Williamsc7935fa2023-10-20 11:19:30 -050061 arg, [](const discrete::LabeledThresholdParam& labeledThresholdParam) {
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000062 return discrete::ThresholdParam(
63 labeledThresholdParam.at_label<ts::UserId>(),
64 utils::enumToString(labeledThresholdParam.at_label<ts::Severity>()),
65 labeledThresholdParam.at_label<ts::DwellTime>(),
66 labeledThresholdParam.at_label<ts::ThresholdValue>());
Patrick Williamsc7935fa2023-10-20 11:19:30 -050067 });
Cezary Zwolak4416fce2021-03-17 03:21:06 +010068}
69
70SensorsInfo fromLabeledSensorsInfo(const std::vector<LabeledSensorInfo>& infos)
71{
72 return utils::transform(infos, [](const LabeledSensorInfo& val) {
73 return SensorsInfo::value_type(
Szymon Dompke94f71c52021-12-10 07:16:33 +010074 sdbusplus::message::object_path(val.at_label<ts::Path>()),
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010075 val.at_label<ts::Metadata>());
Cezary Zwolak4416fce2021-03-17 03:21:06 +010076 });
77}
78
Szymon Dompke94f71c52021-12-10 07:16:33 +010079TriggerThresholdParams
80 fromLabeledThresholdParam(const std::vector<LabeledThresholdParam>& params)
81{
82 namespace ts = utils::tstring;
Krzysztof Grobelny55824552022-02-18 16:15:31 +010083 if (params.empty())
84 {
85 return std::vector<numeric::ThresholdParam>();
86 }
87
Szymon Dompke94f71c52021-12-10 07:16:33 +010088 if (isFirstElementOfType<std::monostate>(params))
89 {
90 return std::vector<discrete::ThresholdParam>();
91 }
92
93 if (isFirstElementOfType<discrete::LabeledThresholdParam>(params))
94 {
95 return utils::transform(params, [](const auto& param) {
96 const discrete::LabeledThresholdParam* paramUnpacked =
97 std::get_if<discrete::LabeledThresholdParam>(&param);
98 if (!paramUnpacked)
99 {
100 throw std::runtime_error(
101 "Mixing threshold types is not allowed");
102 }
103 return discrete::ThresholdParam(
104 paramUnpacked->at_label<ts::UserId>(),
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000105 utils::enumToString(paramUnpacked->at_label<ts::Severity>()),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100106 paramUnpacked->at_label<ts::DwellTime>(),
107 paramUnpacked->at_label<ts::ThresholdValue>());
108 });
109 }
110
111 if (isFirstElementOfType<numeric::LabeledThresholdParam>(params))
112 {
113 return utils::transform(params, [](const auto& param) {
114 const numeric::LabeledThresholdParam* paramUnpacked =
115 std::get_if<numeric::LabeledThresholdParam>(&param);
116 if (!paramUnpacked)
117 {
118 throw std::runtime_error(
119 "Mixing threshold types is not allowed");
120 }
121 return numeric::ThresholdParam(
122 numeric::typeToString(paramUnpacked->at_label<ts::Type>()),
123 paramUnpacked->at_label<ts::DwellTime>(),
124 numeric::directionToString(
125 paramUnpacked->at_label<ts::Direction>()),
126 paramUnpacked->at_label<ts::ThresholdValue>());
127 });
128 }
129
130 throw std::runtime_error("Incorrect threshold params");
131}
132
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100133nlohmann::json labeledThresholdParamsToJson(
134 const LabeledTriggerThresholdParams& labeledThresholdParams)
135{
136 return std::visit([](const auto& lt) { return nlohmann::json(lt); },
137 labeledThresholdParams);
138}
139
Szymon Dompkeaa572362022-03-23 16:31:24 +0100140double stodStrict(const std::string& str)
141{
142 size_t pos = 0;
143 double result = std::stod(str, &pos);
144 if (pos < str.length())
145 {
146 throw std::invalid_argument(
147 "non-numeric characters at the end of string");
148 }
149 return result;
150}
151
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100152} // namespace utils