completed new settings structure and added class to manage remote settings
This commit is contained in:
parent
3d18b72eaf
commit
6c1ce8e654
9 changed files with 125 additions and 21 deletions
|
@ -19,6 +19,7 @@ INCLUDEPATH += $$PWD/headers $$PWD
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
$$PWD/sources/ScStw.cpp \
|
$$PWD/sources/ScStw.cpp \
|
||||||
|
$$PWD/sources/client/scstwremotesettings.cpp \
|
||||||
$$PWD/sources/scstwsettings.cpp \
|
$$PWD/sources/scstwsettings.cpp \
|
||||||
$$PWD/sources/scstwsoundplayer.cpp \
|
$$PWD/sources/scstwsoundplayer.cpp \
|
||||||
$$PWD/sources/scstwlibraries.cpp \
|
$$PWD/sources/scstwlibraries.cpp \
|
||||||
|
@ -28,6 +29,7 @@ SOURCES += \
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
$$PWD/headers/ScStw.hpp \
|
$$PWD/headers/ScStw.hpp \
|
||||||
$$PWD/headers/ScStwLibraries_global.h \
|
$$PWD/headers/ScStwLibraries_global.h \
|
||||||
|
$$PWD/headers/client/scstwremotesettings.h \
|
||||||
$$PWD/headers/scstwlibraries.h \
|
$$PWD/headers/scstwlibraries.h \
|
||||||
$$PWD/headers/scstwrace.h \
|
$$PWD/headers/scstwrace.h \
|
||||||
$$PWD/headers/scstwsettings.h \
|
$$PWD/headers/scstwsettings.h \
|
||||||
|
|
|
@ -61,7 +61,8 @@ public:
|
||||||
RaceStateChanged = 9000,
|
RaceStateChanged = 9000,
|
||||||
TimersChanged = 9001,
|
TimersChanged = 9001,
|
||||||
ExtensionsChanged = 9002,
|
ExtensionsChanged = 9002,
|
||||||
NextStartActionChanged = 9003 /*, ProfilesChanged*/
|
NextStartActionChanged = 9003, /*, ProfilesChanged*/
|
||||||
|
SettingChanged = 9004
|
||||||
};
|
};
|
||||||
Q_ENUM(SignalKey)
|
Q_ENUM(SignalKey)
|
||||||
|
|
||||||
|
|
|
@ -140,14 +140,14 @@ public slots:
|
||||||
* \param value the value to write to
|
* \param value the value to write to
|
||||||
* \return the status code returned by the command
|
* \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
|
* \brief Function to read a setting on the base station
|
||||||
* \param key the key to read from
|
* \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
|
* \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 */
|
/*! Getter fuctions */
|
||||||
|
|
||||||
|
|
35
ScStwLibraries/headers/client/scstwremotesettings.h
Normal file
35
ScStwLibraries/headers/client/scstwremotesettings.h
Normal 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
|
|
@ -9,6 +9,7 @@
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <ScStw.hpp>
|
#include <ScStw.hpp>
|
||||||
|
#include <QDir>
|
||||||
|
|
||||||
class ScStwSettings : public QObject
|
class ScStwSettings : public QObject
|
||||||
{
|
{
|
||||||
|
@ -32,12 +33,16 @@ public:
|
||||||
AtYourMarksSoundDelaySetting,
|
AtYourMarksSoundDelaySetting,
|
||||||
SoundVolumeSetting
|
SoundVolumeSetting
|
||||||
};
|
};
|
||||||
|
|
||||||
Q_ENUM(BaseStationSetting)
|
Q_ENUM(BaseStationSetting)
|
||||||
|
|
||||||
QVariant readSetting(BaseStationSetting key);
|
enum KeyLevelEnum {
|
||||||
bool writeSetting(BaseStationSetting key, QVariant value);
|
KeyLevel = 0
|
||||||
void setDefaultSetting(BaseStationSetting key, QVariant defaultVariant);
|
};
|
||||||
|
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) {
|
static BaseStationSetting keyFromInt(int i) {
|
||||||
QMetaEnum enumeration = QMetaEnum::fromType<BaseStationSetting>();
|
QMetaEnum enumeration = QMetaEnum::fromType<BaseStationSetting>();
|
||||||
|
@ -45,9 +50,9 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
QVariant readSetting(QString key);
|
virtual QVariant readSetting(QString key, int keyInt = -1, int keyLevel = -1);
|
||||||
bool writeSetting(QString key, QVariant value, int keyInt,int keyLevel = -1);
|
virtual bool writeSetting(QString key, QVariant value, int keyInt = -1,int keyLevel = -1);
|
||||||
void setDefaultSetting(QString key, QVariant defaultVariant, int keyInt,int keyLevel = -1);
|
virtual void setDefaultSetting(QString key, QVariant defaultVariant, int keyInt,int keyLevel = -1);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QFile * settingsFile;
|
QFile * settingsFile;
|
||||||
|
|
|
@ -392,15 +392,15 @@ void ScStwClient::handleSignal(QVariantMap data) {
|
||||||
// --- helper functions ---
|
// --- helper functions ---
|
||||||
// ------------------------
|
// ------------------------
|
||||||
|
|
||||||
ScStw::StatusCode ScStwClient::writeRemoteSetting(ScStwSettings::BaseStationSetting key, QString value) {
|
ScStw::StatusCode ScStwClient::writeRemoteSetting(ScStwSettings::BaseStationSetting key, QVariant value) {
|
||||||
QJsonArray requestData;
|
QJsonArray requestData;
|
||||||
requestData.append(key);
|
requestData.append(int(key));
|
||||||
requestData.append(value);
|
requestData.append(QJsonValue::fromVariant(value));
|
||||||
return ScStw::StatusCode(this->sendCommand(3000, requestData)["status"].toInt());
|
return ScStw::StatusCode(this->sendCommand(3000, requestData)["status"].toInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
QString ScStwClient::readRemoteSetting(ScStwSettings::BaseStationSetting key) {
|
QVariant ScStwClient::readRemoteSetting(ScStwSettings::BaseStationSetting key) {
|
||||||
QVariantMap reply = this->sendCommand(3001, key);
|
QVariantMap reply = this->sendCommand(3001, int(key));
|
||||||
if(reply["status"] != 200){
|
if(reply["status"] != 200){
|
||||||
return "false";
|
return "false";
|
||||||
}
|
}
|
||||||
|
|
53
ScStwLibraries/sources/client/scstwremotesettings.cpp
Normal file
53
ScStwLibraries/sources/client/scstwremotesettings.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -26,7 +26,7 @@ ScStwLibraries::ScStwLibraries(QObject *parent) : QObject(parent)
|
||||||
void ScStwLibraries::init() {
|
void ScStwLibraries::init() {
|
||||||
#ifdef ScStwLibraries_QML
|
#ifdef ScStwLibraries_QML
|
||||||
qmlRegisterType<ScStw>("de.itsblue.ScStw", 2, 0, "ScStw");
|
qmlRegisterType<ScStw>("de.itsblue.ScStw", 2, 0, "ScStw");
|
||||||
qRegisterMetaType<ScStwSettings::BaseStationSetting>("ScStw::BaseStationSetting");
|
qRegisterMetaType<ScStwSettings::BaseStationSetting>("ScStwSettings::BaseStationSetting");
|
||||||
qRegisterMetaType<ScStw::SocketCommand>("ScStw::SocketCommand");
|
qRegisterMetaType<ScStw::SocketCommand>("ScStw::SocketCommand");
|
||||||
|
|
||||||
qmlRegisterType<ScStwRace>("de.itsblue.ScStw", 2, 0, "ScStwRace");
|
qmlRegisterType<ScStwRace>("de.itsblue.ScStw", 2, 0, "ScStwRace");
|
||||||
|
@ -34,6 +34,7 @@ void ScStwLibraries::init() {
|
||||||
|
|
||||||
#ifdef ScStwLibraries_ClientLibs
|
#ifdef ScStwLibraries_ClientLibs
|
||||||
qmlRegisterType<ScStwClient>("de.itsblue.ScStw", 2, 0, "ScStwClient");
|
qmlRegisterType<ScStwClient>("de.itsblue.ScStw", 2, 0, "ScStwClient");
|
||||||
|
qmlRegisterType<ScStwSettings>("de.itsblue.ScStw", 2, 0, "ScStwSettings");
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,10 +2,15 @@
|
||||||
|
|
||||||
ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent)
|
ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/settings.json";
|
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||||||
this->settingsFile = new QFile(path);
|
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))
|
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)
|
if(!this->loadSettingsFromFile() && this->settingsFile->size() != 0)
|
||||||
qFatal("[FATAL] Settings file (%s) is of invalid format!", qPrintable(path));
|
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) {
|
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) {
|
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);
|
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];
|
return this->settingsCache[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue