Qt OPC UA Viewer Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the examples of the Qt OPC UA module.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"mainwindow.h"
#include
"opcuamodel.h"
#include
"certificatedialog.h"
#include
"ui_mainwindow.h"
#include <QApplication>
#include <QDir>
#include <QMessageBox>
#include <QTextCharFormat>
#include <QTextBlock>
#include <QOpcUaProvider>
#include <QOpcUaAuthenticationInformation>
#include <QOpcUaErrorState>
static
MainWindow *
mainWindowGlobal =
nullptr
;
static
QtMessageHandler oldMessageHandler =
nullptr
;
static
void
messageHandler(QtMsgType type, const
QMessageLogContext &
amp;context, const
QString &
amp;msg)
{
if
(!
mainWindowGlobal)
return
;
QString message;
QColor color =
Qt::
black;
switch
(type) {
case
QtWarningMsg:
message =
QObject::
tr("Warning"
);
color =
Qt::
darkYellow;
break
;
case
QtCriticalMsg:
message =
QObject::
tr("Critical"
);
color =
Qt::
darkRed;
break
;
case
QtFatalMsg:
message =
QObject::
tr("Fatal"
);
color =
Qt::
darkRed;
break
;
case
QtInfoMsg:
message =
QObject::
tr("Info"
);
break
;
case
QtDebugMsg:
message =
QObject::
tr("Debug"
);
break
;
}
message +=
QLatin1String(": "
);
message +=
msg;
const
QString contextStr =
QStringLiteral(" (%1:%2, %3)"
).arg(context.file).arg(context.line).arg(context.function);
// Logging messages from backends are sent from different threads and need to be
// synchronized with the GUI thread.
QMetaObject::
invokeMethod(mainWindowGlobal, "log"
, Qt::
QueuedConnection,
Q_ARG(QString, message),
Q_ARG(QString, contextStr),
Q_ARG(QColor, color));
if
(oldMessageHandler)
oldMessageHandler(type, context, msg);
}
MainWindow::
MainWindow(const
QString &
amp;initialUrl, QWidget *
parent) : QMainWindow(parent)
, ui(new
Ui::
MainWindow)
, mOpcUaModel(new
OpcUaModel(this
))
, mOpcUaProvider(new
QOpcUaProvider(this
))
{
ui-&
gt;setupUi(this
);
ui-&
gt;host-&
gt;setText(initialUrl);
mainWindowGlobal =
this
;
connect(ui-&
gt;quitAction, &
amp;QAction::
triggered, this
, &
amp;QWidget::
close);
ui-&
gt;quitAction-&
gt;setShortcut(QKeySequence(Qt::
CTRL |
Qt::
Key_Q));
connect(ui-&
gt;aboutAction, &
amp;QAction::
triggered, this
, &
amp;QApplication::
aboutQt);
ui-&
gt;aboutAction-&
gt;setShortcut(QKeySequence(QKeySequence::
HelpContents));
updateUiState();
ui-&
gt;opcUaPlugin-&
gt;addItems(QOpcUaProvider::
availableBackends());
ui-&
gt;treeView-&
gt;setModel(mOpcUaModel);
ui-&
gt;treeView-&
gt;header()-&
gt;setSectionResizeMode(QHeaderView::
ResizeToContents);
if
(ui-&
gt;opcUaPlugin-&
gt;count() ==
0
) {
ui-&
gt;opcUaPlugin-&
gt;setDisabled(true
);
ui-&
gt;connectButton-&
gt;setDisabled(true
);
QMessageBox::
critical(this
, tr("No OPCUA plugins available"
), tr("The list of available OPCUA plugins is empty. No connection possible."
));
}
connect(ui-&
gt;findServersButton, &
amp;QPushButton::
clicked, this
, &
amp;MainWindow::
findServers);
connect(ui-&
gt;host, &
amp;QLineEdit::
returnPressed, this
-&
gt;ui-&
gt;findServersButton,
[this
]() {
this
-&
gt;ui-&
gt;findServersButton-&
gt;animateClick(); }
);
connect(ui-&
gt;getEndpointsButton, &
amp;QPushButton::
clicked, this
, &
amp;MainWindow::
getEndpoints);
connect(ui-&
gt;connectButton, &
amp;QPushButton::
clicked, this
, &
amp;MainWindow::
connectToServer);
oldMessageHandler =
qInstallMessageHandler(&
amp;messageHandler);
setupPkiConfiguration();
m_identity =
m_pkiConfig.applicationIdentity();
}
MainWindow::
~
MainWindow()
{
delete
ui;
}
void
MainWindow::
setupPkiConfiguration()
{
QString pkidir =
QCoreApplication::
applicationDirPath();
#ifdef Q_OS_WIN
pkidir +=
"../"
;
#endif
pkidir +=
"/pki"
;
m_pkiConfig.setClientCertificateFile(pkidir +
"/own/certs/opcuaviewer.der"
);
m_pkiConfig.setPrivateKeyFile(pkidir +
"/own/private/opcuaviewer.pem"
);
m_pkiConfig.setTrustListDirectory(pkidir +
"/trusted/certs"
);
m_pkiConfig.setRevocationListDirectory(pkidir +
"/trusted/crl"
);
m_pkiConfig.setIssuerListDirectory(pkidir +
"/issuers/certs"
);
m_pkiConfig.setIssuerRevocationListDirectory(pkidir +
"/issuers/crl"
);
// create the folders if they don't exist yet
createPkiFolders();
}
void
MainWindow::
createClient()
{
if
(mOpcUaClient ==
nullptr
) {
mOpcUaClient =
mOpcUaProvider-&
gt;createClient(ui-&
gt;opcUaPlugin-&
gt;currentText());
if
(!
mOpcUaClient) {
const
QString message(tr("Connecting to the given sever failed. See the log for details."
));
log(message, QString(), Qt::
red);
QMessageBox::
critical(this
, tr("Failed to connect to server"
), message);
return
;
}
connect(mOpcUaClient, &
amp;QOpcUaClient::
connectError, this
, &
amp;MainWindow::
showErrorDialog);
mOpcUaClient-&
gt;setApplicationIdentity(m_identity);
mOpcUaClient-&
gt;setPkiConfiguration(m_pkiConfig);
if
(mOpcUaClient-&
gt;supportedUserTokenTypes().contains(QOpcUaUserTokenPolicy::TokenType::
Certificate)) {
QOpcUaAuthenticationInformation authInfo;
authInfo.setCertificateAuthentication();
mOpcUaClient-&
gt;setAuthenticationInformation(authInfo);
}
connect(mOpcUaClient, &
amp;QOpcUaClient::
connected, this
, &
amp;MainWindow::
clientConnected);
connect(mOpcUaClient, &
amp;QOpcUaClient::
disconnected, this
, &
amp;MainWindow::
clientDisconnected);
connect(mOpcUaClient, &
amp;QOpcUaClient::
errorChanged, this
, &
amp;MainWindow::
clientError);
connect(mOpcUaClient, &
amp;QOpcUaClient::
stateChanged, this
, &
amp;MainWindow::
clientState);
connect(mOpcUaClient, &
amp;QOpcUaClient::
endpointsRequestFinished, this
, &
amp;MainWindow::
getEndpointsComplete);
connect(mOpcUaClient, &
amp;QOpcUaClient::
findServersFinished, this
, &
amp;MainWindow::
findServersComplete);
}
}
void
MainWindow::
findServers()
{
QStringList localeIds;
QStringList serverUris;
QUrl url(ui-&
gt;host-&
gt;text());
updateUiState();
createClient();
// set default port if missing
if
(url.port() ==
-
1
) url.setPort(4840
);
if
(mOpcUaClient) {
mOpcUaClient-&
gt;findServers(url, localeIds, serverUris);
qDebug() &
lt;&
lt; "Discovering servers on "
&
lt;&
lt; url.toString();
}
}
void
MainWindow::
findServersComplete(const
QList&
lt;QOpcUaApplicationDescription&
gt; &
amp;servers, QOpcUa::
UaStatusCode statusCode)
{
QOpcUaApplicationDescription server;
if
(isSuccessStatus(statusCode)) {
ui-&
gt;servers-&
gt;clear();
for
(const
auto
&
amp;server : servers) {
const
auto
urls =
server.discoveryUrls();
for
(const
auto
&
amp;url : qAsConst(urls))
ui-&
gt;servers-&
gt;addItem(url);
}
}
updateUiState();
}
void
MainWindow::
getEndpoints()
{
ui-&
gt;endpoints-&
gt;clear();
updateUiState();
if
(ui-&
gt;servers-&
gt;currentIndex() &
gt;=
0
) {
const
QString serverUrl =
ui-&
gt;servers-&
gt;currentText();
createClient();
mOpcUaClient-&
gt;requestEndpoints(serverUrl);
}
}
void
MainWindow::
getEndpointsComplete(const
QList&
lt;QOpcUaEndpointDescription&
gt; &
amp;endpoints, QOpcUa::
UaStatusCode statusCode)
{
int
index =
0
;
const
std::
array&
lt;const
char
*
, 4
&
gt; modes =
{
"Invalid"
,
"None"
,
"Sign"
,
"SignAndEncrypt"
}
;
if
(isSuccessStatus(statusCode)) {
mEndpointList =
endpoints;
for
(const
auto
&
amp;endpoint : endpoints) {
if
(endpoint.securityMode() &
gt;=
modes.size()) {
qWarning() &
lt;&
lt; "Invalid security mode"
;
continue
;
}
const
QString EndpointName =
QString("%1 (%2)"
)
.arg(endpoint.securityPolicy(), modes[endpoint.securityMode()]);
ui-&
gt;endpoints-&
gt;addItem(EndpointName, index++
);
}
}
updateUiState();
}
void
MainWindow::
connectToServer()
{
if
(mClientConnected) {
mOpcUaClient-&
gt;disconnectFromEndpoint();
return
;
}
if
(ui-&
gt;endpoints-&
gt;currentIndex() &
gt;=
0
) {
m_endpoint =
mEndpointList[ui-&
gt;endpoints-&
gt;currentIndex()];
createClient();
mOpcUaClient-&
gt;connectToEndpoint(m_endpoint);
}
}
void
MainWindow::
clientConnected()
{
mClientConnected =
true
;
updateUiState();
connect(mOpcUaClient, &
amp;QOpcUaClient::
namespaceArrayUpdated, this
, &
amp;MainWindow::
namespacesArrayUpdated);
mOpcUaClient-&
gt;updateNamespaceArray();
}
void
MainWindow::
clientDisconnected()
{
mClientConnected =
false
;
mOpcUaClient-&
gt;deleteLater();
mOpcUaClient =
nullptr
;
mOpcUaModel-&
gt;setOpcUaClient(nullptr
);
updateUiState();
}
void
MainWindow::
namespacesArrayUpdated(const
QStringList &
amp;namespaceArray)
{
if
(namespaceArray.isEmpty()) {
qWarning() &
lt;&
lt; "Failed to retrieve the namespaces array"
;
return
;
}
disconnect(mOpcUaClient, &
amp;QOpcUaClient::
namespaceArrayUpdated, this
, &
amp;MainWindow::
namespacesArrayUpdated);
mOpcUaModel-&
gt;setOpcUaClient(mOpcUaClient);
ui-&
gt;treeView-&
gt;header()-&
gt;setSectionResizeMode(1
/* Value column*/
, QHeaderView::
Interactive);
}
void
MainWindow::
clientError(QOpcUaClient::
ClientError error)
{
qDebug() &
lt;&
lt; "Client error changed"
&
lt;&
lt; error;
}
void
MainWindow::
clientState(QOpcUaClient::
ClientState state)
{
qDebug() &
lt;&
lt; "Client state changed"
&
lt;&
lt; state;
}
void
MainWindow::
updateUiState()
{
// allow changing the backend only if it was not already created
ui-&
gt;opcUaPlugin-&
gt;setEnabled(mOpcUaClient ==
nullptr
);
ui-&
gt;connectButton-&
gt;setText(mClientConnected ? tr("Disconnect"
) : tr("Connect"
));
if
(mClientConnected) {
ui-&
gt;host-&
gt;setEnabled(false
);
ui-&
gt;servers-&
gt;setEnabled(false
);
ui-&
gt;endpoints-&
gt;setEnabled(false
);
ui-&
gt;findServersButton-&
gt;setEnabled(false
);
ui-&
gt;getEndpointsButton-&
gt;setEnabled(false
);
ui-&
gt;connectButton-&
gt;setEnabled(true
);
}
else
{
ui-&
gt;host-&
gt;setEnabled(true
);
ui-&
gt;servers-&
gt;setEnabled(ui-&
gt;servers-&
gt;count() &
gt; 0
);
ui-&
gt;endpoints-&
gt;setEnabled(ui-&
gt;endpoints-&
gt;count() &
gt; 0
);
ui-&
gt;findServersButton-&
gt;setDisabled(ui-&
gt;host-&
gt;text().isEmpty());
ui-&
gt;getEndpointsButton-&
gt;setEnabled(ui-&
gt;servers-&
gt;currentIndex() !=
-
1
);
ui-&
gt;connectButton-&
gt;setEnabled(ui-&
gt;endpoints-&
gt;currentIndex() !=
-
1
);
}
if
(!
mOpcUaClient) {
ui-&
gt;servers-&
gt;setEnabled(false
);
ui-&
gt;endpoints-&
gt;setEnabled(false
);
ui-&
gt;getEndpointsButton-&
gt;setEnabled(false
);
ui-&
gt;connectButton-&
gt;setEnabled(false
);
}
}
void
MainWindow::
log(const
QString &
amp;text, const
QString &
amp;context, const
QColor &
amp;color)
{
auto
cf =
ui-&
gt;log-&
gt;currentCharFormat();
cf.setForeground(color);
ui-&
gt;log-&
gt;setCurrentCharFormat(cf);
ui-&
gt;log-&
gt;appendPlainText(text);
if
(!
context.isEmpty()) {
cf.setForeground(Qt::
gray);
ui-&
gt;log-&
gt;setCurrentCharFormat(cf);
ui-&
gt;log-&
gt;insertPlainText(context);
}
}
void
MainWindow::
log(const
QString &
amp;text, const
QColor &
amp;color)
{
log(text, QString(), color);
}
bool
MainWindow::
createPkiPath(const
QString &
amp;path)
{
const
QString msg =
tr("Creating PKI path '%1': %2"
);
QDir dir;
const
bool
ret =
dir.mkpath(path);
if
(ret)
qDebug() &
lt;&
lt; msg.arg(path, "SUCCESS."
);
else
qCritical("%s"
, qPrintable(msg.arg(path, "FAILED."
)));
return
ret;
}
bool
MainWindow::
createPkiFolders()
{
bool
result =
createPkiPath(m_pkiConfig.trustListDirectory());
if
(!
result)
return
result;
result =
createPkiPath(m_pkiConfig.revocationListDirectory());
if
(!
result)
return
result;
result =
createPkiPath(m_pkiConfig.issuerListDirectory());
if
(!
result)
return
result;
result =
createPkiPath(m_pkiConfig.issuerRevocationListDirectory());
if
(!
result)
return
result;
return
result;
}
void
MainWindow::
showErrorDialog(QOpcUaErrorState *
errorState)
{
int
result =
0
;
const
QString statuscode =
QOpcUa::
statusToString(errorState-&
gt;errorCode());
QString msg =
errorState-&
gt;isClientSideError() ? tr("The client reported: "
) : tr("The server reported: "
);
switch
(errorState-&
gt;connectionStep()) {
case
QOpcUaErrorState::ConnectionStep::
Unknown:
break
;
case
QOpcUaErrorState::ConnectionStep::
CertificateValidation: {
CertificateDialog dlg(this
);
msg +=
tr("Server certificate validation failed with error 0x%1 (%2).
\n
Click 'Abort' to abort the connect, or 'Ignore' to continue connecting."
)
.arg(static_cast
&
lt;ulong&
gt;(errorState-&
gt;errorCode()), 8
, 16
, QLatin1Char('0'
)).arg(statuscode);
result =
dlg.showCertificate(msg, m_endpoint.serverCertificate(), m_pkiConfig.trustListDirectory());
errorState-&
gt;setIgnoreError(result ==
1
);
}
break
;
case
QOpcUaErrorState::ConnectionStep::
OpenSecureChannel:
msg +=
tr("OpenSecureChannel failed with error 0x%1 (%2)."
).arg(errorState-&
gt;errorCode(), 8
, 16
, QLatin1Char('0'
)).arg(statuscode);
QMessageBox::
warning(this
, tr("Connection Error"
), msg);
break
;
case
QOpcUaErrorState::ConnectionStep::
CreateSession:
msg +=
tr("CreateSession failed with error 0x%1 (%2)."
).arg(errorState-&
gt;errorCode(), 8
, 16
, QLatin1Char('0'
)).arg(statuscode);
QMessageBox::
warning(this
, tr("Connection Error"
), msg);
break
;
case
QOpcUaErrorState::ConnectionStep::
ActivateSession:
msg +=
tr("ActivateSession failed with error 0x%1 (%2)."
).arg(errorState-&
gt;errorCode(), 8
, 16
, QLatin1Char('0'
)).arg(statuscode);
QMessageBox::
warning(this
, tr("Connection Error"
), msg);
break
;
}
}