Add a method to filter Devices

pull/146/head
Michael Zanetti 2019-04-03 18:16:13 +02:00
parent 7bd863b131
commit b2228bcf25
2 changed files with 21 additions and 1 deletions

View File

@ -268,3 +268,22 @@ QDebug operator<<(QDebug dbg, Device *device)
dbg.nospace() << ", deviceClassId" << device->deviceClassId() << ")"; dbg.nospace() << ", deviceClassId" << device->deviceClassId() << ")";
return dbg.space(); return dbg.space();
} }
/*! Filter a Devices list by a parameter. Only Devices having a parameter of the given
\a paramTypeId will be returned. If \a value is given and it is not null, only Devices
with the given \a paramTypeId and the same \a value will be returned.
*/
Devices Devices::filterByParam(const ParamTypeId &paramTypeId, const QVariant &value)
{
Devices ret;
foreach (Device* device, *this) {
if (paramTypeId != paramTypeId) {
continue;
}
if (!value.isNull() && device->paramValue(paramTypeId) != value) {
continue;
}
ret << device;
}
return ret;
}

View File

@ -96,12 +96,13 @@ private:
QDebug operator<<(QDebug dbg, Device *device); QDebug operator<<(QDebug dbg, Device *device);
class Devices: public QList<Device*> class LIBNYMEA_EXPORT Devices: public QList<Device*>
{ {
public: public:
Devices() = default; Devices() = default;
Devices(const QList<Device *> &other); Devices(const QList<Device *> &other);
Device* findById(const DeviceId &id); Device* findById(const DeviceId &id);
Devices filterByParam(const ParamTypeId &paramTypeId, const QVariant &value = QVariant());
}; };
#endif #endif