基本完成网络设置模块,还有一些细节需要完善。

This commit is contained in:
xueyan hu
2021-12-17 18:02:39 +08:00
parent faa69197f3
commit b6475e7ed4
21 changed files with 1979 additions and 421 deletions

View File

@@ -236,7 +236,11 @@ cJSON* cJSON_Parse(const char* value)
ep = 0;
if (!c) return 0; /* memory fail */
if (!parse_value(c, skip(value))) { cJSON_Delete(c); return 0; }
if (!parse_value(c, skip(value)))
{
cJSON_Delete(c);
return 0;
}
return c;
}
@@ -481,12 +485,6 @@ void cJSON_ReplaceItemInArray(cJSON* array, int which, cJSON* newitem) {
if (c == array->child) array->child = newitem; else newitem->prev->next = newitem; c->next = c->prev = 0; cJSON_Delete(c);
}
cJSON* cJSON_FindItemInArray(cJSON* array, int which)
{
cJSON* c = array->child;
while (c && which > 0) c = c->next, which--;
return c;
}
void cJSON_ReplaceItemInObject(cJSON* object, const char* string, cJSON* newitem)
{
@@ -500,14 +498,6 @@ void cJSON_ReplaceItemInObject(cJSON* object, const char* string, cJSON* newit
}
}
cJSON* cJSON_FindItemInObject(cJSON* object, const char* string)
{
int i = 0;
cJSON* c = object->child;
while (c && cJSON_strcasecmp(c->string, string))i++, c = c->next;
return c;
}
/* Create basic types: */
cJSON* cJSON_CreateNull() { cJSON* item = cJSON_New_Item(); if (item)item->type = cJSON_NULL; return item; }
cJSON* cJSON_CreateTrue() { cJSON* item = cJSON_New_Item(); if (item)item->type = cJSON_True; return item; }

View File

@@ -40,36 +40,36 @@ extern "C"
#define cJSON_IsReference 256
/* The cJSON structure: */
typedef struct cJSON {
struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
typedef struct cJSON {
struct cJSON* next, * prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON* child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
int type; /* The type of the item, as above. */
int type; /* The type of the item, as above. */
char *valuestring; /* The item's string, if type==cJSON_String */
int valueint; /* The item's number, if type==cJSON_Number */
double valuedouble; /* The item's number, if type==cJSON_Number */
char* valuestring; /* The item's string, if type==cJSON_String */
int valueint; /* The item's number, if type==cJSON_Number */
double valuedouble; /* The item's number, if type==cJSON_Number */
char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;
char* string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;
typedef struct cJSON_Hooks {
void *(*malloc_fn)(size_t sz);
void (*free_fn)(void *ptr);
} cJSON_Hooks;
typedef struct cJSON_Hooks {
void* (*malloc_fn)(size_t sz);
void (*free_fn)(void* ptr);
} cJSON_Hooks;
/* Supply malloc, realloc and free functions to cJSON */
extern void cJSON_InitHooks(cJSON_Hooks* hooks);
/* Supply malloc, realloc and free functions to cJSON */
extern void cJSON_InitHooks(cJSON_Hooks* hooks);
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
extern cJSON *cJSON_Parse(const char *value);
/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
extern char *cJSON_Print(cJSON *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
extern char *cJSON_PrintUnformatted(cJSON *item);
/* Delete a cJSON entity and all subentities. */
extern void cJSON_Delete(cJSON *c);
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
extern cJSON* cJSON_Parse(const char* value);
/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
extern char* cJSON_Print(cJSON* item);
/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
extern char* cJSON_PrintUnformatted(cJSON* item);
/* Delete a cJSON entity and all subentities. */
extern void cJSON_Delete(cJSON* c);
/* Returns the number of items in an array (or object). */
extern int cJSON_GetArraySize(cJSON* array);
@@ -114,10 +114,6 @@ extern void cJSON_Delete(cJSON *c);
extern void cJSON_ReplaceItemInArray(cJSON* array, int which, cJSON* newitem);
extern void cJSON_ReplaceItemInObject(cJSON* object, const char* string, cJSON* newitem);
/*created by huxy*/
extern cJSON* cJSON_FindItemInObject(cJSON* object, const char* string);
extern cJSON* cJSON_FindItemInArray(cJSON* array, int which);
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())

View File

@@ -13,18 +13,20 @@ const char* strProductFileName = "./cfgs/usct-product.json";
JsonObject::JsonObject()
{
loadcfg();
}
JsonObject::~JsonObject()
{
savecfg();
}
void JsonObject::setJsonString(const char* catergory, const char* stringName, const char* stringValue, bool save)
{
if (!loadcfg())
return;
cJSON* first = cJSON_FindItemInObject((cJSON*)json_root, catergory);
cJSON* first = cJSON_GetObjectItem((cJSON*)json_root, catergory);
if (!first) return;
cJSON* Item = cJSON_CreateString(stringValue);
@@ -40,14 +42,60 @@ char* JsonObject::getJsonString(const char* catergory, const char* stringName)
if (!loadcfg())
return "";
cJSON* first = cJSON_FindItemInObject((cJSON*)json_root, catergory);
cJSON* first = cJSON_GetObjectItem((cJSON*)json_root, catergory);
if (!first) return "";
cJSON* second = cJSON_FindItemInObject(first, stringName);
cJSON* second = cJSON_GetObjectItem(first, stringName);
return second->valuestring;
}
char* JsonObject::getArrayNode(const char* catergory, const char* stringName, int index, const char* id)
{
if (!loadcfg())
return "";
cJSON* first = cJSON_GetObjectItem((cJSON*)json_root, catergory);
if (!first) return "";
cJSON* second = cJSON_GetObjectItem(first, stringName);
if (!second) return "";
cJSON* third = cJSON_GetArrayItem(second, index);
cJSON* fourth = cJSON_GetObjectItem(third, id);
return fourth->valuestring;
}
void JsonObject::setArrayNode(const char* catergory, const char* stringName, int index, const char* id, const char* stringValue)
{
if (!loadcfg())
return;
cJSON* first = cJSON_GetObjectItem((cJSON*)json_root, catergory);
if (!first) return;
cJSON* second = cJSON_GetObjectItem(first, stringName);
if (!second) return;
cJSON* third = cJSON_GetArrayItem(second, index);
cJSON* Item = cJSON_CreateString(stringValue);
cJSON_ReplaceItemInObject(third, id, Item);
}
int JsonObject::getArraySize(const char* catergory, const char* stringName)
{
if (!loadcfg())
return 0;
cJSON* first = cJSON_GetObjectItem((cJSON*)json_root, catergory);
if (!first) return 0;
cJSON* second = cJSON_GetObjectItem(first, stringName);
return cJSON_GetArraySize(second);
}
QStringList JsonObject::protocals()
@@ -55,11 +103,13 @@ QStringList JsonObject::protocals()
if (!loadcfg())
return QStringList();
cJSON* first = cJSON_FindItemInObject((cJSON*)json_root, "protocol");
cJSON* first = cJSON_GetObjectItem((cJSON*)json_root, "protocol");
if (!first) return QStringList();
cJSON* second = cJSON_FindItemInObject(first, "lists");
cJSON* second = cJSON_GetObjectItem(first, "lists");
std::string lans = second->valuestring;
QString str = QString::fromLocal8Bit(QByteArray::fromRawData(lans.c_str(), lans.size()));
return str.split(";");
@@ -90,10 +140,10 @@ QStringList JsonObject::worklistFilters()
if (!loadcfg())
return QStringList();
cJSON* first = cJSON_FindItemInObject((cJSON*)json_root, "worklistfilter");
cJSON* first = cJSON_GetObjectItem((cJSON*)json_root, "worklistfilter");
if (!first) return QStringList();
cJSON* second = cJSON_FindItemInObject(first, "lists");
cJSON* second = cJSON_GetObjectItem(first, "lists");
std::string lans = second->valuestring;
QString str = QString::fromLocal8Bit(QByteArray::fromRawData(lans.c_str(), lans.size()));
return str.split(";");
@@ -107,10 +157,10 @@ QStringList JsonObject::language()
if (!loadcfg())
return QStringList();
cJSON* first = cJSON_FindItemInObject((cJSON*)json_root, "general");
cJSON* first = cJSON_GetObjectItem((cJSON*)json_root, "general");
if (!first) return QStringList();
cJSON* second = cJSON_FindItemInObject(first, "language");
cJSON* second = cJSON_GetObjectItem(first, "language");
std::string lans = second->valuestring;
QString str = QString::fromLocal8Bit(QByteArray::fromRawData(lans.c_str(), lans.size()));
return str.split(";");
@@ -181,29 +231,10 @@ bool JsonObject::loadcfg()
std::string strJsonData = ss.str();
json_root = cJSON_Parse(strJsonData.c_str());
//modify somthing
//QJsonValueRef ref = m_JsonRootObject.find("general").value();
//QJsonObject obj = ref.toObject();
//obj["language"] = language;
//cJSON* parent = cJSON_FindItemInObject(json_root, "waitinglist");
//if (parent)
//{
// //const char* newtime = QDateTime::currentDateTime().toString("yyyyMMdd").toStdString().c_str();
// cJSON* Item = cJSON_CreateString(QDateTime::currentDateTime().toString("yyyy-MM-dd").toStdString().c_str());
// cJSON_ReplaceItemInObject(parent, "waitinglistDefaultDate", Item);
//}
//cJSON* parent = cJSON_FindItemInObject(json_root, "pacsservers");
//if (parent)
//{
// cJSON* p2 = cJSON_FindItemInArray(parent, 1);
// cJSON* Item = cJSON_CreateString("KISS");
// //const char* newtime = QDateTime::currentDateTime().toString("yyyyMMdd").toStdString().c_str();
// cJSON_ReplaceItemInObject(p2, "ae", Item);
//}
inFile.close();
if (!json_root)
return false;
m_bLoaded = true;
return true;
}
@@ -288,31 +319,107 @@ void JsonObject::setServer(ServerType type, const host& list)
}
localhost JsonObject::getLocalHost()
QString JsonObject::interfaceName()
{
return QString(getJsonString("address", "device"));
}
QString JsonObject::passWord()
{
return QString("klxts4047");
}
void JsonObject::setInterfaceName(const QString& name)
{
setJsonString("address", "device", name.toStdString().c_str());
}
localhost lhost;
//lhost.ip = IPConfig::getDeviceIP();
lhost.ip = QString(getJsonString("localhost", "ip"));
lhost.mask = QString(getJsonString("localhost", "mask"));
lhost.gateway = QString(getJsonString("localhost", "gateway"));
return lhost;
bool JsonObject::isDHCP()
{
QVariant tempValue = QString(getJsonString("address", "dhcp"));
return tempValue.toBool();
}
// localhost lhost;
// lhost.ip = QString(getJsonString("localhost", "ip"));
// lhost.mask = QString(getJsonString("localhost", "mask"));
// lhost.gateway = QString(getJsonString("localhost", "gateway"));
// return lhost;
void JsonObject::autoDHCP(bool ena)
{
QString str = QVariant(ena).toString();
setJsonString("address", "dhcp", str.toStdString().c_str());
}
void JsonObject::setLocalHost(const localhost& lh)
IpAddr JsonObject::getDefaultIpAddr()
{
setJsonString("localhost", "ip", lh.ip.toStdString().c_str(), false);
setJsonString("localhost", "mask", lh.mask.toStdString().c_str(), false);
setJsonString("localhost", "gateway", lh.gateway.toStdString().c_str(), false);
IpAddr obj;
//lhost.ip = IPConfig::getDeviceIP();
obj.ip = QString(getJsonString("address", "ip"));
obj.mask = QString(getJsonString("address", "mask"));
return obj;
}
void JsonObject::setDefaultIpAddr(const IpAddr& addr)
{
setJsonString("address", "ip", addr.ip.toStdString().c_str());
setJsonString("address", "mask", addr.mask.toStdString().c_str());
}
QList<QStringList> JsonObject::getIpAddrList()
{
QList<QStringList> obj;
int size = getArraySize("address", "additional");
for (int i = 0; i < size; i++)
{
QStringList temp;
char* ip_str = getArrayNode("address", "additional", i, "ip");
char* mask_str = getArrayNode("address", "additional", i, "netmask");
temp << ip_str << mask_str;
obj.push_back(temp);
}
return obj;
}
void JsonObject::setIpAddrList(const QList<QStringList>& list)
{
for (int i = 0; i < list.size(); i++) {
setArrayNode("address", "additional", i, "ip", list.at(i)[0].toStdString().c_str());
setArrayNode("address", "additional", i, "netmask", list.at(i)[1].toStdString().c_str());
}
savecfg();
}
QString JsonObject::getDefaultGateway()
{
return QString(getJsonString("routing", "defaultgateway"));
}
void JsonObject::setDefaultGateway(const QString& gw)
{
setJsonString("routing", "defaultgateway", gw.toStdString().c_str());
}
QList<QStringList> JsonObject::getIpRouteList()
{
QList<QStringList> obj;
int size = getArraySize("routing", "routingtable");
for (int i = 0; i < size; i++)
{
QStringList temp;
char* des_str = getArrayNode("routing", "routingtable", i, "destination");
char* gw_str = getArrayNode("routing", "routingtable", i, "gateway");
char* nm_str = getArrayNode("routing", "routingtable", i, "netmask");
temp << des_str << nm_str << gw_str;
obj.push_back(temp);
}
return obj;
}
void JsonObject::setIpRouteList(const QList<QStringList>& list)
{
for (int i = 0; i < list.size(); i++) {
setArrayNode("routing", "routingtable", i, "destination", list.at(i)[0].toStdString().c_str());
setArrayNode("routing", "routingtable", i, "netmask", list.at(i)[1].toStdString().c_str());
setArrayNode("routing", "routingtable", i, "gateway", list.at(i)[2].toStdString().c_str());
}
savecfg();
}

View File

@@ -14,11 +14,18 @@ struct host {
QString port;
//QString isDefault;
};
struct localhost {
struct IpAddr {
QString ip;
QString mask;
QString gateway;
};
struct IpRoute
{
QString des;
QString gw;
QString mask;
};
class JsonObject
{
@@ -61,13 +68,36 @@ public:
host getServer(ServerType type);
void setServer(ServerType type, const host& list);
localhost getLocalHost();
void setLocalHost(const localhost& lh);
//for network manager
QString passWord();
QString interfaceName();
void setInterfaceName(const QString& name);
bool isDHCP();
void autoDHCP(bool);
IpAddr getDefaultIpAddr();
void setDefaultIpAddr(const IpAddr& addr);
QList<QStringList> getIpAddrList();
void setIpAddrList(const QList<QStringList>& list);
QString getDefaultGateway();
void setDefaultGateway(const QString& gw);
QList<QStringList> getIpRouteList();
void setIpRouteList(const QList<QStringList>& list);
private:
void setJsonString(const char* catergory, const char* stringName, const char* stringValue, bool save = true);
char* getJsonString(const char* catergory, const char* stringName);
char* getArrayNode(const char* catergory, const char* stringName, int index, const char* id);
void setArrayNode(const char* catergory, const char* stringName, int index, const char* id, const char* stringValue);
int getArraySize(const char* catergory, const char* stringName);
bool loadcfg();
bool savecfg();