httpthreadnum1.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. #include "include/httpthreadnum1.h"
  2. #include <QJsonArray>
  3. #include <QJsonDocument>
  4. #include <QJsonObject>
  5. TOnDevInfoChange callbackDevInfoChange;
  6. int EC_SetOnDevInfoChange(TOnDevInfoChange onChange, void* pUser);
  7. QString g_qsToken; //扩展
  8. QMutex g_QMutex_token;
  9. HttpThreadNum1::HttpThreadNum1()
  10. {
  11. //初始化postApi
  12. //在地址列表map中加入对应的地址
  13. //参数:数据类型和url地址
  14. m_HTN1MapApi.insert(EHTN1TYPE_LoginInfo, m_qsServiceUrl + "/token/api");
  15. m_HTN1MapApi.insert(EHTN1TYPE_GetAllCarState, m_qsServiceUrl + "/v5/api");
  16. m_HTN1MapApi.insert(EHTN1TYPE_DoStartUpCar, m_qsServiceUrl + "/v5/api");
  17. //新增接口
  18. m_HTN1MapApi.insert(EHTN1TYPE_ShutDownCar, m_qsServiceUrl + "/v5/api");
  19. m_HTN1MapApi.insert(EHTN1TYPE_RemoteRequest, m_qsServiceUrl + "/v5/api");
  20. m_HTN1MapApi.insert(EHTN1TYPE_DoStartWorkCar, m_qsServiceUrl + "/v5/api");
  21. m_HTN1MapApi.insert(EHTN1TYPE_AddCar, m_qsServiceUrl + "/v5/api");
  22. m_HTN1MapApi.insert(EHTN1TYPE_RenameCar, m_qsServiceUrl + "/v5/api");
  23. m_HTN1MapApi.insert(EHTN1TYPE_DeleteCar, m_qsServiceUrl + "/v5/api");
  24. m_HTN1MapApi.insert(EHTN1TYPE_TaskList, m_qsServiceUrl + "/v5/api");
  25. m_HTN1MapApi.insert(EHTN1TYPE_StopTask, m_qsServiceUrl + "/v5/api");
  26. m_HTN1MapApi.insert(EHTN1TYPE_CancelTask, m_qsServiceUrl + "/v5/api");
  27. m_HTN1MapApi.insert(EHTN1TYPE_GetSlagState, m_qsServiceUrl + "/v5/api");
  28. m_HTN1MapApi.insert(EHTN1TYPE_GetWarningState, m_qsServiceUrl + "/v5/api");
  29. //创建一个空的Qlist容器
  30. QList<QVariant> listTemp; //QVariant通用类型
  31. for (int i = 0; i < EHTN1TYPE_max; i++)
  32. {
  33. m_listStructReady.insert(i, listTemp);
  34. }
  35. }
  36. void HttpThreadNum1::init()
  37. {
  38. //使用qt自带的start启动线程
  39. this->start();
  40. }
  41. //适用于两种结构体类型所以使用void,传入后再分配强转成对应的类型
  42. //参数iType是QMap的键,structData是QMap的值列表中内容的地址
  43. //作用:将数据传入到双层容器中
  44. void HttpThreadNum1::postData(int iType, void* structData)
  45. {
  46. m_QMutex.lock();
  47. //创建一个接收QMap的值列表中的内容的变量
  48. QVariant qvariant;
  49. if (iType == EHTN1TYPE_LoginInfo)
  50. {
  51. qvariant = QVariant::fromValue(*((S_HTN1_LoginInfo*)structData)); //将自定义结构体类型转换为QVariant类型
  52. }
  53. else if (iType == EHTN1TYPE_GetAllCarState)
  54. {
  55. qvariant = QVariant::fromValue(*((S_HTN1_GetAllCarState*)structData));
  56. }
  57. else if (iType == EHTN1TYPE_DoStartUpCar)
  58. {
  59. qvariant = QVariant::fromValue(*((S_HTN1_DoStartUpCar*)structData));
  60. }
  61. //新增双层容器加数据
  62. else if (iType == EHTN1TYPE_ShutDownCar)
  63. {
  64. qvariant = QVariant::fromValue(*((S_HTN1_ShutDownCar*)structData));
  65. }
  66. else if (iType == EHTN1TYPE_RemoteRequest)
  67. {
  68. qvariant = QVariant::fromValue(*((S_HTN1_RemoteRequest*)structData));
  69. }
  70. else if (iType == EHTN1TYPE_DoStartWorkCar)
  71. {
  72. qvariant = QVariant::fromValue(*((S_HTN1_DoStartWorkCar*)structData));
  73. }
  74. else if (iType == EHTN1TYPE_AddCar)
  75. {
  76. qvariant = QVariant::fromValue(*((S_HTN1_AddCar*)structData));
  77. }
  78. else if (iType == EHTN1TYPE_RenameCar)
  79. {
  80. qvariant = QVariant::fromValue(*((S_HTN1_RenameCar*)structData));
  81. }
  82. else if (iType == EHTN1TYPE_DeleteCar)
  83. {
  84. qvariant = QVariant::fromValue(*((S_HTN1_DeleteCar*)structData));
  85. }
  86. else if (iType == EHTN1TYPE_TaskList)
  87. {
  88. qvariant = QVariant::fromValue(*((S_HTN1_TaskList*)structData));
  89. }
  90. else if (iType == EHTN1TYPE_StopTask)
  91. {
  92. qvariant = QVariant::fromValue(*((S_HTN1_StopTask*)structData));
  93. }
  94. else if (iType == EHTN1TYPE_CancelTask)
  95. {
  96. qvariant = QVariant::fromValue(*((S_HTN1_CancelTask*)structData));
  97. }
  98. else if (iType == EHTN1TYPE_GetSlagState)
  99. {
  100. qvariant = QVariant::fromValue(*((S_HTN1_GetSlagState*)structData));
  101. }
  102. else if (iType == EHTN1TYPE_GetWarningState)
  103. {
  104. qvariant = QVariant::fromValue(*((S_HTN1_GetWarningState*)structData));
  105. }
  106. //使用下标操作符 [] 直接访问 QMap 中键为 iType 的值,该值是一个 QList<QVariant> 对象。
  107. //然后使用 append 函数将 qvariant 添加到该列表的末尾。
  108. //如果键 iType 在 m_listStructReady 中不存在,则会自动创建一个新的键值对,其中键为 iType,值为包含 qvariant 的新列表。
  109. m_listStructReady[iType].append(qvariant);
  110. m_QMutex.unlock();
  111. }
  112. void HttpThreadNum1::setToken(QString qsTok)
  113. {
  114. g_QMutex_token.lock();
  115. g_qsToken = qsTok;
  116. g_QMutex_token.unlock();
  117. }
  118. //初始化的时候调用,启动线程
  119. void HttpThreadNum1::run()
  120. {
  121. //为什么要创建 HttpClient 对象===为了可以在priPostData中调用HttpClient类的postQString方法
  122. HttpClient* m_pHttpClient = new HttpClient;
  123. while (true)
  124. {
  125. //创建一个临时的QMap映射容器存储请求数据队列
  126. QMap<int, QList<QVariant>> mapTemp;
  127. //清空全局的QMap映射容器,清空的时候加锁,防止和写入的时候冲突
  128. m_QMutex.lock();
  129. mapTemp = m_listStructReady;
  130. for (int i = 0; i < EHTN1TYPE_max; i++)
  131. {
  132. m_listStructReady[i].clear();
  133. }
  134. m_QMutex.unlock();
  135. //创建一个迭代器
  136. QMap<int, QList<QVariant>>::iterator iter;
  137. //遍历临时的QMap映射容器
  138. for (iter = mapTemp.begin(); iter != mapTemp.end(); iter++)
  139. {
  140. if (iter.value().count() > 0)
  141. {
  142. //遍历QList请求数据队列
  143. for (int i = 0; i < iter.value().count(); i++)
  144. {
  145. //将QMap中的所有内容全部发出去
  146. priPostData(iter.key(), iter.value()[i], m_pHttpClient);
  147. }
  148. }
  149. }
  150. msleep(10);
  151. }
  152. //TODO:回调
  153. //callbackDevInfoChange(iTemp,msg);
  154. }
  155. //将请求数据队列发送给服务器,并接收服务器返回的数据
  156. void HttpThreadNum1::priPostData(int iType, QVariant structData, void* voidPointer)
  157. {
  158. QVariant resultQVariant;
  159. //post请求数据
  160. //判断类型是登录消息类型
  161. if (iType == EHTN1TYPE_LoginInfo)
  162. {
  163. //创建一个结构体
  164. S_HTN1_LoginReturn resultStruct;
  165. //接收登录信息的内容
  166. S_HTN1_LoginInfo structTemp = structData.value<S_HTN1_LoginInfo>();
  167. //设置错误字段
  168. QString qsError = "出错";
  169. //设置接收到的字符串变量
  170. QString qsRevData = "";
  171. //实际上发过去的是string,使用json转是为了方便拼接
  172. QJsonObject qjsonObject;
  173. qjsonObject.insert("username", structTemp.username);
  174. qjsonObject.insert("password", structTemp.password);
  175. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  176. //postQString中传入url地址,登录的账号密码qstring类型的数据,报错信息,接收到的字符串
  177. //postQString返回为true的时候执行
  178. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  179. {
  180. QJsonParseError jsonError;
  181. //将接收到的json数据保存到json文件中
  182. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  183. //这里也可以不用做错误判断
  184. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
  185. if (doucment.isObject()) { // JSON 文档为对象
  186. QJsonObject object = doucment.object(); // 转化为对象
  187. //将json对象中的内容传入结构体resultStruct中
  188. if (object.contains("message"))
  189. {
  190. resultStruct.message = object.value("message").toVariant().toString();
  191. }
  192. if (object.contains("uid"))
  193. {
  194. resultStruct.uid = object.value("uid").toVariant().toString();
  195. }
  196. if (object.contains("role_name"))
  197. {
  198. resultStruct.role_name = object.value("role_name").toVariant().toString();
  199. }
  200. if (object.contains("code"))
  201. {
  202. resultStruct.code = object.value("code").toVariant().toInt();
  203. }
  204. }
  205. }
  206. //将结构体转换为QVariant类型
  207. emit sig_result(EHTN1TYPE_LoginReturn, QVariant::fromValue(*((S_HTN1_LoginReturn*)&resultStruct)));
  208. }
  209. else
  210. {
  211. // qDebug()<<"连接失败"<<"发送连接失败信号";
  212. emit sig_result(EHTN1TYPE_LoginReturn, EHTN1TYPE_error);
  213. }
  214. }
  215. else if (iType == EHTN1TYPE_GetAllCarState)
  216. {
  217. S_HTN1_ReturnAllCarState resultStruct;
  218. S_HTN1_GetAllCarState structTemp = structData.value<S_HTN1_GetAllCarState>();
  219. QString qsError = "出错";
  220. QString qsRevData = "";
  221. QJsonObject qjsonObject;
  222. // qDebug()<<structTemp->code;
  223. qjsonObject.insert("code", structTemp.code);
  224. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  225. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  226. {
  227. QJsonParseError jsonError;
  228. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  229. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
  230. { // 解析未发生错误
  231. if (doucment.isObject()) { // JSON 文档为对象
  232. QJsonObject object = doucment.object(); // 转化为对象
  233. if (object.contains("code"))
  234. {
  235. resultStruct.code = object.value("code").toVariant().toInt();
  236. }
  237. if (object.contains("data"))
  238. {
  239. //数组的转成document固定写法
  240. QJsonDocument doucment2 = QJsonDocument::fromVariant(object.value("data").toVariant());
  241. // if (variant.type() == QVariant::String) {
  242. if (!doucment2.isArray())
  243. {
  244. qDebug() << "不是数组";
  245. }
  246. //数组
  247. QJsonArray tempQJsonArray = doucment2.array();
  248. for (int i = 0; i < tempQJsonArray.count(); i++)
  249. {
  250. // 解析未发生错误
  251. if (tempQJsonArray[i].isObject())
  252. {
  253. if (tempQJsonArray[i].isObject()) { // JSON文档为对象
  254. S_HTN1_ReturnOneCarState sOneStruct;
  255. QJsonObject object = tempQJsonArray[i].toObject(); //转化为对象
  256. if (object.contains("uuid"))
  257. {
  258. sOneStruct.uuid = object.value("uuid").toVariant().toString();
  259. }
  260. if (object.contains("name"))
  261. {
  262. sOneStruct.name = object.value("name").toVariant().toString();
  263. }
  264. if (object.contains("state"))
  265. {
  266. sOneStruct.state = object.value("state").toVariant().toInt();
  267. }
  268. if (object.contains("check_vehicle_direction"))
  269. {
  270. sOneStruct.check_vehicle_direction = object.value("check_vehicle_direction").toVariant().toBool();
  271. }
  272. if (object.contains("address"))
  273. {
  274. sOneStruct.address = object.value("address").toVariant().toString();
  275. }
  276. if (object.contains("current_vehicle_direction"))
  277. {
  278. sOneStruct.current_vehicle_direction = object.value("current_vehicle_direction").toVariant().toInt();
  279. }
  280. if (object.contains("current_vehicle_weight"))
  281. {
  282. sOneStruct.current_vehicle_weight = object.value("current_vehicle_weight").toVariant().toInt();
  283. }
  284. if (object.contains("vehicle_pot_state"))
  285. {
  286. sOneStruct.vehicle_pot_state = object.value("vehicle_pot_state").toVariant().toInt();
  287. }
  288. resultStruct.data.append(sOneStruct);
  289. }
  290. }
  291. }
  292. // }
  293. }
  294. }
  295. }
  296. emit sig_result(EHTN1TYPE_ReturnAllCarState, QVariant::fromValue(*((S_HTN1_ReturnAllCarState*)&resultStruct)));
  297. // qDebug()<<"http列表成功"<<qsRevData;
  298. }
  299. else
  300. {
  301. // qDebug()<<"连接失败"<<"发送连接失败信号";
  302. emit sig_result(EHTN1TYPE_ReturnAllCarState, EHTN1TYPE_error);
  303. }
  304. }
  305. else if (iType == EHTN1TYPE_DoStartUpCar)
  306. {
  307. S_HTN1_ReturnStartUpState resultStruct;
  308. S_HTN1_DoStartUpCar structTemp = structData.value<S_HTN1_DoStartUpCar>();
  309. QString qsError = "出错";
  310. QString qsRevData = "";
  311. QJsonObject qjsonObject;
  312. // qDebug()<<structTemp->code;
  313. qjsonObject.insert("code", structTemp.code);
  314. qjsonObject.insert("uuid", structTemp.uuid);
  315. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  316. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  317. {
  318. QJsonParseError jsonError;
  319. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  320. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
  321. if (doucment.isObject()) { // JSON 文档为对象
  322. QJsonObject object = doucment.object(); // 转化为对象
  323. if (object.contains("code"))
  324. {
  325. resultStruct.code = object.value("code").toVariant().toInt();
  326. }
  327. if (object.contains("data"))
  328. {
  329. resultStruct.data = object.value("data").toVariant().toString();
  330. }
  331. }
  332. }
  333. emit sig_result(EHTN1TYPE_ReturnStartUpState, QVariant::fromValue(*((S_HTN1_ReturnStartUpState*)&resultStruct)));
  334. // qDebug()<<"http列表成功"<<qsRevData;
  335. }
  336. else
  337. {
  338. // qDebug()<<"连接失败"<<"发送连接失败信号";
  339. emit sig_result(EHTN1TYPE_ReturnStartUpState, EHTN1TYPE_error);
  340. }
  341. }
  342. else if (iType == EHTN1TYPE_ShutDownCar)
  343. {
  344. S_HTN1_ReturnShutDownState resultStruct;
  345. S_HTN1_ShutDownCar structTemp = structData.value<S_HTN1_ShutDownCar>();
  346. QString qsError = "出错";
  347. QString qsRevData = "";
  348. QJsonObject qjsonObject;
  349. // qDebug()<<structTemp->code;
  350. qjsonObject.insert("code", structTemp.code);
  351. qjsonObject.insert("uuid", structTemp.uuid);
  352. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  353. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  354. {
  355. QJsonParseError jsonError;
  356. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  357. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
  358. if (doucment.isObject()) { // JSON 文档为对象
  359. QJsonObject object = doucment.object(); // 转化为对象
  360. if (object.contains("code"))
  361. {
  362. resultStruct.code = object.value("code").toVariant().toInt();
  363. }
  364. if (object.contains("data"))
  365. {
  366. resultStruct.data = object.value("data").toVariant().toString();
  367. }
  368. }
  369. }
  370. emit sig_result(EHTN1TYPE_ReturnShutDownState, QVariant::fromValue(*((S_HTN1_ReturnShutDownState*)&resultStruct)));
  371. // qDebug()<<"http列表成功"<<qsRevData;
  372. }
  373. else
  374. {
  375. // qDebug()<<"连接失败"<<"发送连接失败信号";
  376. emit sig_result(EHTN1TYPE_ReturnShutDownState, EHTN1TYPE_error);
  377. }
  378. }
  379. else if (iType == EHTN1TYPE_RemoteRequest)
  380. {
  381. S_HTN1_ReturnRemoteRequestState resultStruct;
  382. S_HTN1_RemoteRequest structTemp = structData.value<S_HTN1_RemoteRequest>();
  383. QString qsError = "出错";
  384. QString qsRevData = "";
  385. QJsonObject qjsonObject;
  386. // qDebug()<<structTemp->code;
  387. qjsonObject.insert("code", structTemp.code);
  388. qjsonObject.insert("uuid", structTemp.uuid);
  389. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  390. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  391. {
  392. QJsonParseError jsonError;
  393. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  394. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
  395. if (doucment.isObject()) { // JSON 文档为对象
  396. QJsonObject object = doucment.object(); // 转化为对象
  397. if (object.contains("code"))
  398. {
  399. resultStruct.code = object.value("code").toVariant().toInt();
  400. }
  401. if (object.contains("data"))
  402. {
  403. resultStruct.data = object.value("data").toVariant().toString();
  404. }
  405. }
  406. }
  407. emit sig_result(EHTN1TYPE_ReturnRemoteRequestState, QVariant::fromValue(*((S_HTN1_ReturnRemoteRequestState*)&resultStruct)));
  408. // qDebug()<<"http列表成功"<<qsRevData;
  409. }
  410. else
  411. {
  412. // qDebug()<<"连接失败"<<"发送连接失败信号";
  413. emit sig_result(EHTN1TYPE_ReturnRemoteRequestState, EHTN1TYPE_error);
  414. }
  415. }
  416. else if (iType == EHTN1TYPE_DoStartWorkCar)
  417. {
  418. S_HTN1_ReturnDoStartWorkState resultStruct;
  419. S_HTN1_DoStartWorkCar structTemp = structData.value<S_HTN1_DoStartWorkCar>();
  420. QString qsError = "出错";
  421. QString qsRevData = "";
  422. QJsonObject qjsonObject;
  423. // qDebug()<<structTemp->code;
  424. qjsonObject.insert("code", structTemp.code);
  425. qjsonObject.insert("uuid", structTemp.uuid);
  426. qjsonObject.insert("task_type", structTemp.task_type);
  427. qjsonObject.insert("target_point_name", structTemp.target_point_name);
  428. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  429. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  430. {
  431. QJsonParseError jsonError;
  432. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  433. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
  434. if (doucment.isObject()) { // JSON 文档为对象
  435. QJsonObject object = doucment.object(); // 转化为对象
  436. if (object.contains("code"))
  437. {
  438. resultStruct.code = object.value("code").toVariant().toInt();
  439. }
  440. if (object.contains("data"))
  441. {
  442. resultStruct.data = object.value("data").toVariant().toString();
  443. }
  444. }
  445. }
  446. emit sig_result(EHTN1TYPE_ReturnStartWorkState, QVariant::fromValue(*((S_HTN1_ReturnDoStartWorkState*)&resultStruct)));
  447. // qDebug()<<"http列表成功"<<qsRevData;
  448. }
  449. else
  450. {
  451. // qDebug()<<"连接失败"<<"发送连接失败信号";
  452. emit sig_result(EHTN1TYPE_ReturnStartWorkState, EHTN1TYPE_error);
  453. }
  454. }
  455. else if (iType == EHTN1TYPE_AddCar)
  456. {
  457. S_HTN1_ReturnAddCarState resultStruct;
  458. S_HTN1_AddCar structTemp = structData.value<S_HTN1_AddCar>();
  459. QString qsError = "出错";
  460. QString qsRevData = "";
  461. QJsonObject qjsonObject;
  462. // qDebug()<<structTemp->code;
  463. qjsonObject.insert("code", structTemp.code);
  464. qjsonObject.insert("name", structTemp.name);
  465. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  466. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  467. {
  468. QJsonParseError jsonError;
  469. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  470. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
  471. if (doucment.isObject()) { // JSON 文档为对象
  472. QJsonObject object = doucment.object(); // 转化为对象
  473. if (object.contains("code"))
  474. {
  475. resultStruct.code = object.value("code").toVariant().toInt();
  476. }
  477. if (object.contains("data"))
  478. {
  479. resultStruct.data = object.value("data").toVariant().toString();
  480. }
  481. }
  482. }
  483. emit sig_result(EHTN1TYPE_ReturnAddCarState, QVariant::fromValue(*((S_HTN1_ReturnAddCarState*)&resultStruct)));
  484. // qDebug()<<"http列表成功"<<qsRevData;
  485. }
  486. else
  487. {
  488. // qDebug()<<"连接失败"<<"发送连接失败信号";
  489. emit sig_result(EHTN1TYPE_ReturnAddCarState, EHTN1TYPE_error);
  490. }
  491. }
  492. else if (iType == EHTN1TYPE_RenameCar)
  493. {
  494. S_HTN1_ReturnReanmeCarState resultStruct;
  495. S_HTN1_RenameCar structTemp = structData.value<S_HTN1_RenameCar>();
  496. QString qsError = "出错";
  497. QString qsRevData = "";
  498. QJsonObject qjsonObject;
  499. // qDebug()<<structTemp->code;
  500. qjsonObject.insert("code", structTemp.code);
  501. qjsonObject.insert("uuid", structTemp.uuid);
  502. qjsonObject.insert("name", structTemp.name);
  503. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  504. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  505. {
  506. QJsonParseError jsonError;
  507. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  508. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
  509. if (doucment.isObject()) { // JSON 文档为对象
  510. QJsonObject object = doucment.object(); // 转化为对象
  511. if (object.contains("code"))
  512. {
  513. resultStruct.code = object.value("code").toVariant().toInt();
  514. }
  515. if (object.contains("data"))
  516. {
  517. //数组的转成document固定写法
  518. QJsonDocument doucment2 = QJsonDocument::fromVariant(object.value("data").toVariant());
  519. //数组
  520. QJsonArray tempQJsonArray = doucment2.array();
  521. for (int i = 0; i < tempQJsonArray.count(); i++)
  522. {
  523. // 解析未发生错误
  524. if (tempQJsonArray[i].isObject())
  525. {
  526. if (tempQJsonArray[i].isObject()) { // JSON文档为对象
  527. S_HTN1_ReturnReanmeCarState_data sOneStruct;
  528. QJsonObject object = tempQJsonArray[i].toObject(); //转化为对象
  529. if (object.contains("uuid"))
  530. {
  531. sOneStruct.uuid = object.value("uuid").toVariant().toString();
  532. }
  533. if (object.contains("name"))
  534. {
  535. sOneStruct.name = object.value("name").toVariant().toString();
  536. }
  537. if (object.contains("update_at"))
  538. {
  539. sOneStruct.update_at = object.value("update_at").toVariant().toInt();
  540. }
  541. resultStruct.data.append(sOneStruct);
  542. }
  543. }
  544. }
  545. }
  546. }
  547. }
  548. emit sig_result(EHTN1TYPE_ReturnRenameCarState, QVariant::fromValue(*((S_HTN1_ReturnReanmeCarState*)&resultStruct)));
  549. // qDebug()<<"http列表成功"<<qsRevData;
  550. }
  551. else
  552. {
  553. // qDebug()<<"连接失败"<<"发送连接失败信号";
  554. emit sig_result(EHTN1TYPE_ReturnRenameCarState, EHTN1TYPE_error);
  555. }
  556. }
  557. else if (iType == EHTN1TYPE_DeleteCar)
  558. {
  559. S_HTN1_ReturnDeleteCarState resultStruct;
  560. S_HTN1_DeleteCar structTemp = structData.value<S_HTN1_DeleteCar>();
  561. QString qsError = "出错";
  562. QString qsRevData = "";
  563. QJsonObject qjsonObject;
  564. // qDebug()<<structTemp->code;
  565. qjsonObject.insert("code", structTemp.code);
  566. qjsonObject.insert("uuid", structTemp.uuid);
  567. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  568. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  569. {
  570. QJsonParseError jsonError;
  571. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  572. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
  573. if (doucment.isObject()) { // JSON 文档为对象
  574. QJsonObject object = doucment.object(); // 转化为对象
  575. if (object.contains("code"))
  576. {
  577. resultStruct.code = object.value("code").toVariant().toInt();
  578. }
  579. if (object.contains("data"))
  580. {
  581. resultStruct.data = object.value("data").toVariant().toString();
  582. }
  583. }
  584. }
  585. emit sig_result(EHTN1TYPE_ReturnDeleteCarState, QVariant::fromValue(*((S_HTN1_ReturnDeleteCarState*)&resultStruct)));
  586. // qDebug()<<"http列表成功"<<qsRevData;
  587. }
  588. else
  589. {
  590. // qDebug()<<"连接失败"<<"发送连接失败信号";
  591. emit sig_result(EHTN1TYPE_ReturnDeleteCarState, EHTN1TYPE_error);
  592. }
  593. }
  594. else if (iType == EHTN1TYPE_TaskList)
  595. {
  596. S_HTN1_ReturnTaskListData resultStruct;
  597. S_HTN1_TaskList structTemp = structData.value<S_HTN1_TaskList>();
  598. QString qsError = "出错";
  599. QString qsRevData = "";
  600. QJsonObject qjsonObject;
  601. // qDebug()<<structTemp->code;
  602. qjsonObject.insert("code", structTemp.code);
  603. qjsonObject.insert("page", structTemp.page);
  604. qjsonObject.insert("size", structTemp.size);
  605. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  606. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  607. {
  608. QJsonParseError jsonError;
  609. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  610. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
  611. if (doucment.isObject()) { // JSON 文档为对象
  612. QJsonObject object = doucment.object(); // 转化为对象
  613. if (object.contains("code"))
  614. {
  615. resultStruct.code = object.value("code").toVariant().toInt();
  616. }
  617. if (object.contains("page"))
  618. {
  619. resultStruct.page = object.value("page").toVariant().toInt();
  620. }
  621. if (object.contains("size"))
  622. {
  623. resultStruct.size = object.value("size").toVariant().toInt();
  624. }
  625. if (object.contains("total"))
  626. {
  627. resultStruct.total = object.value("total").toVariant().toInt();
  628. }
  629. if (object.contains("data"))
  630. {
  631. //数组的转成document固定写法
  632. QJsonDocument doucment2 = QJsonDocument::fromVariant(object.value("data").toVariant());
  633. //数组
  634. QJsonArray tempQJsonArray = doucment2.array();
  635. for (int i = 0; i < tempQJsonArray.count(); i++)
  636. {
  637. // 解析未发生错误
  638. if (tempQJsonArray[i].isObject())
  639. {
  640. if (tempQJsonArray[i].isObject()) { // JSON文档为对象
  641. S_HTN1_ReturnTaskListData_data sOneStruct;
  642. QJsonObject object = tempQJsonArray[i].toObject(); //转化为对象
  643. if (object.contains("uuid"))
  644. {
  645. sOneStruct.uuid = object.value("uuid").toVariant().toString();
  646. }
  647. if (object.contains("vehicle_uuid"))
  648. {
  649. sOneStruct.vehicle_uuid = object.value("vehicle_uuid").toVariant().toString();
  650. }
  651. if (object.contains("task_type"))
  652. {
  653. sOneStruct.task_type = object.value("task_type").toVariant().toInt();
  654. }
  655. if (object.contains("target_point_name"))
  656. {
  657. sOneStruct.target_point_name = object.value("target_point_name").toVariant().toString();
  658. }
  659. if (object.contains("task_state"))
  660. {
  661. sOneStruct.task_state = object.value("task_state").toVariant().toInt();
  662. }
  663. if (object.contains("create_at"))
  664. {
  665. sOneStruct.create_at = object.value("create_at").toVariant().toInt();
  666. }
  667. resultStruct.data.append(sOneStruct);
  668. }
  669. }
  670. }
  671. }
  672. }
  673. }
  674. emit sig_result(EHTN1TYPE_ReturnTaskListData, QVariant::fromValue(*((S_HTN1_ReturnTaskListData*)&resultStruct)));
  675. // qDebug()<<"http列表成功"<<qsRevData;
  676. }
  677. else
  678. {
  679. // qDebug()<<"连接失败"<<"发送连接失败信号";
  680. emit sig_result(EHTN1TYPE_ReturnTaskListData, EHTN1TYPE_error);
  681. }
  682. }
  683. else if (iType == EHTN1TYPE_StopTask)
  684. {
  685. S_HTN1_ReturnStopTask resultStruct;
  686. S_HTN1_StopTask structTemp = structData.value<S_HTN1_StopTask>();
  687. QString qsError = "出错";
  688. QString qsRevData = "";
  689. QJsonObject qjsonObject;
  690. // qDebug()<<structTemp->code;
  691. qjsonObject.insert("code", structTemp.code);
  692. qjsonObject.insert("uuid", structTemp.uuid);
  693. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  694. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  695. {
  696. QJsonParseError jsonError;
  697. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  698. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
  699. if (doucment.isObject()) { // JSON 文档为对象
  700. QJsonObject object = doucment.object(); // 转化为对象
  701. if (object.contains("code"))
  702. {
  703. resultStruct.code = object.value("code").toVariant().toInt();
  704. }
  705. if (object.contains("data"))
  706. {
  707. resultStruct.data = object.value("data").toVariant().toString();
  708. }
  709. }
  710. }
  711. emit sig_result(EHTN1TYPE_ReturnStopTask, QVariant::fromValue(*((S_HTN1_ReturnStopTask*)&resultStruct)));
  712. // qDebug()<<"http列表成功"<<qsRevData;
  713. }
  714. else
  715. {
  716. // qDebug()<<"连接失败"<<"发送连接失败信号";
  717. emit sig_result(EHTN1TYPE_ReturnStopTask, EHTN1TYPE_error);
  718. }
  719. }
  720. else if (iType == EHTN1TYPE_CancelTask)
  721. {
  722. S_HTN1_ReturnCancelTask resultStruct;
  723. S_HTN1_CancelTask structTemp = structData.value<S_HTN1_CancelTask>();
  724. QString qsError = "出错";
  725. QString qsRevData = "";
  726. QJsonObject qjsonObject;
  727. // qDebug()<<structTemp->code;
  728. qjsonObject.insert("code", structTemp.code);
  729. qjsonObject.insert("uuid", structTemp.uuid);
  730. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  731. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  732. {
  733. QJsonParseError jsonError;
  734. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  735. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
  736. if (doucment.isObject()) { // JSON 文档为对象
  737. QJsonObject object = doucment.object(); // 转化为对象
  738. if (object.contains("code"))
  739. {
  740. resultStruct.code = object.value("code").toVariant().toInt();
  741. }
  742. if (object.contains("data"))
  743. {
  744. resultStruct.data = object.value("data").toVariant().toString();
  745. }
  746. }
  747. }
  748. emit sig_result(EHTN1TYPE_ReturnCancelTask, QVariant::fromValue(*((S_HTN1_ReturnCancelTask*)&resultStruct)));
  749. // qDebug()<<"http列表成功"<<qsRevData;
  750. }
  751. else
  752. {
  753. // qDebug()<<"连接失败"<<"发送连接失败信号";
  754. emit sig_result(EHTN1TYPE_ReturnCancelTask, EHTN1TYPE_error);
  755. }
  756. }
  757. else if (iType == EHTN1TYPE_GetSlagState)
  758. {
  759. S_HTN1_ReturnSlagState resultStruct;
  760. S_HTN1_GetSlagState structTemp = structData.value<S_HTN1_GetSlagState>();
  761. QString qsError = "出错";
  762. QString qsRevData = "";
  763. QJsonObject qjsonObject;
  764. // qDebug()<<structTemp->code;
  765. qjsonObject.insert("code", structTemp.code);
  766. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  767. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  768. {
  769. QJsonParseError jsonError;
  770. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  771. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
  772. if (doucment.isObject()) { // JSON 文档为对象
  773. QJsonObject object = doucment.object(); // 转化为对象
  774. if (object.contains("code"))
  775. {
  776. resultStruct.code = object.value("code").toVariant().toInt();
  777. }
  778. if (object.contains("data"))
  779. {
  780. //数组的转成document固定写法
  781. QJsonDocument doucment2 = QJsonDocument::fromVariant(object.value("data").toVariant());
  782. //数组
  783. QJsonArray tempQJsonArray = doucment2.array();
  784. for (int i = 0; i < tempQJsonArray.count(); i++)
  785. {
  786. // 解析未发生错误
  787. if (tempQJsonArray[i].isObject())
  788. {
  789. if (tempQJsonArray[i].isObject()) { // JSON文档为对象
  790. S_HTN1_ReturnSlagState_data sOneStruct;
  791. QJsonObject object = tempQJsonArray[i].toObject(); //转化为对象
  792. if (object.contains("pot_name"))
  793. {
  794. sOneStruct.pot_name = object.value("pot_name").toVariant().toString();
  795. }
  796. if (object.contains("pot_status"))
  797. {
  798. sOneStruct.pot_status = object.value("pot_status").toVariant().toInt();
  799. }
  800. if (object.contains("pot_number"))
  801. {
  802. sOneStruct.pot_number = object.value("pot_number").toVariant().toInt();
  803. }
  804. resultStruct.data.append(sOneStruct);
  805. }
  806. }
  807. }
  808. }
  809. }
  810. }
  811. emit sig_result(EHTN1TYPE_ReturnSlagState, QVariant::fromValue(*((S_HTN1_ReturnSlagState*)&resultStruct)));
  812. // qDebug()<<"http列表成功"<<qsRevData;
  813. }
  814. else
  815. {
  816. // qDebug()<<"连接失败"<<"发送连接失败信号";
  817. emit sig_result(EHTN1TYPE_ReturnSlagState, EHTN1TYPE_error);
  818. }
  819. }
  820. else if (iType == EHTN1TYPE_GetWarningState)
  821. {
  822. S_HTN1_ReturnWarningState resultStruct;
  823. S_HTN1_GetWarningState structTemp = structData.value<S_HTN1_GetWarningState>();
  824. QString qsError = "出错";
  825. QString qsRevData = "";
  826. QJsonObject qjsonObject;
  827. // qDebug()<<structTemp->code;
  828. qjsonObject.insert("code", structTemp.code);
  829. QString qsPostData(QJsonDocument(qjsonObject).toJson()); //qjson转qstring
  830. if (((HttpClient*)voidPointer)->postQString(m_HTN1MapApi[iType], qsPostData, &qsError, &qsRevData))
  831. {
  832. QJsonParseError jsonError;
  833. QJsonDocument doucment = QJsonDocument::fromJson(qsRevData.toUtf8().data());
  834. if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
  835. if (doucment.isObject()) { // JSON 文档为对象
  836. QJsonObject object = doucment.object(); // 转化为对象
  837. if (object.contains("code"))
  838. {
  839. resultStruct.code = object.value("code").toVariant().toInt();
  840. }
  841. if (object.contains("data"))
  842. {
  843. //数组的转成document固定写法
  844. QJsonDocument doucment2 = QJsonDocument::fromVariant(object.value("data").toVariant());
  845. //数组
  846. QJsonArray tempQJsonArray = doucment2.array();
  847. for (int i = 0; i < tempQJsonArray.count(); i++)
  848. {
  849. // 解析未发生错误
  850. if (tempQJsonArray[i].isObject())
  851. {
  852. if (tempQJsonArray[i].isObject()) { // JSON文档为对象
  853. S_HTN1_ReturnWarningState_data sOneStruct;
  854. QJsonObject object = tempQJsonArray[i].toObject(); //转化为对象
  855. if (object.contains("message"))
  856. {
  857. sOneStruct.message = object.value("message").toVariant().toString();
  858. }
  859. if (object.contains("create_at"))
  860. {
  861. sOneStruct.create_at = object.value("create_at").toVariant().toInt();
  862. }
  863. resultStruct.data.append(sOneStruct);
  864. }
  865. }
  866. }
  867. }
  868. }
  869. }
  870. emit sig_result(EHTN1TYPE_ReturnWarningState, QVariant::fromValue(*((S_HTN1_ReturnWarningState*)&resultStruct)));
  871. // qDebug()<<"http列表成功"<<qsRevData;
  872. }
  873. else
  874. {
  875. // qDebug()<<"连接失败"<<"发送连接失败信号";
  876. emit sig_result(EHTN1TYPE_ReturnWarningState, EHTN1TYPE_error);
  877. }
  878. }
  879. }
  880. //回调函数,暂时没有用
  881. int EC_SetOnDevInfoChange(TOnDevInfoChange onChange, void* pUser)
  882. {
  883. void* m_DevInfoUser = pUser;
  884. callbackDevInfoChange = onChange;
  885. printf("nnnn");
  886. return 0;
  887. }