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; |
| 16 | void OccDBusSensors::setMaxValue(const std::string& path, double value) |
| 17 | { |
| 18 | if (sensors.find(path) == sensors.end()) |
| 19 | { |
| 20 | sensors.emplace( |
| 21 | path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str())); |
| 22 | } |
| 23 | |
| 24 | sensors.at(path)->maxValue(value); |
| 25 | } |
| 26 | |
| 27 | double OccDBusSensors::getMaxValue(const std::string& path) const |
| 28 | { |
| 29 | if (sensors.find(path) != sensors.end()) |
| 30 | { |
| 31 | return sensors.at(path)->maxValue(); |
| 32 | } |
| 33 | |
| 34 | throw std::invalid_argument("Failed to get MaxValue property."); |
| 35 | } |
| 36 | |
| 37 | void OccDBusSensors::setMinValue(const std::string& path, double value) |
| 38 | { |
| 39 | if (sensors.find(path) == sensors.end()) |
| 40 | { |
| 41 | sensors.emplace( |
| 42 | path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str())); |
| 43 | } |
| 44 | |
| 45 | sensors.at(path)->minValue(value); |
| 46 | } |
| 47 | |
| 48 | double OccDBusSensors::getMinValue(const std::string& path) const |
| 49 | { |
| 50 | if (sensors.find(path) != sensors.end()) |
| 51 | { |
| 52 | return sensors.at(path)->minValue(); |
| 53 | } |
| 54 | |
| 55 | throw std::invalid_argument("Failed to get MinValue property."); |
| 56 | } |
| 57 | |
| 58 | void OccDBusSensors::setValue(const std::string& path, double value) |
| 59 | { |
| 60 | if (sensors.find(path) == sensors.end()) |
| 61 | { |
| 62 | sensors.emplace( |
| 63 | path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str())); |
| 64 | } |
| 65 | |
| 66 | sensors.at(path)->value(value); |
| 67 | } |
| 68 | |
| 69 | double OccDBusSensors::getValue(const std::string& path) const |
| 70 | { |
| 71 | if (sensors.find(path) != sensors.end()) |
| 72 | { |
| 73 | return sensors.at(path)->value(); |
| 74 | } |
| 75 | |
| 76 | throw std::invalid_argument("Failed to get Value property."); |
| 77 | } |
| 78 | |
| 79 | void OccDBusSensors::setUnit(const std::string& path, const std::string& value) |
| 80 | { |
| 81 | if (sensors.find(path) == sensors.end()) |
| 82 | { |
| 83 | sensors.emplace( |
| 84 | path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str())); |
| 85 | } |
| 86 | |
| 87 | try |
| 88 | { |
| 89 | sensors.at(path)->unit(SensorIntf::convertUnitFromString(value)); |
| 90 | } |
| 91 | catch (const std::exception& e) |
| 92 | { |
| 93 | log<level::ERR>("set Unit propety failed", entry("ERROR=%s", e.what())); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | std::string OccDBusSensors::getUnit(const std::string& path) const |
| 98 | { |
| 99 | if (sensors.find(path) != sensors.end()) |
| 100 | { |
| 101 | try |
| 102 | { |
| 103 | return SensorIntf::convertUnitToString(sensors.at(path)->unit()); |
| 104 | } |
| 105 | catch (const std::exception& e) |
| 106 | { |
| 107 | log<level::ERR>("get Unit propety failed", |
| 108 | entry("ERROR=%s", e.what())); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | throw std::invalid_argument("Failed to get Unit property."); |
| 113 | } |
| 114 | |
| 115 | void OccDBusSensors::setOperationalStatus(const std::string& path, bool value) |
| 116 | { |
| 117 | if (operationalStatus.find(path) == operationalStatus.end()) |
| 118 | { |
| 119 | operationalStatus.emplace(path, std::make_unique<OperationalStatusIntf>( |
| 120 | utils::getBus(), path.c_str())); |
| 121 | } |
| 122 | |
| 123 | operationalStatus.at(path)->functional(value); |
| 124 | } |
| 125 | |
| 126 | bool OccDBusSensors::getOperationalStatus(const std::string& path) const |
| 127 | { |
| 128 | if (operationalStatus.find(path) != operationalStatus.end()) |
| 129 | { |
| 130 | return operationalStatus.at(path)->functional(); |
| 131 | } |
| 132 | |
| 133 | throw std::invalid_argument("Failed to get OperationalStatus property."); |
| 134 | } |
| 135 | |
| 136 | } // namespace dbus |
| 137 | } // namespace occ |
| 138 | } // namespace open_power |