diff options
author | Sven Gothel <[email protected]> | 2021-08-05 08:40:18 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-08-05 08:40:18 +0200 |
commit | d3f225b2bf987c27802cc70183ec500e20349ea8 (patch) | |
tree | c7ac8d4695a145f3566af87d43d6b561241d3df9 | |
parent | 387647583ff2dd6a420ab60dd1f0225898f8b1c5 (diff) |
BTDeviceRegistry::DeviceQuery: Use explicit enum type distinguishing address or name to allow empty EUI48Sub for all devices
-rw-r--r-- | api/direct_bt/BTDeviceRegistry.hpp | 35 | ||||
-rw-r--r-- | java/org/direct_bt/BTDeviceRegistry.java | 42 | ||||
-rw-r--r-- | src/direct_bt/BTDeviceRegistry.cpp | 6 |
3 files changed, 57 insertions, 26 deletions
diff --git a/api/direct_bt/BTDeviceRegistry.hpp b/api/direct_bt/BTDeviceRegistry.hpp index 1ddae11b..d07af857 100644 --- a/api/direct_bt/BTDeviceRegistry.hpp +++ b/api/direct_bt/BTDeviceRegistry.hpp @@ -42,15 +42,31 @@ namespace direct_bt { * Specifies devices queries to act upon. */ struct DeviceQuery { + /** + * DeviceQuery type, i.e. EUI48Sub or a std::string name. + */ + enum class Type : int { + /** DeviceQuery type, using a sensor device EUI48Sub. */ + EUI48SUB, + /** DeviceQuery type, using a sensor device std::string name. */ + NAME + }; + + Type type; EUI48Sub addressSub; std::string nameSub; - DeviceQuery(const EUI48Sub& as, const std::string& ns) : addressSub(as), nameSub(ns) {} - DeviceQuery() : addressSub(), nameSub() {} + + DeviceQuery(const EUI48Sub& as) : type(Type::EUI48SUB), addressSub(as), nameSub(as.toString()) {} + + DeviceQuery(const std::string& ns) : type(Type::NAME), addressSub(EUI48Sub::ANY_DEVICE), nameSub(ns) {} + + bool isEUI48Sub() const noexcept { return Type::EUI48SUB == type; } + std::string toString() const { - if( addressSub.length>0 ) { - return addressSub.toString(); + if( Type::EUI48SUB == type ) { + return "[a: "+addressSub.toString()+"]"; } else { - return "'"+nameSub+"'"; + return "[n: '"+nameSub+"']"; } } }; @@ -136,8 +152,7 @@ namespace direct_bt { * Example (lambda): * <pre> * [](const EUI48& a, const std::string& n, const DeviceQuery& q)->bool { - * return ( q.addressSub.length>0 && a.contains(q.addressSub) ) || - * ( q.nameSub.length()>0 && n.find(q.nameSub) != std::string::npos ); + * return q.isEUI48Sub() ? a.contains(q.addressSub) : n.find(q.nameSub) != std::string::npos; * }); * </pre> * </p> @@ -169,8 +184,7 @@ namespace direct_bt { */ bool isWaitingForDevice(const EUI48 &address, const std::string &name) { return isWaitingForDevice(address, name, [](const EUI48& a, const std::string& n, const DeviceQuery& q)->bool { - return ( q.addressSub.length>0 && a.contains(q.addressSub) ) || - ( q.nameSub.length()>0 && n.find(q.nameSub) != std::string::npos ); + return q.isEUI48Sub() ? a.contains(q.addressSub) : n.find(q.nameSub) != std::string::npos; }); } @@ -199,8 +213,7 @@ namespace direct_bt { */ bool areAllDevicesProcessed() { return areAllDevicesProcessed( [](const EUI48& a, const std::string& n, const DeviceQuery& q)->bool { - return ( q.addressSub.length>0 && a.contains(q.addressSub) ) || - ( q.nameSub.length()>0 && n.find(q.nameSub) != std::string::npos ); + return q.isEUI48Sub() ? a.contains(q.addressSub) : n.find(q.nameSub) != std::string::npos; }); } diff --git a/java/org/direct_bt/BTDeviceRegistry.java b/java/org/direct_bt/BTDeviceRegistry.java index b4bde061..6199a927 100644 --- a/java/org/direct_bt/BTDeviceRegistry.java +++ b/java/org/direct_bt/BTDeviceRegistry.java @@ -42,18 +42,39 @@ public class BTDeviceRegistry { * Specifies devices queries to act upon. */ public static class DeviceQuery { + /** + * {@link DeviceQuery} type, i.e. {@link EUI48Sub} or a {@link String} name. + */ + public static enum Type { + /** {@link DeviceQuery} type, using a sensor device {@link EUI48Sub}. */ + EUI48SUB, + /** {@link DeviceQuery} type, using a sensor device {@link String} name. */ + NAME + } + + public final Type type; public final EUI48Sub addressSub; public final String nameSub; - public DeviceQuery(final EUI48Sub as, final String ns) { + + public DeviceQuery(final EUI48Sub as) { + type = Type.EUI48SUB; addressSub = as; + nameSub = as.toString(); + } + public DeviceQuery(final String ns) { + type = Type.NAME; + addressSub = EUI48Sub.ANY_DEVICE; nameSub = ns; } + + public final boolean isEUI48Sub() { return Type.EUI48SUB == type; } + @Override public String toString() { - if( addressSub.length>0 ) { - return addressSub.toString(); + if( Type.EUI48SUB == type ) { + return "[a: "+addressSub.toString()+"]"; } else { - return "'"+nameSub+"'"; + return "[n: '"+nameSub+"']"; } } }; @@ -113,10 +134,10 @@ public class BTDeviceRegistry { final EUI48Sub addr1 = new EUI48Sub(); final StringBuilder errmsg = new StringBuilder(); if( EUI48Sub.scanEUI48Sub(addrOrNameSub, addr1, errmsg) ) { - waitForDevices.add( new DeviceQuery( addr1, "" ) ); + waitForDevices.add( new DeviceQuery( addr1 ) ); } else { addr1.clear(); - waitForDevices.add( new DeviceQuery( addr1, addrOrNameSub ) ); + waitForDevices.add( new DeviceQuery( addrOrNameSub ) ); } } public static boolean isWaitingForAnyDevice() { @@ -178,8 +199,7 @@ public class BTDeviceRegistry { * Example (lambda): * <pre> * (final EUI48 a, final String n, final DeviceQuery q) -> { - * return ( q.addressSub.length>0 && a.contains(q.addressSub) ) || - * ( q.nameSub.length()>0 && n.indexOf(q.nameSub) >= 0 ); + * return q.isEUI48Sub() ? a.contains(q.addressSub) : n.indexOf(q.nameSub) >= 0; * } * </pre> * </p> @@ -223,8 +243,7 @@ public class BTDeviceRegistry { public static boolean isWaitingForDevice(final EUI48 address, final String name) { return isWaitingForDevice( address, name, (final EUI48 a, final String n, final DeviceQuery q) -> { - return ( q.addressSub.length>0 && a.contains(q.addressSub) ) || - ( q.nameSub.length()>0 && n.indexOf(q.nameSub) >= 0 ); + return q.isEUI48Sub() ? a.contains(q.addressSub) : n.indexOf(q.nameSub) >= 0; }); } @@ -267,8 +286,7 @@ public class BTDeviceRegistry { */ public static boolean areAllDevicesProcessed() { return areAllDevicesProcessed( (final EUI48 a, final String n, final DeviceQuery q) -> { - return ( q.addressSub.length>0 && a.contains(q.addressSub) ) || - ( q.nameSub.length()>0 && n.indexOf(q.nameSub) >= 0 ); + return q.isEUI48Sub() ? a.contains(q.addressSub) : n.indexOf(q.nameSub) >= 0; }); } diff --git a/src/direct_bt/BTDeviceRegistry.cpp b/src/direct_bt/BTDeviceRegistry.cpp index 71a10291..54123fa0 100644 --- a/src/direct_bt/BTDeviceRegistry.cpp +++ b/src/direct_bt/BTDeviceRegistry.cpp @@ -47,10 +47,10 @@ namespace direct_bt::BTDeviceRegistry { EUI48Sub addr1; std::string errmsg; if( EUI48Sub::scanEUI48Sub(addrOrNameSub, addr1, errmsg) ) { - waitForDevices.emplace_back( addr1, "" ); + waitForDevices.emplace_back( addr1 ); } else { addr1.clear(); - waitForDevices.emplace_back( addr1, addrOrNameSub ); + waitForDevices.emplace_back( addrOrNameSub ); } } bool isWaitingForAnyDevice() { @@ -63,7 +63,7 @@ namespace direct_bt::BTDeviceRegistry { std::string res; jau::for_each(waitForDevices.cbegin(), waitForDevices.cend(), [&res](const DeviceQuery &q) { if( res.length() > 0 ) { - res.append( ", " ); + res.append( ", " ); } res.append( q.toString() ); }); |