some fixes, settings are now working correctly

This commit is contained in:
Dorian Zedler 2020-07-11 20:30:04 +02:00
parent 152887b1e3
commit f336e953eb
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
11 changed files with 165 additions and 137 deletions

View file

@ -23,7 +23,8 @@ SOURCES += \
$$PWD/sources/scstwsoundplayer.cpp \
$$PWD/sources/scstwlibraries.cpp \
$$PWD/sources/scstwrace.cpp \
$$PWD/sources/scstwtimer.cpp
$$PWD/sources/scstwtimer.cpp \
$$PWD/sources/scstwsetting.cpp
HEADERS += \
$$PWD/headers/ScStw.hpp \
@ -32,20 +33,19 @@ HEADERS += \
$$PWD/headers/scstwrace.h \
$$PWD/headers/scstwsettings.h \
$$PWD/headers/scstwsoundplayer.h \
$$PWD/headers/scstwtimer.h
$$PWD/headers/scstwtimer.h \
$$PWD/headers/scstwsetting.h
ScStwLibraries_ClientLibs {
SOURCES += \
$$PWD/sources/client/scstwclient.cpp \
$$PWD/sources/client/scstwremotesettings.cpp \
$$PWD/sources/client/scstwremotemonitorrace.cpp \
$$PWD/sources/client/scstwsetting.cpp
$$PWD/sources/client/scstwremotemonitorrace.cpp
HEADERS += \
$$PWD/headers/client/scstwclient.h \
$$PWD/headers/client/scstwremotesettings.h \
$$PWD/headers/client/scstwremotemonitorrace.h \
$$PWD/headers/client/scstwsetting.h
$$PWD/headers/client/scstwremotemonitorrace.h
INCLUDEPATH += $$PWD/headers/client
}

View file

@ -20,7 +20,7 @@ public:
protected:
QVariant readSetting(QString key, int keyInt, int keyLevel);
bool writeSetting(QString key, QVariant value, int keyInt,int keyLevel = -1);
void setDefaultSetting(QString key, QVariant defaultVariant, int keyInt,int keyLevel = -1);
bool setDefaultSetting(QString key, QVariant defaultVariant, int keyInt,int keyLevel = -1);
private:
ScStwClient * scStwClient;

View file

@ -1,47 +0,0 @@
#ifndef SCSTWQMLSETTING_H
#define SCSTWQMLSETTING_H
#include <scstwsettings.h>
#include <QObject>
class ScStwSetting : public QObject
{
Q_OBJECT
Q_PROPERTY(ScStwSettings* scStwSettings READ getScStwSettings WRITE setScStwSettings NOTIFY scStwSettingsChanged)
Q_PROPERTY(int key READ getKey WRITE setKey NOTIFY keyChanged)
Q_PROPERTY(int keyLevel READ getKeyLevel WRITE setKeyLevel NOTIFY keyLevelChanged)
Q_PROPERTY(QVariant value READ getValue WRITE setValue NOTIFY valueChanged)
public:
explicit ScStwSetting(QObject *parent = nullptr);
private:
ScStwSettings* scStwSettings;
int key;
int keyLevel;
QVariant valueCache;
public slots:
ScStwSettings* getScStwSettings();
void setScStwSettings(ScStwSettings* scStwSettings);
int getKey();
void setKey(int key);
int getKeyLevel();
void setKeyLevel(int keyLevel);
QVariant getValue();
void setValue(QVariant value);
private slots:
void handleSettingChange(int key, int keyLevel, QVariant value);
signals:
void scStwSettingsChanged();
void keyChanged();
void keyLevelChanged();
void valueChanged();
};
#endif // SCSTWQMLSETTING_H

View file

@ -0,0 +1,37 @@
#ifndef SCSTWQMLSETTING_H
#define SCSTWQMLSETTING_H
#include <QObject>
#include <QVariant>
class ScStwSetting : public QObject
{
Q_OBJECT
Q_PROPERTY(QVariant value READ getValue WRITE setValue NOTIFY valueChanged)
protected:
explicit ScStwSetting(QObject *parent = nullptr);
int key;
int keyLevel;
bool hasToReload;
virtual bool writeSetting(int, int, QVariant) = 0;
virtual QVariant readSetting(int, int) = 0;
private:
QVariant valueCache;
public slots:
QVariant getValue();
void setValue(QVariant value);
protected slots:
void handleSettingChange(int key, int keyLevel, QVariant value);
signals:
void valueChanged();
};
#endif // SCSTWQMLSETTING_H

View file

@ -10,6 +10,7 @@
#include <QJsonDocument>
#include <ScStw.hpp>
#include <QDir>
#include <scstwsetting.h>
class ScStwSettings : public QObject
{
@ -17,6 +18,8 @@ class ScStwSettings : public QObject
public:
explicit ScStwSettings(QObject *parent = nullptr);
typedef QString(*keyToStringConverter)(int);
/*!
* \brief The BaseStationSetting enum contains all settings of the base station that can be changed by a client
*
@ -40,21 +43,29 @@ public:
};
Q_ENUM(KeyLevelEnum)
Q_INVOKABLE QVariant readSetting(BaseStationSetting key);
Q_INVOKABLE virtual QVariant readSetting(int key, int level);
Q_INVOKABLE bool writeSetting(BaseStationSetting key, QVariant value);
Q_INVOKABLE virtual bool writeSetting(int key, QVariant value, int level);
Q_INVOKABLE void setDefaultSetting(BaseStationSetting key, QVariant defaultVariant);
virtual QVariant readSetting(BaseStationSetting key);
Q_INVOKABLE virtual QVariant readSetting(int key, int keyLevel);
virtual bool writeSetting(BaseStationSetting key, QVariant value);
Q_INVOKABLE virtual bool writeSetting(int key, int keyLevel, QVariant value);
virtual bool setDefaultSetting(BaseStationSetting key, QVariant defaultValue);
Q_INVOKABLE virtual bool setDefaultSetting(int key, int keyLevel, QVariant defaultValue);
Q_INVOKABLE ScStwSetting * getSetting(int key, int keyLevel);
static BaseStationSetting keyFromInt(int i) {
QMetaEnum enumeration = QMetaEnum::fromType<BaseStationSetting>();
return static_cast<BaseStationSetting>(enumeration.keyToValue(enumeration.valueToKey(i)));
}
static QString keyToString(int key) {
return QMetaEnum::fromType<BaseStationSetting>().valueToKey(key);
}
protected:
virtual QVariant readSetting(QString key, int keyInt = -1, int keyLevel = -1);
virtual bool writeSetting(QString key, QVariant value, int keyInt = -1,int keyLevel = -1);
virtual void setDefaultSetting(QString key, QVariant defaultVariant, int keyInt,int keyLevel = -1);
virtual bool setDefaultSetting(QString key, QVariant defaultValue, int keyInt,int keyLevel = -1);
bool registerKeyLevelHandler(int keyLevel, keyToStringConverter);
private:
QFile * settingsFile;
@ -62,6 +73,30 @@ private:
bool loadSettingsFromFile();
class ScStwSettingInternal: public ScStwSetting {
public:
ScStwSettingInternal(int key, int keyLevel, ScStwSettings*scStwSettings, QObject *parent):ScStwSetting(parent) {
this->scStwSettings = scStwSettings;
connect(this->scStwSettings, &ScStwSettings::settingChanged, this, &ScStwSettingInternal::handleSettingChange);
this->key = key;
this->keyLevel = keyLevel;
this->handleSettingChange(-1, 0, QVariant());
}
protected:
ScStwSettings *scStwSettings;
bool writeSetting(int key, int keyLevel, QVariant value) {
return this->scStwSettings->writeSetting(key, keyLevel, value);
}
QVariant readSetting(int key, int keyLevel) {
return this->scStwSettings->readSetting(key, keyLevel);
}
};
QMap<int, keyToStringConverter> keyToStringConverters;
QMap<int, QMap<int, ScStwSettingInternal*>> internalSettingHandlers;
private slots:
bool writeSettingsToFile();

View file

@ -91,7 +91,7 @@ bool ScStwClient::init() {
this->apiVersion = initResponse["data"].toMap()["apiVersion"].toString();
qDebug() << "[INFO][CLIENT] base station api version is: " << this->apiVersion;
int compareResult = ScStw::firmwareCompare(this->API_VERSION, this->apiVersion);
qDebug() << "compare result is: " << compareResult;
//qDebug() << "compare result is: " << compareResult;
if( compareResult == -3 ){
// the client version is out of date!!
this->closeConnection();
@ -373,7 +373,7 @@ void ScStwClient::handleSignal(QVariantMap data) {
ScStw::SignalKey signalKey = ScStw::signalKeyFromInt(data["header"].toInt());
qDebug() << "got signal: " << signalKey << " with data: " << data["data"];
//qDebug() << "got signal: " << signalKey << " with data: " << data["data"];
switch (signalKey) {
case ScStw::ExtensionsChanged:
@ -409,7 +409,7 @@ QVariant ScStwClient::readRemoteSetting(ScStwSettings::BaseStationSetting key) {
if(reply["status"] != 200){
return "false";
}
return reply["data"].toString();
return reply["data"];
}
void ScStwClient::setIP(QString newIp){

View file

@ -16,28 +16,26 @@ ScStwRemoteSettings::SettingsMode ScStwRemoteSettings::getMode() {
}
QVariant ScStwRemoteSettings::readSetting(QString key, int keyInt, int keyLevel) {
if(this->getMode() == LOCAL || keyLevel > 0)
if(this->getMode() == LOCAL || keyLevel > ScStwSettings::KeyLevel)
return ScStwSettings::readSetting(key, keyInt, keyLevel);
return this->scStwClient->readRemoteSetting(ScStwSettings::BaseStationSetting(keyInt));
}
bool ScStwRemoteSettings::writeSetting(QString key, QVariant value, int keyInt, int keyLevel) {
if(this->getMode() == LOCAL || keyLevel > 0)
if(this->getMode() == LOCAL || keyLevel > ScStwSettings::KeyLevel)
return ScStwSettings::writeSetting(key, value, keyInt, keyLevel);
qDebug() << "changing setting";
ScStw::StatusCode res = this->scStwClient->writeRemoteSetting(ScStwSettings::BaseStationSetting(keyInt), value);
qDebug() << "changed setting " << res;
return res == ScStw::Success;
}
void ScStwRemoteSettings::setDefaultSetting(QString key, QVariant defaultVariant, int keyInt, int keyLevel) {
if(this->getMode() == LOCAL || keyLevel > 0)
bool ScStwRemoteSettings::setDefaultSetting(QString key, QVariant defaultVariant, int keyInt, int keyLevel) {
if(this->getMode() == LOCAL || keyLevel > ScStwSettings::KeyLevel)
return ScStwSettings::setDefaultSetting(key, defaultVariant, keyInt, keyLevel);
return;
return false;
}
void ScStwRemoteSettings::handleClientStateChange() {
@ -48,7 +46,7 @@ void ScStwRemoteSettings::handleClientStateChange() {
void ScStwRemoteSettings::handleBaseStationSignal(ScStw::SignalKey key, QVariant data) {
switch (key) {
case ScStw::SettingChanged:
emit this->settingChanged(data.toMap()["key"].toInt(), 0, data.toMap()["value"]);
emit this->settingChanged(data.toMap()["key"].toInt(), ScStwSettings::KeyLevel, data.toMap()["value"]);
default:
break;
}

View file

@ -1,54 +0,0 @@
#include "scstwsetting.h"
ScStwSetting::ScStwSetting(QObject *parent) : QObject(parent)
{
}
ScStwSettings* ScStwSetting::getScStwSettings(){
return this->scStwSettings;
}
void ScStwSetting::setScStwSettings(ScStwSettings* scStwSettings) {
if(this->scStwSettings != scStwSettings && scStwSettings != nullptr) {
this->scStwSettings = scStwSettings;
connect(this->scStwSettings, &ScStwSettings::settingChanged, this, &ScStwSetting::handleSettingChange);
emit this->scStwSettingsChanged();
}
}
int ScStwSetting::getKey() {
return this->key;
}
void ScStwSetting::setKey(int key) {
if(this->key != key) {
this->key = key;
this->handleSettingChange(this->key, this->keyLevel, this->scStwSettings->readSetting(this->key, this->keyLevel));
emit this->keyChanged();
}
}
int ScStwSetting::getKeyLevel() {
return this->keyLevel;
}
void ScStwSetting::setKeyLevel(int keyLevel) {
if(this->keyLevel != keyLevel) {
this->keyLevel = keyLevel;
this->handleSettingChange(this->key, this->keyLevel, this->scStwSettings->readSetting(this->key, this->keyLevel));
emit this->keyLevelChanged();
}
}
QVariant ScStwSetting::getValue() {
return this->valueCache;
}
void ScStwSetting::setValue(QVariant value) {
if(value != this->valueCache && this->scStwSettings != nullptr) {
this->scStwSettings->writeSetting(this->key, value, this->keyLevel);
}
}
void ScStwSetting::handleSettingChange(int key, int keyLevel, QVariant value) {
if(keyLevel == this->keyLevel && key == this->key) {
this->valueCache = value;
emit this->valueChanged();
}
}

View file

@ -35,7 +35,7 @@ void ScStwLibraries::init() {
#ifdef ScStwLibraries_ClientLibs
qmlRegisterType<ScStwClient>("de.itsblue.ScStw", 2, 0, "ScStwClient");
qmlRegisterType<ScStwSettings>("de.itsblue.ScStw", 2, 0, "ScStwSettings");
qmlRegisterType<ScStwSetting>("de.itsblue.ScStw", 2, 0, "ScStwSetting");
qmlRegisterUncreatableType<ScStwSetting>("de.itsblue.ScStw", 2, 0, "ScStwSetting", "The ScStwSetting is manage by a ScStwSettings instance and therefore not creatable");
#endif
#endif
}

View file

@ -0,0 +1,31 @@
#include "scstwsetting.h"
#include <QtDebug>
ScStwSetting::ScStwSetting(QObject *parent) : QObject(parent)
{
}
QVariant ScStwSetting::getValue() {
if(this->hasToReload) {
this->valueCache = this->readSetting(this->key, this->keyLevel);
this->hasToReload = false;
}
return this->valueCache;
}
void ScStwSetting::setValue(QVariant value) {
if(value != this->valueCache) {
this->writeSetting(this->key, this->keyLevel, value);
}
}
void ScStwSetting::handleSettingChange(int key, int keyLevel, QVariant value) {
if(keyLevel == this->keyLevel && key == this->key) {
this->valueCache = value;
qDebug() << "value changed!!! key: " << key << " new value " << value;
emit this->valueChanged();
}
else if(key == -1 && this->key != -1 && this->keyLevel != -1 && this->keyLevel == keyLevel) {
this->hasToReload = true;
emit this->valueChanged();
}
}

View file

@ -16,32 +16,41 @@ ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent)
qFatal("[FATAL] Settings file (%s) is of invalid format!", qPrintable(path + "/settings.json"));
connect(this, &ScStwSettings::settingChanged, this, &ScStwSettings::writeSettingsToFile);
this->registerKeyLevelHandler(ScStwSettings::KeyLevel, &ScStwSettings::keyToString);
}
QVariant ScStwSettings::readSetting(BaseStationSetting key) {
return this->readSetting(QMetaEnum::fromType<BaseStationSetting>().valueToKey(int(key)), int(key), 0);
return this->readSetting(key, 0);
}
QVariant ScStwSettings::readSetting(int key, int keyLevel) {
if(keyLevel == ScStwSettings::KeyLevel)
return this->readSetting(ScStwSettings::BaseStationSetting(key));
if(this->keyToStringConverters.contains(keyLevel))
return this->readSetting(this->keyToStringConverters[keyLevel](key), key, keyLevel);
else
return QVariant();
}
bool ScStwSettings::writeSetting(BaseStationSetting key, QVariant value) {
return this->writeSetting(QMetaEnum::fromType<BaseStationSetting>().valueToKey(int(key)), value, int(key), 0);
return this->writeSetting(key, 0, value);
}
bool ScStwSettings::writeSetting(int key, QVariant value, int keyLevel) {
if(keyLevel == ScStwSettings::KeyLevel)
return this->writeSetting(ScStwSettings::BaseStationSetting(key), value);
bool ScStwSettings::writeSetting(int key, int keyLevel, QVariant value) {
if(this->keyToStringConverters.contains(keyLevel))
return this->writeSetting(this->keyToStringConverters[keyLevel](key), value, key, keyLevel);
else
return false;
}
void ScStwSettings::setDefaultSetting(BaseStationSetting key, QVariant defaultVariant) {
this->setDefaultSetting(QMetaEnum::fromType<BaseStationSetting>().valueToKey(int(key)), defaultVariant, int(key), 0);
bool ScStwSettings::setDefaultSetting(BaseStationSetting key, QVariant defaultValue) {
return this->setDefaultSetting(key, 0, defaultValue);
}
bool ScStwSettings::setDefaultSetting(int key, int keyLevel, QVariant defaultValue) {
if(this->keyToStringConverters.contains(keyLevel))
return this->setDefaultSetting(this->keyToStringConverters[keyLevel](key), defaultValue, key, keyLevel);
else
return false;
}
QVariant ScStwSettings::readSetting(QString key, int keyInt, int keyLevel) {
@ -63,9 +72,28 @@ bool ScStwSettings::writeSetting(QString key, QVariant value, int keyInt, int ke
return true;
}
void ScStwSettings::setDefaultSetting(QString key, QVariant defaultVariant, int keyInt, int keyLevel) {
bool ScStwSettings::setDefaultSetting(QString key, QVariant defaultValue, int keyInt, int keyLevel) {
if(!this->settingsCache.contains(key))
this->writeSetting(key, defaultVariant, keyInt, keyLevel);
return this->writeSetting(key, defaultValue, keyInt, keyLevel);
return true;
}
ScStwSetting * ScStwSettings::getSetting(int key, int keyLevel) {
if(!this->internalSettingHandlers.contains(keyLevel))
this->internalSettingHandlers.insert(keyLevel, {});
if(!this->internalSettingHandlers[keyLevel].contains(key))
this->internalSettingHandlers[keyLevel].insert(key, new ScStwSettingInternal(key, keyLevel, this, this));
return this->internalSettingHandlers[keyLevel][key];
}
bool ScStwSettings::registerKeyLevelHandler(int keyLevel, keyToStringConverter converterFunction) {
if(this->keyToStringConverters.contains(keyLevel))
return false;
this->keyToStringConverters.insert(keyLevel, converterFunction);
return true;
}
/*