From 979c806dc6d236711a5eb7752d61b8895036192d Mon Sep 17 00:00:00 2001 From: Matthew Barth Date: Tue, 17 Apr 2018 11:37:15 -0500 Subject: Get sensor identifiers function To minimize handling the return cases when getting a sensor object, retrieve the sensor identifiers from a function. The identifiers returned are then checked to be valid before proceeding in creating the sensor object. Tested: Sensor objects created the same Empty id or label cause sensor object to not be created Change-Id: I7c2f0df3fee99448af5365e0a40c4282f9a235fa Signed-off-by: Matthew Barth --- mainloop.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 18 deletions(-) (limited to 'mainloop.cpp') diff --git a/mainloop.cpp b/mainloop.cpp index 03c856d..9a48755 100644 --- a/mainloop.cpp +++ b/mainloop.cpp @@ -195,17 +195,8 @@ auto addValue(const SensorSet::key_type& sensor, return iface; } -/** - * Reads the environment parameters of a sensor and creates an object with - * atleast the `Value` interface, otherwise returns without creating the object. - * If the `Value` interface is successfully created, by reading the sensor's - * corresponding sysfs file's value, the additional interfaces for the sensor - * are created and the InterfacesAdded signal is emitted. The sensor is then - * moved to the list for sensor state monitoring within the main loop. - */ -void MainLoop::getObject(SensorSet::container_t::const_reference sensor) +std::string MainLoop::getID(SensorSet::container_t::const_reference sensor) { - std::string label; std::string id; /* @@ -224,7 +215,7 @@ void MainLoop::getObject(SensorSet::container_t::const_reference sensor) if (id.empty()) { - return; + return id; } } @@ -232,9 +223,38 @@ void MainLoop::getObject(SensorSet::container_t::const_reference sensor) // otherwise use the standard one. id = (id.empty()) ? sensor.first.second : id; - // Ignore inputs without a label. - label = env::getEnv("LABEL", sensor.first.first, id); - if (label.empty()) + return id; +} + +SensorIdentifiers MainLoop::getIdentifiers( + SensorSet::container_t::const_reference sensor) +{ + std::string id = getID(sensor); + std::string label; + + if (!id.empty()) + { + // Ignore inputs without a label. + label = env::getEnv("LABEL", sensor.first.first, id); + } + + return std::make_tuple(std::move(id), + std::move(label)); +} + +/** + * Reads the environment parameters of a sensor and creates an object with + * atleast the `Value` interface, otherwise returns without creating the object. + * If the `Value` interface is successfully created, by reading the sensor's + * corresponding sysfs file's value, the additional interfaces for the sensor + * are created and the InterfacesAdded signal is emitted. The sensor is then + * moved to the list for sensor state monitoring within the main loop. + */ +void MainLoop::getObject(SensorSet::container_t::const_reference sensor) +{ + auto properties = getIdentifiers(sensor); + if (std::get(properties).empty() || + std::get(properties).empty()) { return; } @@ -254,7 +274,7 @@ void MainLoop::getObject(SensorSet::container_t::const_reference sensor) objectPath.append(1, '/'); objectPath.append(hwmon::getNamespace(attrs)); objectPath.append(1, '/'); - objectPath.append(label); + objectPath.append(std::get(properties)); ObjectInfo info(&_bus, std::move(objectPath), Object()); RetryIO retryIO(sysfs::hwmonio::retries, sysfs::hwmonio::delay); @@ -317,8 +337,14 @@ void MainLoop::getObject(SensorSet::container_t::const_reference sensor) #endif } auto sensorValue = valueInterface->value(); - addThreshold(sensor.first.first, id, sensorValue, info); - addThreshold(sensor.first.first, id, sensorValue, info); + addThreshold(sensor.first.first, + std::get(properties), + sensorValue, + info); + addThreshold(sensor.first.first, + std::get(properties), + sensorValue, + info); auto target = addTarget( sensor.first, ioAccess, _devPath, info); @@ -334,7 +360,7 @@ void MainLoop::getObject(SensorSet::container_t::const_reference sensor) auto value = std::make_tuple( std::move(sensor.second), - std::move(label), + std::move(std::get(properties)), std::move(info)); state[std::move(sensor.first)] = std::move(value); -- cgit v1.2.1