forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscovery.cpp
329 lines (286 loc) · 9.98 KB
/
discovery.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright (c) 2016 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#include <QFile>
#include <QHostInfo>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkProxy>
#include <QSysInfo>
#include "de_web_plugin_private.h"
#include "json.h"
#ifdef Q_OS_LINUX
#include <unistd.h>
#endif
/*! Inits the internet discovery manager.
*/
void DeRestPluginPrivate::initInternetDicovery()
{
Q_ASSERT(inetDiscoveryManager == 0);
inetDiscoveryManager = new QNetworkAccessManager;
connect(inetDiscoveryManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(internetDiscoveryFinishedRequest(QNetworkReply*)));
DBG_Assert(gwAnnounceInterval >= 0);
if (gwAnnounceInterval < 0)
{
gwAnnounceInterval = 15;
}
inetDiscoveryTimer = new QTimer(this);
inetDiscoveryTimer->setSingleShot(false);
{
QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery(QNetworkProxyQuery(gwAnnounceUrl));
if (!proxies.isEmpty())
{
const QNetworkProxy &proxy = proxies.first();
if (proxy.type() == QNetworkProxy::HttpProxy || proxy.type() == QNetworkProxy::HttpCachingProxy)
{
gwProxyPort = proxy.port();
gwProxyAddress = proxy.hostName();
inetDiscoveryManager->setProxy(proxy);
QHostInfo::lookupHost(proxy.hostName(),
this, SLOT(inetProxyHostLookupDone(QHostInfo)));
}
}
}
connect(inetDiscoveryTimer, SIGNAL(timeout()),
this, SLOT(internetDiscoveryTimerFired()));
setInternetDiscoveryInterval(gwAnnounceInterval);
// force first run
if (gwAnnounceInterval > 0)
{
QTimer::singleShot(5000, this, SLOT(internetDiscoveryTimerFired()));
}
#ifdef Q_OS_LINUX
// check if we run from shell script
QFile pproc(QString("/proc/%1/cmdline").arg(getppid()));
if (pproc.exists() && pproc.open(QIODevice::ReadOnly))
{
QByteArray name = pproc.readAll();
if (name.endsWith(".sh"))
{
DBG_Printf(DBG_INFO, "runs in shell script %s\n", qPrintable(name));
gwRunFromShellScript = true;
}
else
{
gwRunFromShellScript = false;
DBG_Printf(DBG_INFO, "parent process %s\n", qPrintable(name));
}
}
#else
gwRunFromShellScript = false;
#endif
{
QFile f("/etc/os-release");
if (f.exists() && f.open(QFile::ReadOnly))
{
QTextStream stream(&f);
while (!stream.atEnd())
{
QString line = stream.readLine(200);
QStringList lineLs = line.split(QChar('='));
if (lineLs.size() != 2)
{
continue;
}
if (lineLs[0] == QLatin1String("PRETTY_NAME"))
{
osPrettyName = lineLs[1];
osPrettyName.remove(QChar('"'));
}
}
}
}
#ifdef ARCH_ARM
{ // get raspberry pi revision
QFile f("/proc/cpuinfo");
if (f.exists() && f.open(QFile::ReadOnly))
{
QByteArray arr = f.readAll();
QTextStream stream(arr);
while (!stream.atEnd())
{
QString line = stream.readLine(200);
QStringList lineLs = line.split(QChar(':'));
if (lineLs.size() != 2)
{
continue;
}
if (lineLs[0].startsWith(QLatin1String("Revision")))
{
piRevision = lineLs[1].trimmed();
break;
}
}
}
}
#endif // ARCH_ARM
if (osPrettyName.isEmpty())
{
#ifdef Q_OS_WIN
switch (QSysInfo::WindowsVersion)
{
case QSysInfo::WV_XP: osPrettyName = QLatin1String("WinXP"); break;
case QSysInfo::WV_WINDOWS7: osPrettyName = QLatin1String("Win7"); break;
case QSysInfo::WV_WINDOWS8: osPrettyName = QLatin1String("Win8"); break;
case QSysInfo::WV_WINDOWS8_1: osPrettyName = QLatin1String("Win8.1"); break;
case QSysInfo::WV_WINDOWS10: osPrettyName = QLatin1String("Win10"); break;
default:
osPrettyName = QLatin1String("Win");
break;
}
#endif
#ifdef Q_OS_OSX
osPrettyName = "Mac";
#endif
#ifdef Q_OS_LINUX
osPrettyName = "Linux";
#endif
}
}
/*! Sets the announce interval for internet discovery.
\param minutes a value between 0..180 min
\return true on success
*/
bool DeRestPluginPrivate::setInternetDiscoveryInterval(int minutes)
{
if ((minutes < 0) || (minutes > 180))
{
DBG_Printf(DBG_INFO, "discovery ignored invalid announce interval (%d minutes)\n", minutes);
return false;
}
inetDiscoveryTimer->stop();
gwAnnounceInterval = minutes;
if (gwAnnounceInterval > 0)
{
int msec = 1000 * 60 * gwAnnounceInterval;
inetDiscoveryTimer->start(msec);
DBG_Printf(DBG_INFO, "discovery updated announce interval to %d minutes\n", minutes);
}
return true;
}
/*! Handle announce trigger timer.
*/
void DeRestPluginPrivate::internetDiscoveryTimerFired()
{
if (gwAnnounceInterval > 0)
{
QString str = QString("{ \"name\": \"%1\", \"mac\": \"%2\", \"internal_ip\":\"%3\", \"internal_port\":%4, \"interval\":%5, \"swversion\":\"%6\", \"fwversion\":\"%7\", \"nodecount\":%8, \"uptime\":%9, \"updatechannel\":\"%10\"")
.arg(gwName)
.arg(gwBridgeId)
.arg(gwConfig["ipaddress"].toString())
.arg(gwConfig["port"].toDouble())
.arg(gwAnnounceInterval)
.arg(gwConfig["swversion"].toString())
.arg(gwConfig["fwversion"].toString())
.arg(nodes.size())
.arg(getUptime())
.arg(gwUpdateChannel);
str.append(QString(",\"os\": \"%1\"").arg(osPrettyName));
if (!piRevision.isEmpty())
{
str.append(QString(",\"pirev\": \"%1\"").arg(piRevision));
}
str.append(QChar('}'));
QByteArray data(qPrintable(str));
inetDiscoveryManager->put(QNetworkRequest(QUrl(gwAnnounceUrl)), data);
}
}
/*! Callback for finished HTTP requests.
\param reply the reply to a HTTP request
*/
void DeRestPluginPrivate::internetDiscoveryFinishedRequest(QNetworkReply *reply)
{
DBG_Assert(reply != 0);
if (!reply)
{
return;
}
if (reply->error() == QNetworkReply::NoError)
{
DBG_Printf(DBG_INFO, "Announced to internet\n");
#ifdef ARCH_ARM
// currently this is only supported for the RaspBee Gateway
internetDiscoveryExtractVersionInfo(reply);
#endif // ARCH_ARM
}
else
{
DBG_Printf(DBG_INFO, "discovery network reply error: %s\n", qPrintable(reply->errorString()));
}
reply->deleteLater();
}
/*! Extracts the update channels version info about the deCONZ/WebApp.
\param reply which holds the version info in JSON format
*/
void DeRestPluginPrivate::internetDiscoveryExtractVersionInfo(QNetworkReply *reply)
{
bool ok;
QByteArray content = reply->readAll();
QVariant var = Json::parse(content, ok);
QVariantMap map = var.toMap();
if (!ok || map.isEmpty())
{
DBG_Printf(DBG_ERROR, "discovery couldn't extract version info from reply\n");
}
if (map.contains("versions") && (map["versions"].type() == QVariant::Map))
{
QString version;
QVariantMap versions = map["versions"].toMap();
if (versions.contains(gwUpdateChannel) && (versions[gwUpdateChannel].type() == QVariant::String))
{
version = versions[gwUpdateChannel].toString();
if (!version.isEmpty())
{
if (gwUpdateVersion != version)
{
DBG_Printf(DBG_INFO, "discovery found version %s for update channel %s\n", qPrintable(version), qPrintable(gwUpdateChannel));
gwUpdateVersion = version;
gwSwUpdateState = swUpdateState.readyToInstall;
updateEtag(gwConfigEtag);
}
}
else
{
DBG_Printf(DBG_ERROR, "discovery reply doesn't contain valid version info for update channel %s\n", qPrintable(gwUpdateChannel));
}
}
else
{
DBG_Printf(DBG_ERROR, "discovery reply doesn't contain version info for update channel %s\n", qPrintable(gwUpdateChannel));
}
}
else
{
DBG_Printf(DBG_ERROR, "discovery reply doesn't contain valid version info\n");
}
}
/*! Finished Lookup of http proxy IP address.
\param host holds the proxy host info
*/
void DeRestPluginPrivate::inetProxyHostLookupDone(const QHostInfo &host)
{
if (host.error() != QHostInfo::NoError)
{
DBG_Printf(DBG_ERROR, "Proxy host lookup failed: %s\n", qPrintable(host.errorString()));
return;
}
foreach (const QHostAddress &address, host.addresses())
{
QString addr = address.toString();
if (address.protocol() == QAbstractSocket::IPv4Protocol &&
!addr.isEmpty() && gwProxyAddress != address.toString())
{
DBG_Printf(DBG_INFO, "Found proxy IP address: %s\n", qPrintable(address.toString()));
gwProxyAddress = address.toString();
updateEtag(gwConfigEtag);
return;
}
}
}