blob: ab06a4856a634ae1bf9b76d1bfd73f492b26c479 [file] [log] [blame]
Cezary Zwolak4416fce2021-03-17 03:21:06 +01001#include "utils/conversion_trigger.hpp"
2
3#include "utils/transform.hpp"
4
5#include <sdbusplus/exception.hpp>
6
7namespace utils
8{
9namespace ts = utils::tstring;
10
11LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()(
12 const std::monostate& arg) const
13{
14 throw sdbusplus::exception::SdBusError(
15 static_cast<int>(std::errc::invalid_argument),
16 "Provided threshold parameter is invalid");
17}
18
19LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()(
20 const std::vector<numeric::ThresholdParam>& arg) const
21{
22 return utils::transform(arg, [](const auto& thresholdParam) {
23 const auto& [type, dwellTime, direction, thresholdValue] =
24 thresholdParam;
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010025 return numeric::LabeledThresholdParam(numeric::toType(type), dwellTime,
26 numeric::toDirection(direction),
27 thresholdValue);
Cezary Zwolak4416fce2021-03-17 03:21:06 +010028 });
29}
30
31LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()(
32 const std::vector<discrete::ThresholdParam>& arg) const
33{
34 return utils::transform(arg, [](const auto& thresholdParam) {
35 const auto& [userId, severity, dwellTime, thresholdValue] =
36 thresholdParam;
37 return discrete::LabeledThresholdParam(
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010038 userId, discrete::toSeverity(severity), dwellTime, thresholdValue);
Cezary Zwolak4416fce2021-03-17 03:21:06 +010039 });
40}
41
42TriggerThresholdParams FromLabeledThresholdParamConversion::operator()(
43 const std::vector<numeric::LabeledThresholdParam>& arg) const
44{
45 return utils::transform(
46 arg, [](const numeric::LabeledThresholdParam& labeledThresholdParam) {
47 return numeric::ThresholdParam(
48 numeric::typeToString(
49 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>());
54 });
55}
56
57TriggerThresholdParams FromLabeledThresholdParamConversion::operator()(
58 const std::vector<discrete::LabeledThresholdParam>& arg) const
59{
60 return utils::transform(
61 arg, [](const discrete::LabeledThresholdParam& labeledThresholdParam) {
62 return discrete::ThresholdParam(
63 labeledThresholdParam.at_label<ts::UserId>(),
64 discrete::severityToString(
65 labeledThresholdParam.at_label<ts::Severity>()),
66 labeledThresholdParam.at_label<ts::DwellTime>(),
67 labeledThresholdParam.at_label<ts::ThresholdValue>());
68 });
69}
70
71SensorsInfo fromLabeledSensorsInfo(const std::vector<LabeledSensorInfo>& infos)
72{
73 return utils::transform(infos, [](const LabeledSensorInfo& val) {
74 return SensorsInfo::value_type(
75 sdbusplus::message::object_path(val.at_label<ts::SensorPath>()),
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010076 val.at_label<ts::Metadata>());
Cezary Zwolak4416fce2021-03-17 03:21:06 +010077 });
78}
79
80nlohmann::json labeledThresholdParamsToJson(
81 const LabeledTriggerThresholdParams& labeledThresholdParams)
82{
83 return std::visit([](const auto& lt) { return nlohmann::json(lt); },
84 labeledThresholdParams);
85}
86
87} // namespace utils