Skip to content

Commit f593953

Browse files
committed
DEV sort subdevices by specific order
E.g. a light comes before sensors. The order is given in the sub device JSON description.
1 parent 02d8599 commit f593953

File tree

6 files changed

+66
-5
lines changed

6 files changed

+66
-5
lines changed

de_web_plugin.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17808,6 +17808,18 @@ void DeRestPluginPrivate::pushSensorInfoToCore(Sensor *sensor)
1780817808

1780917809
Q_Q(DeRestPlugin);
1781017810

17811+
bool isMainSubDevice = false;
17812+
Device *device = static_cast<Device*>(sensor->parentResource());
17813+
17814+
if (device)
17815+
{
17816+
const auto &subs = device->subDevices();
17817+
if (!subs.empty() && subs.front() == sensor)
17818+
{
17819+
isMainSubDevice = true;
17820+
}
17821+
}
17822+
1781117823
if (sensor->modelId().startsWith(QLatin1String("FLS-NB")))
1781217824
{ } // use name from light
1781317825
else if (sensor->modelId().startsWith(QLatin1String("D1")) || sensor->modelId().startsWith(QLatin1String("S1")) ||
@@ -17819,19 +17831,19 @@ void DeRestPluginPrivate::pushSensorInfoToCore(Sensor *sensor)
1781917831
{ } // use name from ZHAPresence sensor only
1782017832
else if (sensor->modelId() == QLatin1String("WarningDevice") && sensor->type() == QLatin1String("ZHAAlarm"))
1782117833
{ } // use name from light
17822-
else if (!sensor->name().isEmpty())
17834+
else if (!sensor->name().isEmpty() || isMainSubDevice)
1782317835
{
17824-
q->nodeUpdated(sensor->address().ext(), QLatin1String("name"), sensor->name());
17836+
emit q->nodeUpdated(sensor->address().ext(), QLatin1String("name"), sensor->name());
1782517837
}
1782617838

1782717839
if (!sensor->modelId().isEmpty())
1782817840
{
17829-
q->nodeUpdated(sensor->address().ext(), QLatin1String("modelid"), sensor->modelId());
17841+
emit q->nodeUpdated(sensor->address().ext(), QLatin1String("modelid"), sensor->modelId());
1783017842
}
1783117843

1783217844
if (!sensor->manufacturer().isEmpty())
1783317845
{
17834-
q->nodeUpdated(sensor->address().ext(), QLatin1String("vendor"), sensor->manufacturer());
17846+
emit q->nodeUpdated(sensor->address().ext(), QLatin1String("vendor"), sensor->manufacturer());
1783517847
}
1783617848

1783717849
if (!sensor->swVersion().isEmpty())

device.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,13 @@ void Device::addSubDevice(Resource *sub)
15641564
{
15651565
hnd = sub->handle();
15661566
DEV_CheckReachable(this);
1567+
1568+
std::sort(d->subResourceHandles.begin(), d->subResourceHandles.end(), [](const auto &a, const auto &b)
1569+
{
1570+
if (a.order == 0) { return false; }
1571+
return a.order < b.order;
1572+
});
1573+
15671574
return;
15681575
}
15691576
}

device_descriptions.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,9 @@ static DDF_SubDeviceDescriptor DDF_ReadSubDeviceFile(const QString &path)
14891489
{
14901490
result.restApi = obj.value(QLatin1String("restapi")).toString();
14911491
}
1492+
1493+
result.order = obj.value(QLatin1String("order")).toInt(SUBDEVICE_DEFAULT_ORDER);
1494+
14921495
if (obj.contains(QLatin1String("uuid")))
14931496
{
14941497
const auto uniqueId = obj.value(QLatin1String("uuid"));
@@ -1678,3 +1681,28 @@ static DeviceDescription DDF_MergeGenericItems(const std::vector<DeviceDescripti
16781681

16791682
return result;
16801683
}
1684+
1685+
uint8_t DDF_GetSubDeviceOrder(const QString &type)
1686+
{
1687+
if (type.isEmpty() || type.startsWith(QLatin1String("CLIP")))
1688+
{
1689+
return SUBDEVICE_DEFAULT_ORDER;
1690+
}
1691+
1692+
if (_priv)
1693+
{
1694+
auto i = std::find_if(_priv->subDevices.cbegin(), _priv->subDevices.cend(), [&](const auto &sub)
1695+
{ return sub.name == type; });
1696+
1697+
if (i != _priv->subDevices.cend())
1698+
{
1699+
return i->order;
1700+
}
1701+
}
1702+
1703+
#ifdef QT_DEBUG
1704+
DBG_Printf(DBG_DDF, "DDF: No subdevice for type: %s\n", qPrintable(type));
1705+
#endif
1706+
1707+
return SUBDEVICE_DEFAULT_ORDER;
1708+
}

device_descriptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "resource.h"
1717
#include "sensor.h"
1818

19+
#define SUBDEVICE_DEFAULT_ORDER 200
20+
1921
class Event;
2022

2123
class DDF_ZclReport
@@ -186,6 +188,7 @@ class DDF_SubDeviceDescriptor
186188
QString restApi;
187189
QStringList uniqueId;
188190
std::vector<const char*> items; // RAttrName, etc.
191+
int order;
189192
};
190193

191194
inline bool isValid(const DDF_SubDeviceDescriptor &sub)

resource.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,9 +1477,19 @@ Resource::Handle R_CreateResourceHandle(const Resource *r, size_t containerIndex
14771477
result.hash = qHash(r->item(RAttrUniqueId)->toString());
14781478
result.index = containerIndex;
14791479
result.type = r->prefix()[1];
1480+
result.order = 0;
14801481

14811482
Q_ASSERT(result.type == 's' || result.type == 'l' || result.type == 'd' || result.type == 'g');
14821483
Q_ASSERT(isValid(result));
14831484

1485+
if (result.type == 's' || result.type == 'l')
1486+
{
1487+
const ResourceItem *type = r->item(RAttrType);
1488+
if (type)
1489+
{
1490+
result.order = DDF_GetSubDeviceOrder(type->toString());
1491+
}
1492+
}
1493+
14841494
return result;
14851495
}

resource.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ class Resource
459459
// 'L' LightNode
460460
// 'S' Sensor
461461
char type = 0;
462-
quint8 reserved = 0;
462+
quint8 order = 0;
463463
};
464464

465465
Resource(const char *prefix);
@@ -586,6 +586,7 @@ bool R_SetValueEventOnSet(Resource *r, const char *suffix, const V &val, Resourc
586586
return result;
587587
}
588588

589+
uint8_t DDF_GetSubDeviceOrder(const QString &type);
589590
QLatin1String R_DataTypeToString(ApiDataType type);
590591
Resource::Handle R_CreateResourceHandle(const Resource *r, size_t containerIndex);
591592
inline bool isValid(Resource::Handle hnd) { return hnd.hash != 0 && hnd.index < UINT16_MAX && hnd.type != 0; }

0 commit comments

Comments
 (0)