DEV_InitDeviceFromDescription(): Simplify code
@@ -608,34 +608,29 @@ static bool DEV_InitDeviceFromDescription(Device *device, const DeviceDescriptio
Q_ASSERT(sub.isValid());
const auto uniqueId = uniqueIdFromTemplate(sub.uniqueId, device->item(RAttrExtAddress)->toNumber());
+ Resource *rsub = DEV_GetSubDevice(device, nullptr, uniqueId);
- Resource *rsub = nullptr;
-
- for (auto *r : device->subDevices())
- {
- if (r->item(RAttrUniqueId)->toString() == uniqueId)
- {
- rsub = r; // already existing Resource* for sub-device
- break;
- }
- }
-
- if (!rsub && sub.restApi == QLatin1String("/sensors"))
+ if (rsub)
+ { }
+ else if (sub.restApi == QLatin1String("/sensors"))
{
rsub = DEV_InitSensorNodeFromDescription(device, sub, uniqueId);
}
- else if (!rsub && sub.restApi == QLatin1String("/lights"))
+ else if (sub.restApi == QLatin1String("/lights"))
{
- // TODO create LightNode for compatibility with v1
rsub = DEV_InitLightNodeFromDescription(device, sub, uniqueId);
}
else
{
- // TODO create dynamic Resource*
+ Q_ASSERT(nullptr); // TODO create dynamic Resource*
}
- if (rsub)
+ if (!rsub)
{
+ DBG_Printf(DBG_INFO, "sub-device: %s, failed to setup: %s\n", qPrintable(uniqueId), qPrintable(sub.type));
+ return false;
+ }
+
subCount++;
{
@@ -685,7 +680,7 @@ static bool DEV_InitDeviceFromDescription(Device *device, const DeviceDescriptio
rsub->addStateChange(stateChange);
}
}
- }
+
}
return subCount == description.subDevices.size();
