myStrom: Improve discovery mechanism

pull/599/head
Michael Zanetti 2022-08-25 12:05:54 +02:00
parent 860fbac0e8
commit c7c0a8eacd
4 changed files with 104 additions and 132 deletions

View File

@ -59,16 +59,52 @@ void IntegrationPluginMyStrom::init()
void IntegrationPluginMyStrom::discoverThings(ThingDiscoveryInfo *info)
{
QList<QNetworkReply*> *pendingReplies = new QList<QNetworkReply*>();
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<QNetworkReply *> *pendingReplies)
{
pendingReplies->removeAll(reply);
if (pendingReplies->isEmpty()) {
info->finish(Thing::ThingErrorNoError);
}
}
QUrl IntegrationPluginMyStrom::composeUrl(Thing *thing, const QString &path)
{
QHostAddress address;

View File

@ -34,6 +34,7 @@
#include "integrations/integrationplugin.h"
#include <QUrlQuery>
#include <QNetworkReply>
class PluginTimer;
class ZeroConfServiceBrowser;
@ -57,6 +58,7 @@ public:
void executeAction(ThingActionInfo *info) override;
private:
void finishDiscoveryReply(QNetworkReply* reply, ThingDiscoveryInfo* info, QList<QNetworkReply*> *pendingReplies);
QUrl composeUrl(Thing *thing, const QString &path);
ZeroConfServiceBrowser *m_zeroConf = nullptr;

View File

@ -4,22 +4,27 @@
<context>
<name>IntegrationPluginMyStrom</name>
<message>
<location filename="../integrationpluginmystrom.cpp" line="78"/>
<location filename="../integrationpluginmystrom.cpp" line="109"/>
<source>Device cannot be found on the network.</source>
<translation>Das Gerät konnte nicht im Netzwerk gefunden werden.</translation>
</message>
<message>
<location filename="../integrationpluginmystrom.cpp" line="87"/>
<location filename="../integrationpluginmystrom.cpp" line="119"/>
<source>Error fetching device information from myStrom device.</source>
<translation>Fehler beim Empfangen der Geräteinformationen.</translation>
</message>
<message>
<location filename="../integrationpluginmystrom.cpp" line="95"/>
<location filename="../integrationpluginmystrom.cpp" line="127"/>
<source>Error processing response from myStrom device.</source>
<translation>Fehler beim Bearbeiten der Antwort des myStrom Gerätes.</translation>
</message>
<message>
<location filename="../integrationpluginmystrom.cpp" line="170"/>
<location filename="../integrationpluginmystrom.cpp" line="135"/>
<source>This device does not seem to be a myStrom WiFi switch.</source>
<translation>Dieses Gerät scheint kein myStrom WiFi switch zu sein.</translation>
</message>
<message>
<location filename="../integrationpluginmystrom.cpp" line="213"/>
<source>Error communicating with myStrom device.</source>
<translation>Fehler in der Kommunikation zum myStrom Gerät.</translation>
</message>
@ -27,98 +32,53 @@
<context>
<name>myStrom</name>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="46"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="49"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="36"/>
<source>Connected</source>
<extracomment>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</extracomment>
<extracomment>The name of the StateType ({8864755d-4e6a-4e45-b2db-3eb5d4f3d53d}) of ThingClass switch</extracomment>
<translation>Verbunden</translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="52"/>
<source>Connected changed</source>
<extracomment>The name of the EventType ({8864755d-4e6a-4e45-b2db-3eb5d4f3d53d}) of ThingClass switch</extracomment>
<translation>Verbindung hergestellt oder getrennt</translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="55"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="58"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="39"/>
<source>Current power consumption</source>
<extracomment>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</extracomment>
<extracomment>The name of the StateType ({ccb52b57-5800-4f03-b7fa-f36dcebe1d4e}) of ThingClass switch</extracomment>
<translation>Aktueller Stromverbrauch</translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="61"/>
<source>Current power consumption changed</source>
<extracomment>The name of the EventType ({ccb52b57-5800-4f03-b7fa-f36dcebe1d4e}) of ThingClass switch</extracomment>
<translation>Aktueller Stromverbrauch geändert</translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="64"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="42"/>
<source>ID</source>
<extracomment>The name of the ParamType (ThingClass: switch, Type: thing, ID: {44144c44-a447-4cee-b77a-18454a779a9c})</extracomment>
<translation>ID</translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="67"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="70"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="73"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="45"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="48"/>
<source>Power</source>
<extracomment>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</extracomment>
<translation>An</translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="76"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="79"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="51"/>
<source>Signal strength</source>
<extracomment>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</extracomment>
<extracomment>The name of the StateType ({71723ea3-d2a1-48c4-9a2c-532d938e5022}) of ThingClass switch</extracomment>
<translation>Signalstärke</translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="82"/>
<source>Signal strength changed</source>
<extracomment>The name of the EventType ({71723ea3-d2a1-48c4-9a2c-532d938e5022}) of ThingClass switch</extracomment>
<translation>Signalstärke geändert</translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="85"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="88"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="54"/>
<source>Total energy consumed</source>
<extracomment>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</extracomment>
<extracomment>The name of the StateType ({a3533121-69ee-44fd-8394-13373e8f960e}) of ThingClass switch</extracomment>
<translation>Gesamtenergieverbrauch</translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="91"/>
<source>Total energy consumed changed</source>
<extracomment>The name of the EventType ({a3533121-69ee-44fd-8394-13373e8f960e}) of ThingClass switch</extracomment>
<translation>Gesamtenergieverbrauch geändert</translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="94"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="57"/>
<source>Turn on or off</source>
<extracomment>The name of the ActionType ({717f2593-1544-483b-aac8-f9800b299e4d}) of ThingClass switch</extracomment>
<translation>Ein- oder ausschalten</translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="97"/>
<source>Turned on or off</source>
<extracomment>The name of the EventType ({717f2593-1544-483b-aac8-f9800b299e4d}) of ThingClass switch</extracomment>
<translation>Ein- oder ausgeschaltet</translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="100"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="103"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="60"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="63"/>
<source>myStrom</source>
<extracomment>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})</extraco
<translation>myStrom</translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="106"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="66"/>
<source>myStrom WiFi Switch</source>
<extracomment>The name of the ThingClass ({27dc49c0-58cd-4d5f-a95b-0c437dd828cf})</extracomment>
<translation>myStrom WiFi Switch</translation>

View File

@ -4,22 +4,27 @@
<context>
<name>IntegrationPluginMyStrom</name>
<message>
<location filename="../integrationpluginmystrom.cpp" line="78"/>
<location filename="../integrationpluginmystrom.cpp" line="109"/>
<source>Device cannot be found on the network.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../integrationpluginmystrom.cpp" line="87"/>
<location filename="../integrationpluginmystrom.cpp" line="119"/>
<source>Error fetching device information from myStrom device.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../integrationpluginmystrom.cpp" line="95"/>
<location filename="../integrationpluginmystrom.cpp" line="127"/>
<source>Error processing response from myStrom device.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../integrationpluginmystrom.cpp" line="170"/>
<location filename="../integrationpluginmystrom.cpp" line="135"/>
<source>This device does not seem to be a myStrom WiFi switch.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../integrationpluginmystrom.cpp" line="213"/>
<source>Error communicating with myStrom device.</source>
<translation type="unfinished"></translation>
</message>
@ -27,98 +32,53 @@
<context>
<name>myStrom</name>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="46"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="49"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="36"/>
<source>Connected</source>
<extracomment>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</extracomment>
<extracomment>The name of the StateType ({8864755d-4e6a-4e45-b2db-3eb5d4f3d53d}) of ThingClass switch</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="52"/>
<source>Connected changed</source>
<extracomment>The name of the EventType ({8864755d-4e6a-4e45-b2db-3eb5d4f3d53d}) of ThingClass switch</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="55"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="58"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="39"/>
<source>Current power consumption</source>
<extracomment>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</extracomment>
<extracomment>The name of the StateType ({ccb52b57-5800-4f03-b7fa-f36dcebe1d4e}) of ThingClass switch</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="61"/>
<source>Current power consumption changed</source>
<extracomment>The name of the EventType ({ccb52b57-5800-4f03-b7fa-f36dcebe1d4e}) of ThingClass switch</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="64"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="42"/>
<source>ID</source>
<extracomment>The name of the ParamType (ThingClass: switch, Type: thing, ID: {44144c44-a447-4cee-b77a-18454a779a9c})</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="67"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="70"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="73"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="45"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="48"/>
<source>Power</source>
<extracomment>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</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="76"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="79"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="51"/>
<source>Signal strength</source>
<extracomment>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</extracomment>
<extracomment>The name of the StateType ({71723ea3-d2a1-48c4-9a2c-532d938e5022}) of ThingClass switch</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="82"/>
<source>Signal strength changed</source>
<extracomment>The name of the EventType ({71723ea3-d2a1-48c4-9a2c-532d938e5022}) of ThingClass switch</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="85"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="88"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="54"/>
<source>Total energy consumed</source>
<extracomment>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</extracomment>
<extracomment>The name of the StateType ({a3533121-69ee-44fd-8394-13373e8f960e}) of ThingClass switch</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="91"/>
<source>Total energy consumed changed</source>
<extracomment>The name of the EventType ({a3533121-69ee-44fd-8394-13373e8f960e}) of ThingClass switch</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="94"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="57"/>
<source>Turn on or off</source>
<extracomment>The name of the ActionType ({717f2593-1544-483b-aac8-f9800b299e4d}) of ThingClass switch</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="97"/>
<source>Turned on or off</source>
<extracomment>The name of the EventType ({717f2593-1544-483b-aac8-f9800b299e4d}) of ThingClass switch</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="100"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="103"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="60"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="63"/>
<source>myStrom</source>
<extracomment>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})</extraco
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="106"/>
<location filename="../../../build/nymea-plugins-Desktop-Debug/mystrom/plugininfo.h" line="66"/>
<source>myStrom WiFi Switch</source>
<extracomment>The name of the ThingClass ({27dc49c0-58cd-4d5f-a95b-0c437dd828cf})</extracomment>
<translation type="unfinished"></translation>