1 #include "occ_dbus.hpp"
2 
3 #include "utils.hpp"
4 
5 #include <iostream>
6 #include <phosphor-logging/log.hpp>
7 
8 namespace open_power
9 {
10 namespace occ
11 {
12 namespace dbus
13 {
14 
15 using namespace phosphor::logging;
16 bool OccDBusSensors::setMaxValue(const std::string& path, double value)
17 {
18     if (path.empty())
19     {
20         return false;
21     }
22 
23     if (sensors.find(path) == sensors.end())
24     {
25         sensors.emplace(
26             path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
27     }
28 
29     sensors.at(path)->maxValue(value);
30     return true;
31 }
32 
33 double OccDBusSensors::getMaxValue(const std::string& path) const
34 {
35     if (sensors.find(path) != sensors.end())
36     {
37         return sensors.at(path)->maxValue();
38     }
39 
40     throw std::invalid_argument("Failed to get MaxValue property.");
41 }
42 
43 bool OccDBusSensors::setMinValue(const std::string& path, double value)
44 {
45     if (path.empty())
46     {
47         return false;
48     }
49 
50     if (sensors.find(path) == sensors.end())
51     {
52         sensors.emplace(
53             path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
54     }
55 
56     sensors.at(path)->minValue(value);
57     return true;
58 }
59 
60 double OccDBusSensors::getMinValue(const std::string& path) const
61 {
62     if (sensors.find(path) != sensors.end())
63     {
64         return sensors.at(path)->minValue();
65     }
66 
67     throw std::invalid_argument("Failed to get MinValue property.");
68 }
69 
70 bool OccDBusSensors::setValue(const std::string& path, double value)
71 {
72     if (path.empty())
73     {
74         return false;
75     }
76 
77     if (sensors.find(path) == sensors.end())
78     {
79         sensors.emplace(
80             path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
81     }
82 
83     sensors.at(path)->value(value);
84     return true;
85 }
86 
87 double OccDBusSensors::getValue(const std::string& path) const
88 {
89     if (sensors.find(path) != sensors.end())
90     {
91         return sensors.at(path)->value();
92     }
93 
94     throw std::invalid_argument("Failed to get Value property.");
95 }
96 
97 bool OccDBusSensors::setUnit(const std::string& path, const std::string& value)
98 {
99     if (path.empty())
100     {
101         return false;
102     }
103 
104     if (sensors.find(path) == sensors.end())
105     {
106         sensors.emplace(
107             path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
108     }
109 
110     try
111     {
112         sensors.at(path)->unit(SensorIntf::convertUnitFromString(value));
113     }
114     catch (const std::exception& e)
115     {
116         log<level::ERR>("set Unit propety failed", entry("ERROR=%s", e.what()));
117         return false;
118     }
119 
120     return true;
121 }
122 
123 std::string OccDBusSensors::getUnit(const std::string& path) const
124 {
125     if (sensors.find(path) != sensors.end())
126     {
127         try
128         {
129             return SensorIntf::convertUnitToString(sensors.at(path)->unit());
130         }
131         catch (const std::exception& e)
132         {
133             log<level::ERR>("get Unit propety failed",
134                             entry("ERROR=%s", e.what()));
135         }
136     }
137 
138     throw std::invalid_argument("Failed to get Unit property.");
139 }
140 
141 bool OccDBusSensors::setOperationalStatus(const std::string& path, bool value)
142 {
143     if (path.empty())
144     {
145         return false;
146     }
147 
148     if (operationalStatus.find(path) == operationalStatus.end())
149     {
150         operationalStatus.emplace(path, std::make_unique<OperationalStatusIntf>(
151                                             utils::getBus(), path.c_str()));
152     }
153 
154     operationalStatus.at(path)->functional(value);
155     return true;
156 }
157 
158 bool OccDBusSensors::getOperationalStatus(const std::string& path) const
159 {
160     if (operationalStatus.find(path) != operationalStatus.end())
161     {
162         return operationalStatus.at(path)->functional();
163     }
164 
165     throw std::invalid_argument("Failed to get OperationalStatus property.");
166 }
167 
168 } // namespace dbus
169 } // namespace occ
170 } // namespace open_power
171