blob: 2ed2f3bb06f2f6bb943eeb716fdd1ef79478efab [file] [log] [blame]
George Liu6f777cd2021-06-10 09:16:28 +08001#include "occ_dbus.hpp"
2
3#include "utils.hpp"
4
5#include <iostream>
6#include <phosphor-logging/log.hpp>
7
8namespace open_power
9{
10namespace occ
11{
12namespace dbus
13{
14
15using namespace phosphor::logging;
George Liu0ad9dd82021-06-22 14:46:14 +080016bool OccDBusSensors::setMaxValue(const std::string& path, double value)
George Liu6f777cd2021-06-10 09:16:28 +080017{
George Liu0ad9dd82021-06-22 14:46:14 +080018 if (path.empty())
19 {
20 return false;
21 }
22
George Liu6f777cd2021-06-10 09:16:28 +080023 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);
George Liu0ad9dd82021-06-22 14:46:14 +080030 return true;
George Liu6f777cd2021-06-10 09:16:28 +080031}
32
33double 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
George Liu0ad9dd82021-06-22 14:46:14 +080043bool OccDBusSensors::setMinValue(const std::string& path, double value)
George Liu6f777cd2021-06-10 09:16:28 +080044{
George Liu0ad9dd82021-06-22 14:46:14 +080045 if (path.empty())
46 {
47 return false;
48 }
49
George Liu6f777cd2021-06-10 09:16:28 +080050 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);
George Liu0ad9dd82021-06-22 14:46:14 +080057 return true;
George Liu6f777cd2021-06-10 09:16:28 +080058}
59
60double 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
George Liu0ad9dd82021-06-22 14:46:14 +080070bool OccDBusSensors::setValue(const std::string& path, double value)
George Liu6f777cd2021-06-10 09:16:28 +080071{
George Liu0ad9dd82021-06-22 14:46:14 +080072 if (path.empty())
73 {
74 return false;
75 }
76
George Liu6f777cd2021-06-10 09:16:28 +080077 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);
George Liu0ad9dd82021-06-22 14:46:14 +080084 return true;
George Liu6f777cd2021-06-10 09:16:28 +080085}
86
87double 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
George Liu0ad9dd82021-06-22 14:46:14 +080097bool OccDBusSensors::setUnit(const std::string& path, const std::string& value)
George Liu6f777cd2021-06-10 09:16:28 +080098{
George Liu0ad9dd82021-06-22 14:46:14 +080099 if (path.empty())
100 {
101 return false;
102 }
103
George Liu6f777cd2021-06-10 09:16:28 +0800104 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()));
George Liu0ad9dd82021-06-22 14:46:14 +0800117 return false;
George Liu6f777cd2021-06-10 09:16:28 +0800118 }
George Liu0ad9dd82021-06-22 14:46:14 +0800119
120 return true;
George Liu6f777cd2021-06-10 09:16:28 +0800121}
122
123std::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
George Liu0ad9dd82021-06-22 14:46:14 +0800141bool OccDBusSensors::setOperationalStatus(const std::string& path, bool value)
George Liu6f777cd2021-06-10 09:16:28 +0800142{
George Liu0ad9dd82021-06-22 14:46:14 +0800143 if (path.empty())
144 {
145 return false;
146 }
147
George Liu6f777cd2021-06-10 09:16:28 +0800148 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);
George Liu0ad9dd82021-06-22 14:46:14 +0800155 return true;
George Liu6f777cd2021-06-10 09:16:28 +0800156}
157
158bool 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