some cleanup

This commit is contained in:
Michael Zanetti 2019-02-06 13:45:29 +01:00
parent a2272f9699
commit ec08e6c7d5
10 changed files with 39 additions and 30 deletions

View File

@ -226,10 +226,10 @@ void AWSClient::login(const QString &username, const QString &password, int atte
m_idToken = authenticationResult.value("IdToken").toByteArray(); m_idToken = authenticationResult.value("IdToken").toByteArray();
m_refreshToken = authenticationResult.value("RefreshToken").toByteArray(); m_refreshToken = authenticationResult.value("RefreshToken").toByteArray();
qDebug() << "AWS ID token" << m_idToken; // qDebug() << "AWS ID token" << m_idToken;
QList<QByteArray> jwtParts = m_idToken.split('.'); QList<QByteArray> jwtParts = m_idToken.split('.');
if (jwtParts.count() != 3) { if (jwtParts.count() != 3) {
qWarning() << "JWT token doesn't have 3 parts"; qWarning() << "Error: JWT token doesn't have 3 parts. Cannot retrieve AWS Cognito ID.";
return; return;
} }
// qDebug() << "decoded header:" << QByteArray::fromBase64(jwtParts.at(0)); // qDebug() << "decoded header:" << QByteArray::fromBase64(jwtParts.at(0));
@ -237,7 +237,7 @@ void AWSClient::login(const QString &username, const QString &password, int atte
QJsonDocument tokenPayloadJsonDoc = QJsonDocument::fromJson(QByteArray::fromBase64(jwtParts.at(1))); QJsonDocument tokenPayloadJsonDoc = QJsonDocument::fromJson(QByteArray::fromBase64(jwtParts.at(1)));
m_userId = tokenPayloadJsonDoc.toVariant().toMap().value("cognito:username").toByteArray(); m_userId = tokenPayloadJsonDoc.toVariant().toMap().value("cognito:username").toByteArray();
qDebug() << "Getting cognito ID"; // qDebug() << "Getting cognito ID";
getId(); getId();
}); });
} }
@ -597,7 +597,7 @@ void AWSClient::getId()
} }
m_identityId = jsonDoc.toVariant().toMap().value("IdentityId").toByteArray(); m_identityId = jsonDoc.toVariant().toMap().value("IdentityId").toByteArray();
qDebug() << "Received cognito identity id" << m_identityId;// << qUtf8Printable(data); // qDebug() << "Received cognito identity id" << m_identityId;// << qUtf8Printable(data);
getCredentialsForIdentity(m_identityId); getCredentialsForIdentity(m_identityId);
}); });

View File

@ -393,7 +393,7 @@ bool NymeaConnection::storePem(const QUrl &host, const QByteArray &pem)
bool NymeaConnection::loadPem(const QUrl &host, QByteArray &pem) bool NymeaConnection::loadPem(const QUrl &host, QByteArray &pem)
{ {
QDir dir(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/sslcerts/"); QDir dir(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/sslcerts/");
qDebug() << "Loading certificates from:" << dir.absoluteFilePath(host.host() + ".pem"); // qDebug() << "Loading certificates from:" << dir.absoluteFilePath(host.host() + ".pem");
QFile certFile(dir.absoluteFilePath(host.host() + ".pem")); QFile certFile(dir.absoluteFilePath(host.host() + ".pem"));
if (!certFile.open(QFile::ReadOnly)) { if (!certFile.open(QFile::ReadOnly)) {
return false; return false;

View File

@ -84,12 +84,11 @@ void NymeaHosts::removeHost(NymeaHost *host)
emit countChanged(); emit countChanged();
} }
NymeaHost *NymeaHosts::createHost(const QString &name, const QUrl &url) NymeaHost *NymeaHosts::createHost(const QString &name, const QUrl &url, Connection::BearerType bearerType)
{ {
NymeaHost *host = new NymeaHost(this); NymeaHost *host = new NymeaHost(this);
host->setUuid(QUuid::createUuid());
host->setName(name); host->setName(name);
Connection *connection = new Connection(url, Connection::BearerTypeAll, false, url.toString(), host); Connection *connection = new Connection(url, bearerType, false, url.toString(), host);
host->connections()->addConnection(connection); host->connections()->addConnection(connection);
addHost(host); addHost(host);
return host; return host;
@ -195,6 +194,11 @@ void NymeaHostsFilterModel::setShowUnreachableBearers(bool showUnreachableBearer
} }
} }
NymeaHost *NymeaHostsFilterModel::get(int index) const
{
return m_nymeaDiscovery->nymeaHosts()->get(mapToSource(this->index(index, 0)).row());
}
bool NymeaHostsFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const bool NymeaHostsFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{ {
Q_UNUSED(sourceParent) Q_UNUSED(sourceParent)

View File

@ -25,8 +25,8 @@
#include <QList> #include <QList>
#include <QBluetoothAddress> #include <QBluetoothAddress>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include "nymeahost.h"
class NymeaHost;
class NymeaDiscovery; class NymeaDiscovery;
class NymeaConnection; class NymeaConnection;
@ -49,7 +49,7 @@ public:
void addHost(NymeaHost *host); void addHost(NymeaHost *host);
void removeHost(NymeaHost *host); void removeHost(NymeaHost *host);
Q_INVOKABLE NymeaHost* createHost(const QString &name, const QUrl &url); Q_INVOKABLE NymeaHost* createHost(const QString &name, const QUrl &url, Connection::BearerType bearerType);
Q_INVOKABLE NymeaHost *get(int index) const; Q_INVOKABLE NymeaHost *get(int index) const;
Q_INVOKABLE NymeaHost *find(const QUuid &uuid); Q_INVOKABLE NymeaHost *find(const QUuid &uuid);
@ -89,6 +89,8 @@ public:
bool showUnreachableBearers() const; bool showUnreachableBearers() const;
void setShowUnreachableBearers(bool showUnreachableBearers); void setShowUnreachableBearers(bool showUnreachableBearers);
Q_INVOKABLE NymeaHost* get(int index) const;
signals: signals:
void countChanged(); void countChanged();
void discoveryChanged(); void discoveryChanged();

View File

@ -363,6 +363,7 @@ void JsonRpcClient::dataReceived(const QByteArray &data)
if (!protoVersionString.contains('.')) { if (!protoVersionString.contains('.')) {
protoVersionString.prepend("0."); protoVersionString.prepend("0.");
} }
m_jsonRpcVersion = QVersionNumber::fromString(protoVersionString); m_jsonRpcVersion = QVersionNumber::fromString(protoVersionString);
qDebug() << "Handshake reply:" << "Protocol version:" << protoVersionString << "InitRequired:" << m_initialSetupRequired << "AuthRequired:" << m_authenticationRequired << "PushButtonAvailable:" << m_pushButtonAuthAvailable;; qDebug() << "Handshake reply:" << "Protocol version:" << protoVersionString << "InitRequired:" << m_initialSetupRequired << "AuthRequired:" << m_authenticationRequired << "PushButtonAvailable:" << m_pushButtonAuthAvailable;;
@ -377,6 +378,11 @@ void JsonRpcClient::dataReceived(const QByteArray &data)
emit handshakeReceived(); emit handshakeReceived();
if (m_connection->currentHost()->uuid().isNull()) {
qDebug() << "Updating Server UUID in connection:" << m_connection->currentHost()->uuid().toString() << "->" << m_serverUuid;
m_connection->currentHost()->setUuid(m_serverUuid);
}
if (m_initialSetupRequired) { if (m_initialSetupRequired) {
emit initialSetupRequiredChanged(); emit initialSetupRequiredChanged();
return; return;

View File

@ -102,14 +102,13 @@ Item {
print("Last connected host was", tabSettings.lastConnectedHost) print("Last connected host was", tabSettings.lastConnectedHost)
var cachedHost = discovery.nymeaHosts.find(tabSettings.lastConnectedHost); var cachedHost = discovery.nymeaHosts.find(tabSettings.lastConnectedHost);
if (cachedHost) { if (cachedHost) {
engine.connection.currentHost = cachedHost engine.connection.connect(cachedHost)
} else { return;
print("Warning: There is a last connected host but UUID is unknown to discovery...")
} }
} else { print("Warning: There is a last connected host but UUID is unknown to discovery...")
PlatformHelper.hideSplashScreen();
pageStack.push(Qt.resolvedUrl("connection/ConnectPage.qml"), StackView.Immediate)
} }
PlatformHelper.hideSplashScreen();
pageStack.push(Qt.resolvedUrl("connection/ConnectPage.qml"), StackView.Immediate)
} }

View File

@ -60,7 +60,7 @@ Page {
} }
onClicked: { onClicked: {
if (index === 2) { if (index === 2) {
var host = discovery.nymeaHosts.createHost("Demo server", "nymea://nymea.nymea.io:2222") var host = discovery.nymeaHosts.createHost("Demo server", "nymea://nymea.nymea.io:2222", Connection.BearerTypeCloud)
engine.connection.connect(host) engine.connection.connect(host)
} else { } else {
pageStack.push(model.get(index).page, {nymeaDiscovery: discovery}); pageStack.push(model.get(index).page, {nymeaDiscovery: discovery});
@ -128,7 +128,7 @@ Page {
width: parent.width width: parent.width
height: app.delegateHeight height: app.delegateHeight
objectName: "discoveryDelegate" + index objectName: "discoveryDelegate" + index
property var nymeaHost: discovery.nymeaHosts.get(index) property var nymeaHost: hostsProxy.get(index)
property string defaultConnectionIndex: { property string defaultConnectionIndex: {
var usedConfigIndex = 0; var usedConfigIndex = 0;
for (var i = 1; i < nymeaHost.connections.count; i++) { for (var i = 1; i < nymeaHost.connections.count; i++) {
@ -209,7 +209,9 @@ Page {
onClicked: { onClicked: {
if (model.deviceType === NymeaHost.DeviceTypeNetwork) { if (model.deviceType === NymeaHost.DeviceTypeNetwork) {
swipe.close() swipe.close()
var popup = infoDialog.createObject(app,{nymeaHost: discovery.nymeaHosts.get(index)}) var nymeaHost = hostsProxy.get(index);
print("Getting info for", nymeaHost.name)
var popup = infoDialog.createObject(app,{nymeaHost: nymeaHost})
popup.open() popup.open()
} }
} }
@ -267,7 +269,7 @@ Page {
visible: discovery.nymeaHosts.count === 0 visible: discovery.nymeaHosts.count === 0
text: qsTr("Demo mode (online)") text: qsTr("Demo mode (online)")
onClicked: { onClicked: {
var host = nymeaHosts.createHost("Demo server", "nymea://nymea.nymea.io:2222") var host = nymeaHosts.createHost("Demo server", "nymea://nymea.nymea.io:2222", Connection.BearerTypeCloud)
engine.connection.connect(host) engine.connection.connect(host)
} }
} }

View File

@ -53,7 +53,7 @@ Page {
errorMessage = qsTr("%1:core could not be found on this address. Please make sure you entered the address correctly and that the box is powered on.").arg(app.systemName); errorMessage = qsTr("%1:core could not be found on this address. Please make sure you entered the address correctly and that the box is powered on.").arg(app.systemName);
break; break;
case NymeaConnection.ConnectionStatusConnectionRefused: case NymeaConnection.ConnectionStatusConnectionRefused:
errorMessage = qsTr("The host has rejected our connection. This probably means that %1 stopped running. Did you unplug your %1 box?").arg(app.systemName); errorMessage = qsTr("The host has rejected our connection. This probably means that %1 is not running on this host. Perhaps it's restarting?").arg(app.systemName);
break; break;
case NymeaConnection.ConnectionStatusRemoteHostClosed: case NymeaConnection.ConnectionStatusRemoteHostClosed:
errorMessage = qsTr("%1:core has closed the connection. This probably means it has been turned off or restarted.").arg(app.systemName); errorMessage = qsTr("%1:core has closed the connection. This probably means it has been turned off or restarted.").arg(app.systemName);
@ -70,7 +70,7 @@ Page {
break; break;
case NymeaConnection.ConnectionStatusUnknownError: case NymeaConnection.ConnectionStatusUnknownError:
default: default:
errorMessage = qsTr("An unknown error happened. We're very sorry for that. (Error code: %1)").arg(engine.connection.connectionStatus); errorMessage = qsTr("An unknown error happened. We're very sorry for that.").arg(engine.connection.connectionStatus);
} }
return errorMessage; return errorMessage;
} }

View File

@ -96,12 +96,8 @@ Page {
} }
print("Try to connect ", rpcUrl) print("Try to connect ", rpcUrl)
engine.connection.connect(rpcUrl) var host = discovery.nymeaHosts.createHost("Manual connection", rpcUrl, Connection.BearerTypeCloud);
var page = pageStack.push(Qt.resolvedUrl("ConnectingPage.qml")) engine.connection.connect(host)
page.cancel.connect(function() {
engine.connection.disconnect()
pageStack.pop(root)
})
} }
} }
} }

View File

@ -185,7 +185,7 @@ Item {
readonly property var powerState: device.states.getState(powerStateType.id) readonly property var powerState: device.states.getState(powerStateType.id)
readonly property var brightnessStateType: deviceClass.stateTypes.findByName("brightness"); readonly property var brightnessStateType: deviceClass.stateTypes.findByName("brightness");
readonly property var brightnessState: device.states.getState(brightnessStateType.id) readonly property var brightnessState: brightnessStateType ? device.states.getState(brightnessStateType.id) : null
ThrottledSlider { ThrottledSlider {
Layout.fillWidth: true Layout.fillWidth: true
@ -195,7 +195,7 @@ Item {
enabled: opacity > 0 enabled: opacity > 0
from: 0 from: 0
to: 100 to: 100
value: brightnessState.value value: brightnessState ? brightnessState.value : 0
onMoved: { onMoved: {
var deviceClass = engine.deviceManager.deviceClasses.getDeviceClass(device.deviceClassId); var deviceClass = engine.deviceManager.deviceClasses.getDeviceClass(device.deviceClassId);
var actionType = deviceClass.actionTypes.findByName("brightness"); var actionType = deviceClass.actionTypes.findByName("brightness");