Cleanup discovery and fix comments

master
Simon Stürz 2022-08-08 08:47:02 +02:00
parent a237bb5d3a
commit 60baf97c5f
2 changed files with 45 additions and 45 deletions

View File

@ -40,12 +40,7 @@ SunnyWebBoxDiscovery::SunnyWebBoxDiscovery(NetworkAccessManager *networkAccessMa
m_networkAccessManager(networkAccessManager), m_networkAccessManager(networkAccessManager),
m_networkDeviceDiscovery(networkDeviceDiscovery) m_networkDeviceDiscovery(networkDeviceDiscovery)
{ {
m_gracePeriodTimer.setSingleShot(true);
m_gracePeriodTimer.setInterval(3000);
connect(&m_gracePeriodTimer, &QTimer::timeout, this, [this](){
qCDebug(dcSma()) << "Discovery: SunnyWebBox: Grace period timer triggered.";
finishDiscovery();
});
} }
void SunnyWebBoxDiscovery::startDiscovery() void SunnyWebBoxDiscovery::startDiscovery()
@ -53,14 +48,13 @@ void SunnyWebBoxDiscovery::startDiscovery()
// Clean up // Clean up
m_discoveryResults.clear(); m_discoveryResults.clear();
m_verifiedNetworkDeviceInfos.clear(); m_verifiedNetworkDeviceInfos.clear();
m_gracePeriodTimer.stop();
m_startDateTime = QDateTime::currentDateTime(); m_startDateTime = QDateTime::currentDateTime();
qCInfo(dcSma()) << "Discovery: SunnyWebBox: Start discovering the network..."; qCInfo(dcSma()) << "Discovery: SunnyWebBox: Starting network discovery...";
m_discoveryReply = m_networkDeviceDiscovery->discover(); m_discoveryReply = m_networkDeviceDiscovery->discover();
// Check if all network device infos which might already be discovered here to save time... // Check all network device infos which might already be discovered here to save time...
foreach (const NetworkDeviceInfo &networkDeviceInfo, m_discoveryReply->networkDeviceInfos()) { foreach (const NetworkDeviceInfo &networkDeviceInfo, m_discoveryReply->networkDeviceInfos()) {
checkNetworkDevice(networkDeviceInfo); checkNetworkDevice(networkDeviceInfo);
} }
@ -85,7 +79,10 @@ void SunnyWebBoxDiscovery::startDiscovery()
// If there might be some response after the grace period time, // If there might be some response after the grace period time,
// we don't care any more since there might just waiting for some timeouts... // we don't care any more since there might just waiting for some timeouts...
// If there would be a device, if would have responded. // If there would be a device, if would have responded.
m_gracePeriodTimer.start(); QTimer::singleShot(3000, this, [this](){
qCDebug(dcSma()) << "Discovery: SunnyWebBox: Grace period timer triggered.";
finishDiscovery();
});
}); });
} }
@ -96,58 +93,62 @@ NetworkDeviceInfos SunnyWebBoxDiscovery::discoveryResults() const
void SunnyWebBoxDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo) void SunnyWebBoxDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo)
{ {
if (m_verifiedNetworkDeviceInfos.contains(networkDeviceInfo))
return;
m_verifiedNetworkDeviceInfos.append(networkDeviceInfo);
// Make a simple request and verify if it worked and the expected data gets returned. // Make a simple request and verify if it worked and the expected data gets returned.
SunnyWebBox webBox(m_networkAccessManager, networkDeviceInfo.address(), this); SunnyWebBox webBox(m_networkAccessManager, networkDeviceInfo.address(), this);
QNetworkReply *reply = webBox.sendRequest(networkDeviceInfo.address(), "GetPlantOverview"); QNetworkReply *reply = webBox.sendRequest(networkDeviceInfo.address(), "GetPlantOverview");
m_pendingReplies.append(reply); m_pendingReplies.append(reply);
connect(reply, &QNetworkReply::finished, this, [=](){ connect(reply, &QNetworkReply::finished, this, [=](){
m_pendingReplies.removeAll(reply); m_pendingReplies.removeAll(reply);
reply->deleteLater(); reply->deleteLater();
// Check HTTP reply // Check HTTP reply
if (reply->error() != QNetworkReply::NoError) { if (reply->error() != QNetworkReply::NoError) {
qCDebug(dcSma()) << "Discovery: SunnyWebBox: Checked" << networkDeviceInfo.address().toString() qCDebug(dcSma()) << "Discovery: SunnyWebBox: Checked" << networkDeviceInfo.address().toString()
<< "and a HTTP error occurred:" << reply->errorString() << "Continue..."; << "and a HTTP error occurred:" << reply->errorString() << "Continue...";
return; return;
} }
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
// Check JSON // Check JSON
QJsonParseError error; QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
if (error.error != QJsonParseError::NoError) { if (error.error != QJsonParseError::NoError) {
qCDebug(dcSma()) << "Discovery: SunnyWebBox: Checked" << networkDeviceInfo.address().toString() qCDebug(dcSma()) << "Discovery: SunnyWebBox: Checked" << networkDeviceInfo.address().toString()
<< "and received invalid JSON data:" << error.errorString() << "Continue..."; << "and received invalid JSON data:" << error.errorString() << "Continue...";
return; return;
} }
if (!jsonDoc.isObject()) { if (!jsonDoc.isObject()) {
qCDebug(dcSma()) << "Discovery: SunnyWebBox: Response JSON is not an Object" << networkDeviceInfo.address().toString() << "Continue..."; qCDebug(dcSma()) << "Discovery: SunnyWebBox: Response JSON is not an Object" << networkDeviceInfo.address().toString() << "Continue...";
return; return;
} }
QVariantMap map = jsonDoc.toVariant().toMap(); QVariantMap map = jsonDoc.toVariant().toMap();
if (map["version"] != "1.0") { if (map["version"] != "1.0") {
qCDebug(dcSma()) << "Discovery: SunnyWebBox: API version not supported on" << networkDeviceInfo.address().toString() << "Continue...";; qCDebug(dcSma()) << "Discovery: SunnyWebBox: API version not supported on" << networkDeviceInfo.address().toString() << "Continue...";;
return; return;
} }
if (map.contains("proc") && map.contains("result")) { if (map.contains("proc") && map.contains("result")) {
// Ok, seems to be a Sunny WebBox we are talking to...add to the discovery results... // Ok, seems to be a Sunny WebBox we are talking to...add to the discovery results...
qCDebug(dcSma()) << "Discovery: SunnyWebBox: --> Found Sunny WebBox on" << networkDeviceInfo; qCDebug(dcSma()) << "Discovery: SunnyWebBox: --> Found Sunny WebBox on" << networkDeviceInfo;
m_discoveryResults.append(networkDeviceInfo); m_discoveryResults.append(networkDeviceInfo);
} else { } else {
qCDebug(dcSma()) << "Discovery: SunnyWebBox: Missing proc or result value in response from" << networkDeviceInfo.address().toString() << "Continue..."; qCDebug(dcSma()) << "Discovery: SunnyWebBox: Missing proc or result value in response from" << networkDeviceInfo.address().toString() << "Continue...";
return; return;
} }
}); });
} }
void SunnyWebBoxDiscovery::cleanupPendingReplies() void SunnyWebBoxDiscovery::cleanupPendingReplies()
{ {
foreach (QNetworkReply *reply, m_pendingReplies) { foreach (QNetworkReply *reply, m_pendingReplies) {
m_pendingReplies.removeAll(reply);
reply->abort(); reply->abort();
} }
} }
@ -157,7 +158,7 @@ void SunnyWebBoxDiscovery::finishDiscovery()
qint64 durationMilliSeconds = QDateTime::currentMSecsSinceEpoch() - m_startDateTime.toMSecsSinceEpoch(); qint64 durationMilliSeconds = QDateTime::currentMSecsSinceEpoch() - m_startDateTime.toMSecsSinceEpoch();
qCInfo(dcSma()) << "Discovery: SunnyWebBox: Finished the discovery process. Found" << m_discoveryResults.count() qCInfo(dcSma()) << "Discovery: SunnyWebBox: Finished the discovery process. Found" << m_discoveryResults.count()
<< "Sunny WebBoxes in" << QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz"); << "Sunny WebBoxes in" << QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz");
m_gracePeriodTimer.stop();
cleanupPendingReplies(); cleanupPendingReplies();
emit discoveryFinished(); emit discoveryFinished();
} }

View File

@ -63,7 +63,6 @@ private:
NetworkDeviceInfos m_discoveredNetworkDeviceInfos; NetworkDeviceInfos m_discoveredNetworkDeviceInfos;
NetworkDeviceInfos m_verifiedNetworkDeviceInfos; NetworkDeviceInfos m_verifiedNetworkDeviceInfos;
QTimer m_gracePeriodTimer;
QDateTime m_startDateTime; QDateTime m_startDateTime;
QList<QNetworkReply *> m_pendingReplies; QList<QNetworkReply *> m_pendingReplies;