diff --git a/nanoleaf/integrationpluginnanoleaf.cpp b/nanoleaf/integrationpluginnanoleaf.cpp index 46addcf9..e00659c2 100644 --- a/nanoleaf/integrationpluginnanoleaf.cpp +++ b/nanoleaf/integrationpluginnanoleaf.cpp @@ -52,7 +52,8 @@ void IntegrationPluginNanoleaf::discoverThings(ThingDiscoveryInfo *info) QStringList serialNumbers; foreach (const ZeroConfServiceEntry &entry, m_zeroconfBrowser->serviceEntries()) { - ThingDescriptor descriptor(lightPanelsThingClassId, entry.name(), entry.hostAddress().toString()); + QHostAddress address = QHostAddress(entry.hostAddress().toString()); + ThingDescriptor descriptor(lightPanelsThingClassId, entry.name(), address.toString()); ParamList params; QString serialNo; @@ -80,8 +81,6 @@ void IntegrationPluginNanoleaf::discoverThings(ThingDiscoveryInfo *info) serialNumbers.append(serialNo); qCDebug(dcNanoleaf()) << "Have device" << entry.name() << serialNo << model << firmwareVersion; - params << Param(lightPanelsThingAddressParamTypeId, entry.hostAddress().toString()); - params << Param(lightPanelsThingPortParamTypeId, entry.port()); params << Param(lightPanelsThingModelParamTypeId, model); params << Param(lightPanelsThingSerialNoParamTypeId, serialNo); params << Param(lightPanelsThingFirmwareVersionParamTypeId, firmwareVersion); @@ -101,7 +100,9 @@ void IntegrationPluginNanoleaf::confirmPairing(ThingPairingInfo *info, const QSt { Q_UNUSED(username) Q_UNUSED(secret) - Nanoleaf *nanoleaf = createNanoleafConnection(QHostAddress(info->params().paramValue(lightPanelsThingAddressParamTypeId).toString()), info->params().paramValue(lightPanelsThingPortParamTypeId).toInt()); + QHostAddress address = getHostAddress(info->params().paramValue(lightPanelsThingSerialNoParamTypeId).toString()); + uint port = getPort(info->params().paramValue(lightPanelsThingSerialNoParamTypeId).toString()); + Nanoleaf *nanoleaf = createNanoleafConnection(address, port); nanoleaf->addUser(); //push button pairing m_unfinishedNanoleafConnections.insert(info->thingId(), nanoleaf); m_unfinishedPairing.insert(nanoleaf, info); @@ -116,6 +117,10 @@ void IntegrationPluginNanoleaf::setupThing(ThingSetupInfo *info) { Thing *thing = info->thing(); if(thing->thingClassId() == lightPanelsThingClassId) { + + QString thingSerialNo = thing->paramValue(lightPanelsThingSerialNoParamTypeId).toString(); + qCDebug(dcNanoleaf()) << "Setting up Nanoleaf light panel with serial number:" << thingSerialNo; + pluginStorage()->beginGroup(thing->id().toString()); QString token = pluginStorage()->value("authToken").toString(); pluginStorage()->endGroup(); @@ -128,8 +133,8 @@ void IntegrationPluginNanoleaf::setupThing(ThingSetupInfo *info) return info->finish(Thing::ThingErrorNoError); } else { // This setupDevice is called after a (re)start, with an already added thing - QHostAddress address(thing->paramValue(lightPanelsThingAddressParamTypeId).toString()); - int port = thing->paramValue(lightPanelsThingPortParamTypeId).toInt(); + QHostAddress address = getHostAddress(thing->paramValue(lightPanelsThingSerialNoParamTypeId).toString()); + int port = getPort(thing->paramValue(lightPanelsThingSerialNoParamTypeId).toString()); nanoleaf = createNanoleafConnection(address, port); nanoleaf->setAuthToken(token); nanoleaf->getControllerInfo(); //This is just to check if the thing is available @@ -263,6 +268,44 @@ Nanoleaf *IntegrationPluginNanoleaf::createNanoleafConnection(const QHostAddress return nanoleaf; } +QHostAddress IntegrationPluginNanoleaf::getHostAddress(const QString &serialNumber) +{ + ZeroConfServiceEntry entry; + foreach (const ZeroConfServiceEntry &e, m_zeroconfBrowser->serviceEntries()) { + QString entrySerialNo; + foreach (QString value, entry.txt()) { + if (value.contains("id=")) { + entrySerialNo = value.split("=").last(); + break; + } + } + if (serialNumber == entrySerialNo) { + entry = e; + break; + } + } + return QHostAddress(entry.hostAddress()); +} + +uint IntegrationPluginNanoleaf::getPort(const QString &serialNumber) +{ + ZeroConfServiceEntry entry; + foreach (const ZeroConfServiceEntry &e, m_zeroconfBrowser->serviceEntries()) { + QString entrySerialNo; + foreach (QString value, entry.txt()) { + if (value.contains("id=")) { + entrySerialNo = value.split("=").last(); + break; + } + } + if (serialNumber == entrySerialNo) { + entry = e; + break; + } + } + return entry.port(); +} + void IntegrationPluginNanoleaf::onAuthTokenReceived(const QString &token) { Nanoleaf *nanoleaf = static_cast(sender()); diff --git a/nanoleaf/integrationpluginnanoleaf.h b/nanoleaf/integrationpluginnanoleaf.h index c0a6cfb7..017ed2da 100644 --- a/nanoleaf/integrationpluginnanoleaf.h +++ b/nanoleaf/integrationpluginnanoleaf.h @@ -76,6 +76,8 @@ private: QHash m_asyncBrowserItem; Nanoleaf *createNanoleafConnection(const QHostAddress &address, int port); + QHostAddress getHostAddress(const QString &serialNumber); + uint getPort(const QString &serialNumber); public slots: void onAuthTokenReceived(const QString &token); diff --git a/nanoleaf/integrationpluginnanoleaf.json b/nanoleaf/integrationpluginnanoleaf.json index 38048e66..eaeee40a 100644 --- a/nanoleaf/integrationpluginnanoleaf.json +++ b/nanoleaf/integrationpluginnanoleaf.json @@ -17,19 +17,6 @@ "setupMethod": "pushButton", "browsable": true, "paramTypes": [ - { - "id": "ff57079f-d5ab-4511-8a5c-0726e7b82af6", - "name": "address", - "displayName": "Address", - "type" : "QString" - }, - { - "id": "ba4fd45b-990d-480a-859d-fff7ffba3ba4", - "name": "port", - "displayName": "Port", - "type" : "int", - "readOnly": true - }, { "id": "353d3c71-0ad2-40d5-99f6-cc305e2073f1", "name": "model", diff --git a/nanoleaf/nanoleaf.pro b/nanoleaf/nanoleaf.pro index 3a22fb34..90165867 100644 --- a/nanoleaf/nanoleaf.pro +++ b/nanoleaf/nanoleaf.pro @@ -1,7 +1,5 @@ include(../plugins.pri) -TARGET = $$qtLibraryTarget(nymea_integrationpluginnanoleaf) - QT += network SOURCES += \