Merge PR #760: ESP Somfy RTS: Update to networkdevice interface
This commit is contained in:
commit
e3787e4a52
@ -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());
|
||||
|
||||
@ -95,6 +95,7 @@ public:
|
||||
|
||||
explicit EspSomfyRts(NetworkDeviceMonitor *monitor, QObject *parent = nullptr);
|
||||
|
||||
NetworkDeviceMonitor *monitor() const;
|
||||
QHostAddress address() const;
|
||||
|
||||
bool connected() const;
|
||||
|
||||
@ -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();
|
||||
});
|
||||
@ -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;
|
||||
@ -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);
|
||||
@ -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();
|
||||
|
||||
@ -46,6 +46,7 @@ public:
|
||||
typedef struct Result {
|
||||
QString name;
|
||||
QString firmwareVersion;
|
||||
QHostAddress address;
|
||||
NetworkDeviceInfo networkDeviceInfo;
|
||||
} Result;
|
||||
|
||||
@ -63,9 +64,10 @@ private:
|
||||
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();
|
||||
};
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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": [
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user