Add unit to dbus
Added unit property in dbus interface for new virtual sensor
Signed-off-by: Vijay Khemka <vijaykhemka@fb.com>
Change-Id: I76e3db4699bbb0c5988120f4c1f7f23e72980522
diff --git a/virtualSensor.cpp b/virtualSensor.cpp
index 63baea5..9f2516e 100644
--- a/virtualSensor.cpp
+++ b/virtualSensor.cpp
@@ -314,6 +314,16 @@
return data;
}
+std::map<std::string, ValueIface::Unit> unitMap = {
+ {"temperature", ValueIface::Unit::DegreesC},
+ {"fan_tach", ValueIface::Unit::RPMS},
+ {"voltage", ValueIface::Unit::Volts},
+ {"altitude", ValueIface::Unit::Meters},
+ {"current", ValueIface::Unit::Amperes},
+ {"power", ValueIface::Unit::Watts},
+ {"energy", ValueIface::Unit::Joules},
+ {"utilization", ValueIface::Unit::Percent}};
+
void VirtualSensors::createVirtualSensors()
{
static const Json empty{};
@@ -334,17 +344,29 @@
if (!name.empty() && !sensorType.empty())
{
- std::string objPath(sensorDbusPath);
- objPath += sensorType + "/" + name;
+ if (unitMap.find(sensorType) == unitMap.end())
+ {
+ log<level::ERR>("Sensor type is not supported",
+ entry("TYPE = %s", sensorType.c_str()));
+ }
+ else
+ {
+ std::string objPath(sensorDbusPath);
+ objPath += sensorType + "/" + name;
- auto virtualSensorPtr = std::make_unique<VirtualSensor>(
- bus, objPath.c_str(), j, name);
+ auto virtualSensorPtr = std::make_unique<VirtualSensor>(
+ bus, objPath.c_str(), j, name);
- log<level::INFO>("Added a new virtual sensor",
- entry("NAME = %s", name.c_str()));
- virtualSensorPtr->updateVirtualSensor();
- virtualSensorsMap.emplace(std::move(name),
- std::move(virtualSensorPtr));
+ log<level::INFO>("Added a new virtual sensor",
+ entry("NAME = %s", name.c_str()));
+ virtualSensorPtr->updateVirtualSensor();
+
+ /* Initialize unit value for virtual sensor */
+ virtualSensorPtr->ValueIface::unit(unitMap[sensorType]);
+
+ virtualSensorsMap.emplace(std::move(name),
+ std::move(virtualSensorPtr));
+ }
}
else
{