mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Refactor config API
This commit is contained in:
@@ -1003,8 +1003,8 @@
|
|||||||
"infoDialog_import_comperror_text": "Sad! Your browser doesn't support importing. Please try again with another browser.",
|
"infoDialog_import_comperror_text": "Sad! Your browser doesn't support importing. Please try again with another browser.",
|
||||||
"infoDialog_import_confirm_text": "Are you sure to import \"$1\"? This process can't be reverted!",
|
"infoDialog_import_confirm_text": "Are you sure to import \"$1\"? This process can't be reverted!",
|
||||||
"infoDialog_import_confirm_title": "Confirm import",
|
"infoDialog_import_confirm_title": "Confirm import",
|
||||||
"infoDialog_import_hyperror_text": "The selected configuration file \"$1\" can't be imported. It's not compatible with Hyperion 2.0 and higher!",
|
|
||||||
"infoDialog_import_jsonerror_text": "The selected configuration file \"$1\" is not a .json file, or it's corrupted. Error message: ($2)",
|
"infoDialog_import_jsonerror_text": "The selected configuration file \"$1\" is not a .json file, or it's corrupted. Error message: ($2)",
|
||||||
|
"infoDialog_import_version_error_text": "The selected configuration file \"$1\" can not be imported. It's not compatible with Hyperion 2.0.17 and higher!",
|
||||||
"infoDialog_wizrgb_text": "Your RGB Byte Order is already well adjusted.",
|
"infoDialog_wizrgb_text": "Your RGB Byte Order is already well adjusted.",
|
||||||
"infoDialog_writeconf_error_text": "Saving your configuration failed.",
|
"infoDialog_writeconf_error_text": "Saving your configuration failed.",
|
||||||
"infoDialog_writeimage_error_text": "The selected file \"$1\" is not an image file, or it's corrupted! Please select another image file.",
|
"infoDialog_writeimage_error_text": "The selected file \"$1\" is not an image file, or it's corrupted! Please select another image file.",
|
||||||
|
@@ -28,11 +28,6 @@ $(document).ready(function () {
|
|||||||
// Instance handling
|
// Instance handling
|
||||||
function handleInstanceRename(e) {
|
function handleInstanceRename(e) {
|
||||||
|
|
||||||
conf_editor.on('change', function () {
|
|
||||||
window.readOnlyMode ? $('#btn_cl_save').prop('disabled', true) : $('#btn_submit').prop('disabled', false);
|
|
||||||
window.readOnlyMode ? $('#btn_ma_save').prop('disabled', true) : $('#btn_submit').prop('disabled', false);
|
|
||||||
});
|
|
||||||
|
|
||||||
var inst = e.currentTarget.id.split("_")[1];
|
var inst = e.currentTarget.id.split("_")[1];
|
||||||
showInfoDialog('renInst', $.i18n('conf_general_inst_renreq_t'), getInstanceNameByIndex(inst));
|
showInfoDialog('renInst', $.i18n('conf_general_inst_renreq_t'), getInstanceNameByIndex(inst));
|
||||||
|
|
||||||
@@ -119,14 +114,14 @@ $(document).ready(function () {
|
|||||||
//check file is json
|
//check file is json
|
||||||
var check = isJsonString(content);
|
var check = isJsonString(content);
|
||||||
if (check.length != 0) {
|
if (check.length != 0) {
|
||||||
showInfoDialog('error', "", $.i18n('infoDialog_import_jsonerror_text', f.name, JSON.stringify(check)));
|
showInfoDialog('error', "", $.i18n('infoDialog_import_jsonerror_text', f.name, JSON.stringify(check.message)));
|
||||||
dis_imp_btn(true);
|
dis_imp_btn(true);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
content = JSON.parse(content);
|
content = JSON.parse(content);
|
||||||
//check for hyperion json
|
//check for hyperion json
|
||||||
if (typeof content.leds === 'undefined' || typeof content.general === 'undefined') {
|
if (typeof content.global === 'undefined' || typeof content.instances === 'undefined') {
|
||||||
showInfoDialog('error', "", $.i18n('infoDialog_import_hyperror_text', f.name));
|
showInfoDialog('error', "", $.i18n('infoDialog_import_version_error_text', f.name));
|
||||||
dis_imp_btn(true);
|
dis_imp_btn(true);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -143,10 +138,10 @@ $(document).ready(function () {
|
|||||||
$('#btn_import_conf').off().on('click', function () {
|
$('#btn_import_conf').off().on('click', function () {
|
||||||
showInfoDialog('import', $.i18n('infoDialog_import_confirm_title'), $.i18n('infoDialog_import_confirm_text', confName));
|
showInfoDialog('import', $.i18n('infoDialog_import_confirm_title'), $.i18n('infoDialog_import_confirm_text', confName));
|
||||||
|
|
||||||
$('#id_btn_import').off().on('click', function () {
|
$('#id_btn_import').off().on('click', function () {
|
||||||
requestRestoreConfig(importedConf);
|
requestRestoreConfig(importedConf);
|
||||||
setTimeout(initRestart, 100);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#select_import_conf').off().on('change', function (e) {
|
$('#select_import_conf').off().on('change', function (e) {
|
||||||
@@ -157,18 +152,19 @@ $(document).ready(function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
//export
|
//export
|
||||||
$('#btn_export_conf').off().on('click', function () {
|
$('#btn_export_conf').off().on('click', async () =>
|
||||||
var name = window.serverConfig.general.name;
|
{
|
||||||
|
const name = window.serverConfig.general.name;
|
||||||
|
|
||||||
var d = new Date();
|
const d = new Date();
|
||||||
var month = d.getMonth() + 1;
|
const month = String(d.getMonth() + 1).padStart(2, '0');
|
||||||
var day = d.getDate();
|
const day = String(d.getDate()).padStart(2, '0');
|
||||||
|
const timestamp = `${d.getFullYear()}-${month}-${day}`;
|
||||||
|
|
||||||
var timestamp = d.getFullYear() + '.' +
|
const configBackup = await requestConfig();
|
||||||
(month < 10 ? '0' : '') + month + '.' +
|
if (configBackup.success === true) {
|
||||||
(day < 10 ? '0' : '') + day;
|
download(JSON.stringify(configBackup.info, null, "\t"), 'HyperionBackup-' + timestamp + '_v' + window.currentVersion + '.json', "application/json");
|
||||||
|
}
|
||||||
download(JSON.stringify(window.serverConfig, null, "\t"), 'Hyperion-' + window.currentVersion + '-Backup (' + name + ') ' + timestamp + '.json', "application/json");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//create introduction
|
//create introduction
|
||||||
@@ -180,3 +176,8 @@ $(document).ready(function () {
|
|||||||
|
|
||||||
removeOverlay();
|
removeOverlay();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$(window.hyperion).on("cmd-config-restoreconfig", function (event) {
|
||||||
|
setTimeout(initRestart, 100);
|
||||||
|
});
|
||||||
|
|
||||||
|
@@ -128,7 +128,7 @@ $(document).ready(function () {
|
|||||||
requestSysInfo();
|
requestSysInfo();
|
||||||
});
|
});
|
||||||
|
|
||||||
$(window.hyperion).on("cmd-config-getconfig", function (event) {
|
$(window.hyperion).on("cmd-config-getconfig-old", function (event) {
|
||||||
window.serverConfig = event.response.info;
|
window.serverConfig = event.response.info;
|
||||||
|
|
||||||
window.showOptHelp = window.serverConfig.general.showOptHelp;
|
window.showOptHelp = window.serverConfig.general.showOptHelp;
|
||||||
@@ -278,7 +278,7 @@ $(document).ready(function () {
|
|||||||
window.currentHyperionInstance = 0;
|
window.currentHyperionInstance = 0;
|
||||||
window.currentHyperionInstanceName = getInstanceNameByIndex(0);
|
window.currentHyperionInstanceName = getInstanceNameByIndex(0);
|
||||||
|
|
||||||
requestServerConfig();
|
requestServerConfigOld();
|
||||||
setTimeout(requestServerInfo, 100)
|
setTimeout(requestServerInfo, 100)
|
||||||
setTimeout(requestTokenInfo, 200)
|
setTimeout(requestTokenInfo, 200)
|
||||||
}
|
}
|
||||||
@@ -296,7 +296,7 @@ $(document).ready(function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
$(window.hyperion).on("cmd-instance-switchTo", function (event) {
|
$(window.hyperion).on("cmd-instance-switchTo", function (event) {
|
||||||
requestServerConfig();
|
requestServerConfigOld();
|
||||||
setTimeout(requestServerInfo, 200)
|
setTimeout(requestServerInfo, 200)
|
||||||
setTimeout(requestTokenInfo, 400)
|
setTimeout(requestTokenInfo, 400)
|
||||||
});
|
});
|
||||||
@@ -338,11 +338,6 @@ $(function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// hotfix body padding when bs modals overlap
|
|
||||||
$(document.body).on('hide.bs.modal,hidden.bs.modal', function () {
|
|
||||||
$('body').css('padding-right', '0');
|
|
||||||
});
|
|
||||||
|
|
||||||
//Dark Mode
|
//Dark Mode
|
||||||
$("#btn_darkmode").off().on("click", function (e) {
|
$("#btn_darkmode").off().on("click", function (e) {
|
||||||
if (getStorage("darkMode") != "on") {
|
if (getStorage("darkMode") != "on") {
|
||||||
|
@@ -37,7 +37,6 @@ const ENDLESS = -1;
|
|||||||
function initRestart()
|
function initRestart()
|
||||||
{
|
{
|
||||||
$(window.hyperion).off();
|
$(window.hyperion).off();
|
||||||
requestServerConfigReload();
|
|
||||||
window.watchdog = 10;
|
window.watchdog = 10;
|
||||||
connectionLostDetection('restart');
|
connectionLostDetection('restart');
|
||||||
}
|
}
|
||||||
@@ -138,9 +137,10 @@ function initWebSocket()
|
|||||||
if (error == "Service Unavailable") {
|
if (error == "Service Unavailable") {
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
} else {
|
} else {
|
||||||
$(window.hyperion).trigger({type:"error",reason:error});
|
$(window.hyperion).trigger({type:"error", reason:error});
|
||||||
}
|
}
|
||||||
console.log("[window.websocket::onmessage] ",error)
|
let errorData = response.hasOwnProperty("errorData")? response.errorData : "";
|
||||||
|
console.log("[window.websocket::onmessage] ",error, ", Description:", errorData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -344,6 +344,11 @@ function requestServerConfig()
|
|||||||
sendToHyperion("config", "getconfig");
|
sendToHyperion("config", "getconfig");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function requestServerConfigOld()
|
||||||
|
{
|
||||||
|
sendToHyperion("config", "getconfig-old");
|
||||||
|
}
|
||||||
|
|
||||||
function requestServerConfigReload()
|
function requestServerConfigReload()
|
||||||
{
|
{
|
||||||
sendToHyperion("config", "reload");
|
sendToHyperion("config", "reload");
|
||||||
@@ -522,3 +527,14 @@ async function requestServiceDiscovery(type, params) {
|
|||||||
return sendAsyncToHyperion("service", "discover", data);
|
return sendAsyncToHyperion("service", "discover", data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function requestConfig(globalTypes, instances, instanceTypes) {
|
||||||
|
let globalFilter = { "global": { "types": globalTypes } };
|
||||||
|
let instanceFilter = { "instances": { "ids": instances, "types": instanceTypes } };
|
||||||
|
|
||||||
|
//todo create filter
|
||||||
|
let filter;
|
||||||
|
|
||||||
|
return sendAsyncToHyperion("config", "getconfig", filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -129,7 +129,7 @@ $(document).ready(function () {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
// apply new serverinfos
|
// apply new serverinfos
|
||||||
$(window.hyperion).on("cmd-config-getconfig", function (event) {
|
$(window.hyperion).on("cmd-config-getconfig-old", function (event) {
|
||||||
leds = event.response.info.leds;
|
leds = event.response.info.leds;
|
||||||
grabberConfig = event.response.info.grabberV4L2;
|
grabberConfig = event.response.info.grabberV4L2;
|
||||||
updateLedLayout();
|
updateLedLayout();
|
||||||
|
@@ -239,7 +239,7 @@ function showInfoDialog(type, header, message) {
|
|||||||
$('#id_body').html('<i style="margin-bottom:20px" class="fa fa-warning modal-icon-error">');
|
$('#id_body').html('<i style="margin-bottom:20px" class="fa fa-warning modal-icon-error">');
|
||||||
if (header == "")
|
if (header == "")
|
||||||
$('#id_body').append('<h4 style="font-weight:bold;text-transform:uppercase;">' + $.i18n('infoDialog_general_error_title') + '</h4>');
|
$('#id_body').append('<h4 style="font-weight:bold;text-transform:uppercase;">' + $.i18n('infoDialog_general_error_title') + '</h4>');
|
||||||
$('#id_footer').html('<button type="button" class="btn btn-danger" data-dismiss="modal">' + $.i18n('general_btn_ok') + '</button>');
|
$('#id_footer').html('<button type="button" class="btn btn-danger" data-dismiss-modal="#modal_dialog">' + $.i18n('general_btn_ok') + '</button>');
|
||||||
}
|
}
|
||||||
else if (type == "select") {
|
else if (type == "select") {
|
||||||
$('#id_body').html('<img style="margin-bottom:20px" id="id_logo" src="img/hyperion/logo_positiv.png" alt="Redefine ambient light!">');
|
$('#id_body').html('<img style="margin-bottom:20px" id="id_logo" src="img/hyperion/logo_positiv.png" alt="Redefine ambient light!">');
|
||||||
@@ -256,9 +256,9 @@ function showInfoDialog(type, header, message) {
|
|||||||
$('#id_footer').html('<b>' + $.i18n('InfoDialog_nowrite_foottext') + '</b>');
|
$('#id_footer').html('<b>' + $.i18n('InfoDialog_nowrite_foottext') + '</b>');
|
||||||
}
|
}
|
||||||
else if (type == "import") {
|
else if (type == "import") {
|
||||||
$('#id_body').html('<i style="margin-bottom:20px" class="fa fa-warning modal-icon-warning">');
|
$('#id_body').html('<i style="margin-bottom:20px" class="fa fa-warning modal-icon-warning"></i>');
|
||||||
$('#id_footer').html('<button type="button" id="id_btn_import" class="btn btn-warning" data-dismiss="modal"><i class="fa fa-fw fa-save"></i>' + $.i18n('general_btn_saverestart') + '</button>');
|
$('#id_footer').html('<button type="button" id="id_btn_import" class="btn btn-warning"><i class="fa fa-fw fa-save"></i>' + $.i18n('general_btn_saverestart') + '</button>');
|
||||||
$('#id_footer').append('<button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-fw fa-close"></i>' + $.i18n('general_btn_cancel') + '</button>');
|
$('#id_footer').append('<button type="button" class="btn btn-danger" data-dismiss-modal="#modal_dialog"><i class="fa fa-fw fa-close"></i>' + $.i18n('general_btn_cancel') + '</button>');
|
||||||
}
|
}
|
||||||
else if (type == "delInst") {
|
else if (type == "delInst") {
|
||||||
$('#id_body').html('<i style="margin-bottom:20px" class="fa fa-remove modal-icon-warning">');
|
$('#id_body').html('<i style="margin-bottom:20px" class="fa fa-remove modal-icon-warning">');
|
||||||
@@ -321,7 +321,7 @@ function showInfoDialog(type, header, message) {
|
|||||||
$(document).on('click', '[data-dismiss-modal]', function () {
|
$(document).on('click', '[data-dismiss-modal]', function () {
|
||||||
var target = $(this).data('dismiss-modal');
|
var target = $(this).data('dismiss-modal');
|
||||||
$($.find(target)).modal('hide');
|
$($.find(target)).modal('hide');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function createHintH(type, text, container) {
|
function createHintH(type, text, container) {
|
||||||
@@ -1222,7 +1222,7 @@ function getSystemInfo() {
|
|||||||
info += '- Avail Video Cap.: ' + window.serverInfo.grabbers.video.available + '\n';
|
info += '- Avail Video Cap.: ' + window.serverInfo.grabbers.video.available + '\n';
|
||||||
info += '- Avail Audio Cap.: ' + window.serverInfo.grabbers.audio.available + '\n';
|
info += '- Avail Audio Cap.: ' + window.serverInfo.grabbers.audio.available + '\n';
|
||||||
info += '- Avail Services: ' + window.serverInfo.services + '\n';
|
info += '- Avail Services: ' + window.serverInfo.services + '\n';
|
||||||
info += '- Config path: ' + shy.rootPath + '\n';
|
info += '- Config database: ' + shy.configDatabaseFile + '\n';
|
||||||
info += '- Database: ' + (shy.readOnlyMode ? "ready-only" : "read/write") + '\n';
|
info += '- Database: ' + (shy.readOnlyMode ? "ready-only" : "read/write") + '\n';
|
||||||
info += '- Mode: ' + (shy.isGuiMode ? "GUI" : "Non-GUI") + '\n';
|
info += '- Mode: ' + (shy.isGuiMode ? "GUI" : "Non-GUI") + '\n';
|
||||||
|
|
||||||
|
@@ -245,12 +245,6 @@ protected:
|
|||||||
///
|
///
|
||||||
bool saveSettings(const QJsonObject &data);
|
bool saveSettings(const QJsonObject &data);
|
||||||
|
|
||||||
///
|
|
||||||
/// @brief Restore settings object. Requires ADMIN ACCESS
|
|
||||||
/// @param data The data object
|
|
||||||
///
|
|
||||||
bool restoreSettings(const QJsonObject &data);
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Set the authorizationn state
|
/// @brief Set the authorizationn state
|
||||||
/// @param authorized True, if authorized
|
/// @param authorized True, if authorized
|
||||||
|
@@ -186,7 +186,7 @@ private:
|
|||||||
///
|
///
|
||||||
void handleSourceSelectCommand(const QJsonObject &message, const JsonApiCommand& cmd);
|
void handleSourceSelectCommand(const QJsonObject &message, const JsonApiCommand& cmd);
|
||||||
|
|
||||||
/// Handle an incoming JSON GetConfig message and check subcommand
|
/// Handle an incoming JSON Config message and check subcommand
|
||||||
///
|
///
|
||||||
/// @param message the incoming message
|
/// @param message the incoming message
|
||||||
///
|
///
|
||||||
@@ -204,6 +204,12 @@ private:
|
|||||||
///
|
///
|
||||||
void handleConfigSetCommand(const QJsonObject &message, const JsonApiCommand& cmd);
|
void handleConfigSetCommand(const QJsonObject &message, const JsonApiCommand& cmd);
|
||||||
|
|
||||||
|
/// Handle an incoming JSON GetConfig message from handleConfigCommand()
|
||||||
|
///
|
||||||
|
/// @param message the incoming message
|
||||||
|
///
|
||||||
|
void handleConfigGetCommand(const QJsonObject &message, const JsonApiCommand& cmd);
|
||||||
|
|
||||||
/// Handle an incoming JSON RestoreConfig message from handleConfigCommand()
|
/// Handle an incoming JSON RestoreConfig message from handleConfigCommand()
|
||||||
///
|
///
|
||||||
/// @param message the incoming message
|
/// @param message the incoming message
|
||||||
|
@@ -84,6 +84,7 @@ public:
|
|||||||
DeleteToken,
|
DeleteToken,
|
||||||
Discover,
|
Discover,
|
||||||
GetConfig,
|
GetConfig,
|
||||||
|
GetConfigOld,
|
||||||
GetInfo,
|
GetInfo,
|
||||||
GetPendingTokenRequests,
|
GetPendingTokenRequests,
|
||||||
GetProperties,
|
GetProperties,
|
||||||
@@ -134,6 +135,7 @@ public:
|
|||||||
case DeleteToken: return "deleteToken";
|
case DeleteToken: return "deleteToken";
|
||||||
case Discover: return "discover";
|
case Discover: return "discover";
|
||||||
case GetConfig: return "getconfig";
|
case GetConfig: return "getconfig";
|
||||||
|
case GetConfigOld: return "getconfig-old";
|
||||||
case GetInfo: return "getInfo";
|
case GetInfo: return "getInfo";
|
||||||
case GetPendingTokenRequests: return "getPendingTokenRequests";
|
case GetPendingTokenRequests: return "getPendingTokenRequests";
|
||||||
case GetProperties: return "getProperties";
|
case GetProperties: return "getProperties";
|
||||||
@@ -274,6 +276,7 @@ public:
|
|||||||
{ {"color", ""}, { Command::Color, SubCommand::Empty, Authorization::Yes, InstanceCmd::Multi, NoListenerCmd::Yes} },
|
{ {"color", ""}, { Command::Color, SubCommand::Empty, Authorization::Yes, InstanceCmd::Multi, NoListenerCmd::Yes} },
|
||||||
{ {"componentstate", ""}, { Command::ComponentState, SubCommand::Empty, Authorization::Yes, InstanceCmd::Multi, NoListenerCmd::Yes} },
|
{ {"componentstate", ""}, { Command::ComponentState, SubCommand::Empty, Authorization::Yes, InstanceCmd::Multi, NoListenerCmd::Yes} },
|
||||||
{ {"config", "getconfig"}, { Command::Config, SubCommand::GetConfig, Authorization::Admin, InstanceCmd::Yes, NoListenerCmd::Yes} },
|
{ {"config", "getconfig"}, { Command::Config, SubCommand::GetConfig, Authorization::Admin, InstanceCmd::Yes, NoListenerCmd::Yes} },
|
||||||
|
{ {"config", "getconfig-old"}, { Command::Config, SubCommand::GetConfigOld, Authorization::Admin, InstanceCmd::Yes, NoListenerCmd::Yes} },
|
||||||
{ {"config", "getschema"}, { Command::Config, SubCommand::GetSchema, Authorization::Admin, InstanceCmd::Yes, NoListenerCmd::Yes} },
|
{ {"config", "getschema"}, { Command::Config, SubCommand::GetSchema, Authorization::Admin, InstanceCmd::Yes, NoListenerCmd::Yes} },
|
||||||
{ {"config", "reload"}, { Command::Config, SubCommand::Reload, Authorization::Admin, InstanceCmd::Yes, NoListenerCmd::Yes} },
|
{ {"config", "reload"}, { Command::Config, SubCommand::Reload, Authorization::Admin, InstanceCmd::Yes, NoListenerCmd::Yes} },
|
||||||
{ {"config", "restoreconfig"}, { Command::Config, SubCommand::RestoreConfig, Authorization::Admin, InstanceCmd::Yes, NoListenerCmd::Yes} },
|
{ {"config", "restoreconfig"}, { Command::Config, SubCommand::RestoreConfig, Authorization::Admin, InstanceCmd::Yes, NoListenerCmd::Yes} },
|
||||||
|
@@ -31,6 +31,8 @@ public:
|
|||||||
static QJsonObject getSystemInfo(const Hyperion* hyperion);
|
static QJsonObject getSystemInfo(const Hyperion* hyperion);
|
||||||
QJsonObject discoverSources (const QString& sourceType, const QJsonObject& params);
|
QJsonObject discoverSources (const QString& sourceType, const QJsonObject& params);
|
||||||
|
|
||||||
|
static QJsonObject getConfiguration(const QList<quint8>& instances = {}, bool addGlobalConfig = true, const QStringList& instanceFilteredTypes = {}, const QStringList& globalFilterTypes = {} );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
template<typename GrabberType>
|
template<typename GrabberType>
|
||||||
|
@@ -1,12 +1,7 @@
|
|||||||
#pragma once
|
#ifndef AUTHSTABLE_H
|
||||||
|
#define AUTHSTABLE_H
|
||||||
|
|
||||||
// hyperion
|
|
||||||
#include <db/DBManager.h>
|
#include <db/DBManager.h>
|
||||||
#include <QCryptographicHash>
|
|
||||||
|
|
||||||
// qt
|
|
||||||
#include <QDateTime>
|
|
||||||
#include <QUuid>
|
|
||||||
|
|
||||||
namespace hyperion {
|
namespace hyperion {
|
||||||
const char DEFAULT_USER[] = "Hyperion";
|
const char DEFAULT_USER[] = "Hyperion";
|
||||||
@@ -21,69 +16,30 @@ class AuthTable : public DBManager
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
/// construct wrapper with auth table
|
/// construct wrapper with auth table
|
||||||
AuthTable(const QString& rootPath = "", QObject* parent = nullptr, bool readonlyMode = false)
|
explicit AuthTable(QObject* parent = nullptr);
|
||||||
: DBManager(parent)
|
|
||||||
{
|
|
||||||
setReadonlyMode(readonlyMode);
|
|
||||||
if(!rootPath.isEmpty()){
|
|
||||||
// Init Hyperion database usage
|
|
||||||
setRootPath(rootPath);
|
|
||||||
setDatabaseName("hyperion");
|
|
||||||
}
|
|
||||||
// init Auth table
|
|
||||||
setTable("auth");
|
|
||||||
// create table columns
|
|
||||||
createTable(QStringList()<<"user TEXT"<<"password BLOB"<<"token BLOB"<<"salt BLOB"<<"comment TEXT"<<"id TEXT"<<"created_at TEXT"<<"last_use TEXT");
|
|
||||||
};
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Create a user record, if called on a existing user the auth is recreated
|
/// @brief Create a user record, if called on a existing user the auth is recreated
|
||||||
/// @param[in] user The username
|
/// @param[in] user The username
|
||||||
/// @param[in] pw The password
|
/// @param[in] password The password
|
||||||
/// @return true on success else false
|
/// @return true on success else false
|
||||||
///
|
///
|
||||||
inline bool createUser(const QString& user, const QString& pw)
|
bool createUser(const QString& user, const QString& password);
|
||||||
{
|
|
||||||
// new salt
|
|
||||||
QByteArray salt = QCryptographicHash::hash(QUuid::createUuid().toByteArray(), QCryptographicHash::Sha512).toHex();
|
|
||||||
QVariantMap map;
|
|
||||||
map["user"] = user;
|
|
||||||
map["salt"] = salt;
|
|
||||||
map["password"] = hashPasswordWithSalt(pw,salt);
|
|
||||||
map["created_at"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
|
|
||||||
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("user",user));
|
|
||||||
return createRecord(cond, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Test if user record exists
|
/// @brief Test if user record exists
|
||||||
/// @param[in] user The user id
|
/// @param[in] user The user id
|
||||||
/// @return true on success else false
|
/// @return true on success else false
|
||||||
///
|
///
|
||||||
inline bool userExist(const QString& user)
|
bool userExist(const QString& user);
|
||||||
{
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("user",user));
|
|
||||||
return recordExists(cond);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Test if a user is authorized for access with given pw.
|
/// @brief Test if a user is authorized for access with given pw.
|
||||||
/// @param user The user name
|
/// @param user The user name
|
||||||
/// @param pw The password
|
/// @param password The password
|
||||||
/// @return True on success else false
|
/// @return True on success else false
|
||||||
///
|
///
|
||||||
inline bool isUserAuthorized(const QString& user, const QString& pw)
|
bool isUserAuthorized(const QString& user, const QString& password);
|
||||||
{
|
|
||||||
if(userExist(user) && (calcPasswordHashOfUser(user, pw) == getPasswordHashOfUser(user)))
|
|
||||||
{
|
|
||||||
updateUserUsed(user);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Test if a user token is authorized for access.
|
/// @brief Test if a user token is authorized for access.
|
||||||
@@ -91,197 +47,92 @@ public:
|
|||||||
/// @param token The token
|
/// @param token The token
|
||||||
/// @return True on success else false
|
/// @return True on success else false
|
||||||
///
|
///
|
||||||
inline bool isUserTokenAuthorized(const QString& usr, const QString& token)
|
bool isUserTokenAuthorized(const QString& usr, const QString& token);
|
||||||
{
|
|
||||||
if(getUserToken(usr) == token.toUtf8())
|
|
||||||
{
|
|
||||||
updateUserUsed(usr);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Update token of a user. It's an alternate login path which is replaced on startup. This token is NOT hashed(!)
|
/// @brief Update token of a user. It's an alternate login path which is replaced on startup. This token is NOT hashed(!)
|
||||||
/// @param user The user name
|
/// @param user The user name
|
||||||
/// @return True on success else false
|
/// @return True on success else false
|
||||||
///
|
///
|
||||||
inline bool setUserToken(const QString& user)
|
bool setUserToken(const QString& user);
|
||||||
{
|
|
||||||
QVariantMap map;
|
|
||||||
map["token"] = QCryptographicHash::hash(QUuid::createUuid().toByteArray(), QCryptographicHash::Sha512).toHex();
|
|
||||||
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("user", user));
|
|
||||||
return updateRecord(cond, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Get token of a user. This token is NOT hashed(!)
|
/// @brief Get token of a user. This token is NOT hashed(!)
|
||||||
/// @param user The user name
|
/// @param user The user name
|
||||||
/// @return The token
|
/// @return The token
|
||||||
///
|
///
|
||||||
inline const QByteArray getUserToken(const QString& user)
|
const QByteArray getUserToken(const QString& user);
|
||||||
{
|
|
||||||
QVariantMap results;
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("user", user));
|
|
||||||
getRecord(cond, results, QStringList()<<"token");
|
|
||||||
|
|
||||||
return results["token"].toByteArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief update password of given user. The user should be tested (isUserAuthorized) to verify this change
|
/// @brief update password of given user. The user should be tested (isUserAuthorized) to verify this change
|
||||||
/// @param user The user name
|
/// @param user The user name
|
||||||
/// @param newPw The new password to set
|
/// @param newassword The new password to set
|
||||||
/// @return True on success else false
|
/// @return True on success else false
|
||||||
///
|
///
|
||||||
inline bool updateUserPassword(const QString& user, const QString& newPw)
|
bool updateUserPassword(const QString& user, const QString& newPassword);
|
||||||
{
|
|
||||||
QVariantMap map;
|
|
||||||
map["password"] = calcPasswordHashOfUser(user, newPw);
|
|
||||||
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("user", user));
|
|
||||||
return updateRecord(cond, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Reset password of Hyperion user !DANGER! Used in Hyperion main.cpp
|
/// @brief Reset password of Hyperion user !DANGER! Used in Hyperion main.cpp
|
||||||
/// @return True on success else false
|
/// @return True on success else false
|
||||||
///
|
///
|
||||||
inline bool resetHyperionUser()
|
bool resetHyperionUser();
|
||||||
{
|
|
||||||
QVariantMap map;
|
|
||||||
map["password"] = calcPasswordHashOfUser(hyperion::DEFAULT_USER, hyperion::DEFAULT_PASSWORD);
|
|
||||||
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("user", hyperion::DEFAULT_USER));
|
|
||||||
return updateRecord(cond, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Update 'last_use' column entry for the corresponding user
|
/// @brief Update 'last_use' column entry for the corresponding user
|
||||||
/// @param[in] user The user to search for
|
/// @param[in] user The user to search for
|
||||||
///
|
///
|
||||||
inline void updateUserUsed(const QString& user)
|
void updateUserUsed(const QString& user);
|
||||||
{
|
|
||||||
QVariantMap map;
|
|
||||||
map["last_use"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
|
|
||||||
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("user", user));
|
|
||||||
updateRecord(cond, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Test if token record exists, updates last_use on success
|
/// @brief Test if token record exists, updates last_use on success
|
||||||
/// @param[in] token The token id
|
/// @param[in] token The token id
|
||||||
/// @return true on success else false
|
/// @return true on success else false
|
||||||
///
|
///
|
||||||
inline bool tokenExist(const QString& token)
|
bool tokenExist(const QString& token);
|
||||||
{
|
|
||||||
QVariantMap map;
|
|
||||||
map["last_use"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
|
|
||||||
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("token", hashToken(token)));
|
|
||||||
if(recordExists(cond))
|
|
||||||
{
|
|
||||||
// update it
|
|
||||||
createRecord(cond,map);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Create a new token record with comment
|
/// @brief Create a new token record with comment
|
||||||
/// @param[in] token The token id as plaintext
|
/// @param[in] token The token id as plaintext
|
||||||
/// @param[in] comment The comment for the token (eg a human readable identifier)
|
/// @param[in] comment The comment for the token (eg a human readable identifier)
|
||||||
/// @param[in] id The id for the token
|
/// @param[in] identifier The identifier for the token
|
||||||
/// @return true on success else false
|
/// @return true on success else false
|
||||||
///
|
///
|
||||||
inline bool createToken(const QString& token, const QString& comment, const QString& id)
|
bool createToken(const QString& token, const QString& comment, const QString& identifier);
|
||||||
{
|
|
||||||
QVariantMap map;
|
|
||||||
map["comment"] = comment;
|
|
||||||
map["id"] = idExist(id) ? QUuid::createUuid().toString().remove("{").remove("}").left(5) : id;
|
|
||||||
map["created_at"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
|
|
||||||
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("token", hashToken(token)));
|
|
||||||
return createRecord(cond, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Delete token record by id
|
/// @brief Delete token record by identifier
|
||||||
/// @param[in] id The token id
|
/// @param[in] identifier The token identifier
|
||||||
/// @return true on success else false
|
/// @return true on success else false
|
||||||
///
|
///
|
||||||
inline bool deleteToken(const QString& id)
|
bool deleteToken(const QString& identifier);
|
||||||
{
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("id", id));
|
|
||||||
return deleteRecord(cond);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Rename token record by id
|
/// @brief Rename token record by identifier
|
||||||
/// @param[in] id The token id
|
/// @param[in] identifier The token identifier
|
||||||
/// @param[in] comment The new comment
|
/// @param[in] comment The new comment
|
||||||
/// @return true on success else false
|
/// @return true on success else false
|
||||||
///
|
///
|
||||||
inline bool renameToken(const QString &id, const QString &comment)
|
bool renameToken(const QString &identifier, const QString &comment);
|
||||||
{
|
|
||||||
QVariantMap map;
|
|
||||||
map["comment"] = comment;
|
|
||||||
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("id", id));
|
|
||||||
return updateRecord(cond, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Get all 'comment', 'last_use' and 'id' column entries
|
/// @brief Get all 'comment', 'last_use' and 'id' column entries
|
||||||
/// @return A vector of all lists
|
/// @return A vector of all lists
|
||||||
///
|
///
|
||||||
inline const QVector<QVariantMap> getTokenList()
|
const QVector<QVariantMap> getTokenList();
|
||||||
{
|
|
||||||
QVector<QVariantMap> results;
|
|
||||||
getRecords(results, QStringList() << "comment" << "id" << "last_use");
|
|
||||||
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Test if id exists
|
/// @brief Test if identifier exists
|
||||||
/// @param[in] id The id
|
/// @param[in] identifier The identifier
|
||||||
/// @return true on success else false
|
/// @return true on success else false
|
||||||
///
|
///
|
||||||
inline bool idExist(const QString& id)
|
bool identifierExist(const QString& identifier);
|
||||||
{
|
|
||||||
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("id", id));
|
|
||||||
return recordExists(cond);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Get the passwort hash of a user from db
|
/// @brief Get the passwort hash of a user from db
|
||||||
/// @param user The user name
|
/// @param user The user name
|
||||||
/// @return password as hash
|
/// @return password as hash
|
||||||
///
|
///
|
||||||
inline const QByteArray getPasswordHashOfUser(const QString& user)
|
const QByteArray getPasswordHashOfUser(const QString& user);
|
||||||
{
|
|
||||||
QVariantMap results;
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("user", user));
|
|
||||||
getRecord(cond, results, QStringList()<<"password");
|
|
||||||
|
|
||||||
return results["password"].toByteArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Calc the password hash of a user based on user name and password
|
/// @brief Calc the password hash of a user based on user name and password
|
||||||
@@ -289,36 +140,22 @@ public:
|
|||||||
/// @param pw The password
|
/// @param pw The password
|
||||||
/// @return The calced password hash
|
/// @return The calced password hash
|
||||||
///
|
///
|
||||||
inline const QByteArray calcPasswordHashOfUser(const QString& user, const QString& pw)
|
const QByteArray calcPasswordHashOfUser(const QString& user, const QString& password);
|
||||||
{
|
|
||||||
// get salt
|
|
||||||
QVariantMap results;
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("user", user));
|
|
||||||
getRecord(cond, results, QStringList()<<"salt");
|
|
||||||
|
|
||||||
// calc
|
|
||||||
return hashPasswordWithSalt(pw,results["salt"].toByteArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Create a password hash of plaintex password + salt
|
/// @brief Create a password hash of plaintex password + salt
|
||||||
/// @param pw The plaintext password
|
/// @param password The plaintext password
|
||||||
/// @param salt The salt
|
/// @param salt The salt
|
||||||
/// @return The password hash with salt
|
/// @return The password hash with salt
|
||||||
///
|
///
|
||||||
inline const QByteArray hashPasswordWithSalt(const QString& pw, const QByteArray& salt)
|
const QByteArray hashPasswordWithSalt(const QString& password, const QByteArray& salt);
|
||||||
{
|
|
||||||
return QCryptographicHash::hash(pw.toUtf8().append(salt), QCryptographicHash::Sha512).toHex();
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Create a token hash
|
/// @brief Create a token hash
|
||||||
/// @param token The plaintext token
|
/// @param token The plaintext token
|
||||||
/// @return The token hash
|
/// @return The token hash
|
||||||
///
|
///
|
||||||
inline const QByteArray hashToken(const QString& token)
|
const QByteArray hashToken(const QString& token);
|
||||||
{
|
|
||||||
return QCryptographicHash::hash(token.toUtf8(), QCryptographicHash::Sha512).toHex();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif // AUTHSTABLE_H
|
||||||
|
22
include/db/ConfigImportExport.h
Normal file
22
include/db/ConfigImportExport.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef CONFIGIMPORTEXPORT_H
|
||||||
|
#define CONFIGIMPORTEXPORT_H
|
||||||
|
|
||||||
|
#include <db/DBManager.h>
|
||||||
|
|
||||||
|
#include <QFile>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
|
||||||
|
class ConfigImportExport : public DBManager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ConfigImportExport(QObject* parent = nullptr);
|
||||||
|
|
||||||
|
// TODO: Check naming seConfiguration
|
||||||
|
QPair<bool, QStringList> importJson(const QString& configFile);
|
||||||
|
bool exportJson(const QString& path = "") const;
|
||||||
|
|
||||||
|
QPair<bool, QStringList> setConfiguration(const QJsonObject& config);
|
||||||
|
QJsonObject getConfiguration(const QList<quint8>& instances = {}, bool addGlobalConfig = true, const QStringList& instanceFilteredTypes = {}, const QStringList& globalFilterTypes = {} ) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // CONFIGIMPORTEXPORT_H
|
@@ -5,6 +5,9 @@
|
|||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
#include <QPair>
|
#include <QPair>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
#include <QFileInfo>
|
||||||
|
#include <QDir>
|
||||||
|
#include <QThreadStorage>
|
||||||
|
|
||||||
class QSqlDatabase;
|
class QSqlDatabase;
|
||||||
class QSqlQuery;
|
class QSqlQuery;
|
||||||
@@ -26,13 +29,22 @@ class DBManager : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DBManager(QObject* parent = nullptr);
|
explicit DBManager(QObject* parent = nullptr);
|
||||||
~DBManager() override;
|
|
||||||
|
static void initializeDatabase(const QDir& dataDirectory, bool isReadOnly);
|
||||||
|
|
||||||
|
static QDir getDataDirectory() { return _dataDirectory;}
|
||||||
|
static QDir getDirectory() { return _databaseDirectory;}
|
||||||
|
static QFileInfo getFileInfo() { return _databaseFile;}
|
||||||
|
static bool isReadOnly() { return _isReadOnly; }
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Sets the database in read-only mode.
|
||||||
|
/// Updates will not written to the tables
|
||||||
|
/// @param[in] readOnly True read-only, false - read/write
|
||||||
|
///
|
||||||
|
static void setReadonly(bool isReadOnly) { _isReadOnly = isReadOnly; }
|
||||||
|
|
||||||
/// set root path
|
|
||||||
void setRootPath(const QString& rootPath);
|
|
||||||
/// define the database to work with
|
|
||||||
void setDatabaseName(const QString& dbn) { _dbn = dbn; };
|
|
||||||
/// set a table to work with
|
/// set a table to work with
|
||||||
void setTable(const QString& table);
|
void setTable(const QString& table);
|
||||||
|
|
||||||
@@ -98,6 +110,18 @@ public:
|
|||||||
///
|
///
|
||||||
bool getRecords(QVector<QVariantMap>& results, const QStringList& tColumns = QStringList(), const QStringList& tOrder = QStringList()) const;
|
bool getRecords(QVector<QVariantMap>& results, const QStringList& tColumns = QStringList(), const QStringList& tOrder = QStringList()) const;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Get data of multiple records, you need to specify the columns. This search is without conditions. Good to grab all data from db
|
||||||
|
/// @param[in] conditions condition to search for (WHERE)
|
||||||
|
/// @param[out] results results of query
|
||||||
|
/// @param[in] tColumns target columns to search in (optional) if not provided returns all columns
|
||||||
|
/// @param[in] tOrder target order columns with order by ASC/DESC (optional)
|
||||||
|
/// @return True on success else false
|
||||||
|
///
|
||||||
|
bool getRecords(const VectorPair& conditions, QVector<QVariantMap>& results, const QStringList& tColumns = {}, const QStringList& tOrder = {}) const;
|
||||||
|
|
||||||
|
bool getRecords(const QString& condition, const QVariantList& bindValues, QVector<QVariantMap>& results, const QStringList& tColumns = {}, const QStringList& tOrder = {}) const;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Delete a record determined by conditions
|
/// @brief Delete a record determined by conditions
|
||||||
/// @param[in] conditions conditions of the row to delete it (WHERE)
|
/// @param[in] conditions conditions of the row to delete it (WHERE)
|
||||||
@@ -119,23 +143,26 @@ public:
|
|||||||
///
|
///
|
||||||
bool deleteTable(const QString& table) const;
|
bool deleteTable(const QString& table) const;
|
||||||
|
|
||||||
///
|
bool executeQuery(QSqlQuery& query) const;
|
||||||
/// @brief Sets a table in read-only mode.
|
|
||||||
/// Updates will not written to the table
|
protected:
|
||||||
/// @param[in] readOnly True read-only, false - read/write
|
Logger* _log;
|
||||||
///
|
|
||||||
void setReadonlyMode(bool readOnly) { _readonlyMode = readOnly; };
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static QDir _dataDirectory;
|
||||||
|
static QDir _databaseDirectory;
|
||||||
|
static QFileInfo _databaseFile;
|
||||||
|
static QThreadStorage<QSqlDatabase> _databasePool;
|
||||||
|
static bool _isReadOnly;
|
||||||
|
|
||||||
Logger* _log;
|
/// databse connection & file name, defaults to hyperion
|
||||||
/// databse connection & file name, defaults to hyperion
|
QString _dbn = "hyperion";
|
||||||
QString _dbn = "hyperion";
|
|
||||||
/// table in database
|
|
||||||
QString _table;
|
|
||||||
|
|
||||||
bool _readonlyMode;
|
/// table in database
|
||||||
|
QString _table;
|
||||||
|
|
||||||
/// addBindValue to query given by QVariantList
|
/// addBindValues to query given by QVariantList
|
||||||
void doAddBindValue(QSqlQuery& query, const QVariantList& variants) const;
|
void addBindValues(QSqlQuery& query, const QVariantList& variants) const;
|
||||||
|
|
||||||
|
QString constructExecutedQuery(const QSqlQuery& query) const;
|
||||||
};
|
};
|
||||||
|
@@ -1,11 +1,7 @@
|
|||||||
#pragma once
|
#ifndef INSTANCETABLE_H
|
||||||
|
#define INSTANCETABLE_H
|
||||||
|
|
||||||
// db
|
|
||||||
#include <db/DBManager.h>
|
#include <db/DBManager.h>
|
||||||
#include <db/SettingsTable.h>
|
|
||||||
|
|
||||||
// qt
|
|
||||||
#include <QDateTime>
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Hyperion instance manager specific database interface. prepares also the Hyperion database for all follow up usage (Init QtSqlConnection) along with db name
|
/// @brief Hyperion instance manager specific database interface. prepares also the Hyperion database for all follow up usage (Init QtSqlConnection) along with db name
|
||||||
@@ -14,22 +10,7 @@ class InstanceTable : public DBManager
|
|||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
InstanceTable(const QString& rootPath, QObject* parent = nullptr, bool readonlyMode = false)
|
explicit InstanceTable(QObject* parent = nullptr);
|
||||||
: DBManager(parent)
|
|
||||||
{
|
|
||||||
|
|
||||||
setReadonlyMode(readonlyMode);
|
|
||||||
// Init Hyperion database usage
|
|
||||||
setRootPath(rootPath);
|
|
||||||
setDatabaseName("hyperion");
|
|
||||||
|
|
||||||
// Init instance table
|
|
||||||
setTable("instances");
|
|
||||||
createTable(QStringList()<<"instance INTEGER"<<"friendly_name TEXT"<<"enabled INTEGER DEFAULT 0"<<"last_use TEXT");
|
|
||||||
|
|
||||||
// start/create the first Hyperion instance index 0
|
|
||||||
createInstance();
|
|
||||||
};
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Create a new Hyperion instance entry, the name needs to be unique
|
/// @brief Create a new Hyperion instance entry, the name needs to be unique
|
||||||
@@ -37,53 +18,19 @@ public:
|
|||||||
/// @param[out] inst The id that has been assigned
|
/// @param[out] inst The id that has been assigned
|
||||||
/// @return True on success else false
|
/// @return True on success else false
|
||||||
///
|
///
|
||||||
inline bool createInstance(const QString& name, quint8& inst)
|
bool createInstance(const QString& name, quint8& inst);
|
||||||
{
|
|
||||||
VectorPair fcond;
|
|
||||||
fcond.append(CPair("friendly_name",name));
|
|
||||||
|
|
||||||
// check duplicate
|
///
|
||||||
if(!recordExists(fcond))
|
/// @brief Create first Hyperion instance entry, if index 0 is not found.
|
||||||
{
|
///
|
||||||
inst = 0;
|
void createDefaultInstance();
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("instance",inst));
|
|
||||||
|
|
||||||
// increment to next avail index
|
|
||||||
while(recordExists(cond))
|
|
||||||
{
|
|
||||||
inst++;
|
|
||||||
cond.removeFirst();
|
|
||||||
cond.append(CPair("instance",inst));
|
|
||||||
}
|
|
||||||
// create
|
|
||||||
QVariantMap data;
|
|
||||||
data["friendly_name"] = name;
|
|
||||||
data["instance"] = inst;
|
|
||||||
VectorPair lcond;
|
|
||||||
return createRecord(lcond, data);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Delete a Hyperion instance
|
/// @brief Delete a Hyperion instance
|
||||||
/// @param inst The id that has been assigned
|
/// @param inst The id that has been assigned
|
||||||
/// @return True on success else false
|
/// @return True on success else false
|
||||||
///
|
///
|
||||||
inline bool deleteInstance(quint8 inst)
|
bool deleteInstance(quint8 inst);
|
||||||
{
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("instance",inst));
|
|
||||||
if(deleteRecord(cond))
|
|
||||||
{
|
|
||||||
// delete settings entries
|
|
||||||
SettingsTable settingsTable(inst);
|
|
||||||
settingsTable.deleteInstance();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Assign a new name for the given instance
|
/// @brief Assign a new name for the given instance
|
||||||
@@ -91,141 +38,59 @@ public:
|
|||||||
/// @param name The new name of the instance
|
/// @param name The new name of the instance
|
||||||
/// @return True on success else false (instance not found)
|
/// @return True on success else false (instance not found)
|
||||||
///
|
///
|
||||||
inline bool saveName(quint8 inst, const QString& name)
|
bool saveName(quint8 inst, const QString& name);
|
||||||
{
|
|
||||||
VectorPair fcond;
|
|
||||||
fcond.append(CPair("friendly_name",name));
|
|
||||||
|
|
||||||
// check duplicate
|
|
||||||
if(!recordExists(fcond))
|
|
||||||
{
|
|
||||||
if(instanceExist(inst))
|
|
||||||
{
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("instance",inst));
|
|
||||||
QVariantMap data;
|
|
||||||
data["friendly_name"] = name;
|
|
||||||
|
|
||||||
return updateRecord(cond, data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Get all instances with all columns
|
/// @brief Get all instances with all columns
|
||||||
/// @param justEnabled return just enabled instances if true
|
/// @param onlyEnabled return only enabled instances if true
|
||||||
/// @return The found instances
|
/// @return The found instances
|
||||||
///
|
///
|
||||||
inline QVector<QVariantMap> getAllInstances(bool justEnabled = false)
|
QVector<QVariantMap> getAllInstances(bool onlyEnabled = false);
|
||||||
{
|
|
||||||
QVector<QVariantMap> results;
|
///
|
||||||
getRecords(results, QStringList(), QStringList() << "instance ASC");
|
/// @brief Get all instance IDs
|
||||||
if(justEnabled)
|
/// @param onlyEnabled return only enabled instance IDs if true
|
||||||
{
|
/// @return The found instances
|
||||||
for (auto it = results.begin(); it != results.end();)
|
///
|
||||||
{
|
QList<quint8> getAllInstanceIDs (bool onlyEnabled = false);
|
||||||
if( ! (*it)["enabled"].toBool())
|
|
||||||
{
|
|
||||||
it = results.erase(it);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Test if instance record exists
|
/// @brief Test if instance record exists
|
||||||
/// @param[in] user The user id
|
/// @param[in] user The user id
|
||||||
/// @return true on success else false
|
/// @return true on success else false
|
||||||
///
|
///
|
||||||
inline bool instanceExist(quint8 inst)
|
bool instanceExist(quint8 inst);
|
||||||
{
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("instance",inst));
|
|
||||||
return recordExists(cond);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Get instance name by instance index
|
/// @brief Get instance name by instance index
|
||||||
/// @param index The index to search for
|
/// @param index The index to search for
|
||||||
/// @return The name of this index, may return NOT FOUND if not found
|
/// @return The name of this index, may return NOT FOUND if not found
|
||||||
///
|
///
|
||||||
inline const QString getNamebyIndex(quint8 index)
|
QString getNamebyIndex(quint8 index);
|
||||||
{
|
|
||||||
QVariantMap results;
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("instance", index));
|
|
||||||
getRecord(cond, results, QStringList("friendly_name"));
|
|
||||||
|
|
||||||
QString name = results["friendly_name"].toString();
|
|
||||||
return name.isEmpty() ? "NOT FOUND" : name;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Update 'last_use' timestamp
|
/// @brief Update 'last_use' timestamp
|
||||||
/// @param inst The instance to update
|
/// @param inst The instance to update
|
||||||
|
/// @return True on success else false
|
||||||
///
|
///
|
||||||
inline void setLastUse(quint8 inst)
|
bool setLastUse(quint8 inst);
|
||||||
{
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("instance", inst));
|
|
||||||
QVariantMap map;
|
|
||||||
map["last_use"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
|
|
||||||
updateRecord(cond, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Update 'enabled' column by instance index
|
/// @brief Update 'enabled' column by instance index
|
||||||
/// @param inst The instance to update
|
/// @param inst The instance to update
|
||||||
/// @param newState True when enabled else false
|
/// @param newState True when enabled else false
|
||||||
|
/// @return True on success else false
|
||||||
///
|
///
|
||||||
inline void setEnable(quint8 inst, bool newState)
|
bool setEnable(quint8 inst, bool newState);
|
||||||
{
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("instance", inst));
|
|
||||||
QVariantMap map;
|
|
||||||
map["enabled"] = newState;
|
|
||||||
updateRecord(cond, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Get state of 'enabled' column by instance index
|
/// @brief Get state of 'enabled' column by instance index
|
||||||
/// @param inst The instance to get
|
/// @param inst The instance to get
|
||||||
/// @return True when enabled else false
|
/// @return True when enabled else false
|
||||||
///
|
///
|
||||||
inline bool isEnabled(quint8 inst)
|
bool isEnabled(quint8 inst);
|
||||||
{
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("instance", inst));
|
|
||||||
QVariantMap results;
|
|
||||||
getRecord(cond, results);
|
|
||||||
|
|
||||||
return results["enabled"].toBool();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
///
|
|
||||||
/// @brief Create first Hyperion instance entry, if index 0 is not found.
|
|
||||||
///
|
|
||||||
inline void createInstance()
|
|
||||||
{
|
|
||||||
if(instanceExist(0))
|
|
||||||
setEnable(0, true);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
QVariantMap data;
|
|
||||||
data["friendly_name"] = "First LED Hardware instance";
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("instance", 0));
|
|
||||||
if(createRecord(cond, data))
|
|
||||||
setEnable(0, true);
|
|
||||||
else
|
|
||||||
throw std::runtime_error("Failed to create Hyperion root instance in db! This should never be the case...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif // INSTANCETABLE_H
|
||||||
|
@@ -1,14 +1,9 @@
|
|||||||
#pragma once
|
#ifndef METATABLE_H
|
||||||
|
#define METATABLE_H
|
||||||
|
|
||||||
// hyperion
|
// hyperion
|
||||||
#include <db/DBManager.h>
|
#include <db/DBManager.h>
|
||||||
|
|
||||||
// qt
|
|
||||||
#include <QDateTime>
|
|
||||||
#include <QUuid>
|
|
||||||
#include <QNetworkInterface>
|
|
||||||
#include <QCryptographicHash>
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief meta table specific database interface
|
/// @brief meta table specific database interface
|
||||||
///
|
///
|
||||||
@@ -17,47 +12,13 @@ class MetaTable : public DBManager
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
/// construct wrapper with plugins table and columns
|
/// construct wrapper with plugins table and columns
|
||||||
MetaTable(QObject* parent = nullptr, bool readonlyMode = false)
|
explicit MetaTable(QObject* parent = nullptr);
|
||||||
: DBManager(parent)
|
|
||||||
{
|
|
||||||
setReadonlyMode(readonlyMode);
|
|
||||||
|
|
||||||
setTable("meta");
|
|
||||||
createTable(QStringList()<<"uuid TEXT"<<"created_at TEXT");
|
|
||||||
};
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Get the uuid, if the uuid is not set it will be created
|
/// @brief Get the uuid, if the uuid is not set it will be created
|
||||||
/// @return The uuid
|
/// @return The uuid
|
||||||
///
|
///
|
||||||
inline QString getUUID() const
|
QString getUUID() const;
|
||||||
{
|
|
||||||
QVector<QVariantMap> results;
|
|
||||||
getRecords(results, QStringList() << "uuid");
|
|
||||||
|
|
||||||
for(const auto & entry : results)
|
|
||||||
{
|
|
||||||
if(!entry["uuid"].toString().isEmpty())
|
|
||||||
return entry["uuid"].toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
// create new uuidv5 based on net adapter MAC, save to db and return
|
|
||||||
QString hash;
|
|
||||||
foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
|
|
||||||
{
|
|
||||||
if (!(interface.flags() & QNetworkInterface::IsLoopBack))
|
|
||||||
{
|
|
||||||
hash = QCryptographicHash::hash(interface.hardwareAddress().toLocal8Bit(),QCryptographicHash::Sha1).toHex();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const QString newUuid = QUuid::createUuidV5(QUuid(), hash).toString().mid(1, 36);
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("uuid",newUuid));
|
|
||||||
QVariantMap map;
|
|
||||||
map["created_at"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
|
|
||||||
createRecord(cond, map);
|
|
||||||
|
|
||||||
return newUuid;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif // METATABLE_H
|
||||||
|
@@ -1,12 +1,12 @@
|
|||||||
#pragma once
|
#ifndef SETTINGSTABLE_H
|
||||||
|
#define SETTINGSTABLE_H
|
||||||
|
|
||||||
// hyperion
|
|
||||||
#include <db/DBManager.h>
|
#include <db/DBManager.h>
|
||||||
|
|
||||||
// qt
|
|
||||||
#include <QDateTime>
|
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
|
|
||||||
|
const int GLOABL_INSTANCE_ID = 0;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief settings table db interface
|
/// @brief settings table db interface
|
||||||
///
|
///
|
||||||
@@ -15,14 +15,7 @@ class SettingsTable : public DBManager
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
/// construct wrapper with settings table
|
/// construct wrapper with settings table
|
||||||
SettingsTable(quint8 instance, QObject* parent = nullptr)
|
SettingsTable(quint8 instance, QObject* parent = nullptr);
|
||||||
: DBManager(parent)
|
|
||||||
, _hyperion_inst(instance)
|
|
||||||
{
|
|
||||||
setTable("settings");
|
|
||||||
// create table columns
|
|
||||||
createTable(QStringList()<<"type TEXT"<<"config TEXT"<<"hyperion_inst INTEGER"<<"updated_at TEXT");
|
|
||||||
};
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Create or update a settings record
|
/// @brief Create or update a settings record
|
||||||
@@ -30,19 +23,7 @@ public:
|
|||||||
/// @param[in] config The configuration data
|
/// @param[in] config The configuration data
|
||||||
/// @return true on success else false
|
/// @return true on success else false
|
||||||
///
|
///
|
||||||
inline bool createSettingsRecord(const QString& type, const QString& config) const
|
bool createSettingsRecord(const QString& type, const QString& config) const;
|
||||||
{
|
|
||||||
QVariantMap map;
|
|
||||||
map["config"] = config;
|
|
||||||
map["updated_at"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
|
|
||||||
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("type",type));
|
|
||||||
// when a setting is not global we are searching also for the instance
|
|
||||||
if(!isSettingGlobal(type))
|
|
||||||
cond.append(CPair("AND hyperion_inst",_hyperion_inst));
|
|
||||||
return createRecord(cond, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Test if record exist, type can be global setting or local (instance)
|
/// @brief Test if record exist, type can be global setting or local (instance)
|
||||||
@@ -50,76 +31,33 @@ public:
|
|||||||
/// @param[in] hyperion_inst The instance of hyperion assigned (might be empty)
|
/// @param[in] hyperion_inst The instance of hyperion assigned (might be empty)
|
||||||
/// @return true on success else false
|
/// @return true on success else false
|
||||||
///
|
///
|
||||||
inline bool recordExist(const QString& type) const
|
bool recordExist(const QString& type) const;
|
||||||
{
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("type",type));
|
|
||||||
// when a setting is not global we are searching also for the instance
|
|
||||||
if(!isSettingGlobal(type))
|
|
||||||
cond.append(CPair("AND hyperion_inst",_hyperion_inst));
|
|
||||||
return recordExists(cond);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Get 'config' column of settings entry as QJsonDocument
|
/// @brief Get 'config' column of settings entry as QJsonDocument
|
||||||
/// @param[in] type The settings type
|
/// @param[in] type The settings type
|
||||||
/// @return The QJsonDocument
|
/// @return The QJsonDocument
|
||||||
///
|
///
|
||||||
inline QJsonDocument getSettingsRecord(const QString& type) const
|
QJsonDocument getSettingsRecord(const QString& type) const;
|
||||||
{
|
|
||||||
QVariantMap results;
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("type",type));
|
|
||||||
// when a setting is not global we are searching also for the instance
|
|
||||||
if(!isSettingGlobal(type))
|
|
||||||
cond.append(CPair("AND hyperion_inst",_hyperion_inst));
|
|
||||||
getRecord(cond, results, QStringList("config"));
|
|
||||||
return QJsonDocument::fromJson(results["config"].toByteArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Get 'config' column of settings entry as QString
|
/// @brief Get 'config' column of settings entry as QString
|
||||||
/// @param[in] type The settings type
|
/// @param[in] type The settings type
|
||||||
/// @return The QString
|
/// @return The QString
|
||||||
///
|
///
|
||||||
inline QString getSettingsRecordString(const QString& type) const
|
QString getSettingsRecordString(const QString& type) const;
|
||||||
{
|
|
||||||
QVariantMap results;
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("type",type));
|
|
||||||
// when a setting is not global we are searching also for the instance
|
|
||||||
if(!isSettingGlobal(type))
|
|
||||||
cond.append(CPair("AND hyperion_inst",_hyperion_inst));
|
|
||||||
getRecord(cond, results, QStringList("config"));
|
|
||||||
return results["config"].toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Delete all settings entries associated with this instance, called from InstanceTable of HyperionIManager
|
/// @brief Delete all settings entries associated with this instance, called from InstanceTable of HyperionIManager
|
||||||
///
|
///
|
||||||
inline void deleteInstance() const
|
void deleteInstance() const;
|
||||||
{
|
|
||||||
VectorPair cond;
|
|
||||||
cond.append(CPair("hyperion_inst",_hyperion_inst));
|
|
||||||
deleteRecord(cond);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool isSettingGlobal(const QString& type) const
|
static const QVector<QString>& getGlobalSettingTypes();
|
||||||
{
|
|
||||||
// list of global settings
|
|
||||||
QStringList list;
|
|
||||||
// server port services
|
|
||||||
list << "jsonServer" << "protoServer" << "flatbufServer" << "forwarder" << "webConfig" << "network"
|
|
||||||
// capture
|
|
||||||
<< "framegrabber" << "grabberV4L2" << "grabberAudio"
|
|
||||||
//Events
|
|
||||||
<< "osEvents" << "cecEvents" << "schedEvents"
|
|
||||||
// other
|
|
||||||
<< "logger" << "general";
|
|
||||||
|
|
||||||
return list.contains(type);
|
static bool isSettingGlobal(const QString& type);
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const quint8 _hyperion_inst;
|
const quint8 _hyperion_inst;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif // SETTINGSTABLE_H
|
||||||
|
@@ -23,7 +23,7 @@ class AuthManager : public QObject
|
|||||||
private:
|
private:
|
||||||
friend class HyperionDaemon;
|
friend class HyperionDaemon;
|
||||||
/// constructor is private, can be called from HyperionDaemon
|
/// constructor is private, can be called from HyperionDaemon
|
||||||
AuthManager(QObject *parent = nullptr, bool readonlyMode = false);
|
AuthManager(QObject *parent = nullptr);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
struct AuthDefinition
|
struct AuthDefinition
|
||||||
|
@@ -108,8 +108,6 @@ public:
|
|||||||
///
|
///
|
||||||
QString getActiveDeviceType() const;
|
QString getActiveDeviceType() const;
|
||||||
|
|
||||||
bool getReadOnlyMode() const {return _readOnlyMode; }
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
|
||||||
///
|
///
|
||||||
@@ -340,14 +338,6 @@ public slots:
|
|||||||
///
|
///
|
||||||
bool saveSettings(const QJsonObject& config, bool correct = false);
|
bool saveSettings(const QJsonObject& config, bool correct = false);
|
||||||
|
|
||||||
///
|
|
||||||
/// @brief Restore a complete json config
|
|
||||||
/// @param config The entire config object
|
|
||||||
/// @param correct If true will correct json against schema before save
|
|
||||||
/// @return True on success else false
|
|
||||||
///
|
|
||||||
bool restoreSettings(const QJsonObject& config, bool correct = false);
|
|
||||||
|
|
||||||
/// ############
|
/// ############
|
||||||
/// COMPONENTREGISTER
|
/// COMPONENTREGISTER
|
||||||
///
|
///
|
||||||
@@ -552,7 +542,7 @@ private:
|
|||||||
/// @brief Constructs the Hyperion instance, just accessible for HyperionIManager
|
/// @brief Constructs the Hyperion instance, just accessible for HyperionIManager
|
||||||
/// @param instance The instance index
|
/// @param instance The instance index
|
||||||
///
|
///
|
||||||
Hyperion(quint8 instance, bool readonlyMode = false);
|
Hyperion(quint8 instance);
|
||||||
|
|
||||||
/// instance index
|
/// instance index
|
||||||
const quint8 _instIndex;
|
const quint8 _instIndex;
|
||||||
@@ -615,6 +605,4 @@ private:
|
|||||||
/// Boblight instance
|
/// Boblight instance
|
||||||
BoblightServer* _boblightServer;
|
BoblightServer* _boblightServer;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool _readOnlyMode;
|
|
||||||
};
|
};
|
||||||
|
@@ -59,12 +59,18 @@ public slots:
|
|||||||
///
|
///
|
||||||
QVector<QVariantMap> getInstanceData() const;
|
QVector<QVariantMap> getInstanceData() const;
|
||||||
|
|
||||||
|
QString getInstanceName(quint8 inst = 0);
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Get all instance indicies of running instances
|
/// @brief Get all instance indicies of running instances
|
||||||
///
|
///
|
||||||
QList<quint8> getRunningInstanceIdx() const;
|
QList<quint8> getRunningInstanceIdx() const;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Get all instance indicies configured
|
||||||
|
///
|
||||||
|
QList<quint8> getInstanceIds() const;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Start a Hyperion instance
|
/// @brief Start a Hyperion instance
|
||||||
/// @param instance Instance index
|
/// @param instance Instance index
|
||||||
@@ -115,8 +121,6 @@ public slots:
|
|||||||
///
|
///
|
||||||
bool saveName(quint8 inst, const QString& name);
|
bool saveName(quint8 inst, const QString& name);
|
||||||
|
|
||||||
QString getRootPath() const { return _rootPath; }
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
///
|
///
|
||||||
/// @brief Emits whenever the state of a instance changes according to enum instanceState
|
/// @brief Emits whenever the state of a instance changes according to enum instanceState
|
||||||
@@ -195,9 +199,8 @@ private:
|
|||||||
friend class HyperionDaemon;
|
friend class HyperionDaemon;
|
||||||
///
|
///
|
||||||
/// @brief Construct the Manager
|
/// @brief Construct the Manager
|
||||||
/// @param The root path of all userdata
|
|
||||||
///
|
///
|
||||||
HyperionIManager(const QString& rootPath, QObject* parent = nullptr, bool readonlyMode = false);
|
HyperionIManager(QObject* parent = nullptr);
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Start all instances that are marked as enabled in db. Non blocking
|
/// @brief Start all instances that are marked as enabled in db. Non blocking
|
||||||
@@ -218,12 +221,9 @@ private:
|
|||||||
private:
|
private:
|
||||||
Logger* _log;
|
Logger* _log;
|
||||||
InstanceTable* _instanceTable;
|
InstanceTable* _instanceTable;
|
||||||
const QString _rootPath;
|
|
||||||
QMap<quint8, Hyperion*> _runningInstances;
|
QMap<quint8, Hyperion*> _runningInstances;
|
||||||
|
|
||||||
QList<quint8> _startQueue;
|
QList<quint8> _startQueue;
|
||||||
|
|
||||||
bool _readonlyMode;
|
|
||||||
|
|
||||||
/// All pending requests
|
/// All pending requests
|
||||||
QMap<quint8, PendingRequests> _pendingRequests;
|
QMap<quint8, PendingRequests> _pendingRequests;
|
||||||
};
|
};
|
||||||
|
@@ -4,12 +4,14 @@
|
|||||||
#include <utils/settings.h>
|
#include <utils/settings.h>
|
||||||
|
|
||||||
#include <utils/version.hpp>
|
#include <utils/version.hpp>
|
||||||
|
|
||||||
|
#include <db/SettingsTable.h>
|
||||||
using namespace semver;
|
using namespace semver;
|
||||||
|
|
||||||
// qt includes
|
// qt includes
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
|
||||||
const int GLOABL_INSTANCE_ID = 255;
|
const char DEFAULT_VERSION[] = "2.0.0-alpha.8";
|
||||||
|
|
||||||
class Hyperion;
|
class Hyperion;
|
||||||
class SettingsTable;
|
class SettingsTable;
|
||||||
@@ -26,7 +28,7 @@ public:
|
|||||||
/// @params instance Instance index of HyperionInstanceManager
|
/// @params instance Instance index of HyperionInstanceManager
|
||||||
/// @params parent The parent hyperion instance
|
/// @params parent The parent hyperion instance
|
||||||
///
|
///
|
||||||
SettingsManager(quint8 instance, QObject* parent = nullptr, bool readonlyMode = false);
|
SettingsManager(quint8 instance, QObject* parent = nullptr);
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Save a complete json configuration
|
/// @brief Save a complete json configuration
|
||||||
@@ -52,10 +54,19 @@ public:
|
|||||||
QJsonDocument getSetting(settings::type type) const;
|
QJsonDocument getSetting(settings::type type) const;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief get the full settings object of this instance (with global settings)
|
/// @brief get a single setting json from configuration
|
||||||
|
/// @param type The type as string
|
||||||
|
/// @return The requested json data as QJsonDocument
|
||||||
|
///
|
||||||
|
QJsonDocument getSetting(const QString& type) const;
|
||||||
|
///
|
||||||
|
/// @brief get the selected settings objects of this instance (including global settings)
|
||||||
/// @return The requested json
|
/// @return The requested json
|
||||||
///
|
///
|
||||||
QJsonObject getSettings() const;
|
//QJsonObject getSettings(const QStringList& type = {} ) const;
|
||||||
|
|
||||||
|
QJsonObject getSettings(const QStringList& filteredTypes = {}) const;
|
||||||
|
QJsonObject getSettings(const QVariant& instance, const QStringList& filteredTypes = {} ) const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
///
|
///
|
||||||
@@ -73,8 +84,7 @@ private:
|
|||||||
///
|
///
|
||||||
bool handleConfigUpgrade(QJsonObject& config);
|
bool handleConfigUpgrade(QJsonObject& config);
|
||||||
|
|
||||||
|
bool resolveConfigVersion(const QJsonObject& config);
|
||||||
bool resolveConfigVersion(QJsonObject& config);
|
|
||||||
|
|
||||||
/// Logger instance
|
/// Logger instance
|
||||||
Logger* _log;
|
Logger* _log;
|
||||||
@@ -97,5 +107,4 @@ private:
|
|||||||
semver::version _configVersion;
|
semver::version _configVersion;
|
||||||
semver::version _previousVersion;
|
semver::version _previousVersion;
|
||||||
|
|
||||||
bool _readonlyMode;
|
|
||||||
};
|
};
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
#include <utils/FileUtils.h>
|
#include <utils/FileUtils.h>
|
||||||
|
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
#include <QJsonDocument>
|
||||||
#include <QPair>
|
#include <QPair>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <utils/Logger.h>
|
#include <utils/Logger.h>
|
||||||
@@ -94,4 +95,10 @@ namespace JsonUtils {
|
|||||||
/// @return true on success else false
|
/// @return true on success else false
|
||||||
///
|
///
|
||||||
bool resolveRefs(const QJsonObject& schema, QJsonObject& obj, Logger* log);
|
bool resolveRefs(const QJsonObject& schema, QJsonObject& obj, Logger* log);
|
||||||
|
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Function to convert QJsonValue to QString using QJsonDocument
|
||||||
|
///
|
||||||
|
QString jsonValueToQString(const QJsonValue &value, QJsonDocument::JsonFormat format = QJsonDocument::Compact);
|
||||||
}
|
}
|
||||||
|
@@ -405,20 +405,6 @@ bool API::saveSettings(const QJsonObject &data)
|
|||||||
return isSaved;
|
return isSaved;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool API::restoreSettings(const QJsonObject &data)
|
|
||||||
{
|
|
||||||
bool isRestored {true};
|
|
||||||
if (!_adminAuthorized)
|
|
||||||
{
|
|
||||||
isRestored = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
QMetaObject::invokeMethod(_hyperion, "restoreSettings", Qt::DirectConnection, Q_RETURN_ARG(bool, isRestored), Q_ARG(QJsonObject, data), Q_ARG(bool, true));
|
|
||||||
}
|
|
||||||
return isRestored;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool API::updateHyperionPassword(const QString &password, const QString &newPassword)
|
bool API::updateHyperionPassword(const QString &password, const QString &newPassword)
|
||||||
{
|
{
|
||||||
bool isPwUpdated {true};
|
bool isPwUpdated {true};
|
||||||
|
@@ -10,18 +10,38 @@
|
|||||||
"subcommand": {
|
"subcommand": {
|
||||||
"type" : "string",
|
"type" : "string",
|
||||||
"required" : true,
|
"required" : true,
|
||||||
"enum" : ["getconfig","getschema","setconfig","restoreconfig","reload"]
|
"enum" : ["getconfig","getconfig-old","getschema","setconfig","restoreconfig","reload"]
|
||||||
},
|
|
||||||
"instance" : {
|
|
||||||
"type" : "integer",
|
|
||||||
"minimum": 0,
|
|
||||||
"maximum": 255
|
|
||||||
},
|
},
|
||||||
"tan" : {
|
"tan" : {
|
||||||
"type" : "integer"
|
"type" : "integer"
|
||||||
},
|
},
|
||||||
|
"global" : {
|
||||||
|
"types": {
|
||||||
|
"type": "array",
|
||||||
|
"required": false,
|
||||||
|
"items" : {
|
||||||
|
"type" : "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"instances" : {
|
||||||
|
"ids" : {
|
||||||
|
"type": "array",
|
||||||
|
"required": true,
|
||||||
|
"items" : {},
|
||||||
|
"minItems": 1
|
||||||
|
},
|
||||||
|
"types": {
|
||||||
|
"type": "array",
|
||||||
|
"required": false,
|
||||||
|
"items" :{
|
||||||
|
"type" : "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"type" : "object"
|
"required": false,
|
||||||
|
"$ref": "schema-config-exchange"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
|
@@ -39,6 +39,7 @@
|
|||||||
|
|
||||||
// auth manager
|
// auth manager
|
||||||
#include <hyperion/AuthManager.h>
|
#include <hyperion/AuthManager.h>
|
||||||
|
#include <db/ConfigImportExport.h>
|
||||||
|
|
||||||
#ifdef ENABLE_MDNS
|
#ifdef ENABLE_MDNS
|
||||||
// mDNS discover
|
// mDNS discover
|
||||||
@@ -698,6 +699,10 @@ void JsonAPI::handleConfigCommand(const QJsonObject& message, const JsonApiComma
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case SubCommand::GetConfig:
|
case SubCommand::GetConfig:
|
||||||
|
handleConfigGetCommand(message, cmd);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SubCommand::GetConfigOld:
|
||||||
sendSuccessDataReply(_hyperion->getQJsonConfig(), cmd);
|
sendSuccessDataReply(_hyperion->getQJsonConfig(), cmd);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -722,45 +727,130 @@ void JsonAPI::handleConfigCommand(const QJsonObject& message, const JsonApiComma
|
|||||||
|
|
||||||
void JsonAPI::handleConfigSetCommand(const QJsonObject &message, const JsonApiCommand& cmd)
|
void JsonAPI::handleConfigSetCommand(const QJsonObject &message, const JsonApiCommand& cmd)
|
||||||
{
|
{
|
||||||
if (message.contains("config"))
|
if (DBManager::isReadOnly())
|
||||||
{
|
{
|
||||||
QJsonObject config = message["config"].toObject();
|
sendErrorReply("Database Error", {"Hyperion is running in read-only mode","Configuration updates are not possible"}, cmd);
|
||||||
if (API::isHyperionEnabled())
|
return;
|
||||||
{
|
}
|
||||||
if ( API::saveSettings(config) ) {
|
QJsonObject config = message["config"].toObject();
|
||||||
sendSuccessReply(cmd);
|
|
||||||
} else {
|
QJsonObject schema = QJsonFactory::readSchema(":schema-config-exchange");
|
||||||
sendErrorReply("Save settings failed", cmd);
|
QPair<bool, QStringList> validationResult = JsonUtils::validate("setConfig", config, schema, _log);
|
||||||
|
if (!validationResult.first)
|
||||||
|
{
|
||||||
|
sendErrorReply("Invalid JSON configuration data provided!", validationResult.second, cmd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Implement new setconfig schema
|
||||||
|
if (config.contains("global") || config.contains("instances"))
|
||||||
|
{
|
||||||
|
Warning(_log, "New set config schema is not yet supported!");
|
||||||
|
config.remove("global");
|
||||||
|
config.remove("instanceIds");
|
||||||
|
config.remove("instances");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.isEmpty())
|
||||||
|
{
|
||||||
|
sendErrorReply("Update configuration failed", {"No configuration data provided!"}, cmd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (API::isHyperionEnabled())
|
||||||
|
{
|
||||||
|
if ( API::saveSettings(config) ) {
|
||||||
|
sendSuccessReply(cmd);
|
||||||
|
} else {
|
||||||
|
sendErrorReply("Save settings failed", cmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sendErrorReply("Updating the configuration while Hyperion is disabled is not possible", cmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonAPI::handleConfigGetCommand(const QJsonObject &message, const JsonApiCommand& cmd)
|
||||||
|
{
|
||||||
|
bool addGlobalConfig {false};
|
||||||
|
QStringList globalFilterTypes;
|
||||||
|
|
||||||
|
if (message.contains("global"))
|
||||||
|
{
|
||||||
|
addGlobalConfig = true;
|
||||||
|
const QJsonObject globalConfig = message["global"].toObject();
|
||||||
|
|
||||||
|
const QJsonArray globalTypes = globalConfig["types"].toArray();
|
||||||
|
for (const QJsonValue &type : globalTypes) {
|
||||||
|
if (type.isString()) {
|
||||||
|
globalFilterTypes.append(type.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
{
|
|
||||||
sendErrorReply("Saving configuration while Hyperion is disabled isn't possible", cmd);
|
QList<quint8> instanceListFilter;
|
||||||
|
QStringList instanceFilterTypes;
|
||||||
|
|
||||||
|
bool addInstanceConfig {false};
|
||||||
|
if (message.contains("instances"))
|
||||||
|
{
|
||||||
|
addInstanceConfig = true;
|
||||||
|
const QJsonObject instances = message["instances"].toObject();
|
||||||
|
const QJsonArray instanceIds = instances["ids"].toArray();
|
||||||
|
for (const QJsonValue &idx : instanceIds) {
|
||||||
|
if (idx.isDouble()) {
|
||||||
|
instanceListFilter.append(static_cast<quint8>(idx.toInt()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const QJsonArray instanceTypes = instances["types"].toArray();
|
||||||
|
for (const QJsonValue &type : instanceTypes) {
|
||||||
|
if (type.isString()) {
|
||||||
|
instanceFilterTypes.append(type.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!addGlobalConfig && ! addInstanceConfig)
|
||||||
|
{
|
||||||
|
addGlobalConfig = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const QJsonObject settings = JsonInfo::getConfiguration(instanceListFilter, addGlobalConfig, instanceFilterTypes, globalFilterTypes);
|
||||||
|
if (!settings.empty())
|
||||||
|
{
|
||||||
|
sendSuccessDataReply(settings, cmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sendErrorReply("Generating full config failed", cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonAPI::handleConfigRestoreCommand(const QJsonObject &message, const JsonApiCommand& cmd)
|
void JsonAPI::handleConfigRestoreCommand(const QJsonObject &message, const JsonApiCommand& cmd)
|
||||||
{
|
{
|
||||||
if (message.contains("config"))
|
QJsonObject config = message["config"].toObject();
|
||||||
|
if (API::isHyperionEnabled())
|
||||||
{
|
{
|
||||||
QJsonObject config = message["config"].toObject();
|
ConfigImportExport configImport;
|
||||||
if (API::isHyperionEnabled())
|
QPair<bool, QStringList> result = configImport.setConfiguration(config);
|
||||||
|
if (result.first)
|
||||||
{
|
{
|
||||||
if ( API::restoreSettings(config) )
|
QString infoMsg {"Restarting after importing configuration successfully."};
|
||||||
{
|
sendSuccessDataReply(infoMsg, cmd);
|
||||||
sendSuccessReply(cmd);
|
Info(_log, "%s", QSTRING_CSTR(infoMsg));
|
||||||
}
|
emit signalEvent(Event::Restart);
|
||||||
else
|
|
||||||
{
|
|
||||||
sendErrorReply("Restore settings failed", cmd);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
sendErrorReply("Restoring configuration while Hyperion is disabled is not possible", cmd);
|
sendErrorReply("Restore configuration failed", result.second, cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sendErrorReply("Restoring configuration while Hyperion is disabled is not possible", cmd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonAPI::handleSchemaGetCommand(const QJsonObject& /*message*/, const JsonApiCommand& cmd)
|
void JsonAPI::handleSchemaGetCommand(const QJsonObject& /*message*/, const JsonApiCommand& cmd)
|
||||||
@@ -1156,7 +1246,7 @@ void JsonAPI::handleInstanceCommand(const QJsonObject &message, const JsonApiCom
|
|||||||
{
|
{
|
||||||
QString replyMsg;
|
QString replyMsg;
|
||||||
|
|
||||||
const quint8 &inst = static_cast<quint8>(message["instance"].toInt());
|
const quint8 inst = static_cast<quint8>(message["instance"].toInt());
|
||||||
const QString &name = message["name"].toString();
|
const QString &name = message["name"].toString();
|
||||||
|
|
||||||
switch (cmd.subCommand) {
|
switch (cmd.subCommand) {
|
||||||
@@ -1191,7 +1281,6 @@ void JsonAPI::handleInstanceCommand(const QJsonObject &message, const JsonApiCom
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case SubCommand::DeleteInstance:
|
case SubCommand::DeleteInstance:
|
||||||
handleConfigRestoreCommand(message, cmd);
|
|
||||||
if (API::deleteInstance(inst, replyMsg))
|
if (API::deleteInstance(inst, replyMsg))
|
||||||
{
|
{
|
||||||
sendSuccessReply(cmd);
|
sendSuccessReply(cmd);
|
||||||
@@ -1213,7 +1302,7 @@ void JsonAPI::handleInstanceCommand(const QJsonObject &message, const JsonApiCom
|
|||||||
if (cmd.subCommand == SubCommand::CreateInstance) {
|
if (cmd.subCommand == SubCommand::CreateInstance) {
|
||||||
replyMsg = API::createInstance(name);
|
replyMsg = API::createInstance(name);
|
||||||
} else {
|
} else {
|
||||||
replyMsg = setInstanceName(inst, name);
|
replyMsg = API::setInstanceName(inst, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (replyMsg.isEmpty()) {
|
if (replyMsg.isEmpty()) {
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
#include <db/ConfigImportExport.h>
|
||||||
#include <api/JsonInfo.h>
|
#include <api/JsonInfo.h>
|
||||||
#include <api/API.h>
|
#include <api/API.h>
|
||||||
|
|
||||||
@@ -498,8 +499,8 @@ QJsonObject JsonInfo::getSystemInfo(const Hyperion* hyperion)
|
|||||||
hyperionInfo["gitremote"] = QString(HYPERION_GIT_REMOTE);
|
hyperionInfo["gitremote"] = QString(HYPERION_GIT_REMOTE);
|
||||||
hyperionInfo["time"] = QString(__DATE__ " " __TIME__);
|
hyperionInfo["time"] = QString(__DATE__ " " __TIME__);
|
||||||
hyperionInfo["id"] = AuthManager::getInstance()->getID();
|
hyperionInfo["id"] = AuthManager::getInstance()->getID();
|
||||||
hyperionInfo["rootPath"] = HyperionIManager::getInstance()->getRootPath();
|
hyperionInfo["configDatabaseFile"] = DBManager::getFileInfo().absoluteFilePath();
|
||||||
hyperionInfo["readOnlyMode"] = hyperion->getReadOnlyMode();
|
hyperionInfo["readOnlyMode"] = DBManager::isReadOnly();
|
||||||
|
|
||||||
QCoreApplication* app = QCoreApplication::instance();
|
QCoreApplication* app = QCoreApplication::instance();
|
||||||
hyperionInfo["isGuiMode"] = qobject_cast<QApplication*>(app) != nullptr;
|
hyperionInfo["isGuiMode"] = qobject_cast<QApplication*>(app) != nullptr;
|
||||||
@@ -618,3 +619,9 @@ QJsonArray JsonInfo::discoverScreenInputs(const QJsonObject& params) const
|
|||||||
|
|
||||||
return screenInputs;
|
return screenInputs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QJsonObject JsonInfo::getConfiguration(const QList<quint8>& instancesfilter, bool addGlobalConfig, const QStringList& instanceFilteredTypes, const QStringList& globalFilterTypes )
|
||||||
|
{
|
||||||
|
ConfigImportExport configExport;
|
||||||
|
return configExport.getConfiguration(instancesfilter, addGlobalConfig, instanceFilteredTypes, globalFilterTypes );
|
||||||
|
}
|
||||||
|
176
libsrc/db/AuthTable.cpp
Normal file
176
libsrc/db/AuthTable.cpp
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
|
||||||
|
// hyperion
|
||||||
|
#include <db/AuthTable.h>
|
||||||
|
#include <QCryptographicHash>
|
||||||
|
|
||||||
|
// qt
|
||||||
|
#include <QDateTime>
|
||||||
|
#include <QUuid>
|
||||||
|
|
||||||
|
/// construct wrapper with auth table
|
||||||
|
AuthTable::AuthTable(QObject* parent)
|
||||||
|
: DBManager(parent)
|
||||||
|
{
|
||||||
|
// init Auth table
|
||||||
|
setTable("auth");
|
||||||
|
// create table columns
|
||||||
|
createTable(QStringList()<<"user TEXT"<<"password BLOB"<<"token BLOB"<<"salt BLOB"<<"comment TEXT"<<"id TEXT"<<"created_at TEXT"<<"last_use TEXT");
|
||||||
|
};
|
||||||
|
|
||||||
|
bool AuthTable::createUser(const QString& user, const QString& password)
|
||||||
|
{
|
||||||
|
// new salt
|
||||||
|
QByteArray salt = QCryptographicHash::hash(QUuid::createUuid().toByteArray(), QCryptographicHash::Sha512).toHex();
|
||||||
|
QVariantMap map;
|
||||||
|
map["user"] = user;
|
||||||
|
map["salt"] = salt;
|
||||||
|
map["password"] = hashPasswordWithSalt(password,salt);
|
||||||
|
map["created_at"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
|
||||||
|
|
||||||
|
return createRecord({{"user",user}}, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AuthTable::userExist(const QString& user)
|
||||||
|
{
|
||||||
|
return recordExists({{"user",user}});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AuthTable::isUserAuthorized(const QString& user, const QString& password)
|
||||||
|
{
|
||||||
|
if(userExist(user) && (calcPasswordHashOfUser(user, password) == getPasswordHashOfUser(user)))
|
||||||
|
{
|
||||||
|
updateUserUsed(user);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AuthTable::isUserTokenAuthorized(const QString& usr, const QString& token)
|
||||||
|
{
|
||||||
|
if(getUserToken(usr) == token.toUtf8())
|
||||||
|
{
|
||||||
|
updateUserUsed(usr);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AuthTable::setUserToken(const QString& user)
|
||||||
|
{
|
||||||
|
QVariantMap map;
|
||||||
|
map["token"] = QCryptographicHash::hash(QUuid::createUuid().toByteArray(), QCryptographicHash::Sha512).toHex();
|
||||||
|
|
||||||
|
return updateRecord({{"user",user}}, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
const QByteArray AuthTable::getUserToken(const QString& user)
|
||||||
|
{
|
||||||
|
QVariantMap results;
|
||||||
|
getRecord({{"user",user}}, results, QStringList()<<"token");
|
||||||
|
|
||||||
|
return results["token"].toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AuthTable::updateUserPassword(const QString& user, const QString& newPassword)
|
||||||
|
{
|
||||||
|
QVariantMap map;
|
||||||
|
map["password"] = calcPasswordHashOfUser(user, newPassword);
|
||||||
|
|
||||||
|
return updateRecord({{"user",user}}, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AuthTable::resetHyperionUser()
|
||||||
|
{
|
||||||
|
QVariantMap map;
|
||||||
|
map["password"] = calcPasswordHashOfUser(hyperion::DEFAULT_USER, hyperion::DEFAULT_PASSWORD);
|
||||||
|
|
||||||
|
return updateRecord({{"user", hyperion::DEFAULT_USER}}, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AuthTable::updateUserUsed(const QString& user)
|
||||||
|
{
|
||||||
|
QVariantMap map;
|
||||||
|
map["last_use"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
|
||||||
|
|
||||||
|
updateRecord({{"user",user}}, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AuthTable::tokenExist(const QString& token)
|
||||||
|
{
|
||||||
|
QVariantMap map;
|
||||||
|
map["last_use"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
|
||||||
|
|
||||||
|
VectorPair cond;
|
||||||
|
cond.append(CPair("token", hashToken(token)));
|
||||||
|
if(recordExists(cond))
|
||||||
|
{
|
||||||
|
// update it
|
||||||
|
createRecord(cond,map);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AuthTable::createToken(const QString& token, const QString& comment, const QString& identifier)
|
||||||
|
{
|
||||||
|
QVariantMap map;
|
||||||
|
map["comment"] = comment;
|
||||||
|
map["id"] = identifierExist(identifier) ? QUuid::createUuid().toString().remove("{").remove("}").left(5) : identifier;
|
||||||
|
map["created_at"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
|
||||||
|
|
||||||
|
return createRecord({{"token", hashToken(token)}}, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AuthTable::deleteToken(const QString& identifier)
|
||||||
|
{
|
||||||
|
return deleteRecord({{"id", identifier}});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AuthTable::renameToken(const QString &identifier, const QString &comment)
|
||||||
|
{
|
||||||
|
QVariantMap map;
|
||||||
|
map["comment"] = comment;
|
||||||
|
|
||||||
|
return updateRecord({{"id", identifier}}, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
const QVector<QVariantMap> AuthTable::getTokenList()
|
||||||
|
{
|
||||||
|
QVector<QVariantMap> results;
|
||||||
|
getRecords(results, QStringList() << "comment" << "id" << "last_use");
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AuthTable::identifierExist(const QString& identifier)
|
||||||
|
{
|
||||||
|
return recordExists({{"id", identifier}});
|
||||||
|
}
|
||||||
|
|
||||||
|
const QByteArray AuthTable::getPasswordHashOfUser(const QString& user)
|
||||||
|
{
|
||||||
|
QVariantMap results;
|
||||||
|
getRecord({{"user",user}}, results, QStringList()<<"password");
|
||||||
|
|
||||||
|
return results["password"].toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
const QByteArray AuthTable::calcPasswordHashOfUser(const QString& user, const QString& password)
|
||||||
|
{
|
||||||
|
// get salt
|
||||||
|
QVariantMap results;
|
||||||
|
getRecord({{"user",user}}, results, QStringList()<<"salt");
|
||||||
|
|
||||||
|
// calc
|
||||||
|
return hashPasswordWithSalt(password,results["salt"].toByteArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
const QByteArray AuthTable::hashPasswordWithSalt(const QString& password, const QByteArray& salt)
|
||||||
|
{
|
||||||
|
return QCryptographicHash::hash(password.toUtf8().append(salt), QCryptographicHash::Sha512).toHex();
|
||||||
|
}
|
||||||
|
|
||||||
|
const QByteArray AuthTable::hashToken(const QString& token)
|
||||||
|
{
|
||||||
|
return QCryptographicHash::hash(token.toUtf8(), QCryptographicHash::Sha512).toHex();
|
||||||
|
}
|
@@ -1,10 +1,17 @@
|
|||||||
add_library(database
|
add_library(database
|
||||||
${CMAKE_SOURCE_DIR}/include/db/AuthTable.h
|
${CMAKE_SOURCE_DIR}/include/db/AuthTable.h
|
||||||
${CMAKE_SOURCE_DIR}/include/db/DBManager.h
|
${CMAKE_SOURCE_DIR}/include/db/DBManager.h
|
||||||
|
${CMAKE_SOURCE_DIR}/include/db/ConfigImportExport.h
|
||||||
${CMAKE_SOURCE_DIR}/include/db/InstanceTable.h
|
${CMAKE_SOURCE_DIR}/include/db/InstanceTable.h
|
||||||
${CMAKE_SOURCE_DIR}/include/db/MetaTable.h
|
${CMAKE_SOURCE_DIR}/include/db/MetaTable.h
|
||||||
${CMAKE_SOURCE_DIR}/include/db/SettingsTable.h
|
${CMAKE_SOURCE_DIR}/include/db/SettingsTable.h
|
||||||
|
${CMAKE_SOURCE_DIR}/libsrc/db/AuthTable.cpp
|
||||||
${CMAKE_SOURCE_DIR}/libsrc/db/DBManager.cpp
|
${CMAKE_SOURCE_DIR}/libsrc/db/DBManager.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/libsrc/db/ConfigImportExport.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/libsrc/db/InstanceTable.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/libsrc/db/MetaTable.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/libsrc/db/SettingsTable.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/libsrc/db/DB_schemas.qrc
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(database
|
target_link_libraries(database
|
||||||
|
261
libsrc/db/ConfigImportExport.cpp
Normal file
261
libsrc/db/ConfigImportExport.cpp
Normal file
@@ -0,0 +1,261 @@
|
|||||||
|
#include "db/SettingsTable.h"
|
||||||
|
#include <db/MetaTable.h>
|
||||||
|
#include <db/ConfigImportExport.h>
|
||||||
|
|
||||||
|
#include <db/InstanceTable.h>
|
||||||
|
#include <hyperion/SettingsManager.h>
|
||||||
|
|
||||||
|
#include <utils/JsonUtils.h>
|
||||||
|
#include <utils/jsonschema/QJsonFactory.h>
|
||||||
|
|
||||||
|
#include <HyperionConfig.h>
|
||||||
|
|
||||||
|
#include <QSqlDatabase>
|
||||||
|
#include <QSqlQuery>
|
||||||
|
#include <QSqlError>
|
||||||
|
#include <QDir>
|
||||||
|
|
||||||
|
ConfigImportExport::ConfigImportExport(QObject* parent)
|
||||||
|
: DBManager(parent)
|
||||||
|
{
|
||||||
|
Q_INIT_RESOURCE(DB_schemas);
|
||||||
|
}
|
||||||
|
|
||||||
|
QPair<bool, QStringList> ConfigImportExport::importJson(const QString& configFile)
|
||||||
|
{
|
||||||
|
Info(_log,"Import configuration file '%s'", QSTRING_CSTR(configFile));
|
||||||
|
|
||||||
|
QJsonObject config;
|
||||||
|
QPair<bool, QStringList> result = JsonUtils::readFile(configFile, config, _log, false);
|
||||||
|
|
||||||
|
if (!result.first)
|
||||||
|
{
|
||||||
|
QString errorText = QString("Import configuration file '%s' failed!").arg(configFile);
|
||||||
|
result.second.prepend(errorText);
|
||||||
|
Error(_log, "'%s'", QSTRING_CSTR(errorText));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return setConfiguration(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ConfigImportExport::exportJson(const QString& path) const
|
||||||
|
{
|
||||||
|
bool isExported {false};
|
||||||
|
|
||||||
|
QDir exportPath{path};
|
||||||
|
if (path.isEmpty())
|
||||||
|
{
|
||||||
|
exportPath.setPath(getDataDirectory().absoluteFilePath("archive"));
|
||||||
|
}
|
||||||
|
|
||||||
|
QString jsonFile;
|
||||||
|
if (QDir().mkpath(exportPath.absolutePath()))
|
||||||
|
{
|
||||||
|
const QJsonObject configurtion = getConfiguration();
|
||||||
|
if (!configurtion.isEmpty())
|
||||||
|
{
|
||||||
|
const QJsonObject generalSettings = configurtion.value("global").toObject().value("settings").toObject().value("general").toObject();
|
||||||
|
const QString configVersion = generalSettings.value("configVersion").toString();
|
||||||
|
|
||||||
|
jsonFile = exportPath.absoluteFilePath(QString("HyperionBackup_%1_v%2.json").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd_hh:mm:ss:zzz"), configVersion ));
|
||||||
|
if (FileUtils::writeFile(jsonFile, QJsonDocument(configurtion).toJson(QJsonDocument::Indented), _log))
|
||||||
|
{
|
||||||
|
isExported = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isExported)
|
||||||
|
{
|
||||||
|
Info(_log, "Successfully exported configuration to '%s'", QSTRING_CSTR(jsonFile));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Error(_log, "Failed to export configuration to '%s'", QSTRING_CSTR(jsonFile));
|
||||||
|
}
|
||||||
|
|
||||||
|
return isExported;
|
||||||
|
}
|
||||||
|
|
||||||
|
QPair<bool, QStringList> ConfigImportExport::setConfiguration(const QJsonObject& config)
|
||||||
|
{
|
||||||
|
Debug(_log, "Start import JSON configuration");
|
||||||
|
|
||||||
|
QStringList errorList;
|
||||||
|
if (config.isEmpty())
|
||||||
|
{
|
||||||
|
QString errorText {"No configuration data provided!"};
|
||||||
|
Error(_log, "'%s'", QSTRING_CSTR(errorText));
|
||||||
|
errorList.append(errorText);
|
||||||
|
return qMakePair (false, errorList );
|
||||||
|
}
|
||||||
|
|
||||||
|
// check basic message
|
||||||
|
QJsonObject schema = QJsonFactory::readSchema(":schema-config-exchange");
|
||||||
|
QPair<bool, QStringList> validationResult = JsonUtils::validate("importConfig", config, schema, _log);
|
||||||
|
if (!validationResult.first)
|
||||||
|
{
|
||||||
|
Error(_log, "Invalid JSON configuration data provided!");
|
||||||
|
return qMakePair (false, validationResult.second );
|
||||||
|
}
|
||||||
|
|
||||||
|
Info(_log, "Create backup of current configuration");
|
||||||
|
if (!exportJson())
|
||||||
|
{
|
||||||
|
Warning(_log, "Backup of current configuration failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
QSqlDatabase idb = getDB();
|
||||||
|
if (!idb.transaction())
|
||||||
|
{
|
||||||
|
QString errorText = QString("Could not create a database transaction. Error: %1").arg(idb.lastError().text());
|
||||||
|
Error(_log, "'%s'", QSTRING_CSTR(errorText));
|
||||||
|
errorList.append(errorText);
|
||||||
|
return qMakePair (false, errorList );
|
||||||
|
}
|
||||||
|
|
||||||
|
bool errorOccured {false};
|
||||||
|
if (!deleteTable("instances") || !deleteTable("settings"))
|
||||||
|
{
|
||||||
|
QString errorText = "Failed to clear tables before import";
|
||||||
|
Error(_log, "'%s'", QSTRING_CSTR(errorText));
|
||||||
|
errorList.append(errorText);
|
||||||
|
errorOccured = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SettingsTable settingsTableGlobal(GLOABL_INSTANCE_ID);
|
||||||
|
const QJsonObject globalConfig = config.value("global").toObject();
|
||||||
|
const QJsonObject globalSettings = globalConfig.value("settings").toObject();
|
||||||
|
|
||||||
|
for (QJsonObject::const_iterator it = globalSettings.constBegin(); it != globalSettings.constEnd(); ++it)
|
||||||
|
{
|
||||||
|
if (!settingsTableGlobal.createSettingsRecord(it.key(), JsonUtils::jsonValueToQString(it.value())))
|
||||||
|
{
|
||||||
|
errorOccured = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
InstanceTable instanceTable;
|
||||||
|
const QJsonArray instancesConfig = config.value("instances").toArray();
|
||||||
|
quint8 instanceIdx {0};
|
||||||
|
|
||||||
|
for (auto instanceItem : instancesConfig)
|
||||||
|
{
|
||||||
|
QJsonObject instanceConfig = instanceItem.toObject();
|
||||||
|
QString instanceName = instanceConfig.value("name").toString(QString("Instance %1").arg(instanceIdx));
|
||||||
|
bool isInstanceEnabled = instanceConfig.value("enabled").toBool(true);
|
||||||
|
|
||||||
|
if (instanceIdx == 0)
|
||||||
|
{
|
||||||
|
isInstanceEnabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!instanceTable.createInstance(instanceName, instanceIdx) ||
|
||||||
|
!instanceTable.setEnable(instanceIdx, isInstanceEnabled))
|
||||||
|
{
|
||||||
|
errorOccured = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
SettingsTable settingsTableInstance(instanceIdx);
|
||||||
|
const QJsonObject instanceSettings = instanceConfig.value("settings").toObject();
|
||||||
|
for (QJsonObject::const_iterator it = instanceSettings.constBegin(); it != instanceSettings.constEnd(); ++it)
|
||||||
|
{
|
||||||
|
if (!settingsTableInstance.createSettingsRecord(it.key(), JsonUtils::jsonValueToQString(it.value())))
|
||||||
|
{
|
||||||
|
errorOccured = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
++instanceIdx;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (errorOccured)
|
||||||
|
{
|
||||||
|
QString errorText = "Errors occured during instances' and/or settings' configuration";
|
||||||
|
Error(_log, "'%s'", QSTRING_CSTR(errorText));
|
||||||
|
errorList.append(errorText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (errorOccured)
|
||||||
|
{
|
||||||
|
if (!idb.rollback())
|
||||||
|
{
|
||||||
|
QString errorText = QString("Could not create a database transaction. Error: %1").arg(idb.lastError().text());
|
||||||
|
Error(_log, "'%s'", QSTRING_CSTR(errorText));
|
||||||
|
errorList.append(errorText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!idb.commit())
|
||||||
|
{
|
||||||
|
QString errorText = QString("Could not finalise the database changes. Error: %1").arg(idb.lastError().text());
|
||||||
|
Error(_log, "'%s'", QSTRING_CSTR(errorText));
|
||||||
|
errorList.append(errorText);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(errorList.isEmpty())
|
||||||
|
{
|
||||||
|
Info(_log, "Successfully imported new configuration");
|
||||||
|
}
|
||||||
|
|
||||||
|
return qMakePair (errorList.isEmpty(), errorList );
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject ConfigImportExport::getConfiguration(const QList<quint8>& instancesFilter, bool addGlobalConfig, const QStringList& instanceFilteredTypes, const QStringList& globalFilterTypes ) const
|
||||||
|
{
|
||||||
|
QSqlDatabase idb = getDB();
|
||||||
|
if (!idb.transaction())
|
||||||
|
{
|
||||||
|
Error(_log, "Could not create a database transaction. Error: %s", QSTRING_CSTR(idb.lastError().text()));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
InstanceTable instanceTable;
|
||||||
|
SettingsManager settingsManager(0, nullptr);
|
||||||
|
|
||||||
|
QJsonObject config;
|
||||||
|
|
||||||
|
if (addGlobalConfig)
|
||||||
|
{
|
||||||
|
QJsonObject globalConfig;
|
||||||
|
|
||||||
|
MetaTable metaTable;
|
||||||
|
globalConfig.insert("uuid", metaTable.getUUID());
|
||||||
|
globalConfig.insert("settings", settingsManager.getSettings({}, globalFilterTypes));
|
||||||
|
config.insert("global", globalConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<quint8> instances {instancesFilter};
|
||||||
|
if (instances.isEmpty())
|
||||||
|
{
|
||||||
|
instances = instanceTable.getAllInstanceIDs();
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<quint8> sortedInstances = instances;
|
||||||
|
std::sort(sortedInstances.begin(), sortedInstances.end());
|
||||||
|
|
||||||
|
QJsonArray instanceIdList;
|
||||||
|
QJsonArray configInstanceList;
|
||||||
|
for (const quint8 instanceIdx : sortedInstances)
|
||||||
|
{
|
||||||
|
QJsonObject instanceConfig;
|
||||||
|
instanceConfig.insert("id",instanceIdx);
|
||||||
|
instanceConfig.insert("name", instanceTable.getNamebyIndex(instanceIdx));
|
||||||
|
instanceConfig.insert("enabled", instanceTable.isEnabled(instanceIdx));
|
||||||
|
instanceConfig.insert("settings", settingsManager.getSettings(static_cast<quint8>(instanceIdx), instanceFilteredTypes));
|
||||||
|
configInstanceList.append(instanceConfig);
|
||||||
|
|
||||||
|
instanceIdList.append(instanceIdx);
|
||||||
|
}
|
||||||
|
|
||||||
|
config.insert("instanceIds", instanceIdList);
|
||||||
|
config.insert("instances", configInstanceList);
|
||||||
|
|
||||||
|
if (!idb.commit())
|
||||||
|
{
|
||||||
|
Error(_log, "Could not finalise a database transaction. Error: %s", QSTRING_CSTR(idb.lastError().text()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
@@ -1,38 +1,48 @@
|
|||||||
|
#include "utils/settings.h"
|
||||||
#include <db/DBManager.h>
|
#include <db/DBManager.h>
|
||||||
|
|
||||||
#include <QSqlDatabase>
|
#include <QSqlDatabase>
|
||||||
#include <QSqlError>
|
#include <QSqlError>
|
||||||
#include <QSqlQuery>
|
#include <QSqlQuery>
|
||||||
#include <QSqlRecord>
|
#include <QSqlRecord>
|
||||||
#include <QThreadStorage>
|
|
||||||
#include <QUuid>
|
#include <QUuid>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QMetaType>
|
#include <QMetaType>
|
||||||
|
#include <QJsonObject>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// not in header because of linking
|
#define NO_SQLQUERY_LOGGING
|
||||||
static QString _rootPath;
|
|
||||||
static QThreadStorage<QSqlDatabase> _databasePool;
|
// Constants
|
||||||
|
namespace {
|
||||||
|
const char DATABASE_DIRECTORYNAME[] = "db";
|
||||||
|
const char DATABASE_FILENAME[] = "hyperion.db";
|
||||||
|
|
||||||
|
} //End of constants
|
||||||
|
|
||||||
|
|
||||||
|
QDir DBManager::_dataDirectory;
|
||||||
|
QDir DBManager::_databaseDirectory;
|
||||||
|
QFileInfo DBManager::_databaseFile;
|
||||||
|
QThreadStorage<QSqlDatabase> DBManager::_databasePool;
|
||||||
|
bool DBManager::_isReadOnly {false};
|
||||||
|
|
||||||
DBManager::DBManager(QObject* parent)
|
DBManager::DBManager(QObject* parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, _log(Logger::getInstance("DB"))
|
, _log(Logger::getInstance("DB"))
|
||||||
, _readonlyMode (false)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
DBManager::~DBManager()
|
void DBManager::initializeDatabase(const QDir& dataDirectory, bool isReadOnly)
|
||||||
{
|
{
|
||||||
}
|
_dataDirectory = dataDirectory;
|
||||||
|
_databaseDirectory.setPath(_dataDirectory.absoluteFilePath(DATABASE_DIRECTORYNAME));
|
||||||
void DBManager::setRootPath(const QString& rootPath)
|
QDir().mkpath(_databaseDirectory.absolutePath());
|
||||||
{
|
_databaseFile.setFile(_databaseDirectory,DATABASE_FILENAME);
|
||||||
_rootPath = rootPath;
|
_isReadOnly = isReadOnly;
|
||||||
// create directory
|
|
||||||
QDir().mkpath(_rootPath+"/db");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DBManager::setTable(const QString& table)
|
void DBManager::setTable(const QString& table)
|
||||||
@@ -43,38 +53,39 @@ void DBManager::setTable(const QString& table)
|
|||||||
QSqlDatabase DBManager::getDB() const
|
QSqlDatabase DBManager::getDB() const
|
||||||
{
|
{
|
||||||
if(_databasePool.hasLocalData())
|
if(_databasePool.hasLocalData())
|
||||||
return _databasePool.localData();
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
auto db = QSqlDatabase::addDatabase("QSQLITE", QUuid::createUuid().toString());
|
return _databasePool.localData();
|
||||||
_databasePool.setLocalData(db);
|
}
|
||||||
db.setDatabaseName(_rootPath+"/db/"+_dbn+".db");
|
auto database = QSqlDatabase::addDatabase("QSQLITE", QUuid::createUuid().toString());
|
||||||
if(!db.open())
|
|
||||||
|
if (isReadOnly())
|
||||||
{
|
{
|
||||||
Error(_log, "%s", QSTRING_CSTR(db.lastError().text()));
|
database.setConnectOptions("QSQLITE_OPEN_READONLY");
|
||||||
|
}
|
||||||
|
Debug(Logger::getInstance("DB"), "Database is opened in %s mode", _isReadOnly ? "read-only" : "read/write");
|
||||||
|
|
||||||
|
_databasePool.setLocalData(database);
|
||||||
|
database.setDatabaseName(_databaseFile.absoluteFilePath());
|
||||||
|
if(!database.open())
|
||||||
|
{
|
||||||
|
Error(_log, "%s", QSTRING_CSTR(database.lastError().text()));
|
||||||
throw std::runtime_error("Failed to open database connection!");
|
throw std::runtime_error("Failed to open database connection!");
|
||||||
}
|
}
|
||||||
return db;
|
|
||||||
}
|
return database;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DBManager::createRecord(const VectorPair& conditions, const QVariantMap& columns) const
|
bool DBManager::createRecord(const VectorPair& conditions, const QVariantMap& columns) const
|
||||||
{
|
{
|
||||||
if ( _readonlyMode )
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(recordExists(conditions))
|
if(recordExists(conditions))
|
||||||
{
|
{
|
||||||
// if there is no column data, return
|
// if there is no column data, return
|
||||||
if(columns.isEmpty())
|
if(columns.isEmpty())
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if(!updateRecord(conditions, columns))
|
return updateRecord(conditions, columns);
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QSqlDatabase idb = getDB();
|
QSqlDatabase idb = getDB();
|
||||||
@@ -84,14 +95,15 @@ bool DBManager::createRecord(const VectorPair& conditions, const QVariantMap& co
|
|||||||
QVariantList cValues;
|
QVariantList cValues;
|
||||||
QStringList prep;
|
QStringList prep;
|
||||||
QStringList placeh;
|
QStringList placeh;
|
||||||
|
|
||||||
// prep merge columns & condition
|
// prep merge columns & condition
|
||||||
QVariantMap::const_iterator i = columns.constBegin();
|
QVariantMap::const_iterator columnIter = columns.constBegin();
|
||||||
while (i != columns.constEnd()) {
|
while (columnIter != columns.constEnd()) {
|
||||||
prep.append(i.key());
|
prep.append(columnIter.key());
|
||||||
cValues += i.value();
|
cValues += columnIter.value();
|
||||||
placeh.append("?");
|
placeh.append("?");
|
||||||
|
|
||||||
++i;
|
++columnIter;
|
||||||
}
|
}
|
||||||
for(const auto& pair : conditions)
|
for(const auto& pair : conditions)
|
||||||
{
|
{
|
||||||
@@ -101,21 +113,19 @@ bool DBManager::createRecord(const VectorPair& conditions, const QVariantMap& co
|
|||||||
cValues << pair.second;
|
cValues << pair.second;
|
||||||
placeh.append("?");
|
placeh.append("?");
|
||||||
}
|
}
|
||||||
query.prepare(QString("INSERT INTO %1 ( %2 ) VALUES ( %3 )").arg(_table,prep.join(", ")).arg(placeh.join(", ")));
|
query.prepare(QString("INSERT INTO %1 ( %2 ) VALUES ( %3 )").arg(_table,prep.join(", "), placeh.join(", ")));
|
||||||
// add column & condition values
|
// add column & condition values
|
||||||
doAddBindValue(query, cValues);
|
addBindValues(query, cValues);
|
||||||
if(!query.exec())
|
|
||||||
{
|
return executeQuery(query);
|
||||||
Error(_log, "Failed to create record: '%s' in table: '%s' Error: %s", QSTRING_CSTR(prep.join(", ")), QSTRING_CSTR(_table), QSTRING_CSTR(idb.lastError().text()));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DBManager::recordExists(const VectorPair& conditions) const
|
bool DBManager::recordExists(const VectorPair& conditions) const
|
||||||
{
|
{
|
||||||
if(conditions.isEmpty())
|
if(conditions.isEmpty())
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
QSqlDatabase idb = getDB();
|
QSqlDatabase idb = getDB();
|
||||||
QSqlQuery query(idb);
|
QSqlQuery query(idb);
|
||||||
@@ -127,35 +137,28 @@ bool DBManager::recordExists(const VectorPair& conditions) const
|
|||||||
|
|
||||||
for(const auto& pair : conditions)
|
for(const auto& pair : conditions)
|
||||||
{
|
{
|
||||||
prepCond << pair.first+"=?";
|
prepCond << pair.first+"= ?";
|
||||||
bindVal << pair.second;
|
bindVal << pair.second;
|
||||||
}
|
}
|
||||||
query.prepare(QString("SELECT * FROM %1 %2").arg(_table,prepCond.join(" ")));
|
query.prepare(QString("SELECT * FROM %1 %2").arg(_table,prepCond.join(" ")));
|
||||||
doAddBindValue(query, bindVal);
|
addBindValues(query, bindVal);
|
||||||
if(!query.exec())
|
|
||||||
|
if (!executeQuery(query))
|
||||||
{
|
{
|
||||||
Error(_log, "Failed recordExists(): '%s' in table: '%s' Error: %s", QSTRING_CSTR(prepCond.join(" ")), QSTRING_CSTR(_table), QSTRING_CSTR(idb.lastError().text()));
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int entry = 0;
|
int entry = 0;
|
||||||
while (query.next()) {
|
while (query.next())
|
||||||
|
{
|
||||||
entry++;
|
entry++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(entry)
|
return entry > 0;
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DBManager::updateRecord(const VectorPair& conditions, const QVariantMap& columns) const
|
bool DBManager::updateRecord(const VectorPair& conditions, const QVariantMap& columns) const
|
||||||
{
|
{
|
||||||
if ( _readonlyMode )
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
QSqlDatabase idb = getDB();
|
QSqlDatabase idb = getDB();
|
||||||
QSqlQuery query(idb);
|
QSqlQuery query(idb);
|
||||||
query.setForwardOnly(true);
|
query.setForwardOnly(true);
|
||||||
@@ -164,88 +167,75 @@ bool DBManager::updateRecord(const VectorPair& conditions, const QVariantMap& co
|
|||||||
QStringList prep;
|
QStringList prep;
|
||||||
|
|
||||||
// prepare columns valus
|
// prepare columns valus
|
||||||
QVariantMap::const_iterator i = columns.constBegin();
|
QVariantMap::const_iterator columnIter = columns.constBegin();
|
||||||
while (i != columns.constEnd()) {
|
while (columnIter != columns.constEnd()) {
|
||||||
prep += i.key()+"=?";
|
prep += columnIter.key()+"= ?";
|
||||||
values += i.value();
|
values += columnIter.value();
|
||||||
|
|
||||||
++i;
|
++columnIter;
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare condition values
|
// prepare condition values
|
||||||
QStringList prepCond;
|
QStringList prepCond;
|
||||||
QVariantList prepBindVal;
|
QVariantList prepBindVal;
|
||||||
if(!conditions.isEmpty())
|
if(!conditions.isEmpty()) {
|
||||||
prepCond << "WHERE";
|
prepCond << "WHERE";
|
||||||
|
}
|
||||||
|
|
||||||
for(const auto& pair : conditions)
|
for(const auto& pair : conditions)
|
||||||
{
|
{
|
||||||
prepCond << pair.first+"=?";
|
prepCond << pair.first+"= ?";
|
||||||
prepBindVal << pair.second;
|
prepBindVal << pair.second;
|
||||||
}
|
}
|
||||||
|
|
||||||
query.prepare(QString("UPDATE %1 SET %2 %3").arg(_table,prep.join(", ")).arg(prepCond.join(" ")));
|
query.prepare(QString("UPDATE %1 SET %2 %3").arg(_table,prep.join(", "), prepCond.join(" ")));
|
||||||
// add column values
|
// add column values
|
||||||
doAddBindValue(query, values);
|
addBindValues(query, values);
|
||||||
// add condition values
|
// add condition values
|
||||||
doAddBindValue(query, prepBindVal);
|
addBindValues(query, prepBindVal);
|
||||||
if(!query.exec())
|
|
||||||
{
|
return executeQuery(query);
|
||||||
Error(_log, "Failed to update record: '%s' in table: '%s' Error: %s", QSTRING_CSTR(prepCond.join(" ")), QSTRING_CSTR(_table), QSTRING_CSTR(idb.lastError().text()));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DBManager::getRecord(const VectorPair& conditions, QVariantMap& results, const QStringList& tColumns, const QStringList& tOrder) const
|
bool DBManager::getRecord(const VectorPair& conditions, QVariantMap& results, const QStringList& tColumns, const QStringList& tOrder) const
|
||||||
{
|
{
|
||||||
QSqlDatabase idb = getDB();
|
QVector<QVariantMap> resultVector{};
|
||||||
QSqlQuery query(idb);
|
bool success = getRecords(conditions, resultVector, tColumns, tOrder);
|
||||||
query.setForwardOnly(true);
|
if (success && !resultVector.isEmpty()) {
|
||||||
|
results = resultVector.first();
|
||||||
QString sColumns("*");
|
|
||||||
if(!tColumns.isEmpty())
|
|
||||||
sColumns = tColumns.join(", ");
|
|
||||||
|
|
||||||
QString sOrder("");
|
|
||||||
if(!tOrder.isEmpty())
|
|
||||||
{
|
|
||||||
sOrder = " ORDER BY ";
|
|
||||||
sOrder.append(tOrder.join(", "));
|
|
||||||
}
|
}
|
||||||
// prep conditions
|
return success;
|
||||||
QStringList prepCond;
|
|
||||||
QVariantList bindVal;
|
|
||||||
if(!conditions.isEmpty())
|
|
||||||
prepCond << " WHERE";
|
|
||||||
|
|
||||||
for(const auto& pair : conditions)
|
|
||||||
{
|
|
||||||
prepCond << pair.first+"=?";
|
|
||||||
bindVal << pair.second;
|
|
||||||
}
|
|
||||||
query.prepare(QString("SELECT %1 FROM %2%3%4").arg(sColumns,_table).arg(prepCond.join(" ")).arg(sOrder));
|
|
||||||
doAddBindValue(query, bindVal);
|
|
||||||
|
|
||||||
if(!query.exec())
|
|
||||||
{
|
|
||||||
Error(_log, "Failed to get record: '%s' in table: '%s' Error: %s", QSTRING_CSTR(prepCond.join(" ")), QSTRING_CSTR(_table), QSTRING_CSTR(idb.lastError().text()));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// go to first row
|
|
||||||
query.next();
|
|
||||||
|
|
||||||
QSqlRecord rec = query.record();
|
|
||||||
for(int i = 0; i<rec.count(); i++)
|
|
||||||
{
|
|
||||||
results[rec.fieldName(i)] = rec.value(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DBManager::getRecords(QVector<QVariantMap>& results, const QStringList& tColumns, const QStringList& tOrder) const
|
bool DBManager::getRecords(QVector<QVariantMap>& results, const QStringList& tColumns, const QStringList& tOrder) const
|
||||||
|
{
|
||||||
|
return getRecords({}, results, tColumns, tOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DBManager::getRecords(const VectorPair& conditions, QVector<QVariantMap>& results, const QStringList& tColumns, const QStringList& tOrder) const
|
||||||
|
{
|
||||||
|
// prep conditions
|
||||||
|
QStringList conditionList;
|
||||||
|
QVariantList bindValues;
|
||||||
|
|
||||||
|
for(const auto& pair : conditions)
|
||||||
|
{
|
||||||
|
conditionList << pair.first;
|
||||||
|
if (pair.second.isNull())
|
||||||
|
{
|
||||||
|
conditionList << "IS NULL";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
conditionList << "= ?";
|
||||||
|
bindValues << pair.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return getRecords(conditionList.join((" ")), bindValues, results, tColumns, tOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DBManager::getRecords(const QString& condition, const QVariantList& bindValues, QVector<QVariantMap>& results, const QStringList& tColumns, const QStringList& tOrder) const
|
||||||
{
|
{
|
||||||
QSqlDatabase idb = getDB();
|
QSqlDatabase idb = getDB();
|
||||||
QSqlQuery query(idb);
|
QSqlQuery query(idb);
|
||||||
@@ -253,20 +243,28 @@ bool DBManager::getRecords(QVector<QVariantMap>& results, const QStringList& tCo
|
|||||||
|
|
||||||
QString sColumns("*");
|
QString sColumns("*");
|
||||||
if(!tColumns.isEmpty())
|
if(!tColumns.isEmpty())
|
||||||
|
{
|
||||||
sColumns = tColumns.join(", ");
|
sColumns = tColumns.join(", ");
|
||||||
|
}
|
||||||
|
|
||||||
QString sOrder("");
|
QString sOrder("");
|
||||||
if(!tOrder.isEmpty())
|
if(!tOrder.isEmpty())
|
||||||
{
|
{
|
||||||
sOrder = " ORDER BY ";
|
sOrder = "ORDER BY ";
|
||||||
sOrder.append(tOrder.join(", "));
|
sOrder.append(tOrder.join(", "));
|
||||||
}
|
}
|
||||||
|
|
||||||
query.prepare(QString("SELECT %1 FROM %2%3").arg(sColumns,_table,sOrder));
|
// prep conditions
|
||||||
|
QString prepCond;
|
||||||
if(!query.exec())
|
if(!condition.isEmpty())
|
||||||
|
{
|
||||||
|
prepCond = QString("WHERE %1").arg(condition);
|
||||||
|
}
|
||||||
|
|
||||||
|
query.prepare(QString("SELECT %1 FROM %2 %3 %4").arg(sColumns,_table, prepCond, sOrder));
|
||||||
|
addBindValues(query, bindValues);
|
||||||
|
if (!executeQuery(query))
|
||||||
{
|
{
|
||||||
Error(_log, "Failed to get records: '%s' in table: '%s' Error: %s", QSTRING_CSTR(sColumns), QSTRING_CSTR(_table), QSTRING_CSTR(idb.lastError().text()));
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,11 +286,6 @@ bool DBManager::getRecords(QVector<QVariantMap>& results, const QStringList& tCo
|
|||||||
|
|
||||||
bool DBManager::deleteRecord(const VectorPair& conditions) const
|
bool DBManager::deleteRecord(const VectorPair& conditions) const
|
||||||
{
|
{
|
||||||
if ( _readonlyMode )
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(conditions.isEmpty())
|
if(conditions.isEmpty())
|
||||||
{
|
{
|
||||||
Error(_log, "Oops, a deleteRecord() call wants to delete the entire table (%s)! Denied it", QSTRING_CSTR(_table));
|
Error(_log, "Oops, a deleteRecord() call wants to delete the entire table (%s)! Denied it", QSTRING_CSTR(_table));
|
||||||
@@ -310,29 +303,20 @@ bool DBManager::deleteRecord(const VectorPair& conditions) const
|
|||||||
|
|
||||||
for(const auto& pair : conditions)
|
for(const auto& pair : conditions)
|
||||||
{
|
{
|
||||||
prepCond << pair.first+"=?";
|
prepCond << pair.first+"= ?";
|
||||||
bindValues << pair.second;
|
bindValues << pair.second;
|
||||||
}
|
}
|
||||||
|
|
||||||
query.prepare(QString("DELETE FROM %1 %2").arg(_table,prepCond.join(" ")));
|
query.prepare(QString("DELETE FROM %1 %2").arg(_table,prepCond.join(" ")));
|
||||||
doAddBindValue(query, bindValues);
|
addBindValues(query, bindValues);
|
||||||
if(!query.exec())
|
|
||||||
{
|
return executeQuery(query);
|
||||||
Error(_log, "Failed to delete record: '%s' in table: '%s' Error: %s", QSTRING_CSTR(prepCond.join(" ")), QSTRING_CSTR(_table), QSTRING_CSTR(idb.lastError().text()));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DBManager::createTable(QStringList& columns) const
|
bool DBManager::createTable(QStringList& columns) const
|
||||||
{
|
{
|
||||||
if ( _readonlyMode )
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(columns.isEmpty())
|
if(columns.isEmpty())
|
||||||
{
|
{
|
||||||
Error(_log,"Empty tables aren't supported!");
|
Error(_log,"Empty tables aren't supported!");
|
||||||
@@ -347,9 +331,9 @@ bool DBManager::createTable(QStringList& columns) const
|
|||||||
// empty tables aren't supported by sqlite, add one column
|
// empty tables aren't supported by sqlite, add one column
|
||||||
QString tcolumn = columns.takeFirst();
|
QString tcolumn = columns.takeFirst();
|
||||||
// default CURRENT_TIMESTAMP is not supported by ALTER TABLE
|
// default CURRENT_TIMESTAMP is not supported by ALTER TABLE
|
||||||
if(!query.exec(QString("CREATE TABLE %1 ( %2 )").arg(_table,tcolumn)))
|
query.prepare(QString("CREATE TABLE %1 ( %2 )").arg(_table,tcolumn));
|
||||||
|
if (!executeQuery(query))
|
||||||
{
|
{
|
||||||
Error(_log, "Failed to create table: '%s' Error: %s", QSTRING_CSTR(_table), QSTRING_CSTR(idb.lastError().text()));
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -358,8 +342,8 @@ bool DBManager::createTable(QStringList& columns) const
|
|||||||
int err = 0;
|
int err = 0;
|
||||||
for(const auto& column : columns)
|
for(const auto& column : columns)
|
||||||
{
|
{
|
||||||
QStringList id = column.split(' ');
|
QStringList columName = column.split(' ');
|
||||||
if(rec.indexOf(id.at(0)) == -1)
|
if(rec.indexOf(columName.at(0)) == -1)
|
||||||
{
|
{
|
||||||
if(!createColumn(column))
|
if(!createColumn(column))
|
||||||
{
|
{
|
||||||
@@ -367,79 +351,91 @@ bool DBManager::createTable(QStringList& columns) const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(err)
|
return err == 0;
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DBManager::createColumn(const QString& column) const
|
bool DBManager::createColumn(const QString& column) const
|
||||||
{
|
{
|
||||||
if ( _readonlyMode )
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
QSqlDatabase idb = getDB();
|
QSqlDatabase idb = getDB();
|
||||||
QSqlQuery query(idb);
|
QSqlQuery query(idb);
|
||||||
if(!query.exec(QString("ALTER TABLE %1 ADD COLUMN %2").arg(_table,column)))
|
|
||||||
{
|
query.prepare(QString("ALTER TABLE %1 ADD COLUMN %2").arg(_table,column));
|
||||||
Error(_log, "Failed to create column: '%s' in table: '%s' Error: %s", QSTRING_CSTR(column), QSTRING_CSTR(_table), QSTRING_CSTR(idb.lastError().text()));
|
return executeQuery(query);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DBManager::tableExists(const QString& table) const
|
bool DBManager::tableExists(const QString& table) const
|
||||||
{
|
{
|
||||||
QSqlDatabase idb = getDB();
|
QSqlDatabase idb = getDB();
|
||||||
QStringList tables = idb.tables();
|
QStringList tables = idb.tables();
|
||||||
if(tables.contains(table))
|
return tables.contains(table);
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DBManager::deleteTable(const QString& table) const
|
bool DBManager::deleteTable(const QString& table) const
|
||||||
{
|
{
|
||||||
if ( _readonlyMode )
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(tableExists(table))
|
if(tableExists(table))
|
||||||
{
|
{
|
||||||
QSqlDatabase idb = getDB();
|
QSqlDatabase idb = getDB();
|
||||||
QSqlQuery query(idb);
|
QSqlQuery query(idb);
|
||||||
if(!query.exec(QString("DROP TABLE %1").arg(table)))
|
|
||||||
{
|
query.prepare(QString("DROP TABLE %1").arg(table));
|
||||||
Error(_log, "Failed to delete table: '%s' Error: %s", QSTRING_CSTR(table), QSTRING_CSTR(idb.lastError().text()));
|
return executeQuery(query);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DBManager::doAddBindValue(QSqlQuery& query, const QVariantList& variants) const
|
void DBManager::addBindValues(QSqlQuery& query, const QVariantList& bindValues) const
|
||||||
{
|
{
|
||||||
for(const auto& variant : variants)
|
if (!bindValues.isEmpty())
|
||||||
{
|
{
|
||||||
auto t = variant.userType();
|
for(const auto& value : bindValues)
|
||||||
switch(t)
|
|
||||||
{
|
{
|
||||||
case QMetaType::UInt:
|
query.addBindValue(value);
|
||||||
case QMetaType::Int:
|
|
||||||
case QMetaType::Bool:
|
|
||||||
query.addBindValue(variant.toInt());
|
|
||||||
break;
|
|
||||||
case QMetaType::Double:
|
|
||||||
query.addBindValue(variant.toFloat());
|
|
||||||
break;
|
|
||||||
case QMetaType::QByteArray:
|
|
||||||
query.addBindValue(variant.toByteArray());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
query.addBindValue(variant.toString());
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString DBManager::constructExecutedQuery(const QSqlQuery& query) const
|
||||||
|
{
|
||||||
|
QString executedQuery = query.executedQuery();
|
||||||
|
|
||||||
|
// Check if the query uses positional placeholders
|
||||||
|
if (executedQuery.contains('?')) {
|
||||||
|
QVariantList boundValues = query.boundValues(); // Get bound values as a list
|
||||||
|
// Iterate through the bound values and replace placeholders
|
||||||
|
for (const QVariant &value : boundValues) {
|
||||||
|
// Replace the first occurrence of '?' with the actual value
|
||||||
|
QString valueStr;
|
||||||
|
if (value.canConvert<QString>())
|
||||||
|
{
|
||||||
|
valueStr = value.toString();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
valueStr = "Unkown";
|
||||||
|
}
|
||||||
|
executedQuery.replace(executedQuery.indexOf('?'), 1, valueStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return executedQuery;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DBManager::executeQuery(QSqlQuery& query) const
|
||||||
|
{
|
||||||
|
if(!query.exec())
|
||||||
|
{
|
||||||
|
QString finalQuery = constructExecutedQuery(query);
|
||||||
|
QString errorText = query.lastError().text();
|
||||||
|
|
||||||
|
Debug(_log, "Database Error: '%s', SqlQuery: '%s'", QSTRING_CSTR(errorText), QSTRING_CSTR(finalQuery));
|
||||||
|
Error(_log, "Database Error: '%s'", QSTRING_CSTR(errorText));
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef SQLQUERY_LOGGING
|
||||||
|
QString finalQuery = constructExecutedQuery(query);
|
||||||
|
Debug(_log, "SqlQuery executed: '%s'", QSTRING_CSTR(finalQuery));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
70
libsrc/db/DB_schema/schema-config-exchange.json
Normal file
70
libsrc/db/DB_schema/schema-config-exchange.json
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"required": true,
|
||||||
|
"properties": {
|
||||||
|
"global": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"settings": {
|
||||||
|
"type": "object",
|
||||||
|
"required": true,
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": [
|
||||||
|
"object",
|
||||||
|
"array"
|
||||||
|
],
|
||||||
|
"properties": {},
|
||||||
|
"additionalProperties": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"uuid": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "uuid",
|
||||||
|
"required": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"instanceIds": {
|
||||||
|
"type": "array",
|
||||||
|
"required": false,
|
||||||
|
"items": {
|
||||||
|
"type": "integer"
|
||||||
|
},
|
||||||
|
"minItems": 1
|
||||||
|
},
|
||||||
|
"instances": {
|
||||||
|
"type": "array",
|
||||||
|
"required": false,
|
||||||
|
"items": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"enabled": {
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
|
"id": {
|
||||||
|
"type": "integer",
|
||||||
|
"minimum": 0,
|
||||||
|
"maximum": 255
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string",
|
||||||
|
"minLength": 5
|
||||||
|
},
|
||||||
|
"settings": {
|
||||||
|
"type": "object",
|
||||||
|
"required": true,
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": [
|
||||||
|
"object",
|
||||||
|
"array"
|
||||||
|
],
|
||||||
|
"properties": {},
|
||||||
|
"additionalProperties": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": true
|
||||||
|
}
|
5
libsrc/db/DB_schemas.qrc
Normal file
5
libsrc/db/DB_schemas.qrc
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
<RCC>
|
||||||
|
<qresource prefix="/">
|
||||||
|
<file alias="schema-config-exchange">DB_schema/schema-config-exchange.json</file>
|
||||||
|
</qresource>
|
||||||
|
</RCC>
|
142
libsrc/db/InstanceTable.cpp
Normal file
142
libsrc/db/InstanceTable.cpp
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
|
||||||
|
// db
|
||||||
|
#include <db/InstanceTable.h>
|
||||||
|
#include <db/SettingsTable.h>
|
||||||
|
|
||||||
|
// qt
|
||||||
|
#include <QDateTime>
|
||||||
|
|
||||||
|
InstanceTable::InstanceTable(QObject* parent)
|
||||||
|
: DBManager(parent)
|
||||||
|
{
|
||||||
|
// Init instance table
|
||||||
|
setTable("instances");
|
||||||
|
createTable(QStringList()<<"instance INTEGER"<<"friendly_name TEXT"<<"enabled INTEGER DEFAULT 0"<<"last_use TEXT");
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InstanceTable::createInstance(const QString& name, quint8& inst)
|
||||||
|
{
|
||||||
|
// check duplicate
|
||||||
|
if(!recordExists({{"friendly_name", name}}))
|
||||||
|
{
|
||||||
|
QList<quint8> instanceList = getAllInstanceIDs(false);
|
||||||
|
|
||||||
|
inst = 0;
|
||||||
|
while (instanceList.contains(inst))
|
||||||
|
{
|
||||||
|
++inst;
|
||||||
|
}
|
||||||
|
|
||||||
|
// create
|
||||||
|
QVariantMap data;
|
||||||
|
data["friendly_name"] = name;
|
||||||
|
data["instance"] = inst;
|
||||||
|
return createRecord({}, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InstanceTable::deleteInstance(quint8 inst)
|
||||||
|
{
|
||||||
|
Debug(_log,"");
|
||||||
|
if(deleteRecord({{"instance",inst}}))
|
||||||
|
{
|
||||||
|
// delete settings entries
|
||||||
|
SettingsTable settingsTable(inst);
|
||||||
|
settingsTable.deleteInstance();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InstanceTable::saveName(quint8 inst, const QString& name)
|
||||||
|
{
|
||||||
|
// check duplicate
|
||||||
|
if(!recordExists({{"friendly_name", name}}))
|
||||||
|
{
|
||||||
|
if(instanceExist(inst))
|
||||||
|
{
|
||||||
|
return updateRecord({{"instance",inst}}, {{"friendly_name", name}});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVector<QVariantMap> InstanceTable::getAllInstances(bool onlyEnabled)
|
||||||
|
{
|
||||||
|
QVector<QVariantMap> results;
|
||||||
|
|
||||||
|
VectorPair onlyEnabledCondition {};
|
||||||
|
if (onlyEnabled)
|
||||||
|
{
|
||||||
|
onlyEnabledCondition = {{"enabled", true}};
|
||||||
|
}
|
||||||
|
getRecords(onlyEnabledCondition, results, {}, {"instance ASC"});
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<quint8> InstanceTable::getAllInstanceIDs (bool onlyEnabled)
|
||||||
|
{
|
||||||
|
QVector<QVariantMap> instanceList = getAllInstances(onlyEnabled);
|
||||||
|
QList<quint8> instanceIds;
|
||||||
|
for (const QVariantMap &idx : std::as_const(instanceList))
|
||||||
|
{
|
||||||
|
instanceIds.append(static_cast<quint8>(idx.value("instance").toInt()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return instanceIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InstanceTable::instanceExist(quint8 inst)
|
||||||
|
{
|
||||||
|
return recordExists({{"instance",inst}});
|
||||||
|
}
|
||||||
|
|
||||||
|
QString InstanceTable::getNamebyIndex(quint8 index)
|
||||||
|
{
|
||||||
|
QVariantMap results;
|
||||||
|
getRecord({{"instance", index}}, results, {"friendly_name"});
|
||||||
|
|
||||||
|
QString name = results["friendly_name"].toString();
|
||||||
|
return name.isEmpty() ? "NOT FOUND" : name;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InstanceTable::setLastUse(quint8 inst)
|
||||||
|
{
|
||||||
|
return updateRecord({{"instance", inst}}, {{"last_use", QDateTime::currentDateTimeUtc().toString(Qt::ISODate)}});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InstanceTable::setEnable(quint8 inst, bool newState)
|
||||||
|
{
|
||||||
|
return updateRecord({{"instance", inst}}, {{"enabled", newState}});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InstanceTable::isEnabled(quint8 inst)
|
||||||
|
{
|
||||||
|
QVariantMap results;
|
||||||
|
getRecord({{"instance", inst}}, results);
|
||||||
|
|
||||||
|
return results["enabled"].toBool();
|
||||||
|
}
|
||||||
|
|
||||||
|
void InstanceTable::createDefaultInstance()
|
||||||
|
{
|
||||||
|
if(instanceExist(0))
|
||||||
|
{
|
||||||
|
setEnable(0, true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(createRecord({{"instance", 0}}, {{"friendly_name", "First LED Hardware instance"}}))
|
||||||
|
{
|
||||||
|
setEnable(0, true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Failed to create Hyperion root instance in db! This should never be the case...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
47
libsrc/db/MetaTable.cpp
Normal file
47
libsrc/db/MetaTable.cpp
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
|
||||||
|
#include <db/MetaTable.h>
|
||||||
|
|
||||||
|
// qt
|
||||||
|
#include <QDateTime>
|
||||||
|
#include <QUuid>
|
||||||
|
#include <QNetworkInterface>
|
||||||
|
#include <QCryptographicHash>
|
||||||
|
|
||||||
|
MetaTable::MetaTable(QObject* parent)
|
||||||
|
: DBManager(parent)
|
||||||
|
{
|
||||||
|
setTable("meta");
|
||||||
|
createTable(QStringList()<<"uuid TEXT"<<"created_at TEXT");
|
||||||
|
};
|
||||||
|
|
||||||
|
QString MetaTable::getUUID() const
|
||||||
|
{
|
||||||
|
QVector<QVariantMap> results;
|
||||||
|
getRecords(results, QStringList() << "uuid");
|
||||||
|
|
||||||
|
for(const auto & entry : std::as_const(results))
|
||||||
|
{
|
||||||
|
if(!entry["uuid"].toString().isEmpty())
|
||||||
|
{
|
||||||
|
return entry["uuid"].toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// create new uuidv5 based on net adapter MAC, save to db and return
|
||||||
|
QString hash;
|
||||||
|
foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
|
||||||
|
{
|
||||||
|
if (!(interface.flags() & QNetworkInterface::IsLoopBack))
|
||||||
|
{
|
||||||
|
hash = QCryptographicHash::hash(interface.hardwareAddress().toLocal8Bit(),QCryptographicHash::Sha1).toHex();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const QString newUuid = QUuid::createUuidV5(QUuid(), hash).toString().mid(1, 36);
|
||||||
|
QVariantMap map;
|
||||||
|
map["created_at"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
|
||||||
|
|
||||||
|
createRecord({{"uuid",newUuid}}, map);
|
||||||
|
|
||||||
|
return newUuid;
|
||||||
|
}
|
99
libsrc/db/SettingsTable.cpp
Normal file
99
libsrc/db/SettingsTable.cpp
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
#include <db/SettingsTable.h>
|
||||||
|
|
||||||
|
#include <QDateTime>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
|
||||||
|
SettingsTable::SettingsTable(quint8 instance, QObject* parent)
|
||||||
|
: DBManager(parent)
|
||||||
|
, _hyperion_inst(instance)
|
||||||
|
{
|
||||||
|
setTable("settings");
|
||||||
|
// create table columns
|
||||||
|
createTable(QStringList()<<"type TEXT"<<"config TEXT"<<"hyperion_inst INTEGER"<<"updated_at TEXT");
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SettingsTable::createSettingsRecord(const QString& type, const QString& config) const
|
||||||
|
{
|
||||||
|
QVariantMap map;
|
||||||
|
map["config"] = config;
|
||||||
|
map["updated_at"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
|
||||||
|
|
||||||
|
VectorPair cond;
|
||||||
|
cond.append(CPair("type",type));
|
||||||
|
// when a setting is not global we are searching also for the instance
|
||||||
|
if(!isSettingGlobal(type))
|
||||||
|
{
|
||||||
|
cond.append(CPair("AND hyperion_inst",_hyperion_inst));
|
||||||
|
}
|
||||||
|
return createRecord(cond, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SettingsTable::recordExist(const QString& type) const
|
||||||
|
{
|
||||||
|
VectorPair cond;
|
||||||
|
cond.append(CPair("type",type));
|
||||||
|
// when a setting is not global we are searching also for the instance
|
||||||
|
if(!isSettingGlobal(type))
|
||||||
|
{
|
||||||
|
cond.append(CPair("AND hyperion_inst",_hyperion_inst));
|
||||||
|
}
|
||||||
|
return recordExists(cond);
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonDocument SettingsTable::getSettingsRecord(const QString& type) const
|
||||||
|
{
|
||||||
|
QVariantMap results;
|
||||||
|
VectorPair cond;
|
||||||
|
cond.append(CPair("type",type));
|
||||||
|
// when a setting is not global we are searching also for the instance
|
||||||
|
if(!isSettingGlobal(type))
|
||||||
|
{
|
||||||
|
cond.append(CPair("AND hyperion_inst",_hyperion_inst));
|
||||||
|
}
|
||||||
|
getRecord(cond, results, QStringList("config"));
|
||||||
|
return QJsonDocument::fromJson(results["config"].toByteArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
QString SettingsTable::getSettingsRecordString(const QString& type) const
|
||||||
|
{
|
||||||
|
QVariantMap results;
|
||||||
|
VectorPair cond;
|
||||||
|
cond.append(CPair("type",type));
|
||||||
|
// when a setting is not global we are searching also for the instance
|
||||||
|
if(!isSettingGlobal(type))
|
||||||
|
{
|
||||||
|
cond.append(CPair("AND hyperion_inst",_hyperion_inst));
|
||||||
|
}
|
||||||
|
getRecord(cond, results, QStringList("config"));
|
||||||
|
return results["config"].toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SettingsTable::deleteInstance() const
|
||||||
|
{
|
||||||
|
deleteRecord({{"hyperion_inst",_hyperion_inst}});
|
||||||
|
}
|
||||||
|
|
||||||
|
const QVector<QString>& SettingsTable::getGlobalSettingTypes() {
|
||||||
|
static const QVector<QString> globalSettingTypes = {
|
||||||
|
"jsonServer",
|
||||||
|
"protoServer",
|
||||||
|
"flatbufServer",
|
||||||
|
"forwarder",
|
||||||
|
"webConfig",
|
||||||
|
"network",
|
||||||
|
"framegrabber",
|
||||||
|
"grabberV4L2",
|
||||||
|
"grabberAudio",
|
||||||
|
"osEvents",
|
||||||
|
"cecEvents",
|
||||||
|
"schedEvents",
|
||||||
|
"general",
|
||||||
|
"logger"
|
||||||
|
};
|
||||||
|
return globalSettingTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SettingsTable::isSettingGlobal(const QString& type)
|
||||||
|
{
|
||||||
|
return getGlobalSettingTypes().contains(type);
|
||||||
|
}
|
@@ -10,10 +10,10 @@
|
|||||||
|
|
||||||
AuthManager *AuthManager::manager = nullptr;
|
AuthManager *AuthManager::manager = nullptr;
|
||||||
|
|
||||||
AuthManager::AuthManager(QObject *parent, bool readonlyMode)
|
AuthManager::AuthManager(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, _authTable(new AuthTable("", this, readonlyMode))
|
, _authTable(new AuthTable(this))
|
||||||
, _metaTable(new MetaTable(this, readonlyMode))
|
, _metaTable(new MetaTable(this))
|
||||||
, _pendingRequests()
|
, _pendingRequests()
|
||||||
, _timer(new QTimer(this))
|
, _timer(new QTimer(this))
|
||||||
, _authBlockTimer(new QTimer(this))
|
, _authBlockTimer(new QTimer(this))
|
||||||
@@ -209,7 +209,7 @@ QVector<AuthManager::AuthDefinition> AuthManager::getPendingRequests() const
|
|||||||
|
|
||||||
bool AuthManager::renameToken(const QString &id, const QString &comment)
|
bool AuthManager::renameToken(const QString &id, const QString &comment)
|
||||||
{
|
{
|
||||||
if (_authTable->idExist(id))
|
if (_authTable->identifierExist(id))
|
||||||
{
|
{
|
||||||
if (_authTable->renameToken(id, comment))
|
if (_authTable->renameToken(id, comment))
|
||||||
{
|
{
|
||||||
@@ -222,7 +222,7 @@ bool AuthManager::renameToken(const QString &id, const QString &comment)
|
|||||||
|
|
||||||
bool AuthManager::deleteToken(const QString &id)
|
bool AuthManager::deleteToken(const QString &id)
|
||||||
{
|
{
|
||||||
if (_authTable->idExist(id))
|
if (_authTable->identifierExist(id))
|
||||||
{
|
{
|
||||||
if (_authTable->deleteToken(id))
|
if (_authTable->deleteToken(id))
|
||||||
{
|
{
|
||||||
|
@@ -47,10 +47,10 @@
|
|||||||
#include <boblightserver/BoblightServer.h>
|
#include <boblightserver/BoblightServer.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Hyperion::Hyperion(quint8 instance, bool readonlyMode)
|
Hyperion::Hyperion(quint8 instance)
|
||||||
: QObject()
|
: QObject()
|
||||||
, _instIndex(instance)
|
, _instIndex(instance)
|
||||||
, _settingsManager(new SettingsManager(instance, this, readonlyMode))
|
, _settingsManager(new SettingsManager(instance, this))
|
||||||
, _componentRegister(nullptr)
|
, _componentRegister(nullptr)
|
||||||
, _ledString(LedString::createLedString(getSetting(settings::LEDS).array(), hyperion::createColorOrder(getSetting(settings::DEVICE).object())))
|
, _ledString(LedString::createLedString(getSetting(settings::LEDS).array(), hyperion::createColorOrder(getSetting(settings::DEVICE).object())))
|
||||||
, _imageProcessor(nullptr)
|
, _imageProcessor(nullptr)
|
||||||
@@ -73,7 +73,6 @@ Hyperion::Hyperion(quint8 instance, bool readonlyMode)
|
|||||||
#if defined(ENABLE_BOBLIGHT_SERVER)
|
#if defined(ENABLE_BOBLIGHT_SERVER)
|
||||||
, _boblightServer(nullptr)
|
, _boblightServer(nullptr)
|
||||||
#endif
|
#endif
|
||||||
, _readOnlyMode(readonlyMode)
|
|
||||||
{
|
{
|
||||||
qRegisterMetaType<ComponentList>("ComponentList");
|
qRegisterMetaType<ComponentList>("ComponentList");
|
||||||
|
|
||||||
@@ -320,16 +319,22 @@ QJsonDocument Hyperion::getSetting(settings::type type) const
|
|||||||
return _settingsManager->getSetting(type);
|
return _settingsManager->getSetting(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Remove function, if UI is able to handle full configuration
|
||||||
|
QJsonObject Hyperion::getQJsonConfig() const
|
||||||
|
{
|
||||||
|
const QJsonObject instanceConfig = _settingsManager->getSettings();
|
||||||
|
const QJsonObject globalConfig = _settingsManager->getSettings({},QStringList());
|
||||||
|
|
||||||
|
QVariantMap map = instanceConfig.toVariantMap();
|
||||||
|
map.insert(globalConfig.toVariantMap());
|
||||||
|
return QJsonObject::fromVariantMap(map);
|
||||||
|
}
|
||||||
|
|
||||||
bool Hyperion::saveSettings(const QJsonObject& config, bool correct)
|
bool Hyperion::saveSettings(const QJsonObject& config, bool correct)
|
||||||
{
|
{
|
||||||
return _settingsManager->saveSettings(config, correct);
|
return _settingsManager->saveSettings(config, correct);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Hyperion::restoreSettings(const QJsonObject& config, bool correct)
|
|
||||||
{
|
|
||||||
return _settingsManager->restoreSettings(config, correct);
|
|
||||||
}
|
|
||||||
|
|
||||||
int Hyperion::getLatchTime() const
|
int Hyperion::getLatchTime() const
|
||||||
{
|
{
|
||||||
return _ledDeviceWrapper->getLatchTime();
|
return _ledDeviceWrapper->getLatchTime();
|
||||||
@@ -597,11 +602,6 @@ int Hyperion::setEffect(const QString &effectName, const QJsonObject &args, int
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
QJsonObject Hyperion::getQJsonConfig() const
|
|
||||||
{
|
|
||||||
return _settingsManager->getSettings();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Hyperion::setLedMappingType(int mappingType)
|
void Hyperion::setLedMappingType(int mappingType)
|
||||||
{
|
{
|
||||||
if(mappingType != _imageProcessor->getUserLedMappingType())
|
if(mappingType != _imageProcessor->getUserLedMappingType())
|
||||||
|
@@ -9,15 +9,14 @@
|
|||||||
|
|
||||||
HyperionIManager* HyperionIManager::HIMinstance;
|
HyperionIManager* HyperionIManager::HIMinstance;
|
||||||
|
|
||||||
HyperionIManager::HyperionIManager(const QString& rootPath, QObject* parent, bool readonlyMode)
|
HyperionIManager::HyperionIManager(QObject* parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, _log(Logger::getInstance("HYPERION-INSTMGR"))
|
, _log(Logger::getInstance("HYPERION-INSTMGR"))
|
||||||
, _instanceTable( new InstanceTable(rootPath, this, readonlyMode) )
|
, _instanceTable( new InstanceTable())
|
||||||
, _rootPath( rootPath )
|
|
||||||
, _readonlyMode(readonlyMode)
|
|
||||||
{
|
{
|
||||||
HIMinstance = this;
|
HIMinstance = this;
|
||||||
qRegisterMetaType<InstanceState>("InstanceState");
|
qRegisterMetaType<InstanceState>("InstanceState");
|
||||||
|
_instanceTable->createDefaultInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
Hyperion* HyperionIManager::getHyperionInstance(quint8 instance)
|
Hyperion* HyperionIManager::getHyperionInstance(quint8 instance)
|
||||||
@@ -45,14 +44,32 @@ QVector<QVariantMap> HyperionIManager::getInstanceData() const
|
|||||||
return instances;
|
return instances;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString HyperionIManager::getInstanceName(quint8 inst)
|
||||||
|
{
|
||||||
|
return _instanceTable->getNamebyIndex(inst);
|
||||||
|
}
|
||||||
|
|
||||||
QList<quint8> HyperionIManager::getRunningInstanceIdx() const
|
QList<quint8> HyperionIManager::getRunningInstanceIdx() const
|
||||||
{
|
{
|
||||||
return _runningInstances.keys();
|
return _runningInstances.keys();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QList<quint8> HyperionIManager::getInstanceIds() const
|
||||||
|
{
|
||||||
|
return _instanceTable->getAllInstanceIDs();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void HyperionIManager::startAll()
|
void HyperionIManager::startAll()
|
||||||
{
|
{
|
||||||
for(const auto & entry : _instanceTable->getAllInstances(true))
|
const QVector<QVariantMap> instances = _instanceTable->getAllInstances(true);
|
||||||
|
if (instances.isEmpty())
|
||||||
|
{
|
||||||
|
Error(_log, "No enabled instances found to be started");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(const auto & entry : instances)
|
||||||
{
|
{
|
||||||
startInstance(entry["instance"].toInt());
|
startInstance(entry["instance"].toInt());
|
||||||
}
|
}
|
||||||
@@ -62,7 +79,7 @@ void HyperionIManager::stopAll()
|
|||||||
{
|
{
|
||||||
// copy the instances due to loop corruption, even with .erase() return next iter
|
// copy the instances due to loop corruption, even with .erase() return next iter
|
||||||
QMap<quint8, Hyperion*> instCopy = _runningInstances;
|
QMap<quint8, Hyperion*> instCopy = _runningInstances;
|
||||||
for(const auto instance : instCopy)
|
for(auto *const instance : instCopy)
|
||||||
{
|
{
|
||||||
instance->stop();
|
instance->stop();
|
||||||
}
|
}
|
||||||
@@ -131,7 +148,7 @@ bool HyperionIManager::startInstance(quint8 inst, bool block, QObject* caller, i
|
|||||||
{
|
{
|
||||||
QThread* hyperionThread = new QThread();
|
QThread* hyperionThread = new QThread();
|
||||||
hyperionThread->setObjectName("HyperionThread");
|
hyperionThread->setObjectName("HyperionThread");
|
||||||
Hyperion* hyperion = new Hyperion(inst, _readonlyMode);
|
Hyperion* hyperion = new Hyperion(inst);
|
||||||
hyperion->moveToThread(hyperionThread);
|
hyperion->moveToThread(hyperionThread);
|
||||||
// setup thread management
|
// setup thread management
|
||||||
connect(hyperionThread, &QThread::started, hyperion, &Hyperion::start);
|
connect(hyperionThread, &QThread::started, hyperion, &Hyperion::start);
|
||||||
@@ -156,7 +173,7 @@ bool HyperionIManager::startInstance(quint8 inst, bool block, QObject* caller, i
|
|||||||
|
|
||||||
if(block)
|
if(block)
|
||||||
{
|
{
|
||||||
while(!hyperionThread->isRunning()){};
|
while(!hyperionThread->isRunning()){}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_pendingRequests.contains(inst) && caller != nullptr)
|
if (!_pendingRequests.contains(inst) && caller != nullptr)
|
||||||
@@ -203,10 +220,10 @@ bool HyperionIManager::stopInstance(quint8 inst)
|
|||||||
|
|
||||||
bool HyperionIManager::createInstance(const QString& name, bool start)
|
bool HyperionIManager::createInstance(const QString& name, bool start)
|
||||||
{
|
{
|
||||||
quint8 inst;
|
quint8 inst = 0;
|
||||||
if(_instanceTable->createInstance(name, inst))
|
if(_instanceTable->createInstance(name, inst))
|
||||||
{
|
{
|
||||||
Info(_log,"New Hyperion instance created with name '%s'",QSTRING_CSTR(name));
|
Info(_log,"New Hyperion instance [%d] created with name '%s'", inst, QSTRING_CSTR(name));
|
||||||
emit instanceStateChanged(InstanceState::H_CREATED, inst, name);
|
emit instanceStateChanged(InstanceState::H_CREATED, inst, name);
|
||||||
emit change();
|
emit change();
|
||||||
|
|
||||||
@@ -221,7 +238,9 @@ bool HyperionIManager::deleteInstance(quint8 inst)
|
|||||||
{
|
{
|
||||||
// inst 0 can't be deleted
|
// inst 0 can't be deleted
|
||||||
if(!isInstAllowed(inst))
|
if(!isInstAllowed(inst))
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// stop it if required as blocking and wait
|
// stop it if required as blocking and wait
|
||||||
stopInstance(inst);
|
stopInstance(inst);
|
||||||
|
@@ -12,30 +12,20 @@
|
|||||||
#include <utils/jsonschema/QJsonFactory.h>
|
#include <utils/jsonschema/QJsonFactory.h>
|
||||||
#include <utils/jsonschema/QJsonSchemaChecker.h>
|
#include <utils/jsonschema/QJsonSchemaChecker.h>
|
||||||
|
|
||||||
// write config to filesystem
|
|
||||||
#include <utils/JsonUtils.h>
|
|
||||||
|
|
||||||
#include <utils/version.hpp>
|
#include <utils/version.hpp>
|
||||||
|
|
||||||
using namespace semver;
|
using namespace semver;
|
||||||
|
|
||||||
// Constants
|
|
||||||
namespace {
|
|
||||||
const char DEFAULT_VERSION[] = "2.0.0-alpha.8";
|
|
||||||
} //End of constants
|
|
||||||
|
|
||||||
QJsonObject SettingsManager::schemaJson;
|
QJsonObject SettingsManager::schemaJson;
|
||||||
|
|
||||||
SettingsManager::SettingsManager(quint8 instance, QObject* parent, bool readonlyMode)
|
SettingsManager::SettingsManager(quint8 instance, QObject* parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, _log(Logger::getInstance("SETTINGSMGR", "I" + QString::number(instance)))
|
, _log(Logger::getInstance("SETTINGSMGR", "I" + QString::number(instance)))
|
||||||
, _instance(instance)
|
, _instance(instance)
|
||||||
, _sTable(new SettingsTable(instance, this))
|
, _sTable(new SettingsTable(instance, this))
|
||||||
, _configVersion(DEFAULT_VERSION)
|
, _configVersion(DEFAULT_VERSION)
|
||||||
, _previousVersion(DEFAULT_VERSION)
|
, _previousVersion(DEFAULT_VERSION)
|
||||||
, _readonlyMode(readonlyMode)
|
|
||||||
{
|
{
|
||||||
_sTable->setReadonlyMode(_readonlyMode);
|
|
||||||
// get schema
|
// get schema
|
||||||
if (schemaJson.isEmpty())
|
if (schemaJson.isEmpty())
|
||||||
{
|
{
|
||||||
@@ -188,26 +178,75 @@ SettingsManager::SettingsManager(quint8 instance, QObject* parent, bool readonly
|
|||||||
|
|
||||||
QJsonDocument SettingsManager::getSetting(settings::type type) const
|
QJsonDocument SettingsManager::getSetting(settings::type type) const
|
||||||
{
|
{
|
||||||
return _sTable->getSettingsRecord(settings::typeToString(type));
|
return getSetting(settings::typeToString(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonObject SettingsManager::getSettings() const
|
QJsonDocument SettingsManager::getSetting(const QString& type) const
|
||||||
{
|
{
|
||||||
QJsonObject config;
|
return _sTable->getSettingsRecord(type);
|
||||||
for (const auto& key : _qconfig.keys())
|
}
|
||||||
|
|
||||||
|
QJsonObject SettingsManager::getSettings(const QStringList& filteredTypes ) const
|
||||||
|
{
|
||||||
|
return getSettings(_instance, filteredTypes);
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject SettingsManager::getSettings(const QVariant& instance, const QStringList& filteredTypes ) const
|
||||||
|
{
|
||||||
|
QJsonObject settingsObject;
|
||||||
|
QStringList settingsKeys({ "type", "config" });
|
||||||
|
QString settingsCondition;
|
||||||
|
QVariantList conditionValues;
|
||||||
|
|
||||||
|
if (instance.isNull() )
|
||||||
{
|
{
|
||||||
//Read all records from database to ensure that global settings are read across instances
|
settingsCondition = "hyperion_inst IS NULL";
|
||||||
QJsonDocument doc = _sTable->getSettingsRecord(key);
|
}
|
||||||
if (doc.isArray())
|
else
|
||||||
{
|
{
|
||||||
config.insert(key, doc.array());
|
settingsCondition = "hyperion_inst = ?";
|
||||||
|
conditionValues.append(instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!filteredTypes.isEmpty())
|
||||||
|
{
|
||||||
|
QStringList seletedSettingTypes;
|
||||||
|
for (const auto &type : filteredTypes) {
|
||||||
|
seletedSettingTypes << QString("%1=?").arg("type");
|
||||||
|
conditionValues.append(type);
|
||||||
}
|
}
|
||||||
else
|
settingsCondition += QString (" AND (%1)").arg(seletedSettingTypes.join(" OR "));
|
||||||
|
}
|
||||||
|
|
||||||
|
QVector<QVariantMap> settingsList;
|
||||||
|
if (_sTable->getRecords(settingsCondition, conditionValues, settingsList, settingsKeys))
|
||||||
|
{
|
||||||
|
for (const QVariantMap &setting : std::as_const(settingsList))
|
||||||
{
|
{
|
||||||
config.insert(key, doc.object());
|
QString type = setting.value("type").toString();
|
||||||
|
QByteArray configObject = setting.value("config").toByteArray();
|
||||||
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(configObject);
|
||||||
|
|
||||||
|
if (!jsonDoc.isNull())
|
||||||
|
{
|
||||||
|
QJsonValue config;
|
||||||
|
|
||||||
|
if (jsonDoc.isArray())
|
||||||
|
{
|
||||||
|
config = jsonDoc.array();
|
||||||
|
}
|
||||||
|
else if (jsonDoc.isObject())
|
||||||
|
{
|
||||||
|
config = jsonDoc.object();
|
||||||
|
}
|
||||||
|
settingsObject.insert(type, config);
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
qWarning() << "Failed to parse JSON string:" << configObject;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return config;
|
return settingsObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SettingsManager::restoreSettings(QJsonObject config, bool correct)
|
bool SettingsManager::restoreSettings(QJsonObject config, bool correct)
|
||||||
@@ -299,7 +338,7 @@ inline QString fixVersion(const QString& version)
|
|||||||
return newVersion;
|
return newVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SettingsManager::resolveConfigVersion(QJsonObject& config)
|
bool SettingsManager::resolveConfigVersion(const QJsonObject& config)
|
||||||
{
|
{
|
||||||
bool isValid = false;
|
bool isValid = false;
|
||||||
if (config.contains("general"))
|
if (config.contains("general"))
|
||||||
|
@@ -7,6 +7,8 @@
|
|||||||
//qt includes
|
//qt includes
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
#include <QJsonArray>
|
||||||
|
#include <QJsonDocument>
|
||||||
#include <QJsonParseError>
|
#include <QJsonParseError>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
|
||||||
@@ -158,4 +160,31 @@ namespace JsonUtils {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString jsonValueToQString(const QJsonValue &value, QJsonDocument::JsonFormat format)
|
||||||
|
{
|
||||||
|
switch (value.type()) {
|
||||||
|
case QJsonValue::Object:
|
||||||
|
{
|
||||||
|
return QJsonDocument(value.toObject()).toJson(format);
|
||||||
|
}
|
||||||
|
case QJsonValue::Array:
|
||||||
|
{
|
||||||
|
return QJsonDocument(value.toArray()).toJson(format);
|
||||||
|
}
|
||||||
|
case QJsonValue::String:
|
||||||
|
case QJsonValue::Double:
|
||||||
|
case QJsonValue::Bool:
|
||||||
|
{
|
||||||
|
return value.toString();
|
||||||
|
}
|
||||||
|
case QJsonValue::Null:
|
||||||
|
{
|
||||||
|
return "Null";
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
@@ -65,14 +65,14 @@
|
|||||||
|
|
||||||
HyperionDaemon* HyperionDaemon::daemon = nullptr;
|
HyperionDaemon* HyperionDaemon::daemon = nullptr;
|
||||||
|
|
||||||
HyperionDaemon::HyperionDaemon(const QString& rootPath, QObject* parent, bool logLvlOverwrite, bool readonlyMode)
|
HyperionDaemon::HyperionDaemon(const QString& rootPath, QObject* parent, bool logLvlOverwrite)
|
||||||
: QObject(parent), _log(Logger::getInstance("DAEMON"))
|
: QObject(parent), _log(Logger::getInstance("DAEMON"))
|
||||||
, _instanceManager(new HyperionIManager(rootPath, this, readonlyMode))
|
, _instanceManager(new HyperionIManager(this))
|
||||||
, _settingsManager(new SettingsManager(GLOABL_INSTANCE_ID, this, readonlyMode)) // init settings, this settingsManager accesses global settings which are independent from instances
|
, _settingsManager(new SettingsManager(GLOABL_INSTANCE_ID, this)) // init settings, this settingsManager accesses global settings which are independent from instances
|
||||||
#if defined(ENABLE_EFFECTENGINE)
|
#if defined(ENABLE_EFFECTENGINE)
|
||||||
, _pyInit(new PythonInit())
|
, _pyInit(new PythonInit())
|
||||||
#endif
|
#endif
|
||||||
, _authManager(new AuthManager(this, readonlyMode))
|
, _authManager(new AuthManager(this))
|
||||||
, _netOrigin(new NetOrigin(this))
|
, _netOrigin(new NetOrigin(this))
|
||||||
, _currVideoMode(VideoMode::VIDEO_2D)
|
, _currVideoMode(VideoMode::VIDEO_2D)
|
||||||
{
|
{
|
||||||
|
@@ -108,7 +108,7 @@ class HyperionDaemon : public QObject
|
|||||||
friend SysTray;
|
friend SysTray;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
HyperionDaemon(const QString& rootPath, QObject *parent, bool logLvlOverwrite, bool readonlyMode = false);
|
HyperionDaemon(const QString& rootPath, QObject *parent, bool logLvlOverwrite);
|
||||||
~HyperionDaemon() override;
|
~HyperionDaemon() override;
|
||||||
|
|
||||||
///
|
///
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -35,6 +36,7 @@
|
|||||||
#include <commandline/Parser.h>
|
#include <commandline/Parser.h>
|
||||||
#include <commandline/IntOption.h>
|
#include <commandline/IntOption.h>
|
||||||
#include <utils/DefaultSignalHandler.h>
|
#include <utils/DefaultSignalHandler.h>
|
||||||
|
#include <db/ConfigImportExport.h>
|
||||||
#include <../../include/db/AuthTable.h>
|
#include <../../include/db/AuthTable.h>
|
||||||
|
|
||||||
#include "detectProcess.h"
|
#include "detectProcess.h"
|
||||||
@@ -131,7 +133,10 @@ int main(int argc, char** argv)
|
|||||||
BooleanOption & versionOption = parser.add<BooleanOption> (0x0, "version", "Show version information");
|
BooleanOption & versionOption = parser.add<BooleanOption> (0x0, "version", "Show version information");
|
||||||
Option & userDataOption = parser.add<Option> ('u', "userdata", "Overwrite user data path, defaults to home directory of current user (%1)", QDir::homePath() + "/.hyperion");
|
Option & userDataOption = parser.add<Option> ('u', "userdata", "Overwrite user data path, defaults to home directory of current user (%1)", QDir::homePath() + "/.hyperion");
|
||||||
BooleanOption & resetPassword = parser.add<BooleanOption> (0x0, "resetPassword", "Lost your password? Reset it with this option back to 'hyperion'");
|
BooleanOption & resetPassword = parser.add<BooleanOption> (0x0, "resetPassword", "Lost your password? Reset it with this option back to 'hyperion'");
|
||||||
|
BooleanOption & readOnlyModeOption = parser.add<BooleanOption> (0x0, "readonlyMode", "Start in read-only mode. No updates will be written to the database");
|
||||||
BooleanOption & deleteDB = parser.add<BooleanOption> (0x0, "deleteDatabase", "Start all over? This Option will delete the database");
|
BooleanOption & deleteDB = parser.add<BooleanOption> (0x0, "deleteDatabase", "Start all over? This Option will delete the database");
|
||||||
|
Option & importConfig = parser.add<Option> (0x0, "importConfig", "Replace the current configuration database by a new configuration");
|
||||||
|
Option & exportConfigPath = parser.add<Option> (0x0, "exportConfig", "Export the current configuration database, defaults to home directory of current user (%1)", QDir::homePath() + "/.hyperion//archive");
|
||||||
BooleanOption & silentOption = parser.add<BooleanOption> ('s', "silent", "Do not print any outputs");
|
BooleanOption & silentOption = parser.add<BooleanOption> ('s', "silent", "Do not print any outputs");
|
||||||
BooleanOption & verboseOption = parser.add<BooleanOption> ('v', "verbose", "Increase verbosity");
|
BooleanOption & verboseOption = parser.add<BooleanOption> ('v', "verbose", "Increase verbosity");
|
||||||
BooleanOption & debugOption = parser.add<BooleanOption> ('d', "debug", "Show debug messages");
|
BooleanOption & debugOption = parser.add<BooleanOption> ('d', "debug", "Show debug messages");
|
||||||
@@ -268,9 +273,16 @@ int main(int argc, char** argv)
|
|||||||
bool readonlyMode = false;
|
bool readonlyMode = false;
|
||||||
|
|
||||||
QString userDataPath(userDataOption.value(parser));
|
QString userDataPath(userDataOption.value(parser));
|
||||||
|
|
||||||
QDir userDataDirectory(userDataPath);
|
QDir userDataDirectory(userDataPath);
|
||||||
QFileInfo dbFile(userDataDirectory.absolutePath() +"/db/hyperion.db");
|
|
||||||
|
|
||||||
|
if (parser.isSet(readOnlyModeOption))
|
||||||
|
{
|
||||||
|
readonlyMode = true;
|
||||||
|
Debug(log,"Force readonlyMode");
|
||||||
|
}
|
||||||
|
DBManager::initializeDatabase(userDataDirectory, readonlyMode);
|
||||||
|
QFileInfo dbFile(DBManager::getFileInfo());
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -285,9 +297,32 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
readonlyMode = true;
|
readonlyMode = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(parser.isSet(exportConfigPath))
|
||||||
|
{
|
||||||
|
ConfigImportExport configExporter;
|
||||||
|
|
||||||
|
QString path = exportConfigPath.value(parser);
|
||||||
|
if (path.isEmpty())
|
||||||
|
{
|
||||||
|
path = userDataDirectory.absolutePath() + "/archive";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!configExporter.exportJson(path))
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Configuration export failed'");
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
if(parser.isSet(exportConfigPath))
|
||||||
|
{
|
||||||
|
throw std::runtime_error("The configuration cannot be exported. The database file '" + dbFile.absoluteFilePath().toStdString() + "' does not exist.'");
|
||||||
|
}
|
||||||
|
|
||||||
if (!userDataDirectory.mkpath(dbFile.absolutePath()))
|
if (!userDataDirectory.mkpath(dbFile.absolutePath()))
|
||||||
{
|
{
|
||||||
if (!userDataDirectory.isReadable() || !dbFile.isWritable())
|
if (!userDataDirectory.isReadable() || !dbFile.isWritable())
|
||||||
@@ -302,11 +337,11 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
if ( readonlyMode )
|
if ( readonlyMode )
|
||||||
{
|
{
|
||||||
Error(log,"Password reset is not possible. The user data path '%s' is not writeable.", QSTRING_CSTR(userDataDirectory.absolutePath()));
|
Error(log,"Password reset is not possible. Hyperion's database '%s' is not writeable.", QSTRING_CSTR(dbFile.absolutePath()));
|
||||||
throw std::runtime_error("Password reset failed");
|
throw std::runtime_error("Password reset failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
AuthTable* table = new AuthTable(userDataDirectory.absolutePath());
|
AuthTable* table = new AuthTable();
|
||||||
if(table->resetHyperionUser()){
|
if(table->resetHyperionUser()){
|
||||||
Info(log,"Password reset successful");
|
Info(log,"Password reset successful");
|
||||||
delete table;
|
delete table;
|
||||||
@@ -323,7 +358,7 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
if ( readonlyMode )
|
if ( readonlyMode )
|
||||||
{
|
{
|
||||||
Error(log,"Deleting the configuration database is not possible. The user data path '%s' is not writeable.", QSTRING_CSTR(dbFile.absolutePath()));
|
Error(log,"Deleting the configuration database is not possible. Hyperion's database '%s' is not writeable.", QSTRING_CSTR(dbFile.absolutePath()));
|
||||||
throw std::runtime_error("Deleting the configuration database failed");
|
throw std::runtime_error("Deleting the configuration database failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -331,7 +366,7 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
if (!QFile::remove(dbFile.absoluteFilePath()))
|
if (!QFile::remove(dbFile.absoluteFilePath()))
|
||||||
{
|
{
|
||||||
Info(log,"Failed to delete Database!");
|
Error(log,"Failed to delete Database!");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -341,11 +376,29 @@ int main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Warning(log,"Configuration database [%s] does not exist!", QSTRING_CSTR(dbFile.absoluteFilePath()));
|
Warning(log,"Configuration database '%s' does not exist!", QSTRING_CSTR(dbFile.absoluteFilePath()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Info(log,"Starting Hyperion [%sGUI mode] - %s, %s, built: %s:%s", isGuiApp ? "": "non-", HYPERION_VERSION, HYPERION_BUILD_ID, __DATE__, __TIME__);
|
QString configFile(importConfig.value(parser));
|
||||||
|
if (!configFile.isEmpty())
|
||||||
|
{
|
||||||
|
if ( readonlyMode )
|
||||||
|
{
|
||||||
|
Error(log,"Configuration import is not possible. Hyperion's database '%s' is not writeable.", QSTRING_CSTR(dbFile.absolutePath()));
|
||||||
|
throw std::runtime_error("Configuration import failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigImportExport configImporter;
|
||||||
|
if (!configImporter.importJson(configFile).first)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Configuration import failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Info(log,"Starting Hyperion [%sGUI mode], DB is %s - %s, %s, built: %s:%s", isGuiApp ? "": "non-", readonlyMode ? "read-only" : "read/write", HYPERION_VERSION, HYPERION_BUILD_ID, __DATE__, __TIME__);
|
||||||
Debug(log,"QtVersion [%s]", QT_VERSION_STR);
|
Debug(log,"QtVersion [%s]", QT_VERSION_STR);
|
||||||
|
|
||||||
if ( !readonlyMode )
|
if ( !readonlyMode )
|
||||||
@@ -354,13 +407,13 @@ int main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Warning(log,"The user data path '%s' is not writeable. Hyperion starts in read-only mode. Configuration updates will not be persisted!", QSTRING_CSTR(userDataDirectory.absolutePath()));
|
Warning(log,"The database file '%s' is set not writeable. Hyperion starts in read-only mode. Configuration updates will not be persisted!", QSTRING_CSTR(dbFile.absoluteFilePath()));
|
||||||
}
|
}
|
||||||
|
|
||||||
HyperionDaemon* hyperiond = nullptr;
|
HyperionDaemon* hyperiond = nullptr;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
hyperiond = new HyperionDaemon(userDataDirectory.absolutePath(), qApp, bool(logLevelCheck), readonlyMode);
|
hyperiond = new HyperionDaemon(userDataDirectory.absolutePath(), qApp, bool(logLevelCheck));
|
||||||
}
|
}
|
||||||
catch (std::exception& e)
|
catch (std::exception& e)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user