Skip to content

Commit

Permalink
EspSomfyRts: Update to networkdevice interface
Browse files Browse the repository at this point in the history
  • Loading branch information
t-mon committed Dec 18, 2024
1 parent 61f7364 commit 12ca5b7
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 26 deletions.
5 changes: 5 additions & 0 deletions espsomfyrts/espsomfyrts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ EspSomfyRts::EspSomfyRts(NetworkDeviceMonitor *monitor, QObject *parent)
});
}

NetworkDeviceMonitor *EspSomfyRts::monitor() const
{
return m_monitor;
}

QHostAddress EspSomfyRts::address() const
{
return QHostAddress(m_websocketUrl.host());
Expand Down
1 change: 1 addition & 0 deletions espsomfyrts/espsomfyrts.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class EspSomfyRts : public QObject

explicit EspSomfyRts(NetworkDeviceMonitor *monitor, QObject *parent = nullptr);

NetworkDeviceMonitor *monitor() const;
QHostAddress address() const;

bool connected() const;
Expand Down
18 changes: 12 additions & 6 deletions espsomfyrts/espsomfyrtsdiscovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ void EspSomfyRtsDiscovery::startDiscovery()
m_startDateTime = QDateTime::currentDateTime();

NetworkDeviceDiscoveryReply *discoveryReply = m_networkDeviceDiscovery->discover();
connect(discoveryReply, &NetworkDeviceDiscoveryReply::networkDeviceInfoAdded, this, &EspSomfyRtsDiscovery::checkNetworkDevice);
connect(discoveryReply, &NetworkDeviceDiscoveryReply::hostAddressDiscovered, this, &EspSomfyRtsDiscovery::checkNetworkDevice);
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){
qCDebug(dcESPSomfyRTS()) << "Discovery: Network discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "network devices";
m_networkDeviceInfos = discoveryReply->networkDeviceInfos();
m_gracePeriodTimer.start();
discoveryReply->deleteLater();
});
Expand All @@ -66,18 +67,18 @@ QList<EspSomfyRtsDiscovery::Result> EspSomfyRtsDiscovery::results() const
return m_results;
}

void EspSomfyRtsDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo)
void EspSomfyRtsDiscovery::checkNetworkDevice(const QHostAddress &address)
{
qCDebug(dcESPSomfyRTS()) << "Discovery: Verifying" << networkDeviceInfo;
qCDebug(dcESPSomfyRTS()) << "Discovery: Verifying" << address;
QUrl url;
url.setScheme("http");
url.setHost(networkDeviceInfo.address().toString());
url.setHost(address.toString());
url.setPort(8081);
url.setPath("/discovery");

QNetworkReply *reply = m_networkManager->get(QNetworkRequest(url));
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [this, reply, networkDeviceInfo](){
connect(reply, &QNetworkReply::finished, this, [this, reply, address](){
if (reply->error() != QNetworkReply::NoError) {
qCDebug(dcESPSomfyRTS()) << "Discovery: Reply finished with error" << reply->errorString() << "Continue...";
return;
Expand All @@ -95,7 +96,7 @@ void EspSomfyRtsDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDe
if (responseMap.contains("model") && responseMap.value("model").toString().toLower().contains("espsomfyrts")) {

Result result;
result.networkDeviceInfo = networkDeviceInfo;
result.address = address;
result.name = responseMap.value("serverId").toString();
result.firmwareVersion = responseMap.value("version").toString();
m_results.append(result);
Expand All @@ -109,6 +110,11 @@ void EspSomfyRtsDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDe
void EspSomfyRtsDiscovery::finishDiscovery()
{
qint64 durationMilliSeconds = QDateTime::currentMSecsSinceEpoch() - m_startDateTime.toMSecsSinceEpoch();

// Fill in all network device infos we have
for (int i = 0; i < m_results.count(); i++)
m_results[i].networkDeviceInfo = m_networkDeviceInfos.get(m_results.at(i).address);

qCDebug(dcESPSomfyRTS()) << "Discovery: Finished the discovery process. Found" << m_results.count()
<< "ESPSomfy-RTS devices in" << QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz");
m_gracePeriodTimer.stop();
Expand Down
4 changes: 3 additions & 1 deletion espsomfyrts/espsomfyrtsdiscovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class EspSomfyRtsDiscovery : public QObject
typedef struct Result {
QString name;
QString firmwareVersion;
QHostAddress address;
NetworkDeviceInfo networkDeviceInfo;
} Result;

Expand All @@ -63,9 +64,10 @@ class EspSomfyRtsDiscovery : public QObject
QTimer m_gracePeriodTimer;
QDateTime m_startDateTime;

NetworkDeviceInfos m_networkDeviceInfos;
QList<EspSomfyRtsDiscovery::Result> m_results;

void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo);
void checkNetworkDevice(const QHostAddress &address);

void finishDiscovery();
};
Expand Down
36 changes: 19 additions & 17 deletions espsomfyrts/integrationpluginespsomfyrts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,25 @@ void IntegrationPluginEspSomfyRts::discoverThings(ThingDiscoveryInfo *info)
qCInfo(dcESPSomfyRTS()) << "Discovery finished. Found" << discovery->results().count() << "devices";
foreach (const EspSomfyRtsDiscovery::Result &result, discovery->results()) {
qCInfo(dcESPSomfyRTS()) << "Discovered device on" << result.networkDeviceInfo;
if (result.networkDeviceInfo.macAddress().isNull())
continue;

QString title = "ESP Somfy RTS (" + result.name + ")";
QString description = result.networkDeviceInfo.address().toString() + " (" + result.networkDeviceInfo.macAddress() + ")";
QString description = result.networkDeviceInfo.address().toString();

ThingDescriptor descriptor(espSomfyRtsThingClassId, title, description);

ParamList params;
params << Param(espSomfyRtsThingMacAddressParamTypeId, result.networkDeviceInfo.thingParamValueMacAddress());
params << Param(espSomfyRtsThingHostNameParamTypeId, result.networkDeviceInfo.thingParamValueHostName());
params << Param(espSomfyRtsThingAddressParamTypeId, result.networkDeviceInfo.thingParamValueAddress());
descriptor.setParams(params);

// Check if we already have set up this device
Things existingThings = myThings().filterByParam(espSomfyRtsThingMacAddressParamTypeId, result.networkDeviceInfo.macAddress());
if (existingThings.count() == 1) {
qCDebug(dcESPSomfyRTS()) << "This thing already exists in the system." << existingThings.first() << result.networkDeviceInfo;
descriptor.setThingId(existingThings.first()->id());
Thing *existingThing = myThings().findByParams(params);
if (existingThing) {
qCDebug(dcESPSomfyRTS()) << "This thing already exists in the system:" << result.networkDeviceInfo;
descriptor.setThingId(existingThing->id());
}

ParamList params;
params << Param(espSomfyRtsThingMacAddressParamTypeId, result.networkDeviceInfo.macAddress());
descriptor.setParams(params);
info->addThingDescriptor(descriptor);
}

Expand All @@ -104,15 +105,13 @@ void IntegrationPluginEspSomfyRts::setupThing(ThingSetupInfo *info)
return;
}

MacAddress macAddress(thing->paramValue(espSomfyRtsThingMacAddressParamTypeId).toString());
if (!macAddress.isValid()) {
qCWarning(dcESPSomfyRTS()) << "Invalid MAC address, cannot set up thing" << thing << thing->params();
info->finish(Thing::ThingErrorHardwareNotAvailable);
NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(thing);
if (!monitor) {
qCWarning(dcESPSomfyRTS()) << "Could not register monitor with the given parameters" << thing << thing->params();
info->finish(Thing::ThingErrorInvalidParameter);
return;
}

NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(macAddress);

EspSomfyRts *somfy = new EspSomfyRts(monitor, thing);
m_somfys.insert(thing, somfy);

Expand Down Expand Up @@ -171,7 +170,10 @@ void IntegrationPluginEspSomfyRts::postSetupThing(Thing *thing)

void IntegrationPluginEspSomfyRts::thingRemoved(Thing *thing)
{
Q_UNUSED(thing)
if (thing->thingClassId() == espSomfyRtsThingClassId) {
EspSomfyRts *somfy = m_somfys.take(thing);
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(somfy->monitor());
}
}

void IntegrationPluginEspSomfyRts::executeAction(ThingActionInfo *info)
Expand Down
23 changes: 21 additions & 2 deletions espsomfyrts/integrationpluginespsomfyrts.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,32 @@
"displayName": "ESPSomfy-RTS",
"id": "9a477bbe-81f0-46ad-ae62-715c2bba2f1f",
"createMethods": ["Discovery", "User"],
"interfaces": ["gateway", "wirelessconnectable" ],
"interfaces": ["gateway", "wirelessconnectable", "networkdevice"],
"paramTypes": [
{
"id": "3e473059-dc06-4da6-93e5-b27db497a887",
"name": "address",
"displayName": "Host address",
"type": "QString",
"inputType": "IPv4Address",
"defaultValue": ""
},
{
"id": "6426dbbd-978f-4e69-bc07-2d35fd8be88b",
"name": "hostName",
"displayName": "Host name",
"type": "QString",
"inputType": "TextLine",
"defaultValue": ""
},
{
"id": "0e30e30f-ad96-417e-b739-cac85f75de39",
"name":"macAddress",
"displayName": "MAC address",
"type": "QString"
"type": "QString",
"inputType": "MacAddress",
"readOnly": true,
"defaultValue": ""
}
],
"stateTypes": [
Expand Down

0 comments on commit 12ca5b7

Please sign in to comment.