entity-manager: probe: factor out probe logic
Move the probe matching logic to a function so unit tests can be written
for it.
Change-Id: Ie6d1ec82deb48437138c9065aea3a0eeacc9cffb
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/src/Utils.cpp b/src/Utils.cpp
index b872ee7..27f1c15 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -391,3 +391,66 @@
}
return ret;
}
+
+bool matchProbe(const nlohmann::json& probe, const BasicVariantType& dbusValue)
+{
+ bool deviceMatches = true;
+
+ switch (probe.type())
+ {
+ case nlohmann::json::value_t::string:
+ {
+ std::regex search(probe.get<std::string>());
+ std::smatch regMatch;
+
+ // convert value to string respresentation
+ std::string probeValue =
+ std::visit(VariantToStringVisitor(), dbusValue);
+ if (!std::regex_search(probeValue, regMatch, search))
+ {
+ deviceMatches = false;
+ break;
+ }
+ break;
+ }
+ case nlohmann::json::value_t::boolean:
+ case nlohmann::json::value_t::number_unsigned:
+ {
+ unsigned int probeValue =
+ std::visit(VariantToUnsignedIntVisitor(), dbusValue);
+
+ if (probeValue != probe.get<unsigned int>())
+ {
+ deviceMatches = false;
+ }
+ break;
+ }
+ case nlohmann::json::value_t::number_integer:
+ {
+ int probeValue = std::visit(VariantToIntVisitor(), dbusValue);
+
+ if (probeValue != probe.get<int>())
+ {
+ deviceMatches = false;
+ }
+ break;
+ }
+ case nlohmann::json::value_t::number_float:
+ {
+ float probeValue = std::visit(VariantToFloatVisitor(), dbusValue);
+
+ if (probeValue != probe.get<float>())
+ {
+ deviceMatches = false;
+ }
+ break;
+ }
+ default:
+ {
+ std::cerr << "unexpected dbus probe type " << probe.type_name()
+ << "\n";
+ deviceMatches = false;
+ }
+ }
+ return deviceMatches;
+}