George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 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; |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 16 | bool OccDBusSensors::setMaxValue(const std::string& path, double value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 17 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 18 | if (path.empty()) |
| 19 | { |
| 20 | return false; |
| 21 | } |
| 22 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 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); |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 30 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 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 | |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 43 | bool OccDBusSensors::setMinValue(const std::string& path, double value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 44 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 45 | if (path.empty()) |
| 46 | { |
| 47 | return false; |
| 48 | } |
| 49 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 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); |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 57 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 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 | |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 70 | bool OccDBusSensors::setValue(const std::string& path, double value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 71 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 72 | if (path.empty()) |
| 73 | { |
| 74 | return false; |
| 75 | } |
| 76 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 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); |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 84 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 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 | |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 97 | bool OccDBusSensors::setUnit(const std::string& path, const std::string& value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 98 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 99 | if (path.empty()) |
| 100 | { |
| 101 | return false; |
| 102 | } |
| 103 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 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())); |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 117 | return false; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 118 | } |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 119 | |
| 120 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 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 | |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 141 | bool OccDBusSensors::setOperationalStatus(const std::string& path, bool value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 142 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 143 | if (path.empty()) |
| 144 | { |
| 145 | return false; |
| 146 | } |
| 147 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 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); |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 155 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 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 |