forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
de_web_widget.cpp
178 lines (148 loc) · 5.2 KB
/
de_web_widget.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
/*
* Copyright (c) 2013-2020 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 <QAction>
#include <QLabel>
#include <QNetworkInterface>
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
#include "de_web_widget.h"
#include "ui_de_web_widget.h"
QAction *readBindingTableAction = nullptr;
/*! Constructor. */
DeRestWidget::DeRestWidget(QWidget *parent, DeRestPlugin *_plugin) :
QDialog(parent),
ui(new Ui::DeWebWidget),
plugin(_plugin)
{
ui->setupUi(this);
setWindowTitle(tr("DE REST-API"));
deCONZ::ApsController *apsCtrl = deCONZ::ApsController::instance();
quint16 httpPort = apsCtrl ? deCONZ::ApsController::instance()->getParameter(deCONZ::ParamHttpPort) : 0;
ui->ipAddressesLabel->setTextFormat(Qt::RichText);
ui->ipAddressesLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
ui->ipAddressesLabel->setOpenExternalLinks(true);
ui->gitCommitLabel->setText(QLatin1String(GIT_COMMMIT));
QString str;
QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
QList<QNetworkInterface>::Iterator ifi = ifaces.begin();
QList<QNetworkInterface>::Iterator ifend = ifaces.end();
for (; ifi != ifend; ++ifi)
{
QString name = ifi->humanReadableName();
// filter
if (name.contains("br-", Qt::CaseInsensitive) ||
name.contains("docker", Qt::CaseInsensitive) ||
name.contains("vm", Qt::CaseInsensitive) ||
name.contains("virtual", Qt::CaseInsensitive) ||
name.contains("loop", Qt::CaseInsensitive))
{
continue;
}
QList<QNetworkAddressEntry> addr = ifi->addressEntries();
QList<QNetworkAddressEntry>::Iterator i = addr.begin();
QList<QNetworkAddressEntry>::Iterator end = addr.end();
for (; i != end; ++i)
{
QHostAddress a = i->ip();
if (a.protocol() == QAbstractSocket::IPv4Protocol)
{
QString url = QString("http://%1:%2").arg(a.toString()).arg(httpPort);
str.append("<b>");
str.append(ifi->humanReadableName());
str.append("</b> ");
str.append(QString("<a href=\"%1\">%2</a><br/>").arg(url).arg(url));
}
}
}
if (httpPort == 0)
{
str = tr("No HTTP server is running");
}
ui->ipAddressesLabel->setText(str);
//
connect(deCONZ::ApsController::instance(), &deCONZ::ApsController::nodeEvent, this, &DeRestWidget::nodeEvent);
// keyboard shortcuts
readBindingTableAction = new QAction(tr("Read binding table"), this);
readBindingTableAction->setShortcut(Qt::CTRL + Qt::Key_B);
readBindingTableAction->setProperty("type", "node-action");
readBindingTableAction->setProperty("actionid", "read-binding-table");
readBindingTableAction->setEnabled(m_selectedNodeAddress.hasExt());
connect(readBindingTableAction, &QAction::triggered, this, &DeRestWidget::readBindingTableTriggered);
addAction(readBindingTableAction);
}
/*! Deconstructor. */
DeRestWidget::~DeRestWidget()
{
Q_ASSERT(ui);
delete ui;
ui = nullptr;
}
/*! Returns true if the plugin is active. */
bool DeRestWidget::pluginActive() const
{
if (ui)
{
return ui->pluginActiveCheckBox->isChecked();
}
return false;
}
void DeRestWidget::readBindingTableTriggered()
{
if (m_selectedNodeAddress.hasExt())
{
auto *restNode = dynamic_cast<RestNodeBase*>(plugin->d->getLightNodeForAddress(m_selectedNodeAddress));
if (!restNode)
{
restNode = dynamic_cast<RestNodeBase*>(plugin->d->getSensorNodeForAddress(m_selectedNodeAddress));
}
if (restNode)
{
restNode->setMgmtBindSupported(true);
DBG_Printf(DBG_INFO, "read binding table for %s (%s) \n", qPrintable(m_selectedNodeAddress.toStringExt()), qPrintable(m_selectedNodeAddress.toStringNwk()));
plugin->d->readBindingTable(restNode, 0);
}
}
}
void DeRestWidget::nodeEvent(const deCONZ::NodeEvent &event)
{
if (event.node() && event.event() == deCONZ::NodeEvent::NodeSelected)
{
m_selectedNodeAddress = event.node()->address();
readBindingTableAction->setEnabled(m_selectedNodeAddress.hasExt());
}
else if (event.event() == deCONZ::NodeEvent::NodeDeselected)
{
m_selectedNodeAddress = {};
readBindingTableAction->setEnabled(false);
}
}
void DeRestWidget::showEvent(QShowEvent *)
{
deCONZ::ApsController *apsCtrl = deCONZ::ApsController::instance();
if (!apsCtrl)
{
return;
}
QByteArray sec0 = apsCtrl->getParameter(deCONZ::ParamSecurityMaterial0);
if (!sec0.isEmpty())
{
QByteArray installCode;
for (int i = 0; i < 4; i++)
{
installCode += sec0.mid(i * 4, 4);
if (i < 3) { installCode += ' '; }
}
ui->labelInstallCode->setText(installCode);
}
else
{
ui->labelInstallCode->setText(tr("not available"));
}
}