Skip to content

Commit b37026e

Browse files
author
jenkins
committed
Merge PR #760: ESP Somfy RTS: Update to networkdevice interface
2 parents bd30d2b + 12ca5b7 commit b37026e

6 files changed

+61
-26
lines changed

Diff for: espsomfyrts/espsomfyrts.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ EspSomfyRts::EspSomfyRts(NetworkDeviceMonitor *monitor, QObject *parent)
8080
});
8181
}
8282

83+
NetworkDeviceMonitor *EspSomfyRts::monitor() const
84+
{
85+
return m_monitor;
86+
}
87+
8388
QHostAddress EspSomfyRts::address() const
8489
{
8590
return QHostAddress(m_websocketUrl.host());

Diff for: espsomfyrts/espsomfyrts.h

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class EspSomfyRts : public QObject
9595

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

98+
NetworkDeviceMonitor *monitor() const;
9899
QHostAddress address() const;
99100

100101
bool connected() const;

Diff for: espsomfyrts/espsomfyrtsdiscovery.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ void EspSomfyRtsDiscovery::startDiscovery()
5353
m_startDateTime = QDateTime::currentDateTime();
5454

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

69-
void EspSomfyRtsDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo)
70+
void EspSomfyRtsDiscovery::checkNetworkDevice(const QHostAddress &address)
7071
{
71-
qCDebug(dcESPSomfyRTS()) << "Discovery: Verifying" << networkDeviceInfo;
72+
qCDebug(dcESPSomfyRTS()) << "Discovery: Verifying" << address;
7273
QUrl url;
7374
url.setScheme("http");
74-
url.setHost(networkDeviceInfo.address().toString());
75+
url.setHost(address.toString());
7576
url.setPort(8081);
7677
url.setPath("/discovery");
7778

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

9798
Result result;
98-
result.networkDeviceInfo = networkDeviceInfo;
99+
result.address = address;
99100
result.name = responseMap.value("serverId").toString();
100101
result.firmwareVersion = responseMap.value("version").toString();
101102
m_results.append(result);
@@ -109,6 +110,11 @@ void EspSomfyRtsDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDe
109110
void EspSomfyRtsDiscovery::finishDiscovery()
110111
{
111112
qint64 durationMilliSeconds = QDateTime::currentMSecsSinceEpoch() - m_startDateTime.toMSecsSinceEpoch();
113+
114+
// Fill in all network device infos we have
115+
for (int i = 0; i < m_results.count(); i++)
116+
m_results[i].networkDeviceInfo = m_networkDeviceInfos.get(m_results.at(i).address);
117+
112118
qCDebug(dcESPSomfyRTS()) << "Discovery: Finished the discovery process. Found" << m_results.count()
113119
<< "ESPSomfy-RTS devices in" << QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz");
114120
m_gracePeriodTimer.stop();

Diff for: espsomfyrts/espsomfyrtsdiscovery.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class EspSomfyRtsDiscovery : public QObject
4646
typedef struct Result {
4747
QString name;
4848
QString firmwareVersion;
49+
QHostAddress address;
4950
NetworkDeviceInfo networkDeviceInfo;
5051
} Result;
5152

@@ -63,9 +64,10 @@ class EspSomfyRtsDiscovery : public QObject
6364
QTimer m_gracePeriodTimer;
6465
QDateTime m_startDateTime;
6566

67+
NetworkDeviceInfos m_networkDeviceInfos;
6668
QList<EspSomfyRtsDiscovery::Result> m_results;
6769

68-
void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo);
70+
void checkNetworkDevice(const QHostAddress &address);
6971

7072
void finishDiscovery();
7173
};

Diff for: espsomfyrts/integrationpluginespsomfyrts.cpp

+19-17
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,25 @@ void IntegrationPluginEspSomfyRts::discoverThings(ThingDiscoveryInfo *info)
6666
qCInfo(dcESPSomfyRTS()) << "Discovery finished. Found" << discovery->results().count() << "devices";
6767
foreach (const EspSomfyRtsDiscovery::Result &result, discovery->results()) {
6868
qCInfo(dcESPSomfyRTS()) << "Discovered device on" << result.networkDeviceInfo;
69-
if (result.networkDeviceInfo.macAddress().isNull())
70-
continue;
7169

7270
QString title = "ESP Somfy RTS (" + result.name + ")";
73-
QString description = result.networkDeviceInfo.address().toString() + " (" + result.networkDeviceInfo.macAddress() + ")";
71+
QString description = result.networkDeviceInfo.address().toString();
7472

7573
ThingDescriptor descriptor(espSomfyRtsThingClassId, title, description);
7674

75+
ParamList params;
76+
params << Param(espSomfyRtsThingMacAddressParamTypeId, result.networkDeviceInfo.thingParamValueMacAddress());
77+
params << Param(espSomfyRtsThingHostNameParamTypeId, result.networkDeviceInfo.thingParamValueHostName());
78+
params << Param(espSomfyRtsThingAddressParamTypeId, result.networkDeviceInfo.thingParamValueAddress());
79+
descriptor.setParams(params);
80+
7781
// Check if we already have set up this device
78-
Things existingThings = myThings().filterByParam(espSomfyRtsThingMacAddressParamTypeId, result.networkDeviceInfo.macAddress());
79-
if (existingThings.count() == 1) {
80-
qCDebug(dcESPSomfyRTS()) << "This thing already exists in the system." << existingThings.first() << result.networkDeviceInfo;
81-
descriptor.setThingId(existingThings.first()->id());
82+
Thing *existingThing = myThings().findByParams(params);
83+
if (existingThing) {
84+
qCDebug(dcESPSomfyRTS()) << "This thing already exists in the system:" << result.networkDeviceInfo;
85+
descriptor.setThingId(existingThing->id());
8286
}
8387

84-
ParamList params;
85-
params << Param(espSomfyRtsThingMacAddressParamTypeId, result.networkDeviceInfo.macAddress());
86-
descriptor.setParams(params);
8788
info->addThingDescriptor(descriptor);
8889
}
8990

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

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

114-
NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(macAddress);
115-
116115
EspSomfyRts *somfy = new EspSomfyRts(monitor, thing);
117116
m_somfys.insert(thing, somfy);
118117

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

172171
void IntegrationPluginEspSomfyRts::thingRemoved(Thing *thing)
173172
{
174-
Q_UNUSED(thing)
173+
if (thing->thingClassId() == espSomfyRtsThingClassId) {
174+
EspSomfyRts *somfy = m_somfys.take(thing);
175+
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(somfy->monitor());
176+
}
175177
}
176178

177179
void IntegrationPluginEspSomfyRts::executeAction(ThingActionInfo *info)

Diff for: espsomfyrts/integrationpluginespsomfyrts.json

+21-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,32 @@
1313
"displayName": "ESPSomfy-RTS",
1414
"id": "9a477bbe-81f0-46ad-ae62-715c2bba2f1f",
1515
"createMethods": ["Discovery", "User"],
16-
"interfaces": ["gateway", "wirelessconnectable" ],
16+
"interfaces": ["gateway", "wirelessconnectable", "networkdevice"],
1717
"paramTypes": [
18+
{
19+
"id": "3e473059-dc06-4da6-93e5-b27db497a887",
20+
"name": "address",
21+
"displayName": "Host address",
22+
"type": "QString",
23+
"inputType": "IPv4Address",
24+
"defaultValue": ""
25+
},
26+
{
27+
"id": "6426dbbd-978f-4e69-bc07-2d35fd8be88b",
28+
"name": "hostName",
29+
"displayName": "Host name",
30+
"type": "QString",
31+
"inputType": "TextLine",
32+
"defaultValue": ""
33+
},
1834
{
1935
"id": "0e30e30f-ad96-417e-b739-cac85f75de39",
2036
"name":"macAddress",
2137
"displayName": "MAC address",
22-
"type": "QString"
38+
"type": "QString",
39+
"inputType": "MacAddress",
40+
"readOnly": true,
41+
"defaultValue": ""
2342
}
2443
],
2544
"stateTypes": [

0 commit comments

Comments
 (0)