Files
gcs-nf/App/Config/Config.cpp
T

260 lines
5.5 KiB
C++

#include "Config.h"
Config::Config(QObject *parent) :
QObject(parent)
{
qDebug() << "Config Operation";
QDir *Dir = new QDir;
if(!Dir->exists("./Config"))
if(Dir->mkdir("./Config"))//如果文件夹不存在就新建
{
qInfo() << "create config dir";
}
delete Dir;
Dir = nullptr;
loadJson(ConfigFile);
}
Config::~Config()
{
}
void Config::loadJson(const QString& jsonFilename)
{
if (jsonFilename.isEmpty()) {
return;
}
qDebug() << "Loading " << jsonFilename;
QFile jsonFile(jsonFilename);
if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "Unable to open file" << jsonFilename << jsonFile.errorString();
return;
}
QByteArray bytes = jsonFile.readAll();
jsonFile.close();
QJsonParseError jsonParseError;
QJsonDocument doc = QJsonDocument::fromJson(bytes, &jsonParseError);
if (jsonParseError.error != QJsonParseError::NoError) {
qWarning() << jsonFilename << "Unable to open json document" << jsonParseError.errorString();
return;
}
ConfigJson = doc.object();
/*
QJsonObject json = doc.object();
QJsonValue jsonValue = json.value("config");
if(jsonValue.isObject())
{
ConfigJson = jsonValue.toObject();
}
*/
//qDebug() << ConfigJson;
}
QJsonObject Config::getJsonObject(QString key)
{
QJsonObject obj;
QJsonValue Value = ConfigJson.value(key);
if(Value.isObject())
{
obj = Value.toObject();
}
return obj;
}
void Config::setJsonObject(QString key,QJsonValue value)
{
ConfigJson[key] = value;
QFile file(ConfigFile);
if(!file.open(QIODevice::ReadWrite|QFile::Truncate)) {
qDebug() << "File open error";
}
QJsonDocument doc(QJsonDocument::fromJson(file.readAll()));
doc.setObject(ConfigJson);
file.write(doc.toJson());
file.close();
}
QString Config::getCommandFile()
{
QJsonObject obj = getJsonObject("commandfile");
QString file = obj.value("path").toString();
return file;
}
void Config::setCommandFile(QString file)
{
QJsonObject obj;
obj["path"] = file;
setJsonObject("commandfile",obj);
}
void Config::getConnet(QString key,QVariant *type,
QVariant *iauto,
QVariant *par1,QVariant *par2,
QVariant *par3,QVariant *par4,
QVariant *par5)
{
QJsonObject obj = getJsonObject(key);
*type = obj.value("type").toString();
*iauto = obj.value("auto").toBool();
*par1 = obj.value("param1").toString();
*par2 = obj.value("param2").toString();
*par3 = obj.value("param3").toString();
*par4 = obj.value("param4").toString();
*par5 = obj.value("param5").toString();
}
void Config::setConnet(QString key, QVariant type,
QVariant iauto,
QVariant par1, QVariant par2,
QVariant par3, QVariant par4,
QVariant par5)
{
QJsonObject obj;
obj["type"] = type.toString();
obj["auto"] = iauto.toBool();
obj["param1"] = par1.toString();
obj["param2"] = par2.toString();
obj["param3"] = par3.toString();
obj["param4"] = par4.toString();
obj["param5"] = par5.toString();
setJsonObject(key,obj);
}
void Config::get_GCS_ID(QVariant *id)
{
QJsonObject obj = getJsonObject("GlobalSetting");
*id = obj.value("GCS").toInt();
}
void Config::set_GCS_ID(QVariant id)
{
QJsonObject obj;
obj["GCS"] = id.toInt();
setJsonObject("GlobalSetting",obj);
}
void Config::getServoOffset(QVariant *la,QVariant *ra,
QVariant *le,QVariant *re,
QVariant *ru)
{
QJsonObject obj = getJsonObject("ServoOffset");
*la = obj.value("la").toDouble();
*ra = obj.value("ra").toDouble();
*le = obj.value("le").toDouble();
*re = obj.value("re").toDouble();
*ru = obj.value("ru").toDouble();
}
void Config::setServoOffset(QVariant la,QVariant ra,
QVariant le,QVariant re,
QVariant ru)
{
QJsonObject obj;
obj["la"] = la.toDouble();
obj["ra"] = ra.toDouble();
obj["le"] = le.toDouble();
obj["re"] = re.toDouble();
obj["ru"] = ru.toDouble();
setJsonObject("ServoOffset",obj);
}
void Config::getHeartBeat(QVariant *state,QVariant *frq)
{
QJsonObject obj = getJsonObject("HeartBeat");
*state = obj.value("state").toBool();
*frq = obj.value("frq").toDouble();
}
void Config::setHeartBeat(QVariant state,QVariant frq)
{
QJsonObject obj;
obj["state"] = state.toBool();
obj["frq"] = frq.toDouble();
setJsonObject("HeartBeat",obj);
}
void Config::getMapType(QVariant *type)
{
QJsonObject obj = getJsonObject("Map");
*type = obj.value("MapType").toString();
}
void Config::setMapType(QVariant type)
{
QJsonObject obj;
obj["MapType"] = type.toString();
setJsonObject("Map",obj);
}
void Config::getBeep(QVariant *value)
{
QJsonObject obj = getJsonObject("Warnning");
*value = obj.value("Beep").toBool();
}
void Config::setBeep(QVariant value)
{
QJsonObject obj;
obj["Beep"] = value.toBool();
setJsonObject("Warnning",obj);
}