completed new settings structure and added class to manage remote settings

This commit is contained in:
Dorian Zedler 2020-07-10 15:20:13 +02:00
parent 3d18b72eaf
commit 6c1ce8e654
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
9 changed files with 125 additions and 21 deletions

View file

@ -19,6 +19,7 @@ INCLUDEPATH += $$PWD/headers $$PWD
SOURCES += \
$$PWD/sources/ScStw.cpp \
$$PWD/sources/client/scstwremotesettings.cpp \
$$PWD/sources/scstwsettings.cpp \
$$PWD/sources/scstwsoundplayer.cpp \
$$PWD/sources/scstwlibraries.cpp \
@ -28,6 +29,7 @@ SOURCES += \
HEADERS += \
$$PWD/headers/ScStw.hpp \
$$PWD/headers/ScStwLibraries_global.h \
$$PWD/headers/client/scstwremotesettings.h \
$$PWD/headers/scstwlibraries.h \
$$PWD/headers/scstwrace.h \
$$PWD/headers/scstwsettings.h \

View file

@ -61,7 +61,8 @@ public:
RaceStateChanged = 9000,
TimersChanged = 9001,
ExtensionsChanged = 9002,
NextStartActionChanged = 9003 /*, ProfilesChanged*/
NextStartActionChanged = 9003, /*, ProfilesChanged*/
SettingChanged = 9004
};
Q_ENUM(SignalKey)

View file

@ -140,14 +140,14 @@ public slots:
* \param value the value to write to
* \return the status code returned by the command
*/
ScStw::StatusCode writeRemoteSetting(ScStwSettings::BaseStationSetting key, QString value);
ScStw::StatusCode writeRemoteSetting(ScStwSettings::BaseStationSetting key, QVariant value);
/*!
* \brief Function to read a setting on the base station
* \param key the key to read from
* \return the value of the key or "false" if the key is not found or an error occured
*/
QString readRemoteSetting(ScStwSettings::BaseStationSetting key);
QVariant readRemoteSetting(ScStwSettings::BaseStationSetting key);
/*! Getter fuctions */

View file

@ -0,0 +1,35 @@
#ifndef SCSTWREMOTESETTINGS_H
#define SCSTWREMOTESETTINGS_H
#include <QObject>
#include "scstwsettings.h"
#include "scstwclient.h"
#include <QMetaEnum>
class ScStwRemoteSettings : public ScStwSettings
{
Q_OBJECT
public:
ScStwRemoteSettings(ScStwClient * scStwClient, QObject * parent = nullptr);
enum SettingsMode {
LOCAL,
REMOTE
};
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);
private:
ScStwClient * scStwClient;
SettingsMode getMode();
private slots:
void handleClientStateChange();
void handleBaseStationSignal(ScStw::SignalKey key, QVariant data);
};
#endif // SCSTWREMOTESETTINGS_H

View file

@ -9,6 +9,7 @@
#include <QStandardPaths>
#include <QJsonDocument>
#include <ScStw.hpp>
#include <QDir>
class ScStwSettings : public QObject
{
@ -32,12 +33,16 @@ public:
AtYourMarksSoundDelaySetting,
SoundVolumeSetting
};
Q_ENUM(BaseStationSetting)
QVariant readSetting(BaseStationSetting key);
bool writeSetting(BaseStationSetting key, QVariant value);
void setDefaultSetting(BaseStationSetting key, QVariant defaultVariant);
enum KeyLevelEnum {
KeyLevel = 0
};
Q_ENUM(KeyLevelEnum)
Q_INVOKABLE QVariant readSetting(BaseStationSetting key);
Q_INVOKABLE bool writeSetting(BaseStationSetting key, QVariant value);
Q_INVOKABLE void setDefaultSetting(BaseStationSetting key, QVariant defaultVariant);
static BaseStationSetting keyFromInt(int i) {
QMetaEnum enumeration = QMetaEnum::fromType<BaseStationSetting>();
@ -45,9 +50,9 @@ public:
}
protected:
QVariant readSetting(QString key);
bool writeSetting(QString key, QVariant value, int keyInt,int keyLevel = -1);
void setDefaultSetting(QString key, QVariant defaultVariant, int keyInt,int keyLevel = -1);
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);
private:
QFile * settingsFile;

View file

@ -392,15 +392,15 @@ void ScStwClient::handleSignal(QVariantMap data) {
// --- helper functions ---
// ------------------------
ScStw::StatusCode ScStwClient::writeRemoteSetting(ScStwSettings::BaseStationSetting key, QString value) {
ScStw::StatusCode ScStwClient::writeRemoteSetting(ScStwSettings::BaseStationSetting key, QVariant value) {
QJsonArray requestData;
requestData.append(key);
requestData.append(value);
requestData.append(int(key));
requestData.append(QJsonValue::fromVariant(value));
return ScStw::StatusCode(this->sendCommand(3000, requestData)["status"].toInt());
}
QString ScStwClient::readRemoteSetting(ScStwSettings::BaseStationSetting key) {
QVariantMap reply = this->sendCommand(3001, key);
QVariant ScStwClient::readRemoteSetting(ScStwSettings::BaseStationSetting key) {
QVariantMap reply = this->sendCommand(3001, int(key));
if(reply["status"] != 200){
return "false";
}

View file

@ -0,0 +1,53 @@
#include "../../headers/client/scstwremotesettings.h"
ScStwRemoteSettings::ScStwRemoteSettings(ScStwClient * scStwClient, QObject * parent) : ScStwSettings(parent)
{
this->scStwClient = scStwClient;
connect(this->scStwClient, &ScStwClient::gotSignal, this, &ScStwRemoteSettings::handleBaseStationSignal);
}
ScStwRemoteSettings::SettingsMode ScStwRemoteSettings::getMode() {
if(this->scStwClient->getState() == ScStwClient::CONNECTED)
return ScStwRemoteSettings::REMOTE;
else
return ScStwRemoteSettings::LOCAL;
}
QVariant ScStwRemoteSettings::readSetting(QString key, int keyInt, int keyLevel) {
if(this->getMode() == LOCAL || keyLevel > 0)
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)
return ScStwSettings::writeSetting(key, value, keyInt, keyLevel);
this->scStwClient->writeRemoteSetting(ScStwSettings::BaseStationSetting(keyInt), value);
emit this->settingChanged(keyInt, keyLevel);
return true;
}
void ScStwRemoteSettings::setDefaultSetting(QString key, QVariant defaultVariant, int keyInt, int keyLevel) {
if(this->getMode() == LOCAL || keyLevel > 0)
return ScStwSettings::setDefaultSetting(key, defaultVariant, keyInt, keyLevel);
return;
}
void ScStwRemoteSettings::handleClientStateChange() {
if(this->scStwClient->getState() == ScStwClient::DISCONNECTED)
emit this->settingChanged(-1, 0);
}
void ScStwRemoteSettings::handleBaseStationSignal(ScStw::SignalKey key, QVariant data) {
switch (key) {
case ScStw::SettingChanged:
emit this->settingChanged(data.toMap()["key"].toInt(), 0);
default:
break;
}
}

View file

@ -26,7 +26,7 @@ ScStwLibraries::ScStwLibraries(QObject *parent) : QObject(parent)
void ScStwLibraries::init() {
#ifdef ScStwLibraries_QML
qmlRegisterType<ScStw>("de.itsblue.ScStw", 2, 0, "ScStw");
qRegisterMetaType<ScStwSettings::BaseStationSetting>("ScStw::BaseStationSetting");
qRegisterMetaType<ScStwSettings::BaseStationSetting>("ScStwSettings::BaseStationSetting");
qRegisterMetaType<ScStw::SocketCommand>("ScStw::SocketCommand");
qmlRegisterType<ScStwRace>("de.itsblue.ScStw", 2, 0, "ScStwRace");
@ -34,6 +34,7 @@ void ScStwLibraries::init() {
#ifdef ScStwLibraries_ClientLibs
qmlRegisterType<ScStwClient>("de.itsblue.ScStw", 2, 0, "ScStwClient");
qmlRegisterType<ScStwSettings>("de.itsblue.ScStw", 2, 0, "ScStwSettings");
#endif
#endif
}

View file

@ -2,10 +2,15 @@
ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent)
{
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/settings.json";
this->settingsFile = new QFile(path);
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir dir(path);
this->settingsFile = new QFile(path+ "/settings.json");
if(!this->settingsFile->exists())
if(!dir.mkpath(path))
qFatal("[FATAL] Couldn't create settings dir %s", qPrintable(path));
if(!this->settingsFile->open(QFile::ReadWrite))
qFatal("[FATAL] Couldn't open settings file %s", qPrintable(path));
qFatal("[FATAL] Couldn't open settings file %s", qPrintable(path + "/settings.json"));
if(!this->loadSettingsFromFile() && this->settingsFile->size() != 0)
qFatal("[FATAL] Settings file (%s) is of invalid format!", qPrintable(path));
@ -14,7 +19,7 @@ ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent)
}
QVariant ScStwSettings::readSetting(BaseStationSetting key) {
return this->readSetting(QMetaEnum::fromType<BaseStationSetting>().valueToKey(int(key)));
return this->readSetting(QMetaEnum::fromType<BaseStationSetting>().valueToKey(int(key)), int(key), 0);
}
bool ScStwSettings::writeSetting(BaseStationSetting key, QVariant value) {
@ -25,7 +30,9 @@ void ScStwSettings::setDefaultSetting(BaseStationSetting key, QVariant defaultVa
this->setDefaultSetting(QMetaEnum::fromType<BaseStationSetting>().valueToKey(int(key)), defaultVariant, int(key), 0);
}
QVariant ScStwSettings::readSetting(QString key) {
QVariant ScStwSettings::readSetting(QString key, int keyInt, int keyLevel) {
Q_UNUSED(keyInt)
Q_UNUSED(keyLevel)
return this->settingsCache[key];
}