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

373 lines
8.8 KiB
C++
Raw Normal View History

2024-09-22 10:53:28 +08:00
#include "Config.h"
2020-09-29 15:14:56 +08:00
2020-10-06 09:34:04 +08:00
Config::Config(QObject *parent) :
QObject(parent)
2020-09-29 15:14:56 +08:00
{
qDebug() << "Config Operation";
QDir *Dir = new QDir;
if(!Dir->exists("./Config"))
if(Dir->mkdir("./Config"))//如果文件夹不存在就新建
{
qInfo() << "create config dir";
}
delete Dir;
Dir = nullptr;
2020-10-07 21:53:36 +08:00
loadJson(ConfigFile);
2020-09-29 15:14:56 +08:00
}
2020-09-30 13:52:57 +08:00
Config::~Config()
2020-09-29 15:14:56 +08:00
{
}
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;
}
2020-10-07 21:53:36 +08:00
ConfigJson = doc.object();
/*
2020-09-29 15:14:56 +08:00
QJsonObject json = doc.object();
2020-10-06 09:34:04 +08:00
QJsonValue jsonValue = json.value("config");
2020-09-29 15:14:56 +08:00
2020-10-07 21:53:36 +08:00
if(jsonValue.isObject())
{
ConfigJson = jsonValue.toObject();
}
*/
2020-10-06 09:34:04 +08:00
}
2020-09-29 15:14:56 +08:00
2020-10-07 21:53:36 +08:00
QJsonObject Config::getJsonObject(QString key)
2020-10-06 09:34:04 +08:00
{
2020-10-07 21:53:36 +08:00
QJsonObject obj;
QJsonValue Value = ConfigJson.value(key);
if(Value.isObject())
{
obj = Value.toObject();
}
return obj;
2020-09-29 15:14:56 +08:00
}
2020-10-07 21:53:36 +08:00
void Config::setJsonObject(QString key,QJsonValue value)
2020-10-06 09:34:04 +08:00
{
2020-09-29 15:14:56 +08:00
2020-10-07 21:53:36 +08:00
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();
2020-10-06 09:34:04 +08:00
}
2020-09-29 15:14:56 +08:00
2020-10-07 21:53:36 +08:00
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);
}
2020-10-13 18:41:23 +08:00
void Config::getConnet(QString key,QVariant *type,
2020-10-07 21:53:36 +08:00
QVariant *iauto,
QVariant *par1,QVariant *par2,
QVariant *par3,QVariant *par4,
QVariant *par5)
{
QJsonObject obj = getJsonObject(key);
*type = obj.value("type").toString();
2020-11-20 18:57:45 +08:00
*iauto = obj.value("auto").toBool();
2020-10-07 21:53:36 +08:00
*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();
}
2020-10-13 18:41:23 +08:00
void Config::setConnet(QString key, QVariant type,
2020-10-07 21:53:36 +08:00
QVariant iauto,
2020-10-13 18:41:23 +08:00
QVariant par1, QVariant par2,
QVariant par3, QVariant par4,
2020-10-07 21:53:36 +08:00
QVariant par5)
{
QJsonObject obj;
obj["type"] = type.toString();
2020-11-20 18:57:45 +08:00
obj["auto"] = iauto.toBool();
2020-10-07 21:53:36 +08:00
obj["param1"] = par1.toString();
obj["param2"] = par2.toString();
obj["param3"] = par3.toString();
obj["param4"] = par4.toString();
obj["param5"] = par5.toString();
setJsonObject(key,obj);
}
2020-11-21 18:19:28 +08:00
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);
}
2021-04-25 17:10:46 +08:00
void Config::getServoOffset(QVariant *dirla,QVariant *dirra,QVariant *dirle,QVariant *dirre,QVariant *dirru,
QVariant *maxla,QVariant *maxra,QVariant *maxle,QVariant *maxre,QVariant *maxru,
QVariant *scalela,QVariant *scalera,QVariant *scalele,QVariant *scalere,QVariant *scaleru,
QVariant *la,QVariant *ra,QVariant *le,QVariant *re,QVariant *ru)
2020-12-08 22:21:52 +08:00
{
QJsonObject obj = getJsonObject("ServoOffset");
2020-11-21 18:19:28 +08:00
2021-04-25 17:10:46 +08:00
*dirla = obj.value("dirla").toDouble();
*dirra = obj.value("dirra").toDouble();
*dirle = obj.value("dirle").toDouble();
*dirre = obj.value("dirre").toDouble();
*dirru = obj.value("dirru").toDouble();
*maxla = obj.value("maxla").toDouble();
*maxra = obj.value("maxra").toDouble();
*maxle = obj.value("maxle").toDouble();
*maxre = obj.value("maxre").toDouble();
*maxru = obj.value("maxru").toDouble();
*scalela = obj.value("scalela").toDouble();
*scalera = obj.value("scalera").toDouble();
*scalele = obj.value("scalele").toDouble();
*scalere = obj.value("scalere").toDouble();
*scaleru = obj.value("scaleru").toDouble();
2020-12-08 22:21:52 +08:00
*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();
}
2020-10-07 21:53:36 +08:00
2021-04-25 17:10:46 +08:00
void Config::setServoOffset(QVariant dirla,QVariant dirra,QVariant dirle,QVariant dirre,QVariant dirru,
QVariant maxla,QVariant maxra,QVariant maxle,QVariant maxre,QVariant maxru,
QVariant scalela,QVariant scalera,QVariant scalele,QVariant scalere,QVariant scaleru,
QVariant la,QVariant ra,QVariant le,QVariant re,QVariant ru)
2020-12-08 22:21:52 +08:00
{
QJsonObject obj;
2020-09-29 15:14:56 +08:00
2021-04-25 17:10:46 +08:00
obj["dirla"] = dirla.toDouble();
obj["dirra"] = dirra.toDouble();
obj["dirle"] = dirle.toDouble();
obj["dirre"] = dirre.toDouble();
obj["dirru"] = dirru.toDouble();
obj["maxla"] = maxla.toDouble();
obj["maxra"] = maxra.toDouble();
obj["maxle"] = maxle.toDouble();
obj["maxre"] = maxre.toDouble();
obj["maxru"] = maxru.toDouble();
obj["scalela"] = scalela.toDouble();
obj["scalera"] = scalera.toDouble();
obj["scalele"] = scalele.toDouble();
obj["scalere"] = scalere.toDouble();
obj["scaleru"] = scaleru.toDouble();
2020-12-08 22:21:52 +08:00
obj["la"] = la.toDouble();
obj["ra"] = ra.toDouble();
obj["le"] = le.toDouble();
obj["re"] = re.toDouble();
obj["ru"] = ru.toDouble();
setJsonObject("ServoOffset",obj);
}
2020-09-29 15:14:56 +08:00
2020-12-10 18:47:49 +08:00
void Config::getHeartBeat(QVariant *state,QVariant *frq)
{
QJsonObject obj = getJsonObject("HeartBeat");
2020-09-29 15:14:56 +08:00
2020-12-10 18:47:49 +08:00
*state = obj.value("state").toBool();
*frq = obj.value("frq").toDouble();
}
2020-09-29 15:14:56 +08:00
2020-12-10 18:47:49 +08:00
void Config::setHeartBeat(QVariant state,QVariant frq)
{
QJsonObject obj;
obj["state"] = state.toBool();
obj["frq"] = frq.toDouble();
setJsonObject("HeartBeat",obj);
}
2022-01-24 18:30:30 +08:00
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);
}
2025-02-18 15:20:13 +08:00
void Config::setReferencePoint(double lon, double lat, double hight)
{
QJsonObject obj;
obj["lon"] = lon;
obj["lat"] = lat;
obj["hight"] = hight;
setJsonObject("ReferencePoint",obj);
}
void Config::getReferencePoint(double &lon, double &lat, double &hight)
{
QJsonObject obj = getJsonObject("ReferencePoint");
if(obj.empty() )
{
return;
}
lon = obj["lon"].toDouble();
lat = obj["lat"].toDouble();
hight = obj["hight"].toDouble();
}
2023-02-13 21:55:03 +08:00
void Config::getBeep(QVariant *value)
{
QJsonObject obj = getJsonObject("Warnning");
*value = obj.value("Beep").toBool();
}
void Config::setBeep(QVariant value)
{
QJsonObject obj;
2022-01-24 18:30:30 +08:00
2023-02-13 21:55:03 +08:00
obj["Beep"] = value.toBool();
setJsonObject("Warnning",obj);
}
void Config::getTTS(QVariant *value)
{
QJsonObject obj = getJsonObject("Sound");
*value = obj.value("tts").toBool();
}
void Config::setTTS(QVariant value)
{
QJsonObject obj;
2022-01-24 18:30:30 +08:00
2023-02-13 21:55:03 +08:00
obj["tts"] = value.toBool();
setJsonObject("Sound",obj);
}
2024-09-22 10:53:28 +08:00
void Config::get_ExportInfo(QVariant *state, QVariant *flag, QHostAddress *addr, QVariant *port, QString &id)
2023-02-13 21:55:03 +08:00
{
QJsonObject obj = getJsonObject("ExportInfo");
*state = obj.value("state").toBool();
*flag = obj.value("flag").toInt();
*addr = obj.value("addr").toString();
*port = obj.value("port").toInt();
2024-09-22 10:53:28 +08:00
id = obj.value("id").toString();
2023-02-13 21:55:03 +08:00
}
2024-09-22 10:53:28 +08:00
void Config::set_ExportInfo(QVariant state, QVariant flag, QHostAddress addr, QVariant port, QString id)
2023-02-13 21:55:03 +08:00
{
QJsonObject obj;
obj["state"] = state.toBool();
obj["flag"] = flag.toInt();
obj["addr"] = addr.toString();
obj["port"] = port.toInt();
2024-09-22 10:53:28 +08:00
obj["id"] = id;
qDebug() << "id: " << obj["id"].toString();
2023-02-13 21:55:03 +08:00
setJsonObject("ExportInfo",obj);
}
2022-01-24 18:30:30 +08:00