blob: e1b835ad63bd473b30d83af88148f9e9d77d4142 [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) {
24 const auto& [type, dwellTime, direction, thresholdValue] =
25 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) {
36 const auto& [userId, severity, dwellTime, thresholdValue] =
37 thresholdParam;
38 return discrete::LabeledThresholdParam(
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010039 userId, discrete::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(
47 arg, [](const numeric::LabeledThresholdParam& labeledThresholdParam) {
48 return numeric::ThresholdParam(
49 numeric::typeToString(
50 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>());
55 });
56}
57
58TriggerThresholdParams 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
72SensorsInfo fromLabeledSensorsInfo(const std::vector<LabeledSensorInfo>& infos)
73{
74 return utils::transform(infos, [](const LabeledSensorInfo& val) {
75 return SensorsInfo::value_type(
Szymon Dompke94f71c52021-12-10 07:16:33 +010076 sdbusplus::message::object_path(val.at_label<ts::Path>()),
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010077 val.at_label<ts::Metadata>());
Cezary Zwolak4416fce2021-03-17 03:21:06 +010078 });
79}
80
Szymon Dompke94f71c52021-12-10 07:16:33 +010081TriggerThresholdParams
82 fromLabeledThresholdParam(const std::vector<LabeledThresholdParam>& params)
83{
84 namespace ts = utils::tstring;
85 if (isFirstElementOfType<std::monostate>(params))
86 {
87 return std::vector<discrete::ThresholdParam>();
88 }
89
90 if (isFirstElementOfType<discrete::LabeledThresholdParam>(params))
91 {
92 return utils::transform(params, [](const auto& param) {
93 const discrete::LabeledThresholdParam* paramUnpacked =
94 std::get_if<discrete::LabeledThresholdParam>(&param);
95 if (!paramUnpacked)
96 {
97 throw std::runtime_error(
98 "Mixing threshold types is not allowed");
99 }
100 return discrete::ThresholdParam(
101 paramUnpacked->at_label<ts::UserId>(),
102 discrete::severityToString(
103 paramUnpacked->at_label<ts::Severity>()),
104 paramUnpacked->at_label<ts::DwellTime>(),
105 paramUnpacked->at_label<ts::ThresholdValue>());
106 });
107 }
108
109 if (isFirstElementOfType<numeric::LabeledThresholdParam>(params))
110 {
111 return utils::transform(params, [](const auto& param) {
112 const numeric::LabeledThresholdParam* paramUnpacked =
113 std::get_if<numeric::LabeledThresholdParam>(&param);
114 if (!paramUnpacked)
115 {
116 throw std::runtime_error(
117 "Mixing threshold types is not allowed");
118 }
119 return numeric::ThresholdParam(
120 numeric::typeToString(paramUnpacked->at_label<ts::Type>()),
121 paramUnpacked->at_label<ts::DwellTime>(),
122 numeric::directionToString(
123 paramUnpacked->at_label<ts::Direction>()),
124 paramUnpacked->at_label<ts::ThresholdValue>());
125 });
126 }
127
128 throw std::runtime_error("Incorrect threshold params");
129}
130
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100131nlohmann::json labeledThresholdParamsToJson(
132 const LabeledTriggerThresholdParams& labeledThresholdParams)
133{
134 return std::visit([](const auto& lt) { return nlohmann::json(lt); },
135 labeledThresholdParams);
136}
137
138} // namespace utils