From af9ce18404810d5a8dd01a6367558777fd309c28 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sun, 7 Jul 2024 19:13:29 +0200 Subject: [PATCH] Actualyl implement solalaweb backend access --- AppSettingsPage.qml | 26 ++++++++++++-- appsettings.cpp | 85 ++++++++++++++++++++++++++++++++++++++------ appsettings.h | 14 ++++++++ deviceconnection.cpp | 35 ++++++++++++++++++ 4 files changed, 148 insertions(+), 12 deletions(-) diff --git a/AppSettingsPage.qml b/AppSettingsPage.qml index 8a879bd..969e4bb 100644 --- a/AppSettingsPage.qml +++ b/AppSettingsPage.qml @@ -1,5 +1,6 @@ import QtQuick import QtQuick.Controls +import QtQuick.Dialogs import QtQuick.Layouts import EVChargerApp @@ -24,6 +25,7 @@ NavigationPage { SpinBox { value: theSettings.numberOfAppInstances onValueModified: theSettings.numberOfAppInstances = value + from: 1 } } } @@ -44,7 +46,17 @@ NavigationPage { } Button { - text: qsTr("Select...") + text: { + console.log('solalawebKey', theSettings.solalawebKey); + return theSettings.solalawebKey == "" ? qsTr("Select...") : qsTr("Replace...") + } + onClicked: keyFileDialog.open() + + FileDialog { + id: keyFileDialog + fileMode: FileDialog.OpenFile + onAccepted: theSettings.loadSolalawebKey(selectedFile) + } } Label { @@ -53,7 +65,17 @@ NavigationPage { } Button { - text: qsTr("Select...") + text: { + console.log('solalawebCert', theSettings.solalawebCert); + return theSettings.solalawebCert == "" ? qsTr("Select...") : qsTr("Replace...") + } + onClicked: certFileDialog.open() + + FileDialog { + id: certFileDialog + fileMode: FileDialog.OpenFile + onAccepted: theSettings.loadSolalawebCert(selectedFile) + } } } } diff --git a/appsettings.cpp b/appsettings.cpp index 323acf6..0d65114 100644 --- a/appsettings.cpp +++ b/appsettings.cpp @@ -1,5 +1,8 @@ #include "appsettings.h" +#include +#include + #include std::vector AppSettings::getSavedDevices() @@ -71,22 +74,84 @@ void AppSettings::removeSavedDevice(const QString &serial) int AppSettings::numberOfAppInstances() const { - if (!m_numberOfAppInstances) - { - bool ok{}; - int numberOfAppInstances = value("numberOfAppInstances", 1).toInt(&ok); - if (!ok) - numberOfAppInstances = 1; - m_numberOfAppInstances = numberOfAppInstances; - return numberOfAppInstances; - } + if (m_numberOfAppInstances) + return *m_numberOfAppInstances; - return *m_numberOfAppInstances; + bool ok{}; + int numberOfAppInstances = value("numberOfAppInstances", 1).toInt(&ok); + if (!ok || numberOfAppInstances < 1) + numberOfAppInstances = 1; + m_numberOfAppInstances = numberOfAppInstances; + return numberOfAppInstances; } void AppSettings::setNumberOfAppInstances(int numberOfAppInstances) { + if (numberOfAppInstances < 1) + return; setValue("numberOfAppInstances", numberOfAppInstances); m_numberOfAppInstances = numberOfAppInstances; emit numberOfAppInstancesChanged(numberOfAppInstances); } + +QString AppSettings::solalawebKey() const +{ + if (m_solalawebKey) + return *m_solalawebKey; + + auto solalawebKey = value("solalawebKey").toString(); + m_solalawebKey = solalawebKey; + return solalawebKey; +} + +void AppSettings::setSolalawebKey(const QString &solalawebKey) +{ + setValue("solalawebKey", solalawebKey); + m_solalawebKey = solalawebKey; + emit solalawebKeyChanged(solalawebKey); +} + +bool AppSettings::loadSolalawebKey(const QString &url) +{ + QFile file{QQmlFile::urlToLocalFileOrQrc(url)}; + if (!file.open(QFile::ReadOnly)) + { + qWarning() << "Could not open file:" << file.errorString(); + return false; + } + + setSolalawebKey(file.readAll()); + + return true; +} + +QString AppSettings::solalawebCert() const +{ + if (m_solalawebCert) + return *m_solalawebCert; + + auto solalawebCert = value("solalawebCert").toString(); + m_solalawebCert = solalawebCert; + return solalawebCert; +} + +void AppSettings::setSolalawebCert(const QString &solalawebCert) +{ + setValue("solalawebCert", solalawebCert); + m_solalawebCert = solalawebCert; + emit solalawebCertChanged(solalawebCert); +} + +bool AppSettings::loadSolalawebCert(const QString &url) +{ + QFile file{QQmlFile::urlToLocalFileOrQrc(url)}; + if (!file.open(QFile::ReadOnly)) + { + qWarning() << "Could not open file:" << file.errorString(); + return false; + } + + setSolalawebCert(file.readAll()); + + return true; +} diff --git a/appsettings.h b/appsettings.h index e7e22d8..1d0caa2 100644 --- a/appsettings.h +++ b/appsettings.h @@ -19,6 +19,8 @@ class AppSettings : public QSettings Q_OBJECT QML_ELEMENT Q_PROPERTY(int numberOfAppInstances READ numberOfAppInstances WRITE setNumberOfAppInstances NOTIFY numberOfAppInstancesChanged FINAL) + Q_PROPERTY(QString solalawebKey READ solalawebKey WRITE setSolalawebKey NOTIFY solalawebKeyChanged FINAL) + Q_PROPERTY(QString solalawebCert READ solalawebCert WRITE setSolalawebCert NOTIFY solalawebCertChanged FINAL) public: std::vector getSavedDevices(); @@ -29,9 +31,21 @@ public: int numberOfAppInstances() const; void setNumberOfAppInstances(int numberOfAppInstances); + QString solalawebKey() const; + void setSolalawebKey(const QString &solalawebKey); + Q_INVOKABLE bool loadSolalawebKey(const QString &url); + + QString solalawebCert() const; + void setSolalawebCert(const QString &solalawebCert); + Q_INVOKABLE bool loadSolalawebCert(const QString &url); + signals: void numberOfAppInstancesChanged(int numberOfAppInstances); + void solalawebKeyChanged(const QString &solalawebKey); + void solalawebCertChanged(const QString &solalawebCert); private: mutable std::optional m_numberOfAppInstances; + mutable std::optional m_solalawebKey; + mutable std::optional m_solalawebCert; }; diff --git a/deviceconnection.cpp b/deviceconnection.cpp index f8c0520..01cb432 100644 --- a/deviceconnection.cpp +++ b/deviceconnection.cpp @@ -6,6 +6,9 @@ #include #include #include +#include +#include +#include #include @@ -57,6 +60,38 @@ void DeviceConnection::setUrl(const QString &url) emit urlChanged(m_url = url); emit logMessage(tr("Connecting to %0").arg(m_url)); + + if (m_settings && !m_settings->solalawebKey().isEmpty() && !m_settings->solalawebCert().isEmpty()) + { + auto sslConfig = m_websocket.sslConfiguration(); + + { + QSslCertificate cert{m_settings->solalawebCert().toUtf8()}; + if (cert.isNull()) + { + emit logMessage(tr("Could not parse solalaweb certificate!")); + goto after; + } + qDebug() << "cert" << cert.issuerDisplayName(); + sslConfig.setLocalCertificate(cert); + } + + { + QSslKey key{m_settings->solalawebKey().toUtf8(), QSsl::KeyAlgorithm::Rsa, QSsl::Pem}; + if (key.isNull()) + { + emit logMessage(tr("Could not parse solalaweb key!")); + goto after; + } + qDebug() << "key" << key.algorithm(); + sslConfig.setPrivateKey(key); + } + + m_websocket.setSslConfiguration(sslConfig); + } + +after: + m_websocket.open(QUrl{m_url}); }