HttpClient.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2021-11-03 14:08:57
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2021-11-18 08:57:05
  8. */
  9. #include "HttpClient.h"
  10. #include <httplib.h>
  11. using namespace httplib;
  12. namespace MIVA
  13. {
  14. std::shared_ptr<httplib::Client> m_httpClient = NULL;
  15. std::shared_ptr<HttpClient> httpClient = NULL;
  16. HttpClient::HttpClient(){
  17. }
  18. HttpClient::~HttpClient(){
  19. }
  20. /**
  21. * @description: 创建对象
  22. * @param {*}
  23. * @return {*}
  24. */
  25. std::shared_ptr<HttpClient> HttpClient::CreateNew(){
  26. if(httpClient == NULL) httpClient = std::make_shared<HttpClient>();
  27. return httpClient;
  28. }
  29. /**
  30. * @description: 初始化
  31. * @param {*}
  32. * @return {*}
  33. */
  34. int32_t HttpClient::Init(std::string account,std::string url, int port){
  35. this->account = account;
  36. m_httpClient = std::make_shared<httplib::Client>(url, port);
  37. if(m_httpClient == NULL){
  38. ErrorL << "HttpClient initialization failed";
  39. return ERR;
  40. }
  41. if(this->LoginNetty() != OK){
  42. ErrorL << "Failed to log in to netty";
  43. return ERR;
  44. }
  45. return OK;
  46. }
  47. /**
  48. * @description: 登录Netty换取Token
  49. * @param {*}
  50. * @return {*}
  51. */
  52. int32_t HttpClient::LoginNetty(){
  53. // 获取时间
  54. char ctime[80];
  55. getDataTime(ctime);
  56. string reqTime = ctime;
  57. std::string reqSign = md5(this->privateKey + this->account + this->pwd + reqTime);
  58. httplib::Params params;
  59. params.emplace("account", this->account);
  60. params.emplace("reqSign", reqSign);
  61. params.emplace("reqTime", reqTime);
  62. if (auto res = m_httpClient->Post("/FACEREC-SYSTEM/user/pass/loginByNetty",params)){
  63. if (res->status == 200) {
  64. NettyHttpResultMsg<NettyHttpNull,NettyHttpToken> nettyHttpResultMsg;
  65. // 解析数据
  66. if(!nettyHttpResultMsg.jsonToObject(res->body)){
  67. return ERR;
  68. }
  69. // 获取token
  70. if(nettyHttpResultMsg.extendDatas.size()){
  71. this->authToken = nettyHttpResultMsg.extendDatas[0].authToken;
  72. this->refreshToken = nettyHttpResultMsg.extendDatas[0].refreshToken;
  73. }
  74. }
  75. }else{
  76. auto err = res.error();
  77. return ERR;
  78. }
  79. return OK;
  80. }
  81. /**
  82. * @description: 获取用户数据
  83. * @param {*}
  84. * @return {*}
  85. */
  86. int32_t HttpClient::GetUserData()
  87. {
  88. httplib::Headers headers = {
  89. { "authToken", this->authToken},
  90. { "authUid", this->account }
  91. };
  92. if (auto res = m_httpClient->Get("/FACEREC-DEVICE/carInfo/tree",headers)){
  93. if (res->status == 200) {
  94. NettyHttpResultMsg<NettyHttpCarInfo,NettyHttpNull> nettyHttpResultMsg;
  95. // 解析数据
  96. if(nettyHttpResultMsg.jsonToObject(res->body)){
  97. vector<NettyHttpCarInfo>::iterator iter;
  98. for(iter=nettyHttpResultMsg.datas.begin(); iter!=nettyHttpResultMsg.datas.end(); iter++){
  99. if(!iter->NettyClientDataSync()){
  100. cout << "CarInfo 数据同步失败" << endl;
  101. }
  102. }
  103. }else{
  104. cout << "carInfo 数据解析失败" << endl;
  105. return ERR;
  106. }
  107. return OK;
  108. }else {
  109. // 有可能是token是失效
  110. this->authToken = "";
  111. }
  112. }else{
  113. auto err = res.error();
  114. return ERR;
  115. }
  116. return ERR;
  117. }
  118. /**
  119. * @description: 获取峰谷配置列表
  120. * @param {*}
  121. * @return {*}
  122. */
  123. int32_t HttpClient:: GetPeakValleyConfig()
  124. {
  125. httplib::Headers headers = {
  126. { "authToken", this->authToken},
  127. { "authUid", this->account }
  128. };
  129. if (auto res = m_httpClient->Get("/FACEREC-DEVICE/peakValleyConfig/list",headers)){
  130. if (res->status == 200) {
  131. NettyHttpResultMsg<NettyHttpPeakValleyConfig,NettyHttpNull> nettyHttpResultMsg;
  132. // 解析数据
  133. if(nettyHttpResultMsg.jsonToObject(res->body)){
  134. vector<NettyHttpPeakValleyConfig>::iterator iter;
  135. nettyHttpResultMsg.ClearTable("PeakValleyConfig");
  136. for(iter=nettyHttpResultMsg.datas.begin(); iter!=nettyHttpResultMsg.datas.end(); iter++){
  137. if(!iter->NettyClientDataSync()){
  138. cout << "PeakValleyConfig 数据同步失败" << endl;
  139. }
  140. }
  141. }else{
  142. cout << "peakValleyConfig 数据解析失败" << endl;
  143. return ERR;
  144. }
  145. return OK;
  146. }else {
  147. // 有可能是token是失效
  148. this->authToken = "";
  149. }
  150. }else{
  151. auto err = res.error();
  152. return ERR;
  153. }
  154. return ERR;
  155. }
  156. /**
  157. * @description: 获取拥挤度配置
  158. * @param {*}
  159. * @return {*}
  160. */
  161. int32_t HttpClient::GetCrowdingDegreeConfig()
  162. {
  163. std::string str = "/FACEREC-SYSTEM/config/find?";
  164. str += "authToken=" + this->authToken;
  165. str += "&authUid=" + this->account;
  166. str += "&conKey=crowdingDegreeConfig";
  167. if (auto res = m_httpClient->Get(str.c_str())){
  168. if (res->status == 200) {
  169. NettyHttpResultMsg<NettyHttpCrowdingDegreeConfig,NettyHttpNull> nettyHttpResultMsg;
  170. nettyHttpResultMsg.ClearTable("SystemConfig");
  171. // 解析数据
  172. if(nettyHttpResultMsg.jsonToObject(res->body)){
  173. vector<NettyHttpCrowdingDegreeConfig>::iterator iter;
  174. for(iter=nettyHttpResultMsg.datas.begin(); iter!= nettyHttpResultMsg.datas.end(); iter++){
  175. if(!iter->NettyClientDataSync()){
  176. cout << "CrowdingDegreeConfig 数据同步失败" << endl;
  177. }
  178. }
  179. }else{
  180. cout << "CrowdingDegreeConfig 数据解析失败" << endl;
  181. return ERR;
  182. }
  183. return OK;
  184. }else {
  185. // 有可能是token是失效
  186. this->authToken = "";
  187. }
  188. }else{
  189. auto err = res.error();
  190. return ERR;
  191. }
  192. return ERR;
  193. }
  194. /**
  195. * @description: 获取摄像头配置
  196. * @param {*}
  197. * @return {*}
  198. */
  199. int32_t HttpClient::GetVideoConfig()
  200. {
  201. vector<vector<string>> sqlRet;
  202. SqlWriter sqlSelect("SELECT dv_Id FROM MIVA_DB.`DataSources` WHERE Del = 0");
  203. sqlSelect << sqlRet;
  204. for(auto &line : sqlRet){
  205. std::string str = "/FACEREC-DEVICE/deviceConfig/findByDvId?";
  206. str += "authToken=" + this->authToken;
  207. str += "&authUid=" + this->account;
  208. str += "&dvId=" + line[0];
  209. if (auto res = m_httpClient->Get(str.c_str())){
  210. if (res->status == 200) {
  211. NettyHttpResultMsg<NettyHttpDeviceConfig,NettyHttpNull> nettyHttpResultMsg;
  212. // 解析数据
  213. if(nettyHttpResultMsg.jsonToObject(res->body)){
  214. vector<NettyHttpDeviceConfig>::iterator iter;
  215. for(iter=nettyHttpResultMsg.datas.begin(); iter!= nettyHttpResultMsg.datas.end(); iter++){
  216. if(iter->algoType == 1){
  217. if(!iter->NettyClientDataSync()){
  218. cout << "NettyHttpDeviceConfig 数据同步失败" << endl;
  219. }
  220. }
  221. }
  222. }else{
  223. cout << "NettyHttpDeviceConfig 数据解析失败" << endl;
  224. return ERR;
  225. }
  226. }else {
  227. // 有可能是token是失效
  228. this->authToken = "";
  229. }
  230. }else{
  231. auto err = res.error();
  232. return ERR;
  233. }
  234. }
  235. return OK;
  236. }
  237. /**
  238. * @description: 获取时间
  239. * @param {char} *ctime
  240. * @return {*}
  241. */
  242. void HttpClient::getDataTime(char *ctime)
  243. {
  244. time_t rawtime;
  245. struct tm *info;
  246. time(&rawtime);
  247. info = localtime(&rawtime);
  248. strftime(ctime, 80, "%Y-%m-%d %H:%M:%S", info);
  249. }
  250. /**
  251. * @description: 判断是否获取到Token
  252. * @param {*}
  253. * @return {*}
  254. */
  255. bool HttpClient::alive(){
  256. return this->authToken == "" ? false : true;
  257. }
  258. } // namespace MIVA