Actualyl implement solalaweb backend access

This commit is contained in:
2024-07-07 19:13:29 +02:00
parent 7b93efe4dd
commit af9ce18404
4 changed files with 148 additions and 12 deletions

View File

@@ -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)
}
}
}
}

View File

@@ -1,5 +1,8 @@
#include "appsettings.h"
#include <QFile>
#include <QQmlFile>
#include <algorithm>
std::vector<SavedDevice> 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;
}

View File

@@ -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<SavedDevice> 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<int> m_numberOfAppInstances;
mutable std::optional<QString> m_solalawebKey;
mutable std::optional<QString> m_solalawebCert;
};

View File

@@ -6,6 +6,9 @@
#include <QJsonParseError>
#include <QPasswordDigestor>
#include <QMessageAuthenticationCode>
#include <QSslConfiguration>
#include <QSslCertificate>
#include <QSslKey>
#include <utility>
@@ -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});
}