George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 1 | #include "occ_dbus.hpp" |
| 2 | |
| 3 | #include "utils.hpp" |
| 4 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
| 6 | |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame^] | 7 | #include <iostream> |
| 8 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 9 | namespace open_power |
| 10 | { |
| 11 | namespace occ |
| 12 | { |
| 13 | namespace dbus |
| 14 | { |
| 15 | |
| 16 | using namespace phosphor::logging; |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 17 | bool OccDBusSensors::setMaxValue(const std::string& path, double value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 18 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 19 | if (path.empty()) |
| 20 | { |
| 21 | return false; |
| 22 | } |
| 23 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 24 | 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 Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 31 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | double 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 Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 44 | bool OccDBusSensors::setMinValue(const std::string& path, double value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 45 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 46 | if (path.empty()) |
| 47 | { |
| 48 | return false; |
| 49 | } |
| 50 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 51 | 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 Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 58 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | double 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 Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 71 | bool OccDBusSensors::setValue(const std::string& path, double value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 72 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 73 | if (path.empty()) |
| 74 | { |
| 75 | return false; |
| 76 | } |
| 77 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 78 | 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 Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 85 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | double 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 Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 98 | bool OccDBusSensors::setUnit(const std::string& path, const std::string& value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 99 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 100 | if (path.empty()) |
| 101 | { |
| 102 | return false; |
| 103 | } |
| 104 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 105 | 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 Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 118 | return false; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 119 | } |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 120 | |
| 121 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | std::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 Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 142 | bool OccDBusSensors::setOperationalStatus(const std::string& path, bool value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 143 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 144 | if (path.empty()) |
| 145 | { |
| 146 | return false; |
| 147 | } |
| 148 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 149 | 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 Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 156 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | bool 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 |