diff --git a/mystrom/integrationpluginmystrom.cpp b/mystrom/integrationpluginmystrom.cpp index 943d456c..43d51226 100644 --- a/mystrom/integrationpluginmystrom.cpp +++ b/mystrom/integrationpluginmystrom.cpp @@ -59,16 +59,52 @@ void IntegrationPluginMyStrom::init() void IntegrationPluginMyStrom::discoverThings(ThingDiscoveryInfo *info) { + QList *pendingReplies = new QList(); + connect(info, &ThingDiscoveryInfo::finished, this, [pendingReplies](){ + delete pendingReplies; + }); + foreach (const ZeroConfServiceEntry &entry, m_zeroConf->serviceEntries()) { qCDebug(dcMyStrom()) << "Found myStrom device:" << entry; if (entry.protocol() != QAbstractSocket::IPv4Protocol) { continue; } - ThingDescriptor descriptor(switchThingClassId, entry.name(), entry.hostAddress().toString()); - descriptor.setParams({Param(switchThingIdParamTypeId, entry.txt("id"))}); - info->addThingDescriptor(descriptor); + QUrl infoUrl; + infoUrl.setScheme("http"); + infoUrl.setHost(entry.hostAddress().toString()); + infoUrl.setPath("/api/v1/info"); + + QNetworkRequest request(infoUrl); + QNetworkReply *reply = hardwareManager()->networkManager()->get(request); + pendingReplies->append(reply); + connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); + connect(reply, &QNetworkReply::finished, info, [=](){ + if (reply->error() != QNetworkReply::NoError) { + finishDiscoveryReply(reply, info, pendingReplies); + return; + } + QByteArray data = reply->readAll(); + QJsonParseError error; + QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); + if (error.error != QJsonParseError::NoError) { + finishDiscoveryReply(reply, info, pendingReplies); + return; + } + qCDebug(dcMyStrom) << "Info response:" << qUtf8Printable(jsonDoc.toJson()); + + QVariantMap deviceInfo = jsonDoc.toVariant().toMap(); + if (deviceInfo.value("type").toInt() == 107) { + ThingDescriptor descriptor(switchThingClassId, entry.name(), entry.hostAddress().toString()); + descriptor.setParams({Param(switchThingIdParamTypeId, entry.txt("id"))}); + info->addThingDescriptor(descriptor); + } + finishDiscoveryReply(reply, info, pendingReplies); + }); + } + + if (pendingReplies->isEmpty()) { + info->finish(Thing::ThingErrorNoError); } - info->finish(Thing::ThingErrorNoError); } void IntegrationPluginMyStrom::setupThing(ThingSetupInfo *info) @@ -98,6 +134,12 @@ void IntegrationPluginMyStrom::setupThing(ThingSetupInfo *info) } qCDebug(dcMyStrom()) << "Device info:" << qUtf8Printable(jsonDoc.toJson(QJsonDocument::Indented)); + QVariantMap deviceInfo = jsonDoc.toVariant().toMap(); + if (deviceInfo.value("type").toInt() != 107) { + qCWarning(dcMyStrom()) << "This device does not seem to be a myStrom WiFi switch"; + info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("This device does not seem to be a myStrom WiFi switch.")); + return; + } info->finish(Thing::ThingErrorNoError); info->thing()->setStateValue("connected", true); @@ -185,6 +227,14 @@ void IntegrationPluginMyStrom::executeAction(ThingActionInfo *info) } } +void IntegrationPluginMyStrom::finishDiscoveryReply(QNetworkReply *reply, ThingDiscoveryInfo *info, QList *pendingReplies) +{ + pendingReplies->removeAll(reply); + if (pendingReplies->isEmpty()) { + info->finish(Thing::ThingErrorNoError); + } +} + QUrl IntegrationPluginMyStrom::composeUrl(Thing *thing, const QString &path) { QHostAddress address; diff --git a/mystrom/integrationpluginmystrom.h b/mystrom/integrationpluginmystrom.h index 4084a7d3..8091eaa4 100644 --- a/mystrom/integrationpluginmystrom.h +++ b/mystrom/integrationpluginmystrom.h @@ -34,6 +34,7 @@ #include "integrations/integrationplugin.h" #include +#include class PluginTimer; class ZeroConfServiceBrowser; @@ -57,6 +58,7 @@ public: void executeAction(ThingActionInfo *info) override; private: + void finishDiscoveryReply(QNetworkReply* reply, ThingDiscoveryInfo* info, QList *pendingReplies); QUrl composeUrl(Thing *thing, const QString &path); ZeroConfServiceBrowser *m_zeroConf = nullptr; diff --git a/mystrom/translations/fc7f1a24-42a8-45ce-9ffb-136292eb0164-de.ts b/mystrom/translations/fc7f1a24-42a8-45ce-9ffb-136292eb0164-de.ts index 0bf735c5..a8cb2cd8 100644 --- a/mystrom/translations/fc7f1a24-42a8-45ce-9ffb-136292eb0164-de.ts +++ b/mystrom/translations/fc7f1a24-42a8-45ce-9ffb-136292eb0164-de.ts @@ -4,22 +4,27 @@ IntegrationPluginMyStrom - + Device cannot be found on the network. Das Gerät konnte nicht im Netzwerk gefunden werden. - + Error fetching device information from myStrom device. Fehler beim Empfangen der Geräteinformationen. - + Error processing response from myStrom device. Fehler beim Bearbeiten der Antwort des myStrom Gerätes. - + + This device does not seem to be a myStrom WiFi switch. + Dieses Gerät scheint kein myStrom WiFi switch zu sein. + + + Error communicating with myStrom device. Fehler in der Kommunikation zum myStrom Gerät. @@ -27,98 +32,53 @@ myStrom - - + Connected - The name of the ParamType (ThingClass: switch, EventType: connected, ID: {8864755d-4e6a-4e45-b2db-3eb5d4f3d53d}) ----------- -The name of the StateType ({8864755d-4e6a-4e45-b2db-3eb5d4f3d53d}) of ThingClass switch + The name of the StateType ({8864755d-4e6a-4e45-b2db-3eb5d4f3d53d}) of ThingClass switch Verbunden - - Connected changed - The name of the EventType ({8864755d-4e6a-4e45-b2db-3eb5d4f3d53d}) of ThingClass switch - Verbindung hergestellt oder getrennt - - - - + Current power consumption - The name of the ParamType (ThingClass: switch, EventType: currentPower, ID: {ccb52b57-5800-4f03-b7fa-f36dcebe1d4e}) ----------- -The name of the StateType ({ccb52b57-5800-4f03-b7fa-f36dcebe1d4e}) of ThingClass switch + The name of the StateType ({ccb52b57-5800-4f03-b7fa-f36dcebe1d4e}) of ThingClass switch Aktueller Stromverbrauch - - Current power consumption changed - The name of the EventType ({ccb52b57-5800-4f03-b7fa-f36dcebe1d4e}) of ThingClass switch - Aktueller Stromverbrauch geändert - - - + ID The name of the ParamType (ThingClass: switch, Type: thing, ID: {44144c44-a447-4cee-b77a-18454a779a9c}) ID - - - + + Power The name of the ParamType (ThingClass: switch, ActionType: power, ID: {717f2593-1544-483b-aac8-f9800b299e4d}) ---------- -The name of the ParamType (ThingClass: switch, EventType: power, ID: {717f2593-1544-483b-aac8-f9800b299e4d}) ----------- The name of the StateType ({717f2593-1544-483b-aac8-f9800b299e4d}) of ThingClass switch An - - + Signal strength - The name of the ParamType (ThingClass: switch, EventType: signalStrength, ID: {71723ea3-d2a1-48c4-9a2c-532d938e5022}) ----------- -The name of the StateType ({71723ea3-d2a1-48c4-9a2c-532d938e5022}) of ThingClass switch + The name of the StateType ({71723ea3-d2a1-48c4-9a2c-532d938e5022}) of ThingClass switch Signalstärke - - Signal strength changed - The name of the EventType ({71723ea3-d2a1-48c4-9a2c-532d938e5022}) of ThingClass switch - Signalstärke geändert - - - - + Total energy consumed - The name of the ParamType (ThingClass: switch, EventType: totalEnergyConsumed, ID: {a3533121-69ee-44fd-8394-13373e8f960e}) ----------- -The name of the StateType ({a3533121-69ee-44fd-8394-13373e8f960e}) of ThingClass switch + The name of the StateType ({a3533121-69ee-44fd-8394-13373e8f960e}) of ThingClass switch Gesamtenergieverbrauch - - Total energy consumed changed - The name of the EventType ({a3533121-69ee-44fd-8394-13373e8f960e}) of ThingClass switch - Gesamtenergieverbrauch geändert - - - + Turn on or off The name of the ActionType ({717f2593-1544-483b-aac8-f9800b299e4d}) of ThingClass switch Ein- oder ausschalten - - Turned on or off - The name of the EventType ({717f2593-1544-483b-aac8-f9800b299e4d}) of ThingClass switch - Ein- oder ausgeschaltet - - - - + + myStrom The name of the vendor ({884b2875-759a-4373-aca4-6a8cb7220f85}) ---------- @@ -126,7 +86,7 @@ The name of the plugin myStrom ({fc7f1a24-42a8-45ce-9ffb-136292eb0164})myStrom - + myStrom WiFi Switch The name of the ThingClass ({27dc49c0-58cd-4d5f-a95b-0c437dd828cf}) myStrom WiFi Switch diff --git a/mystrom/translations/fc7f1a24-42a8-45ce-9ffb-136292eb0164-en_US.ts b/mystrom/translations/fc7f1a24-42a8-45ce-9ffb-136292eb0164-en_US.ts index 3e30f8f8..ff57ff08 100644 --- a/mystrom/translations/fc7f1a24-42a8-45ce-9ffb-136292eb0164-en_US.ts +++ b/mystrom/translations/fc7f1a24-42a8-45ce-9ffb-136292eb0164-en_US.ts @@ -4,22 +4,27 @@ IntegrationPluginMyStrom - + Device cannot be found on the network. - + Error fetching device information from myStrom device. - + Error processing response from myStrom device. - + + This device does not seem to be a myStrom WiFi switch. + + + + Error communicating with myStrom device. @@ -27,98 +32,53 @@ myStrom - - + Connected - The name of the ParamType (ThingClass: switch, EventType: connected, ID: {8864755d-4e6a-4e45-b2db-3eb5d4f3d53d}) ----------- -The name of the StateType ({8864755d-4e6a-4e45-b2db-3eb5d4f3d53d}) of ThingClass switch + The name of the StateType ({8864755d-4e6a-4e45-b2db-3eb5d4f3d53d}) of ThingClass switch - - Connected changed - The name of the EventType ({8864755d-4e6a-4e45-b2db-3eb5d4f3d53d}) of ThingClass switch - - - - - + Current power consumption - The name of the ParamType (ThingClass: switch, EventType: currentPower, ID: {ccb52b57-5800-4f03-b7fa-f36dcebe1d4e}) ----------- -The name of the StateType ({ccb52b57-5800-4f03-b7fa-f36dcebe1d4e}) of ThingClass switch + The name of the StateType ({ccb52b57-5800-4f03-b7fa-f36dcebe1d4e}) of ThingClass switch - - Current power consumption changed - The name of the EventType ({ccb52b57-5800-4f03-b7fa-f36dcebe1d4e}) of ThingClass switch - - - - + ID The name of the ParamType (ThingClass: switch, Type: thing, ID: {44144c44-a447-4cee-b77a-18454a779a9c}) - - - + + Power The name of the ParamType (ThingClass: switch, ActionType: power, ID: {717f2593-1544-483b-aac8-f9800b299e4d}) ---------- -The name of the ParamType (ThingClass: switch, EventType: power, ID: {717f2593-1544-483b-aac8-f9800b299e4d}) ----------- The name of the StateType ({717f2593-1544-483b-aac8-f9800b299e4d}) of ThingClass switch - - + Signal strength - The name of the ParamType (ThingClass: switch, EventType: signalStrength, ID: {71723ea3-d2a1-48c4-9a2c-532d938e5022}) ----------- -The name of the StateType ({71723ea3-d2a1-48c4-9a2c-532d938e5022}) of ThingClass switch + The name of the StateType ({71723ea3-d2a1-48c4-9a2c-532d938e5022}) of ThingClass switch - - Signal strength changed - The name of the EventType ({71723ea3-d2a1-48c4-9a2c-532d938e5022}) of ThingClass switch - - - - - + Total energy consumed - The name of the ParamType (ThingClass: switch, EventType: totalEnergyConsumed, ID: {a3533121-69ee-44fd-8394-13373e8f960e}) ----------- -The name of the StateType ({a3533121-69ee-44fd-8394-13373e8f960e}) of ThingClass switch + The name of the StateType ({a3533121-69ee-44fd-8394-13373e8f960e}) of ThingClass switch - - Total energy consumed changed - The name of the EventType ({a3533121-69ee-44fd-8394-13373e8f960e}) of ThingClass switch - - - - + Turn on or off The name of the ActionType ({717f2593-1544-483b-aac8-f9800b299e4d}) of ThingClass switch - - Turned on or off - The name of the EventType ({717f2593-1544-483b-aac8-f9800b299e4d}) of ThingClass switch - - - - - + + myStrom The name of the vendor ({884b2875-759a-4373-aca4-6a8cb7220f85}) ---------- @@ -126,7 +86,7 @@ The name of the plugin myStrom ({fc7f1a24-42a8-45ce-9ffb-136292eb0164}) - + myStrom WiFi Switch The name of the ThingClass ({27dc49c0-58cd-4d5f-a95b-0c437dd828cf})