hyperion.ng/libsrc/hyperion/SettingsManager.cpp

231 lines
6.0 KiB
C++
Raw Normal View History

2018-12-27 23:11:32 +01:00
// proj
#include <hyperion/SettingsManager.h>
// util
#include <utils/JsonUtils.h>
#include <db/SettingsTable.h>
2018-12-27 23:11:32 +01:00
// json schema process
#include <utils/jsonschema/QJsonFactory.h>
#include <utils/jsonschema/QJsonSchemaChecker.h>
// write config to filesystem
#include <utils/JsonUtils.h>
QJsonObject SettingsManager::schemaJson;
2020-08-08 13:09:15 +02:00
SettingsManager::SettingsManager(quint8 instance, QObject* parent)
: QObject(parent)
2020-07-19 15:37:47 +02:00
, _log(Logger::getInstance("SETTINGSMGR"))
, _sTable(new SettingsTable(instance, this))
2018-12-27 23:11:32 +01:00
{
// get schema
if(schemaJson.isEmpty())
{
2018-12-30 22:07:53 +01:00
Q_INIT_RESOURCE(resource);
2018-12-27 23:11:32 +01:00
try
{
schemaJson = QJsonFactory::readSchema(":/hyperion-schema");
}
catch(const std::runtime_error& error)
{
throw std::runtime_error(error.what());
}
}
2018-12-27 23:11:32 +01:00
// get default config
QJsonObject defaultConfig;
if(!JsonUtils::readFile(":/hyperion_default.config", defaultConfig, _log))
throw std::runtime_error("Failed to read default config");
// transform json to string lists
QStringList keyList = defaultConfig.keys();
QStringList defValueList;
for(const auto & key : keyList)
2018-12-27 23:11:32 +01:00
{
if(defaultConfig[key].isObject())
2018-12-27 23:11:32 +01:00
{
defValueList << QString(QJsonDocument(defaultConfig[key].toObject()).toJson(QJsonDocument::Compact));
2018-12-27 23:11:32 +01:00
}
else if(defaultConfig[key].isArray())
2018-12-27 23:11:32 +01:00
{
defValueList << QString(QJsonDocument(defaultConfig[key].toArray()).toJson(QJsonDocument::Compact));
2018-12-27 23:11:32 +01:00
}
}
// fill database with default data if required
for(const auto & key : keyList)
{
QString val = defValueList.takeFirst();
// prevent overwrite
if(!_sTable->recordExist(key))
_sTable->createSettingsRecord(key,val);
}
2018-12-27 23:11:32 +01:00
// need to validate all data in database constuct the entire data object
// TODO refactor schemaChecker to accept QJsonArray in validate(); QJsonDocument container? To validate them per entry...
QJsonObject dbConfig;
for(const auto & key : keyList)
{
QJsonDocument doc = _sTable->getSettingsRecord(key);
if(doc.isArray())
dbConfig[key] = doc.array();
else
dbConfig[key] = doc.object();
}
2018-12-27 23:11:32 +01:00
// possible data upgrade steps to prevent data loss
if(handleConfigUpgrade(dbConfig))
{
saveSettings(dbConfig, true);
}
// validate full dbconfig against schema, on error we need to rewrite entire table
QJsonSchemaChecker schemaChecker;
schemaChecker.setSchema(schemaJson);
QPair<bool,bool> valid = schemaChecker.validate(dbConfig);
// check if our main schema syntax is IO
if (!valid.second)
2018-12-27 23:11:32 +01:00
{
for (auto & schemaError : schemaChecker.getMessages())
2018-12-27 23:11:32 +01:00
Error(_log, "Schema Syntax Error: %s", QSTRING_CSTR(schemaError));
throw std::runtime_error("The config schema has invalid syntax. This should never happen! Go fix it!");
2018-12-27 23:11:32 +01:00
}
if (!valid.first)
2018-12-27 23:11:32 +01:00
{
Info(_log,"Table upgrade required...");
dbConfig = schemaChecker.getAutoCorrectedConfig(dbConfig);
2018-12-27 23:11:32 +01:00
for (auto & schemaError : schemaChecker.getMessages())
2018-12-27 23:11:32 +01:00
Warning(_log, "Config Fix: %s", QSTRING_CSTR(schemaError));
saveSettings(dbConfig);
2018-12-27 23:11:32 +01:00
}
else
_qconfig = dbConfig;
2018-12-27 23:11:32 +01:00
Disentangle LedDevice/LinearColorSmoothing, Bug Fixes & Test support (#654) * Handle Exceptions in main & Pythoninit * Have SSDPDiscover generic again * Have SSDPDiscover generic again * Change Info- to Debug logs as technical service messages * Nanoleaf - When switched on, ensure UDP mode * Include SQL Database in Cross-Compile instructions * Fix Clazy (QT code checker) and clang Warnings * Stop LedDevice:write for disabled device * Nanoleaf: Fix uint printfs * NanoLeaf: Fix indents to tabs * NanoLeaf - Add debug verbosity switches * Device switchability support, FileDevice with timestamp support * Nanoleaf Light Panels now support External Control V2 * Enhance LedDeviceFile by Timestamp + fix readyness * Stop color stream, if LedDevice disabled * Nanoleaf - remove switchability * Fix MultiColorAdjustment, if led-range is greater lednum * Fix logging * LedFileDevice/LedDevice - add testing support * New "Led Test" effect * LedDeviceFile - Add chrono include + Allow Led rewrites for testing * Stabilize Effects for LedDevices where latchtime = 0 * Update LedDeviceFile, allow latchtime = 0 * Distangle LinearColorSmoothing and LEDDevice, Fix Effect configuration updates * Updates LedDeviceFile - Initialize via Open * Updates LedDeviceNanoleaf - Initialize via Open, Remove throwing exceptions * Updates ProviderUDP - Remove throwing exceptions * Framebuffer - Use precise timer * TestSpi - Align to LedDevice updates * Pretty Print CrossCompileHowTo as markdown-file * Ensure that output is only written when LedDevice is ready * Align APA102 Device to new device staging * Logger - Remove clang warnings on extra semicolon * Devices SPI - Align to Device stages and methods * Fix cppcheck and clang findings * Add Code-Template for new Devices * Align devices to stages and methods, clean-up some code * Allow to reopen LedDevice without restart * Revert change "Remove Connect (PriorityMuxer::visiblePriorityChanged -> Hyperion::update) due to double writes" * Remove visiblePriorityChanged from LedDevice to decouple LedDevice from hyperion logic * Expose LedDevice getLedCount and align signedness
2020-02-10 15:21:58 +01:00
Debug(_log,"Settings database initialized");
2018-12-27 23:11:32 +01:00
}
2020-08-08 13:09:15 +02:00
QJsonDocument SettingsManager::getSetting(settings::type type) const
2018-12-27 23:11:32 +01:00
{
return _sTable->getSettingsRecord(settings::typeToString(type));
2018-12-27 23:11:32 +01:00
}
2020-08-08 13:09:15 +02:00
bool SettingsManager::saveSettings(QJsonObject config, bool correct)
2018-12-27 23:11:32 +01:00
{
// optional data upgrades e.g. imported legacy/older configs
// handleConfigUpgrade(config);
2018-12-27 23:11:32 +01:00
// we need to validate data against schema
QJsonSchemaChecker schemaChecker;
schemaChecker.setSchema(schemaJson);
if (!schemaChecker.validate(config).first)
{
if(!correct)
{
Error(_log,"Failed to save configuration, errors during validation");
return false;
}
Warning(_log,"Fixing json data!");
config = schemaChecker.getAutoCorrectedConfig(config);
for (auto & schemaError : schemaChecker.getMessages())
2018-12-27 23:11:32 +01:00
Warning(_log, "Config Fix: %s", QSTRING_CSTR(schemaError));
}
// store the new config
_qconfig = config;
// extract keys and data
QStringList keyList = config.keys();
QStringList newValueList;
for(const auto & key : keyList)
{
if(config[key].isObject())
{
newValueList << QString(QJsonDocument(config[key].toObject()).toJson(QJsonDocument::Compact));
}
else if(config[key].isArray())
{
newValueList << QString(QJsonDocument(config[key].toArray()).toJson(QJsonDocument::Compact));
}
}
// compare database data with new data to emit/save changes accordingly
for(const auto & key : keyList)
{
QString data = newValueList.takeFirst();
if(_sTable->getSettingsRecordString(key) != data)
{
_sTable->createSettingsRecord(key, data);
2018-12-27 23:11:32 +01:00
emit settingsChanged(settings::stringToType(key), QJsonDocument::fromJson(data.toLocal8Bit()));
}
}
2018-12-27 23:11:32 +01:00
return true;
}
bool SettingsManager::handleConfigUpgrade(QJsonObject& config)
{
bool migrated = false;
// LED LAYOUT UPGRADE
// from { hscan: { minimum: 0.2, maximum: 0.3 }, vscan: { minimum: 0.2, maximumn: 0.3 } }
// from { h: { min: 0.2, max: 0.3 }, v: { min: 0.2, max: 0.3 } }
// to { hmin: 0.2, hmax: 0.3, vmin: 0.2, vmax: 0.3}
if(config.contains("leds"))
{
const QJsonArray ledarr = config["leds"].toArray();
const QJsonObject led = ledarr[0].toObject();
if(led.contains("hscan") || led.contains("h"))
{
const bool whscan = led.contains("hscan");
QJsonArray newLedarr;
for(const auto & entry : ledarr)
{
const QJsonObject led = entry.toObject();
QJsonObject hscan;
QJsonObject vscan;
QJsonValue hmin;
QJsonValue hmax;
QJsonValue vmin;
QJsonValue vmax;
QJsonObject nL;
if(whscan)
{
hscan = led["hscan"].toObject();
vscan = led["vscan"].toObject();
hmin = hscan["minimum"];
hmax = hscan["maximum"];
vmin = vscan["minimum"];
vmax = vscan["maximum"];
}
else
{
hscan = led["h"].toObject();
vscan = led["v"].toObject();
hmin = hscan["min"];
hmax = hscan["max"];
vmin = vscan["min"];
vmax = vscan["max"];
}
// append to led object
nL["hmin"] = hmin;
nL["hmax"] = hmax;
nL["vmin"] = vmin;
nL["vmax"] = vmax;
newLedarr.append(nL);
}
// replace
config["leds"] = newLedarr;
migrated = true;
Debug(_log,"LED Layout migrated");
}
}
return migrated;
}