sp_httpDefine.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. #include "sp_httpDefine.h"
  2. #include "sp_cfg.h"
  3. #include "json/json.h"
  4. #include <sstream>
  5. #include "SpBase.h"
  6. #include <time.h>
  7. #include <ctime>
  8. #include <iomanip>
  9. #include <fstream>
  10. #include <ostream>
  11. #include <iostream>
  12. #include "SpUtility.h"
  13. //#include "cpp-httplib/httplib.h"
  14. #define QUERY_TERMINAL_MAX_WAIT_TIME 10000
  15. CAutoArray<CSimpleString> generateUrlArr(CSimpleString url, CSimpleString appendStr)
  16. {
  17. auto urlArr = url.Split('|');
  18. for (int i = 0; i < urlArr.GetCount(); i++)
  19. urlArr[i].Append(appendStr);
  20. return urlArr;
  21. }
  22. bool VTMErrMsgCfgRet::Parse(string strData)
  23. {
  24. Json::Value root;
  25. Json::Reader reader;
  26. reader.parse(strData, root, false);
  27. if (root["code"].isString())
  28. {
  29. std::string return_code = root["code"].asString();
  30. if (return_code != "SUC0000")
  31. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get VTMErrMsgCfgRet Failed , code:%s", return_code.c_str());
  32. }
  33. do
  34. {
  35. //judge success
  36. if (root["success"].isBool())
  37. {
  38. m_result = root["success"].asBool();
  39. if (root["error_msg"].isString())
  40. error_msg = root["error_msg"].asString();
  41. if (!m_result)
  42. {
  43. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get VTMErrMsgCfgRet::success False");
  44. break;
  45. }
  46. }
  47. else
  48. {
  49. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get VTMErrMsgCfgRet::success not bool");
  50. break;
  51. }
  52. //judge data
  53. if (!root[TOTALCONFIG_HEAD].isObject())
  54. {
  55. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get VTMErrMsgCfgRet::data not object");
  56. break;
  57. }
  58. auto json_data = root[TOTALCONFIG_HEAD];
  59. //get total
  60. if (json_data[VTMERRMSG_CONFIG_TOTAL].isInt())
  61. total = json_data[VTMERRMSG_CONFIG_TOTAL].asInt();
  62. else
  63. {
  64. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get VTMErrMsgCfgRet::data::%s not int", VTMERRMSG_CONFIG_TOTAL);
  65. break;
  66. }
  67. //get version_no
  68. if (json_data[VTMERRMSG_CONFIG_VERSION].isString())
  69. version_no = json_data[VTMERRMSG_CONFIG_VERSION].asString();
  70. else
  71. {
  72. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get VTMErrMsgCfgRet::data::%s not string", VTMERRMSG_CONFIG_VERSION);
  73. break;
  74. }
  75. //get page_total
  76. if (json_data[VTMERRMSG_CONFIG_PAGETOTAL].isInt())
  77. page_total = json_data[VTMERRMSG_CONFIG_PAGETOTAL].asInt();
  78. else
  79. {
  80. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get VTMErrMsgCfgRet::data::%s not int", VTMERRMSG_CONFIG_PAGETOTAL);
  81. break;
  82. }
  83. if (!json_data[VTMERRMSG_CONFIG_CONFIG].isArray())
  84. {
  85. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get VTMErrMsgCfgRet::data::%s not array", VTMERRMSG_CONFIG_CONFIG);
  86. break;
  87. }
  88. //parse config
  89. auto vtmerrmsg_config = json_data[VTMERRMSG_CONFIG_CONFIG];
  90. for (Json::Value::ArrayIndex i = 0; i != vtmerrmsg_config.size(); i++) {
  91. std::string errorCode = vtmerrmsg_config[i][VTMERRMSG_CONFIG_ERRORCODE].asString();
  92. std::string description = vtmerrmsg_config[i][VTMERRMSG_CONFIG_DESCRIPETION].asString();
  93. std::string remark = vtmerrmsg_config[i][VTMERRMSG_CONFIG_REMARK].asString();
  94. errorCodeArr.push_back(errorCode);
  95. descriptionArr.push_back(description);
  96. remarkArr.push_back(remark);
  97. }
  98. return true;
  99. } while (false);
  100. return false;
  101. }
  102. bool TerminalCfgRet::Parse(string strData) {
  103. Json::Value root;
  104. Json::Reader reader;
  105. reader.parse(strData, root, false);
  106. if (root["code"].isString())
  107. {
  108. std::string return_code = root["code"].asString();
  109. if (return_code != "SUC0000")
  110. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get RootCfgRet Failed , code:%s", return_code.c_str());
  111. }
  112. do
  113. {
  114. //judge success
  115. if (root["success"].isBool())
  116. {
  117. m_result = root["success"].asBool();
  118. if (!m_result)
  119. {
  120. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::success False");
  121. break;
  122. }
  123. }
  124. else
  125. {
  126. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::success not bool");
  127. break;
  128. }
  129. //judge data
  130. if (!root[TOTALCONFIG_HEAD].isObject())
  131. {
  132. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data not object");
  133. break;
  134. }
  135. auto json_data = root[TOTALCONFIG_HEAD];
  136. if (!json_data[TOTALCONFIG_CENTER_HEAD].isObject())
  137. {
  138. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data::%s not object", TOTALCONFIG_CENTER_HEAD);
  139. break;
  140. }
  141. if (!json_data[TOTALCONFIG_ROOT_HEAD].isObject())
  142. {
  143. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data::%s not object", TOTALCONFIG_ROOT_HEAD);
  144. break;
  145. }
  146. if (!json_data[TOTALCONFIG_SHELL_HEAD].isObject())
  147. {
  148. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data::%s not object", TOTALCONFIG_SHELL_HEAD);
  149. break;
  150. }
  151. //parse center_config
  152. auto terminal_config = json_data[TOTALCONFIG_CENTER_HEAD];
  153. if (!terminal_config[TOTALCONFIG_CENTER_UPDATE].isBool())
  154. {
  155. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data::center_config_dto::update not Bool");
  156. break;
  157. }
  158. if (!terminal_config[TOTALCONFIG_CENTER_RESET].isBool())
  159. {
  160. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data::center_config_dto::reset not Bool");
  161. break;
  162. }
  163. center_update = terminal_config[TOTALCONFIG_CENTER_UPDATE].asBool();
  164. center_reset = terminal_config[TOTALCONFIG_CENTER_RESET].asBool();
  165. //只有有center_update时才进行解析
  166. if (center_update)
  167. {
  168. if (!terminal_config[TOTALCONFIG_CENTER_VERSION].isString())
  169. {
  170. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data::center_config_dto::sm3 not string");
  171. break;
  172. }
  173. if (!terminal_config[TOTALCONFIG_CONFIG_HEAD].isArray())
  174. {
  175. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data::center_config_dto::config not array");
  176. break;
  177. }
  178. center_version = terminal_config[TOTALCONFIG_CENTER_VERSION].asString();
  179. auto tmp_center_config = terminal_config[TOTALCONFIG_CONFIG_HEAD];
  180. for (Json::Value::ArrayIndex i = 0; i != tmp_center_config.size(); i++) {
  181. std::string module = tmp_center_config[i][TOTALCONFIG_CONFIG_MODULE].asString();
  182. std::string name = tmp_center_config[i][TOTALCONFIG_CONFIG_NAME].asString();
  183. std::string value = tmp_center_config[i][TOTALCONFIG_CONFIG_VALUE].asString();
  184. if (center_config.find(module) == center_config.end())
  185. center_config.insert(std::make_pair(module, std::map<std::string, std::string>()));
  186. center_config[module][name] = value;
  187. }
  188. }
  189. //parse root_config
  190. auto root_config_json = json_data[TOTALCONFIG_ROOT_HEAD];
  191. if (!root_config_json[TOTALCONFIG_ROOT_UPDATE].isBool())
  192. {
  193. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data::root_config_dto::update not Bool");
  194. break;
  195. }
  196. root_update = root_config_json[TOTALCONFIG_ROOT_UPDATE].asBool();
  197. if (root_update)//坑爹啊,竟然说这个root_update==false就是终端没有上收
  198. {
  199. if (!root_config_json[TOTALCONFIG_ROOT_VERSION].isString())
  200. {
  201. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data::root_config_dto::version_no not string");
  202. break;
  203. }
  204. if (!root_config_json[TOTALCONFIG_CONFIG_HEAD].isArray())
  205. {
  206. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data::root_config_dto::config not array");
  207. break;
  208. }
  209. root_version = root_config_json[TOTALCONFIG_ROOT_VERSION].asString();
  210. auto tmp_root_config = root_config_json[TOTALCONFIG_CONFIG_HEAD];
  211. for (Json::Value::ArrayIndex i = 0; i != tmp_root_config.size(); i++) {
  212. std::string module = tmp_root_config[i][TOTALCONFIG_CONFIG_MODULE].asString();
  213. std::string name = tmp_root_config[i][TOTALCONFIG_CONFIG_NAME].asString();
  214. std::string value = tmp_root_config[i][TOTALCONFIG_CONFIG_VALUE].asString();
  215. if (root_config.find(module) == root_config.end())
  216. root_config.insert(std::make_pair(module, std::map<std::string, std::string>()));
  217. root_config[module][name] = value;
  218. }
  219. }
  220. //parse shell_config
  221. auto shell_config_json = json_data[TOTALCONFIG_SHELL_HEAD];
  222. if (!shell_config_json[TOTALCONFIG_SHELL_UPDATE].isBool())
  223. {
  224. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data::shell_config_dto::update not Bool");
  225. break;
  226. }
  227. shell_update = shell_config_json[TOTALCONFIG_SHELL_UPDATE].asBool();
  228. if (shell_update)
  229. {
  230. if(!shell_config_json[TOTALCONFIG_SHELL_VERSION].isString())
  231. {
  232. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data::shell_config_dto::version_no not string");
  233. break;
  234. }
  235. if (!shell_config_json[TOTALCONFIG_CONFIG_HEAD].isArray())
  236. {
  237. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get TerminalCfgRet::data::shell_config_dto::config not string");
  238. break;
  239. }
  240. shell_version = shell_config_json[TOTALCONFIG_SHELL_VERSION].asString();
  241. auto tmp_shell_config = shell_config_json[TOTALCONFIG_CONFIG_HEAD];
  242. for (Json::Value::ArrayIndex i = 0; i != tmp_shell_config.size(); i++) {
  243. std::string module = tmp_shell_config[i][TOTALCONFIG_CONFIG_MODULE].asString();
  244. std::string name = tmp_shell_config[i][TOTALCONFIG_CONFIG_NAME].asString();
  245. std::string value = tmp_shell_config[i][TOTALCONFIG_CONFIG_VALUE].asString();
  246. if (shell_config.find(module) == shell_config.end())
  247. shell_config.insert(std::make_pair(module, std::map<std::string, std::string>()));
  248. shell_config[module][name] = value;
  249. }
  250. }
  251. return true;
  252. } while (false);
  253. return false;
  254. }
  255. std::pair<bool, std::string> VTMErrMsgCfgRet::saveVTMErrToFile(const std::string fileName)
  256. {
  257. // 转换为Json::Value
  258. Json::Value root;
  259. Json::Value config;
  260. for (int i = 0; i < errorCodeArr.size(); i++)
  261. {
  262. Json::Value error;
  263. error[VTMERRMSG_CONFIG_ERRORCODE] = errorCodeArr[i];
  264. error[VTMERRMSG_CONFIG_DESCRIPETION] = descriptionArr[i];
  265. error[VTMERRMSG_CONFIG_REMARK] = remarkArr[i];
  266. config.append(error);
  267. }
  268. root[VTMERRMSG_CONFIG_CONFIG] = config;
  269. Json::StyledWriter writer; // 或者 Json::FastWriter writer;
  270. std::string output = writer.write(root);
  271. std::ofstream outputFile(fileName);
  272. if (!outputFile.is_open())
  273. return std::make_pair(false, "file open failed");
  274. outputFile << output;
  275. outputFile.close();
  276. return std::make_pair(true, output);
  277. }
  278. std::string TerminalCfgRet::ConvertMapMapConfigToStr(const std::map<std::string, std::map<std::string, std::string>>& tmpMap)
  279. {
  280. // 转换为Json::Value
  281. Json::Value jsonMap;
  282. std::map<std::string, std::map<std::string, std::string>>::const_iterator outer_it = tmpMap.begin();
  283. std::map<std::string, std::map<std::string, std::string>>::const_iterator outer_end = tmpMap.end();
  284. for (; outer_it != outer_end; ++outer_it) {
  285. Json::Value innerMap;
  286. std::map<std::string, std::string>::const_iterator inner_it = outer_it->second.begin();
  287. std::map<std::string, std::string>::const_iterator inner_end = outer_it->second.end();
  288. for (; inner_it != inner_end; ++inner_it) {
  289. innerMap[inner_it->first] = inner_it->second;
  290. }
  291. jsonMap[outer_it->first] = innerMap;
  292. }
  293. // 生成std::string
  294. Json::StyledWriter writer; // 或者 Json::FastWriter writer;
  295. std::string output = writer.write(jsonMap);
  296. return output;
  297. }
  298. bool TerminalCfgRet::saveMapMapToFile(const std::string fileName, const std::map<std::string, std::map<std::string, std::string>>& tmpMap)
  299. {
  300. std::ofstream outputFile(fileName);
  301. if (!outputFile.is_open())
  302. return false;
  303. std::string cur = TerminalCfgRet::ConvertMapMapConfigToStr(tmpMap);
  304. /*for test
  305. std::map<std::string, std::map<std::string, std::string>> testConfig;
  306. ConvertStrToDeviceConfigMap(cur, testConfig);
  307. */
  308. outputFile << cur;
  309. outputFile.close();
  310. return true;
  311. }
  312. string TerminalCfgRet::readStrFromFile(const std::string fileName)
  313. {
  314. std::ifstream inputFile(fileName);
  315. if (!inputFile.is_open()) {
  316. return "";
  317. }
  318. std::string str((std::istreambuf_iterator<char>(inputFile)),
  319. std::istreambuf_iterator<char>());
  320. inputFile.close();
  321. return str;
  322. }
  323. string VTMErrMsgCfgReq::ToJson()
  324. {
  325. Json::Value root;
  326. root["terminal_no"] = terminal_no;
  327. root["page_num"] = page_num;
  328. Json::FastWriter writer;
  329. std::string json_str = writer.write(root);
  330. return json_str;
  331. }
  332. string TerminalCfgReq::ToJson() {
  333. // 创建一个json对象,并把结构体的数据复制到json对象中
  334. Json::Value root;
  335. root["terminal_no"] = terminalNo;
  336. if (center_version.length() > 0 && center_config.size() > 0)
  337. {
  338. root["center_version"] = center_version;
  339. Json::Value center_sub(Json::arrayValue);
  340. for (auto p = center_config.begin(); p != center_config.end(); p++) {
  341. for (auto q = p->second.begin(); q != p->second.end(); q++) {
  342. Json::Value tmp;
  343. tmp["module"] = p->first;
  344. tmp["name"] = q->first;
  345. tmp["value"] = q->second;
  346. center_sub.append(tmp);
  347. }
  348. }
  349. root["center_config"] = center_sub;
  350. }
  351. if (root_version.length() > 0 && root_config.size() > 0)
  352. {
  353. root["root_version"] = root_version;
  354. Json::Value root_sub(Json::arrayValue);
  355. for (auto p = root_config.begin(); p != root_config.end(); p++) {
  356. for (auto q = p->second.begin(); q != p->second.end(); q++) {
  357. Json::Value tmp;
  358. tmp["module"] = p->first;
  359. tmp["name"] = q->first;
  360. tmp["value"] = q->second;
  361. root_sub.append(tmp);
  362. }
  363. }
  364. root["root_config"] = root_sub;
  365. }
  366. if (shell_version.length() > 0 && shell_config.size() > 0)
  367. {
  368. root["shell_version"] = root_version;
  369. Json::Value shell_sub(Json::arrayValue);
  370. for (auto p = shell_config.begin(); p != shell_config.end(); p++) {
  371. for (auto q = p->second.begin(); q != p->second.end(); q++) {
  372. Json::Value tmp;
  373. tmp["module"] = p->first;
  374. tmp["name"] = q->first;
  375. tmp["value"] = q->second;
  376. shell_sub.append(tmp);
  377. }
  378. }
  379. root["shell_config"] = shell_sub;
  380. }
  381. // 创建一个json写入器,并把json对象转换为字符串
  382. Json::FastWriter writer;
  383. std::string json_str = writer.write(root);
  384. return json_str;
  385. }
  386. bool QueryTokenHTTPRet::Parse(std::string strData) {
  387. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("QueryTokenHTTPRet: data = %s", strData.c_str());
  388. Json::Value root;
  389. Json::Reader reader;
  390. reader.parse(strData, root);
  391. if (root.isNull() || root["data"].isNull())
  392. return false;
  393. m_token = root["data"].asString();
  394. return true;
  395. }
  396. string QueryTokenHTTPReq::ToJson() {
  397. CSimpleString businessId_str = CSimpleString::Format("{\"installVersion\":\"%s\",\"terminalNo\":\"%s\"}", installVersion.c_str(), businessId.c_str());
  398. Json::Value root;
  399. root["channelId"] = channelId;
  400. root["clientSecret"] = tokenSecret;
  401. root["businessId"] = businessId_str.GetData();
  402. Json::FastWriter writer;
  403. std::string json_str = writer.write(root);
  404. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryTokenHTTPReq:%s", json_str.c_str());
  405. return json_str;
  406. }
  407. string TerminalVerUpdateReq::ToJson() {
  408. Json::Value root;
  409. root["terminal_no"] = terminal_no;
  410. root["center_config_version"] = center_config_version;
  411. root["root_config_version"] = root_config_version;
  412. root["terminal_update_time"] = terminal_update_time;
  413. root["shell_config_version"] = shell_config_version;
  414. Json::FastWriter writer;
  415. std::string json_str = writer.write(root);
  416. return json_str;
  417. }
  418. bool TerminalVerUpdateRet::Parse(string strData) {
  419. Json::Value root;
  420. Json::Reader reader;
  421. reader.parse(strData, root, false);
  422. if (root["code"].isString())
  423. {
  424. std::string t_code = root["code"].asString();
  425. if (t_code != "SUC0000")
  426. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Can not get TerminalVerUpdateRet info , code:%d, data:%s", t_code.c_str(), strData.c_str());
  427. }
  428. if (root["success"].isBool())
  429. m_result = root["success"].asBool();
  430. return m_result;
  431. }
  432. DWORD getTerminalCfgInfoThread(LPVOID param)
  433. {
  434. TerminalCfgReq* req = (TerminalCfgReq*)param;
  435. IHttpFunc* http_client = create_http(LogCallback);
  436. if (http_client != NULL) {
  437. bool ret = http_client->Post(*req, req->ret);
  438. http_client->Destory();
  439. if (!ret)
  440. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("getTerminalCfgInfoThread failed, url:%s, terminalNo:%s, center_version:%s, root_version:%s",
  441. req->m_url.c_str(), req->terminalNo.c_str(), req->center_version.c_str(), req->root_version.c_str());
  442. else
  443. return req->ret.m_result ? 0 : -1;
  444. }
  445. Sleep(QUERY_TERMINAL_MAX_WAIT_TIME);//触发timeout
  446. return -1;
  447. }
  448. DWORD getVTMErrMsgCfgInfoThread(LPVOID param)
  449. {
  450. VTMErrMsgCfgReq* req = (VTMErrMsgCfgReq*)param;
  451. IHttpFunc* http_client = create_http(LogCallback);
  452. if (http_client != NULL) {
  453. bool ret = http_client->Post(*req, req->ret);
  454. http_client->Destory();
  455. if (!ret)
  456. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("getVTMErrMsgCfgInfoThread failed, url:%s, terminalNo:%s, page_num:%d",
  457. req->m_url.c_str(), req->terminal_no.c_str(), req->page_num);
  458. else
  459. return req->ret.m_result ? 0 : -1;
  460. }
  461. Sleep(QUERY_TERMINAL_MAX_WAIT_TIME);//触发timeout
  462. return -1;
  463. }
  464. DWORD getTokenThread(LPVOID param)
  465. {
  466. QueryTokenHTTPReq* req = (QueryTokenHTTPReq*)param;
  467. IHttpFunc* http_client = create_http(LogCallback);
  468. if (http_client != NULL) {
  469. bool ret = http_client->Post(*req, req->ret);
  470. http_client->Destory();
  471. if (ret && req->ret.m_token.length() > 0)
  472. {
  473. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("update token success"/*, req->ret.m_token.c_str()*/);
  474. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("update token success, %s", req->ret.m_token.c_str());
  475. return 0;
  476. }
  477. else
  478. {
  479. Sleep(10000);//触发timeout
  480. return -1;
  481. }
  482. }
  483. Sleep(10000);//触发timeout
  484. return -1;
  485. }
  486. std::pair<bool, std::string> refreshToken(std::string terminalNo, std::string installVersion,std::string channelId, std::string tokenSecret, std::string commonUrl)
  487. {
  488. //只要调用这个接口,就会获取新的token
  489. //判断是否应该刷新,在其他逻辑中判断
  490. CSimpleString tmpUrl = commonUrl.c_str();
  491. auto urlArr = tmpUrl.Split('|');
  492. static std::vector<QueryTokenHTTPReq> reqArr;//避免函数结束被析构
  493. reqArr.clear();
  494. for (int i = 0; i < urlArr.GetCount(); i++)
  495. {
  496. CSimpleString curUrl = urlArr[i];
  497. if (curUrl.GetLength() == 0)
  498. {
  499. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryToken error, pos:%d, %s", i, tmpUrl.GetData());
  500. continue;
  501. }
  502. QueryTokenHTTPReq req;
  503. req.channelId = channelId;
  504. req.tokenSecret = tokenSecret;
  505. req.businessId = terminalNo;
  506. req.installVersion = installVersion;
  507. req.m_url = curUrl.GetData();
  508. req.m_url.append("/api/auth/v2/token");
  509. req.m_headers.emplace(std::make_pair("Content-Type", "application/json"));
  510. reqArr.emplace_back(req);
  511. }
  512. std::vector<HANDLE> threadArr;//创建多个线程获取token
  513. for (int i = 0; i < reqArr.size(); i++)
  514. threadArr.push_back(CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&getTokenThread, &(reqArr[i]), 0, NULL));
  515. bool t_isGetTokenSuccess = false;
  516. auto tokenRet = WaitForMultipleObjects(threadArr.size(), threadArr.data(), false, 10000);
  517. if (tokenRet >= WAIT_OBJECT_0 && tokenRet <= WAIT_OBJECT_0 + threadArr.size())
  518. {
  519. DWORD exitCode = INT_MAX;
  520. GetExitCodeThread(threadArr[tokenRet - WAIT_OBJECT_0], &exitCode);
  521. if (exitCode == 0)
  522. {
  523. t_isGetTokenSuccess = true;
  524. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("total getToken success from url %s", reqArr[tokenRet - WAIT_OBJECT_0].m_url.c_str());
  525. //Token_Save::channelId = channelId.c_str();
  526. //Token_Save::token = reqArr[tokenRet - WAIT_OBJECT_0].ret.m_token;
  527. return std::make_pair(true, reqArr[tokenRet - WAIT_OBJECT_0].ret.m_token);
  528. }
  529. }
  530. LogWarn(Severity_Low, Error_Bug, GET_TOKEN_ERR, "total getToken failed");
  531. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI("refreshToken").setLogCode("QLR0402Z10A00003")("total getToken failed");
  532. return std::make_pair(false, "");
  533. }
  534. std::pair<bool, VTMErrMsgCfgRet> GetVTMErrMsgCfgFromUrl(CSimpleString url, const std::string& terminalNo)
  535. {
  536. bool ret = false;
  537. VTMErrMsgCfgRet dst;
  538. std::string rightUrl;
  539. auto urlArr = generateUrlArr(url, "/api/unify/config/query/error");
  540. if (urlArr.GetCount() == 0)
  541. {
  542. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetTerminalCfgFromUrl::urlArr is null");
  543. return std::make_pair(false, dst);
  544. }
  545. static std::vector<VTMErrMsgCfgReq> reqArr;
  546. reqArr.clear(); //每次需清理,避免多次范文
  547. for (int i = 0; i < urlArr.GetCount(); i++)
  548. {
  549. auto curUrl = urlArr[i];
  550. if (curUrl.GetLength() == 0)
  551. {
  552. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetVTMErrMsgCfgFromUrl::urlArr may be wrong, pos:%d, %s", i, url.GetData());
  553. continue;
  554. }
  555. VTMErrMsgCfgReq req;
  556. req.m_url = curUrl;
  557. req.terminal_no = terminalNo;
  558. req.page_num = 1;//the first time ,get the first page of VTM err msg list.
  559. req.m_timeOut = QUERY_TERMINAL_MAX_WAIT_TIME;
  560. reqArr.push_back(req);
  561. }
  562. std::vector<HANDLE> threadArr;//创建多个线程访问
  563. for (int i = 0; i < reqArr.size(); i++)
  564. threadArr.push_back(CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&getVTMErrMsgCfgInfoThread, &(reqArr[i]), 0, NULL));
  565. auto grayRet = WaitForMultipleObjects(threadArr.size(), threadArr.data(), false, QUERY_TERMINAL_MAX_WAIT_TIME);
  566. if (grayRet >= WAIT_OBJECT_0 && grayRet <= WAIT_OBJECT_0 + threadArr.size())
  567. {
  568. DWORD exitCode = INT_MAX;
  569. GetExitCodeThread(threadArr[grayRet - WAIT_OBJECT_0], &exitCode);
  570. if (exitCode == 0)
  571. {
  572. ret = true;
  573. dst = reqArr[grayRet - WAIT_OBJECT_0].ret;
  574. rightUrl = reqArr[grayRet - WAIT_OBJECT_0].m_url;
  575. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetVTMErrMsgCfgFromUrl success from url %s", reqArr[grayRet - WAIT_OBJECT_0].m_url.c_str());
  576. }
  577. }
  578. for (auto it = threadArr.begin(); it != threadArr.end(); it++)
  579. CloseHandle(*it);
  580. //after first time get page 2, then get the other pages
  581. for (int i = 2; i <= dst.page_total; i++)
  582. {
  583. VTMErrMsgCfgReq req;
  584. req.m_url = rightUrl;
  585. req.terminal_no = terminalNo;
  586. req.page_num = i;
  587. req.m_timeOut = QUERY_TERMINAL_MAX_WAIT_TIME;
  588. IHttpFunc* http_client = create_http(LogCallback);
  589. if (http_client != NULL) {
  590. ret = http_client->Post(req, req.ret);
  591. http_client->Destory();
  592. if (!ret)
  593. {
  594. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("getVTMErrMsgCfgInfoThread failed, url:%s, terminalNo:%s, page_num:%d",
  595. req.m_url.c_str(), req.terminal_no.c_str(), req.page_num);
  596. break;
  597. }
  598. dst.descriptionArr.insert(dst.descriptionArr.end(), req.ret.descriptionArr.begin(), req.ret.descriptionArr.end());
  599. dst.errorCodeArr.insert(dst.errorCodeArr.end(), req.ret.errorCodeArr.begin(), req.ret.errorCodeArr.end());
  600. dst.remarkArr.insert(dst.remarkArr.end(), req.ret.remarkArr.begin(), req.ret.remarkArr.end());
  601. }
  602. }
  603. return std::make_pair(ret, dst);
  604. }
  605. std::pair<bool, TerminalCfgRet> GetTerminalCfgFromUrl(CSimpleString url, const std::string& terminalNo,
  606. const std::string& center_version, const std::string& root_version, const std::string& shell_version,
  607. const std::map<std::string, std::map<std::string, std::string>>& tmp_centerConfig,
  608. const std::map<std::string, std::map<std::string, std::string>>& tmp_rootConfig,
  609. const std::map<std::string, std::map<std::string, std::string>>& tmd_shellConfig)
  610. {
  611. bool ret = false;
  612. TerminalCfgRet dst;
  613. auto urlArr = generateUrlArr(url, "/api/unify/config/query");
  614. if (urlArr.GetCount() == 0)
  615. {
  616. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetTerminalCfgFromUrl::urlArr is null");
  617. return std::make_pair(false, dst);
  618. }
  619. static std::vector<TerminalCfgReq> reqArr;
  620. reqArr.clear(); //每次需清理,避免多次范文
  621. for (int i = 0; i < urlArr.GetCount(); i++)
  622. {
  623. auto curUrl = urlArr[i];
  624. if (curUrl.GetLength() == 0)
  625. {
  626. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetTerminalCfgFromUrl::urlArr may be wrong, pos:%d, %s", i, url.GetData());
  627. continue;
  628. }
  629. TerminalCfgReq req;
  630. req.m_url = curUrl;
  631. req.terminalNo = terminalNo;
  632. req.center_version = center_version;
  633. req.root_version = root_version;
  634. req.shell_version = shell_version;
  635. req.center_config = tmp_centerConfig;
  636. req.root_config = tmp_rootConfig;
  637. req.shell_config = tmd_shellConfig;
  638. req.m_timeOut = QUERY_TERMINAL_MAX_WAIT_TIME;
  639. reqArr.emplace_back(req);
  640. }
  641. std::vector<HANDLE> threadArr;//创建多个线程访问
  642. for (int i = 0; i < reqArr.size(); i++)
  643. threadArr.push_back(CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&getTerminalCfgInfoThread, &(reqArr[i]), 0, NULL));
  644. auto grayRet = WaitForMultipleObjects(threadArr.size(), threadArr.data(), false, QUERY_TERMINAL_MAX_WAIT_TIME);
  645. if (grayRet >= WAIT_OBJECT_0 && grayRet <= WAIT_OBJECT_0 + threadArr.size())
  646. {
  647. DWORD exitCode = INT_MAX;
  648. GetExitCodeThread(threadArr[grayRet - WAIT_OBJECT_0], &exitCode);
  649. if (exitCode == 0)
  650. {
  651. ret = true;
  652. dst = reqArr[grayRet - WAIT_OBJECT_0].ret;
  653. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetTerminalCfgFromUrl success from url %s, update:%d",
  654. reqArr[grayRet - WAIT_OBJECT_0].m_url.c_str(), dst.update);
  655. }
  656. }
  657. for(auto it = threadArr.begin(); it != threadArr.end(); it++)
  658. CloseHandle(*it);
  659. return std::make_pair(ret, dst);
  660. }
  661. DWORD uploadTerminalVersionThread(LPVOID param)
  662. {
  663. TerminalVerUpdateReq* req = (TerminalVerUpdateReq*)param;
  664. TerminalVerUpdateRet dst;
  665. IHttpFunc* http_client = create_http(LogCallback);
  666. if (http_client != NULL) {
  667. bool ret = http_client->Post(*req, dst);
  668. http_client->Destory();
  669. if (!ret)
  670. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uplaodTerminalVersionThread failed, url:%s, terminalNo:%s, rootVer:%s, centerVer:%s",
  671. req->m_url.c_str(), req->terminal_no.c_str(), req->root_config_version.c_str(), req->center_config_version.c_str());
  672. else
  673. return dst.m_result ? 0 : -1;
  674. }
  675. Sleep(QUERY_TERMINAL_MAX_WAIT_TIME);//触发timeout
  676. return -1;
  677. }
  678. bool UploadTerminalVersionFromUrl(CSimpleString url, std::string terminalNo, std::string center_config_version, std::string root_config_version,
  679. std::string shell_config_version)
  680. {
  681. bool ret = false;
  682. auto urlArr = generateUrlArr(url, "/api/unify/config/add/version");
  683. if (urlArr.GetCount() == 0)
  684. {
  685. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UploadTerminalVersionFromUrl::urlArr is null");
  686. return false;
  687. }
  688. static std::vector<TerminalVerUpdateReq> reqArr;
  689. reqArr.clear(); //每次需清理,避免多次
  690. auto getTimeStr = []() -> std::string {
  691. std::time_t now = std::time(nullptr);
  692. std::tm local_time = *std::localtime(&now);
  693. // 将时间格式化为字符串
  694. std::ostringstream oss;
  695. oss << std::put_time(&local_time, "%Y-%m-%d %H:%M:%S");
  696. std::string local_time_str = oss.str();
  697. return local_time_str;
  698. };
  699. for (int i = 0; i < urlArr.GetCount(); i++)
  700. {
  701. auto curUrl = urlArr[i];
  702. if (curUrl.GetLength() == 0)
  703. {
  704. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UploadTerminalVersionFromUrl::urlArr may be wrong, pos:%d, %s", i, url.GetData());
  705. continue;
  706. }
  707. TerminalVerUpdateReq req;
  708. req.m_url = curUrl;
  709. req.terminal_no = terminalNo;
  710. req.center_config_version = center_config_version;
  711. req.root_config_version = root_config_version;
  712. req.shell_config_version = shell_config_version;
  713. req.terminal_update_time = getTimeStr();
  714. reqArr.emplace_back(req);
  715. }
  716. std::vector<HANDLE> threadArr;//创建多个线程访问
  717. for (int i = 0; i < reqArr.size(); i++)
  718. threadArr.push_back(CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&uploadTerminalVersionThread, &(reqArr[i]), 0, NULL));
  719. auto grayRet = WaitForMultipleObjects(threadArr.size(), threadArr.data(), false, QUERY_TERMINAL_MAX_WAIT_TIME);
  720. if (grayRet >= WAIT_OBJECT_0 && grayRet <= WAIT_OBJECT_0 + threadArr.size())
  721. {
  722. DWORD exitCode = INT_MAX;
  723. GetExitCodeThread(threadArr[grayRet - WAIT_OBJECT_0], &exitCode);
  724. if (exitCode == 0)
  725. {
  726. ret = true;
  727. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UploadTerminalVersionFromUrl success from url %s", reqArr[grayRet - WAIT_OBJECT_0].m_url.c_str());
  728. }
  729. }
  730. for (auto it = threadArr.begin(); it != threadArr.end(); it++)
  731. CloseHandle(*it);
  732. return ret;
  733. }
  734. int ConvertStrToVTMErrMsg(std::string input, CAutoArray<CSimpleStringA>& strErrorCodeArr
  735. , CAutoArray<CSimpleStringA>& strDescriptionArr, CAutoArray<CSimpleStringA>& strRemarkArr)
  736. {
  737. // 解析为Json::Value
  738. Json::Reader reader;
  739. Json::Value jsonValue;
  740. bool success = reader.parse(input, jsonValue);
  741. if (!success)
  742. return Error_Param;
  743. if (!jsonValue[VTMERRMSG_CONFIG_CONFIG].isArray())
  744. return Error_NotSupport;
  745. const Json::Value &config = jsonValue[VTMERRMSG_CONFIG_CONFIG];
  746. std::vector<CSimpleString> errorCodeArr, descriptionArr, remarkArr;
  747. for (auto it = config.begin(); it != config.end(); it++)
  748. {
  749. CSimpleString errorCode = (*it)[VTMERRMSG_CONFIG_ERRORCODE].asString().c_str();
  750. CSimpleString description = (*it)[VTMERRMSG_CONFIG_DESCRIPETION].asString().c_str();
  751. CSimpleString remark = SP::Utility::ToLower((*it)[VTMERRMSG_CONFIG_REMARK].asString()).c_str();
  752. errorCodeArr.push_back(errorCode);
  753. descriptionArr.push_back(description);
  754. remarkArr.push_back(remark);
  755. }
  756. strErrorCodeArr.Init(errorCodeArr.size());
  757. strDescriptionArr.Init(errorCodeArr.size());
  758. strRemarkArr.Init(errorCodeArr.size());
  759. for (int i = 0; i < errorCodeArr.size(); i++)
  760. {
  761. strErrorCodeArr[i] = errorCodeArr[i];
  762. strDescriptionArr[i] = descriptionArr[i];
  763. strRemarkArr[i] = remarkArr[i];
  764. }
  765. return Error_Succeed;
  766. }
  767. std::pair<bool, std::string> ConvertStrToDeviceConfigMap(std::string input, std::map<std::string, std::map<std::string, std::string>>& m_deviceConfig)
  768. {
  769. // 解析为Json::Value
  770. Json::Reader reader;
  771. Json::Value jsonValue;
  772. bool success = reader.parse(input, jsonValue);
  773. if (!success)
  774. return std::make_pair(false,
  775. CSimpleStringA::Format("ConvertStrToDeviceConfig Failed to parse input: %s", reader.getFormattedErrorMessages().c_str()).GetData());
  776. // 转换为std::map
  777. m_deviceConfig.clear();
  778. Json::Value::Members outer_members = jsonValue.getMemberNames();
  779. for (Json::Value::Members::iterator outer_it = outer_members.begin(); outer_it != outer_members.end(); ++outer_it) {
  780. const std::string& outer_key = *outer_it;
  781. const Json::Value& inner_value = jsonValue[outer_key];
  782. std::map<std::string, std::string> inner_map;
  783. Json::Value::Members inner_members = inner_value.getMemberNames();
  784. for (Json::Value::Members::iterator inner_it = inner_members.begin(); inner_it != inner_members.end(); ++inner_it) {
  785. const std::string& inner_key = *inner_it;
  786. const std::string& inner_value = jsonValue[outer_key][inner_key].asString();
  787. inner_map.insert(std::make_pair(inner_key, inner_value));
  788. }
  789. m_deviceConfig.insert(std::make_pair(outer_key, inner_map));
  790. }
  791. // 输出结果
  792. return std::make_pair(true, "");
  793. }
  794. /*
  795. std::pair<bool, std::string> readTerminalNo_byHttpServer()
  796. {
  797. httplib::Server server;
  798. std::string terminalNo;
  799. server.Post("/set_terminal", (httplib::Server::Handler)[&](const httplib::Request& req, httplib::Response& res) {
  800. if (req.headers.find("Content-Type") != req.headers.end() &&
  801. req.headers.find("Content-Type")->second == "application/json") {
  802. // Parse JSON payload
  803. terminalNo = req.body.c_str();
  804. server.stop();
  805. return;
  806. }
  807. // Respond with an error if parsing or validation fails
  808. res.status = 400;
  809. res.set_content("Invalid JSON or missing terminalNo", "text/plain");
  810. });
  811. server.listen("0.0.0.0", 8080);
  812. return std::make_pair(true, terminalNo);
  813. }
  814. */