From 895fecbeb765c2463598481471efcc8d07062363 Mon Sep 17 00:00:00 2001 From: luoliangyi <87842688@qq.com> Date: Fri, 15 Jul 2022 13:45:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E6=8A=A5=E5=AE=89=E8=A3=85=EF=BC=8C?= =?UTF-8?q?=E5=B4=A9=E6=BA=83=E7=AD=89=E6=97=A5=E5=BF=97=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E6=90=BA=E5=B8=A6=E4=B8=8AappName=E7=AD=89=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/version/HGVersion.h | 5 ++ modules/version/HGVersionImpl.cpp | 118 +++++++++++++++++++++++++----- modules/version/HGVersionImpl.hpp | 1 + 3 files changed, 107 insertions(+), 17 deletions(-) diff --git a/modules/version/HGVersion.h b/modules/version/HGVersion.h index b519995a..5772b1a8 100644 --- a/modules/version/HGVersion.h +++ b/modules/version/HGVersion.h @@ -9,6 +9,11 @@ HG_DECLARE_HANDLE(HGVersionMgr); #define HGVERSION_APPNAME_SCANNER "Scanner" #define HGVERSION_APPNAME_DRIVER "Driver" +#define HGVERSION_DEVACTION_START "start" +#define HGVERSION_DEVACTION_CLOSE "close" +#define HGVERSION_DEVACTION_CLEAN "clean" +#define HGVERSION_DEVACTION_COUNT "count" + typedef struct { HGBool postCrashInfo; /* 是否上报崩溃信息 */ diff --git a/modules/version/HGVersionImpl.cpp b/modules/version/HGVersionImpl.cpp index 29cae9ed..8a63c624 100644 --- a/modules/version/HGVersionImpl.cpp +++ b/modules/version/HGVersionImpl.cpp @@ -536,31 +536,104 @@ static HGResult PostInfo(int type, const std::string& appName, const std::string std::string osName = GetOSName(); std::string archName = GetArchName(); std::string oemName = GetOemName(); - std::string source = osName + "-" + archName + "-" + oemName; - char json[1024]; - if (1 == type || 2 == type) + std::string jsonStr; + cJSON* json = cJSON_CreateObject(); + if (NULL != json) { - sprintf(json, "{\"type\":%d, \"mac\":\"%s\", \"localid\":\"%s\", \"v\":\"%s\", \"ref\":\"%s\", \"desc\":\"%s\"}", - type, macList[0].c_str(), md5Str, version.c_str(), source.c_str(), desc.c_str()); - } - else if (3 == type) - { - sprintf(json, "{\"type\":%d, \"mac\":\"%s\", \"localid\":\"%s\", \"v\":\"%s\", \"ref\":\"%s\", \"desc\":\"%s\", \"crashaddress\":\"%s\", \"crash_data\":\"%s\"}", - type, macList[0].c_str(), md5Str, version.c_str(), source.c_str(), desc.c_str(), exceptionAddr.c_str(), crashFileUrl.c_str()); - } - else - { - assert(4 == type); - sprintf(json, "{\"type\":%d, \"mac\":\"%s\", \"localid\":\"%s\", \"v\":\"%s\", \"ref\":\"%s\", \"desc\":\"%s\", \"content\":\"%s\", \"contact\":\"%s\"}", - type, macList[0].c_str(), md5Str, version.c_str(), source.c_str(), desc.c_str(), feedbackInfo.c_str(), feedbackContact.c_str()); + cJSON_AddItemToObject(json, "type", cJSON_CreateNumber(type)); + cJSON_AddItemToObject(json, "mac", cJSON_CreateString(macList[0].c_str())); + cJSON_AddItemToObject(json, "localid", cJSON_CreateString(md5Str)); + cJSON_AddItemToObject(json, "v", cJSON_CreateString(version.c_str())); + cJSON_AddItemToObject(json, "desc", cJSON_CreateString(desc.c_str())); + cJSON_AddItemToObject(json, "sys", cJSON_CreateString(osName.c_str())); + cJSON_AddItemToObject(json, "cpu", cJSON_CreateString(archName.c_str())); + cJSON_AddItemToObject(json, "oem", cJSON_CreateString(oemName.c_str())); + cJSON_AddItemToObject(json, "app", cJSON_CreateString(appName.c_str())); + + if (3 == type) + { + cJSON_AddItemToObject(json, "crashaddress", cJSON_CreateString(exceptionAddr.c_str())); + cJSON_AddItemToObject(json, "crash_data", cJSON_CreateString(crashFileUrl.c_str())); + } + else if (4 == type) + { + cJSON_AddItemToObject(json, "contact", cJSON_CreateString(feedbackContact.c_str())); + cJSON_AddItemToObject(json, "content", cJSON_CreateString(feedbackInfo.c_str())); + } + + jsonStr = cJSON_Print(json); + cJSON_Delete(json); } struct curl_slist* headers = NULL; headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_POST, 1); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonStr.c_str()); + + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out); + + /* Perform the request, res will get the return code */ + CURLcode res = curl_easy_perform(curl); + /* Check for errors */ + if (res != CURLE_OK) + fprintf(stderr, "curl_easy_perform() failed: %s", curl_easy_strerror(res)); + else + { + std::string str_json = out.str(); // 返回值 + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "str_json=%s", str_json.c_str()); + + cJSON* json = cJSON_Parse(str_json.c_str()); + if (NULL != json) + { + cJSON* p = json->child; + while (NULL != p) + { + if (0 == strcmp(p->string, "code") && p->type == cJSON_Number) + { + if (1 == p->valueint) + ret = HGBASE_ERR_OK; + } + + p = p->next; + } + + cJSON_Delete(json); + } + } + + curl_slist_free_all(headers); + /* always cleanup */ + curl_easy_cleanup(curl); + } + + return ret; +} + +static HGResult PostDeviceInfo(const std::string& sn, const std::string& type, const std::string& name, + const std::string& fw, const std::string& act, int cnt) +{ + HGResult ret = HGBASE_ERR_FAIL; + CURL* curl = curl_easy_init(); + if (NULL != curl) + { + std::stringstream out; + + char countStr[16]; + sprintf(countStr, "%d", cnt); + std::string url = "http://up.appqy.com/api/bhs?sn=" + sn + "&type=" + type + "&name=" + name + "&fw=" + fw + + "&act=" + act + "&cnt" + countStr; + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2); + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + + struct curl_slist* headers = NULL; + headers = curl_slist_append(headers, "Accept-Encoding:gzip, deflate, sdch"); + headers = curl_slist_append(headers, "Accept-Language:zh-CN,zh;q=0.8"); + headers = curl_slist_append(headers, "Connection:keep-alive"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out); @@ -825,9 +898,20 @@ HGResult HGVersionMgrImpl::PostUninstallInfo(const HGChar* appName, const HGChar HGResult HGVersionMgrImpl::PostCrashInfo(const HGChar* appName, const HGChar* desc, const HGChar* crashFilePath, const HGChar* exceptionAddr) { + if (!m_serverCfg.postCrashInfo) + return HGBASE_ERR_FAIL; + return ::PostCrashInfo(appName, desc, crashFilePath, exceptionAddr); } +HGResult HGVersionMgrImpl::PostDeviceInfo(const HGChar* sn, const HGChar* type, const HGChar* name, const HGChar* fw, const HGChar* act, int cnt) +{ + if (!m_serverCfg.postDeviceInfo) + return HGBASE_ERR_FAIL; + + return ::PostDeviceInfo(sn, type, name, fw, act, cnt); +} + HGResult HGVersionMgrImpl::PostUserFeedback(const HGChar* appName, const HGChar* desc, const HGChar* feedback, const HGChar* contact) { return ::PostUserFeedback(appName, desc, feedback, contact); diff --git a/modules/version/HGVersionImpl.hpp b/modules/version/HGVersionImpl.hpp index e29767f7..b4f1358f 100644 --- a/modules/version/HGVersionImpl.hpp +++ b/modules/version/HGVersionImpl.hpp @@ -14,6 +14,7 @@ public: HGResult PostInstallInfo(const HGChar* appName, const HGChar* desc); HGResult PostUninstallInfo(const HGChar* appName, const HGChar* desc); HGResult PostCrashInfo(const HGChar* appName, const HGChar* desc, const HGChar* crashFilePath, const HGChar* exceptionAddr); + HGResult PostDeviceInfo(const HGChar *sn, const HGChar* type, const HGChar* name, const HGChar* fw, const HGChar* act, int cnt); HGResult PostUserFeedback(const HGChar* appName, const HGChar* desc, const HGChar* feedback, const HGChar* contact); HGResult GetVersionList(const HGChar* appName, HGVersionInfo** info, HGUInt* count); HGResult HttpDownload(const HGChar* url, const HGChar* saveFilePath, HGHttpDownloadFunc func, HGPointer param);