jhws.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. * jhws.cpp
  3. *
  4. * Created on: 2022年4月22日
  5. * Author: pengzc
  6. */
  7. #include "jhws.h"
  8. #include "base/http_client.h"
  9. #include "base/json_object.h"
  10. #include "base/base.hpp"
  11. #include "json/json.h"
  12. #include "core/houseinfo.h"
  13. #include "utils/Log.h"
  14. #include <list>
  15. #include <vector>
  16. #include <iterator>
  17. #include <stdlib.h>
  18. #include "storage/StoragePreferences.h"
  19. #include "sip_config.h"
  20. #include "core/urlencode.h"
  21. #include "core/utilities.h"
  22. namespace jhws {
  23. const char* kBaseUrl = "http://wuye.jhws.top";
  24. const char* bearer = "bearer ";
  25. std::string authorization = StoragePreferences::getString(SIP_JHWS_TOKEN, "");
  26. int ErrorInfo(base::HttpResponse response) {
  27. Json::Reader reader;
  28. Json::Value root;
  29. LOGD("连接失败");
  30. LOGD("返回体:%s",response.Body().c_str());
  31. LOGD("状态码:%d", response.StatusCode());
  32. LOGD("错误码:%d", response.ErrorCode());
  33. LOGD("错误信息:%s", response.ErrorMessage().c_str());
  34. LOGD("http信息:%s", response.ToString().c_str());
  35. reader.parse(response.Body(), root);
  36. if(response.StatusCode() == 0 && response.ErrorCode() == 28) {
  37. return 1;
  38. }
  39. if(response.StatusCode() == 400 && response.ErrorCode() == 0) {
  40. if(root["code"].asInt() == 400)
  41. {
  42. return -1;
  43. }
  44. return root["code"].asInt();
  45. }
  46. if(response.StatusCode() == 401 && response.ErrorCode() == 0 && root["code"].asInt() == 1001) {
  47. return 4;
  48. }
  49. return 2;
  50. }
  51. // int ErrorJson(base::HttpResponse response) {
  52. // Json::Reader reader;
  53. // Json::Value root;
  54. //
  55. // if(!reader.parse(response.Body(), root)) {
  56. // LOGD("解析错误不是Json格式!");
  57. // return 2;
  58. // }
  59. //
  60. // if(!root.isMember("data") || root["data"][0].isNull() || !root["data"].isArray()) {
  61. // LOGD("解析数据体:%s", root.toStyledString().c_str());
  62. // return 3;
  63. // }
  64. // return 2;
  65. // }
  66. int GetToken(const std::string& id, const std::string& secret, const std::string& encrypted, j::house_setting* profile) {
  67. base::HttpClient client;
  68. base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/client/anon/client_token", "");
  69. req.AppendHeader("client_id", id);
  70. req.AppendHeader("client_secret", secret);
  71. req.AppendHeader("encrypted", encrypted);
  72. client.SetConnectionTimeout(2000);
  73. client.SetTimeout(3000);
  74. base::HttpResponse response = client.Do(req);
  75. Json::Reader reader;
  76. Json::Value root;
  77. //判断返回是否正确
  78. if(response.StatusCode() != 200) {
  79. return ErrorInfo(response);
  80. }
  81. //判断是否为Json格式
  82. if(!reader.parse(response.Body(), root)) {
  83. LOGD("解析错误不是Json格式!");
  84. return 2;
  85. }
  86. if(!root.isMember("data")){
  87. return 2;
  88. }
  89. std::string accessToken;
  90. accessToken = root["data"]["accessToken"].asString();
  91. profile->Token = bearer + std::string(accessToken);
  92. authorization = profile->Token;
  93. // StoragePreferences::putString(SIP_JHWS_TOKEN, profile->Token);
  94. return 0;
  95. }
  96. int GetCommunities(jhws::CommunityList& list, j::house_setting* profile) {
  97. if(authorization == ""){
  98. int j = GetToken("778558024368394240", "d1330c96195c3b7b653cd60bb0ea5ded", "false", profile);
  99. if(j == 1) return 1;
  100. }
  101. // LOGD("author: %s", authorization.c_str());
  102. std::string paramurl;
  103. encodeUrl(profile->name, paramurl);
  104. paramurl = "?code=" + std::string(profile->code) + "&name=" + std::string(paramurl);
  105. // LOGD("paramurl: %s", paramurl.c_str());
  106. base::HttpClient client;
  107. base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/community/grant/communitys" + std::string(paramurl), "");
  108. req.AppendHeader("Authorization", authorization);
  109. client.SetConnectionTimeout(2000);
  110. client.SetTimeout(3000);
  111. base::HttpResponse response = client.Do(req);
  112. if(response.StatusCode() != 200) {
  113. return ErrorInfo(response);
  114. }
  115. Json::Reader reader;
  116. Json::Value root;
  117. jhws::Community s;
  118. if(!reader.parse(response.Body(), root)) {
  119. LOGD("解析错误不是Json格式!");
  120. return 2;
  121. }
  122. if(!root.isMember("data") || root["data"][0].isNull() || !root["data"].isArray()) {
  123. LOGD("解析数据体:%s", root.toStyledString().c_str());
  124. return 3;
  125. }
  126. for(unsigned int i = 0; i < root["data"].size(); i++) {
  127. s.code = root["data"][i]["code"].asInt();
  128. s.name = root["data"][i]["name"].asString();
  129. s.communityId = root["data"][i]["id"].asString();
  130. list.push_back(s);
  131. }
  132. return 0;
  133. }
  134. int GetAreas(jhws::AreaList& list, j::house_setting* profile) {
  135. std::string Paramurl;
  136. std::string communityId = profile->communityId;
  137. // LOGD("Area这里:%s", communityId.c_str());
  138. base::HttpClient client;
  139. base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/houseinfo/grant/area/"+std::string(communityId)+"/areas", "");
  140. req.AppendHeader("Authorization", authorization);
  141. client.SetConnectionTimeout(2000);
  142. client.SetTimeout(3000);
  143. base::HttpResponse response = client.Do(req);
  144. if(response.StatusCode() != 200) {
  145. return ErrorInfo(response);
  146. }
  147. Json::Reader reader;
  148. Json::Value root;
  149. jhws::Area s;
  150. if(!reader.parse(response.Body(), root)) {
  151. LOGD("解析错误不是Json格式!");
  152. return 2;
  153. }
  154. if(!root.isMember("data") || root["data"][0].isNull()) {
  155. return 3;
  156. }
  157. for(unsigned int i = 0; i < root["data"].size(); i++) {
  158. s.code = root["data"][i]["code"].asInt();
  159. s.name = root["data"][i]["name"].asString();
  160. s.areaId = root["data"][i]["id"].asString();
  161. list.push_back(s);
  162. }
  163. return 0;
  164. }
  165. int GetBuildings(BuildingList& list, j::house_setting* profile) {
  166. std::string Paramurl;
  167. std::string areaId = profile->areaId;
  168. // LOGD("build这里:%s", areaId.c_str());
  169. base::HttpClient client;
  170. base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/houseinfo/grant/building/"+std::string(areaId)+"/buildings", "");
  171. req.AppendHeader("Authorization", authorization);
  172. client.SetConnectionTimeout(2000);
  173. client.SetTimeout(3000);
  174. base::HttpResponse response = client.Do(req);
  175. // LOGD("得到的数据:%s", response.Body().c_str());
  176. if(response.StatusCode() != 200) {
  177. return ErrorInfo(response);
  178. }
  179. Json::Reader reader;
  180. Json::Value root;
  181. jhws::Building s;
  182. if(!reader.parse(response.Body(), root)) {
  183. LOGD("解析错误不是Json格式!");
  184. return 2;
  185. }
  186. if(!root.isMember("data") || root["data"][0].isNull()) {
  187. return 3;
  188. }
  189. for(unsigned int i = 0; i < root["data"].size(); i++) {
  190. s.code = root["data"][i]["code"].asInt();
  191. s.name = root["data"][i]["name"].asString();
  192. s.buildId = root["data"][i]["id"].asString();
  193. list.push_back(s);
  194. }
  195. return 0;
  196. }
  197. int GetUnits(UnitList& list, j::house_setting* profile) {
  198. std::string buildId = profile->buildId;
  199. base::HttpClient client;
  200. base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/houseinfo/grant/unit/"+std::string(buildId)+"/units", "");
  201. req.AppendHeader("Authorization", authorization);
  202. client.SetConnectionTimeout(2000);
  203. client.SetTimeout(3000);
  204. base::HttpResponse response = client.Do(req);
  205. // LOGD("得到的数据:%s", response.Body().c_str());
  206. if(response.StatusCode() != 200) {
  207. return ErrorInfo(response);
  208. }
  209. Json::Reader reader;
  210. Json::Value root;
  211. jhws::Unit s;
  212. if(!reader.parse(response.Body(), root)) {
  213. LOGD("解析错误不是Json格式!");
  214. return 2;
  215. }
  216. if(!root.isMember("data") || root["data"][0].isNull()) {
  217. return 3;
  218. }
  219. for(unsigned int i = 0; i < root["data"].size(); i++) {
  220. s.code = root["data"][i]["code"].asInt();
  221. s.name = root["data"][i]["name"].asString();
  222. s.unitId = root["data"][i]["id"].asString();
  223. list.push_back(s);
  224. }
  225. return 0;
  226. }
  227. int GetFloors(FloorList& list, j::house_setting* profile) {
  228. std::string unitId = profile->unitId;
  229. base::HttpClient client;
  230. base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/houseinfo/grant/floor/"+std::string(unitId)+"/floors", "");
  231. req.AppendHeader("Authorization", authorization);
  232. client.SetConnectionTimeout(2000);
  233. client.SetTimeout(3000);
  234. base::HttpResponse response = client.Do(req);
  235. // LOGD("得到的数据:%s", response.Body().c_str());
  236. if(response.StatusCode() != 200) {
  237. return ErrorInfo(response);
  238. }
  239. Json::Reader reader;
  240. Json::Value root;
  241. jhws::Floor s;
  242. if(!reader.parse(response.Body(), root)) {
  243. LOGD("解析错误不是Json格式!");
  244. return 2;
  245. }
  246. if(!root.isMember("data") || root["data"][0].isNull()) {
  247. return 3;
  248. }
  249. for(unsigned int i = 0; i < root["data"].size(); i++) {
  250. s.code = root["data"][i]["code"].asInt();
  251. s.name = root["data"][i]["name"].asString();
  252. s.floorId = root["data"][i]["id"].asString();
  253. list.push_back(s);
  254. }
  255. return 0;
  256. }
  257. int GetRooms(RoomList& list, j::house_setting* profile) {
  258. std::string floorId = profile->floorId;
  259. base::HttpClient client;
  260. base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/houseinfo/grant/room/"+std::string(floorId)+"/rooms", "");
  261. req.AppendHeader("Authorization", authorization);
  262. client.SetConnectionTimeout(2000);
  263. client.SetTimeout(3000);
  264. base::HttpResponse response = client.Do(req);
  265. // LOGD("得到的数据:%s", response.Body().c_str());
  266. if(response.StatusCode() != 200) {
  267. return ErrorInfo(response);
  268. }
  269. Json::Reader reader;
  270. Json::Value root;
  271. jhws::Room s;
  272. if(!reader.parse(response.Body(), root)) {
  273. LOGD("解析错误不是Json格式!");
  274. return 2;
  275. }
  276. if(!root.isMember("data") || root["data"][0].isNull()) {
  277. return 3;
  278. }
  279. for(unsigned int i = 0; i < root["data"].size(); i++) {
  280. s.code = root["data"][i]["code"].asInt();
  281. s.name = root["data"][i]["name"].asString();
  282. s.roomId = root["data"][i]["id"].asString();
  283. list.push_back(s);
  284. }
  285. return 0;
  286. }
  287. int BindDevice(j::house_setting* profile) {
  288. Json::Value body;
  289. Json::FastWriter writer;
  290. body["uid"] = GetDeviceUid();
  291. body["versionNumber"] = StoragePreferences::getString(JHWS_VERSION_NUMBER, to_string(GetVersionNo()));
  292. body["type"] = "indoor";
  293. body["hardwareType"] = "linux";
  294. body["communityId"] = profile->communityId;
  295. body["areaId"] = profile->areaId;
  296. body["buildingId"] = profile->buildId;
  297. body["unitId"] = profile->unitId;
  298. body["floorId"] = profile->floorId;
  299. body["roomId"] = profile->roomId;
  300. body["password"] = profile->password;
  301. writer.write(body);
  302. LOGD("body: %s", body.toStyledString().c_str());
  303. base::HttpClient client;
  304. base::HttpRequest req("POST", std::string(kBaseUrl) + "/api/device/grant/" + std::string(profile->replace), body.toStyledString());
  305. req.AppendHeader("Authorization", authorization);
  306. req.ContentType("application/json");
  307. client.SetConnectionTimeout(2000);
  308. client.SetTimeout(3000);
  309. base::HttpResponse response = client.Do(req);
  310. Json::Reader reader;
  311. Json::Value root;
  312. if(response.StatusCode() != 200) {
  313. return ErrorInfo(response);
  314. }
  315. if(!reader.parse(response.Body(), root)) {
  316. LOGD("解析错误不是Json格式!");
  317. return 2;
  318. }
  319. //LOGD("收到返回信息:%s", response.Body().c_str());
  320. if(!root.isMember("code") && root["code"].isNull()) {
  321. return 3;
  322. }
  323. return 0;
  324. }
  325. int GetDevice(DeviceList& list, j::house_setting* profile) {
  326. std::string paramurl;
  327. std::string type = "indoor";
  328. paramurl = "?type=" + std::string(type) + "&communityId=" + std::string(profile->communityId) + "&unitId=" + std::string(profile->unitId);
  329. base::HttpClient client;
  330. base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/device/grant/gates" + std::string(paramurl), "");
  331. req.AppendHeader("Authorization", authorization);
  332. client.SetConnectionTimeout(2000);
  333. client.SetTimeout(3000);
  334. base::HttpResponse response = client.Do(req);
  335. Json::Reader reader;
  336. Json::Value root;
  337. jhws::Device s;
  338. if(response.StatusCode() != 200) {
  339. return ErrorInfo(response);
  340. }
  341. if(!reader.parse(response.Body(), root)) {
  342. LOGD("解析错误不是Json格式!");
  343. LOGD("解析错误不是Json格式!%s", response.Body().c_str());
  344. return 2;
  345. }
  346. if(!root.isMember("data") || root["data"][0].isNull() || !root["data"].isArray()) {
  347. return 3;
  348. }
  349. for(unsigned int i = 0; i < root["data"].size(); i++) {
  350. if(root["data"][i]["type"].asString() == "outdoor") {
  351. s.type = root["data"][i]["type"].asString();
  352. s.deviceNumber = root["data"][i]["deviceNumber"].asString();
  353. s.areaname = root["data"][i]["areaName"].asString();
  354. s.buildname = root["data"][i]["buildingName"].asString();
  355. s.unitname = root["data"][i]["unitName"].asString();
  356. s.communitycode = root["data"][i]["communityCode"].asInt();
  357. s.areacode = root["data"][i]["areaCode"].asInt();
  358. s.buildcode = root["data"][i]["buildingCode"].asInt();
  359. s.unitcode = root["data"][i]["unitCode"].asInt();
  360. list.push_back(s);
  361. } else if(root["data"][i]["type"].asString() == "wall") {
  362. s.type = root["data"][i]["type"].asString();
  363. s.deviceNumber = root["data"][i]["deviceNumber"].asString();
  364. s.areaname = root["data"][i]["areaName"].asString();
  365. s.communitycode = root["data"][i]["communityCode"].asInt();
  366. s.areacode = root["data"][i]["areaCode"].asInt();
  367. list.push_back(s);
  368. }
  369. }
  370. return 0;
  371. }
  372. int GetUpdate(Update& list) {
  373. std::string paramurl;
  374. std::string uid = GetDeviceUid();
  375. // LOGD("deviceuid: %s", uid.c_str());
  376. std::string communityId = StoragePreferences::getString(SIP_JHWS_COMMUNITYID, "");
  377. std::string roomId = StoragePreferences::getString(SIP_JHWS_ROOMID, "");
  378. paramurl = "?uid="+ std::string(uid) + "&deviceType=indoor&communityId=" + std::string(communityId) + "&roomId=" + std::string(roomId);
  379. // LOGD("updateTime: %s", roomId.c_str());
  380. base::HttpClient client;
  381. base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/update/grant" + std::string(paramurl), "");
  382. req.AppendHeader("Authorization", authorization);
  383. client.SetConnectionTimeout(2000);
  384. client.SetTimeout(3000);
  385. base::HttpResponse response = client.Do(req);
  386. Json::Reader reader;
  387. Json::Value root;
  388. if(response.StatusCode() != 200) {
  389. return ErrorInfo(response);
  390. }
  391. if(!reader.parse(response.Body(), root)) {
  392. LOGD("解析错误不是Json格式!%s", response.Body().c_str());
  393. return 2;
  394. }
  395. if(!root.isMember("data") || root["data"].isNull()) {
  396. return 3;
  397. }
  398. long long num1 = (long long)root["data"]["device"].asDouble();
  399. long long num2 = (long long)root["data"]["notice"].asDouble();
  400. list.device = to_string(num1);
  401. list.notice = to_string(num2);
  402. // LOGD("---------1---------");
  403. return 0;
  404. }
  405. int GetMessage(MessageList& list) {
  406. // LOGD("---------2---------");
  407. std::string paramurl;
  408. std::string communityId = StoragePreferences::getString(SIP_JHWS_COMMUNITYID, "");
  409. std::string roomId = StoragePreferences::getString(SIP_JHWS_ROOMID, "");
  410. std::string updateTime = StoragePreferences::getString(SIP_JHWS_UPDATETIME, "0");
  411. // std::string updateTime = "0";
  412. paramurl = "?communityId=" + std::string(communityId) + "&roomId=" + std::string(roomId) + "&updateTime=" + std::string(updateTime);
  413. // LOGD("updateTime: %s", updateTime.c_str());
  414. base::HttpClient client;
  415. base::HttpRequest req("GET", std::string(kBaseUrl) + "/api/notice/grant/notices" + std::string(paramurl), "");
  416. req.AppendHeader("Authorization", authorization);
  417. client.SetConnectionTimeout(2000);
  418. client.SetTimeout(3000);
  419. base::HttpResponse response = client.Do(req);
  420. Json::Reader reader;
  421. Json::Value root;
  422. jhws::Message s;
  423. if(response.StatusCode() != 200) {
  424. return ErrorInfo(response);
  425. }
  426. if(!reader.parse(response.Body(), root)) {
  427. LOGD("解析错误不是Json格式!%s", response.Body().c_str());
  428. return 2;
  429. }
  430. if(!root.isMember("data") || root["data"][0].isNull() || !root["data"].isArray()) {
  431. return 3;
  432. }
  433. for(unsigned int i = 0; i < root["data"].size(); i++) {
  434. s.content = root["data"][i]["content"].asString();
  435. s.title = root["data"][i]["title"].asString();
  436. long long num = (long long)root["data"][i]["updateTime"].asDouble();
  437. s.updateTime = to_string(num);
  438. list.push_back(s);
  439. }
  440. StoragePreferences::putString(SIP_JHWS_UPDATETIME, list.back().updateTime);
  441. // LOGD("---------3---------");
  442. return 0;
  443. }
  444. int Alarm(j::house_setting* profile) {
  445. std::string paramurl;
  446. std::string type = "indoor";
  447. paramurl = "?type=" + std::string(type) + "&communityId=" + std::string(profile->communityId) + "&roomId=" + std::string(profile->roomId);
  448. base::HttpClient client;
  449. base::HttpRequest req("POST", std::string(kBaseUrl) + "/api/alarm/grant" + std::string(paramurl), "");
  450. req.AppendHeader("Authorization", authorization);
  451. client.SetConnectionTimeout(2000);
  452. client.SetTimeout(3000);
  453. base::HttpResponse response = client.Do(req);
  454. Json::Reader reader;
  455. Json::Value root;
  456. if(response.StatusCode() != 200) {
  457. return ErrorInfo(response);
  458. }
  459. if(!reader.parse(response.Body(), root)) {
  460. return 2;
  461. }
  462. if(!root.isMember("data") || root["data"][0].isNull() || !root["data"].isArray()) {
  463. return 3;
  464. }
  465. if(root.isMember("data")) {
  466. if(root["data"].isMember("type")) {
  467. std::string type = root["data"]["type"].asString();
  468. }
  469. if(root["data"].isMember("communityId")) {
  470. std::string communityId = root["data"]["communityId"].asString();
  471. }
  472. if(root["data"].isMember("unitId")) {
  473. std::string unitId = root["data"]["unitId"].asString();
  474. }
  475. } else {
  476. LOGD("错误没有名为data的key!");
  477. }
  478. return 0;
  479. }
  480. } /* namespace jhws */