blob: dde2e7d75dd1afdba39eb2e1d85daeb1df1f8056 [file] [log] [blame]
George Liu6f777cd2021-06-10 09:16:28 +08001#include "occ_dbus.hpp"
2
3#include "utils.hpp"
4
George Liu6f777cd2021-06-10 09:16:28 +08005#include <phosphor-logging/log.hpp>
6
George Liub5ca1012021-09-10 12:53:11 +08007#include <iostream>
8
George Liu6f777cd2021-06-10 09:16:28 +08009namespace open_power
10{
11namespace occ
12{
13namespace dbus
14{
15
16using namespace phosphor::logging;
George Liu0ad9dd82021-06-22 14:46:14 +080017bool OccDBusSensors::setMaxValue(const std::string& path, double value)
George Liu6f777cd2021-06-10 09:16:28 +080018{
George Liu0ad9dd82021-06-22 14:46:14 +080019 if (path.empty())
20 {
21 return false;
22 }
23
George Liu6f777cd2021-06-10 09:16:28 +080024 if (sensors.find(path) == sensors.end())
25 {
26 sensors.emplace(
27 path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
28 }
29
30 sensors.at(path)->maxValue(value);
George Liu0ad9dd82021-06-22 14:46:14 +080031 return true;
George Liu6f777cd2021-06-10 09:16:28 +080032}
33
34double OccDBusSensors::getMaxValue(const std::string& path) const
35{
36 if (sensors.find(path) != sensors.end())
37 {
38 return sensors.at(path)->maxValue();
39 }
40
41 throw std::invalid_argument("Failed to get MaxValue property.");
42}
43
George Liu0ad9dd82021-06-22 14:46:14 +080044bool OccDBusSensors::setMinValue(const std::string& path, double value)
George Liu6f777cd2021-06-10 09:16:28 +080045{
George Liu0ad9dd82021-06-22 14:46:14 +080046 if (path.empty())
47 {
48 return false;
49 }
50
George Liu6f777cd2021-06-10 09:16:28 +080051 if (sensors.find(path) == sensors.end())
52 {
53 sensors.emplace(
54 path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
55 }
56
57 sensors.at(path)->minValue(value);
George Liu0ad9dd82021-06-22 14:46:14 +080058 return true;
George Liu6f777cd2021-06-10 09:16:28 +080059}
60
61double OccDBusSensors::getMinValue(const std::string& path) const
62{
63 if (sensors.find(path) != sensors.end())
64 {
65 return sensors.at(path)->minValue();
66 }
67
68 throw std::invalid_argument("Failed to get MinValue property.");
69}
70
George Liu0ad9dd82021-06-22 14:46:14 +080071bool OccDBusSensors::setValue(const std::string& path, double value)
George Liu6f777cd2021-06-10 09:16:28 +080072{
George Liu0ad9dd82021-06-22 14:46:14 +080073 if (path.empty())
74 {
75 return false;
76 }
77
George Liu6f777cd2021-06-10 09:16:28 +080078 if (sensors.find(path) == sensors.end())
79 {
80 sensors.emplace(
81 path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
82 }
83
84 sensors.at(path)->value(value);
George Liu0ad9dd82021-06-22 14:46:14 +080085 return true;
George Liu6f777cd2021-06-10 09:16:28 +080086}
87
88double OccDBusSensors::getValue(const std::string& path) const
89{
90 if (sensors.find(path) != sensors.end())
91 {
92 return sensors.at(path)->value();
93 }
94
95 throw std::invalid_argument("Failed to get Value property.");
96}
97
George Liu0ad9dd82021-06-22 14:46:14 +080098bool OccDBusSensors::setUnit(const std::string& path, const std::string& value)
George Liu6f777cd2021-06-10 09:16:28 +080099{
George Liu0ad9dd82021-06-22 14:46:14 +0800100 if (path.empty())
101 {
102 return false;
103 }
104
George Liu6f777cd2021-06-10 09:16:28 +0800105 if (sensors.find(path) == sensors.end())
106 {
107 sensors.emplace(
108 path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
109 }
110
111 try
112 {
113 sensors.at(path)->unit(SensorIntf::convertUnitFromString(value));
114 }
115 catch (const std::exception& e)
116 {
117 log<level::ERR>("set Unit propety failed", entry("ERROR=%s", e.what()));
George Liu0ad9dd82021-06-22 14:46:14 +0800118 return false;
George Liu6f777cd2021-06-10 09:16:28 +0800119 }
George Liu0ad9dd82021-06-22 14:46:14 +0800120
121 return true;
George Liu6f777cd2021-06-10 09:16:28 +0800122}
123
124std::string OccDBusSensors::getUnit(const std::string& path) const
125{
126 if (sensors.find(path) != sensors.end())
127 {
128 try
129 {
130 return SensorIntf::convertUnitToString(sensors.at(path)->unit());
131 }
132 catch (const std::exception& e)
133 {
134 log<level::ERR>("get Unit propety failed",
135 entry("ERROR=%s", e.what()));
136 }
137 }
138
139 throw std::invalid_argument("Failed to get Unit property.");
140}
141
George Liu0ad9dd82021-06-22 14:46:14 +0800142bool OccDBusSensors::setOperationalStatus(const std::string& path, bool value)
George Liu6f777cd2021-06-10 09:16:28 +0800143{
George Liu0ad9dd82021-06-22 14:46:14 +0800144 if (path.empty())
145 {
146 return false;
147 }
148
George Liu6f777cd2021-06-10 09:16:28 +0800149 if (operationalStatus.find(path) == operationalStatus.end())
150 {
151 operationalStatus.emplace(path, std::make_unique<OperationalStatusIntf>(
152 utils::getBus(), path.c_str()));
153 }
154
155 operationalStatus.at(path)->functional(value);
George Liu0ad9dd82021-06-22 14:46:14 +0800156 return true;
George Liu6f777cd2021-06-10 09:16:28 +0800157}
158
159bool OccDBusSensors::getOperationalStatus(const std::string& path) const
160{
161 if (operationalStatus.find(path) != operationalStatus.end())
162 {
163 return operationalStatus.at(path)->functional();
164 }
165
166 throw std::invalid_argument("Failed to get OperationalStatus property.");
167}
168
169} // namespace dbus
170} // namespace occ
171} // namespace open_power