123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- /*
- * @Description:
- * @Version: 1.0
- * @Autor: lishengyin
- * @Date: 2021-11-03 14:08:57
- * @LastEditors: lishengyin
- * @LastEditTime: 2021-11-18 08:57:05
- */
- #include "HttpClient.h"
- #include <httplib.h>
- using namespace httplib;
- namespace MIVA
- {
- std::shared_ptr<httplib::Client> m_httpClient = NULL;
- std::shared_ptr<HttpClient> httpClient = NULL;
- HttpClient::HttpClient(){
-
- }
-
- HttpClient::~HttpClient(){
-
- }
- /**
- * @description: 创建对象
- * @param {*}
- * @return {*}
- */
- std::shared_ptr<HttpClient> HttpClient::CreateNew(){
- if(httpClient == NULL) httpClient = std::make_shared<HttpClient>();
- return httpClient;
- }
- /**
- * @description: 初始化
- * @param {*}
- * @return {*}
- */
- int32_t HttpClient::Init(std::string account,std::string url, int port){
- this->account = account;
- m_httpClient = std::make_shared<httplib::Client>(url, port);
- if(m_httpClient == NULL){
- ErrorL << "HttpClient initialization failed";
- return ERR;
- }
- if(this->LoginNetty() != OK){
- ErrorL << "Failed to log in to netty";
- return ERR;
- }
- return OK;
- }
- /**
- * @description: 登录Netty换取Token
- * @param {*}
- * @return {*}
- */
- int32_t HttpClient::LoginNetty(){
- // 获取时间
- char ctime[80];
- getDataTime(ctime);
- string reqTime = ctime;
- std::string reqSign = md5(this->privateKey + this->account + this->pwd + reqTime);
- httplib::Params params;
- params.emplace("account", this->account);
- params.emplace("reqSign", reqSign);
- params.emplace("reqTime", reqTime);
- if (auto res = m_httpClient->Post("/FACEREC-SYSTEM/user/pass/loginByNetty",params)){
- if (res->status == 200) {
- NettyHttpResultMsg<NettyHttpNull,NettyHttpToken> nettyHttpResultMsg;
- // 解析数据
- if(!nettyHttpResultMsg.jsonToObject(res->body)){
- return ERR;
- }
- // 获取token
- if(nettyHttpResultMsg.extendDatas.size()){
- this->authToken = nettyHttpResultMsg.extendDatas[0].authToken;
- this->refreshToken = nettyHttpResultMsg.extendDatas[0].refreshToken;
- }
- }
- }else{
- auto err = res.error();
- return ERR;
- }
- return OK;
- }
- /**
- * @description: 获取用户数据
- * @param {*}
- * @return {*}
- */
- int32_t HttpClient::GetUserData()
- {
- httplib::Headers headers = {
- { "authToken", this->authToken},
- { "authUid", this->account }
- };
-
- if (auto res = m_httpClient->Get("/FACEREC-DEVICE/carInfo/tree",headers)){
- if (res->status == 200) {
- NettyHttpResultMsg<NettyHttpCarInfo,NettyHttpNull> nettyHttpResultMsg;
- // 解析数据
- if(nettyHttpResultMsg.jsonToObject(res->body)){
- vector<NettyHttpCarInfo>::iterator iter;
- for(iter=nettyHttpResultMsg.datas.begin(); iter!=nettyHttpResultMsg.datas.end(); iter++){
- if(!iter->NettyClientDataSync()){
- cout << "CarInfo 数据同步失败" << endl;
- }
- }
- }else{
- cout << "carInfo 数据解析失败" << endl;
- return ERR;
- }
- return OK;
- }else {
- // 有可能是token是失效
- this->authToken = "";
- }
- }else{
- auto err = res.error();
- return ERR;
- }
- return ERR;
- }
- /**
- * @description: 获取峰谷配置列表
- * @param {*}
- * @return {*}
- */
- int32_t HttpClient:: GetPeakValleyConfig()
- {
- httplib::Headers headers = {
- { "authToken", this->authToken},
- { "authUid", this->account }
- };
- if (auto res = m_httpClient->Get("/FACEREC-DEVICE/peakValleyConfig/list",headers)){
- if (res->status == 200) {
- NettyHttpResultMsg<NettyHttpPeakValleyConfig,NettyHttpNull> nettyHttpResultMsg;
- // 解析数据
- if(nettyHttpResultMsg.jsonToObject(res->body)){
- vector<NettyHttpPeakValleyConfig>::iterator iter;
- nettyHttpResultMsg.ClearTable("PeakValleyConfig");
- for(iter=nettyHttpResultMsg.datas.begin(); iter!=nettyHttpResultMsg.datas.end(); iter++){
- if(!iter->NettyClientDataSync()){
- cout << "PeakValleyConfig 数据同步失败" << endl;
- }
- }
- }else{
- cout << "peakValleyConfig 数据解析失败" << endl;
- return ERR;
- }
- return OK;
- }else {
- // 有可能是token是失效
- this->authToken = "";
- }
- }else{
- auto err = res.error();
- return ERR;
- }
- return ERR;
- }
- /**
- * @description: 获取拥挤度配置
- * @param {*}
- * @return {*}
- */
- int32_t HttpClient::GetCrowdingDegreeConfig()
- {
- std::string str = "/FACEREC-SYSTEM/config/find?";
- str += "authToken=" + this->authToken;
- str += "&authUid=" + this->account;
- str += "&conKey=crowdingDegreeConfig";
- if (auto res = m_httpClient->Get(str.c_str())){
- if (res->status == 200) {
- NettyHttpResultMsg<NettyHttpCrowdingDegreeConfig,NettyHttpNull> nettyHttpResultMsg;
- nettyHttpResultMsg.ClearTable("SystemConfig");
- // 解析数据
- if(nettyHttpResultMsg.jsonToObject(res->body)){
- vector<NettyHttpCrowdingDegreeConfig>::iterator iter;
- for(iter=nettyHttpResultMsg.datas.begin(); iter!= nettyHttpResultMsg.datas.end(); iter++){
- if(!iter->NettyClientDataSync()){
- cout << "CrowdingDegreeConfig 数据同步失败" << endl;
- }
- }
- }else{
- cout << "CrowdingDegreeConfig 数据解析失败" << endl;
- return ERR;
- }
- return OK;
- }else {
- // 有可能是token是失效
- this->authToken = "";
- }
- }else{
- auto err = res.error();
- return ERR;
- }
- return ERR;
- }
- /**
- * @description: 获取摄像头配置
- * @param {*}
- * @return {*}
- */
- int32_t HttpClient::GetVideoConfig()
- {
- vector<vector<string>> sqlRet;
- SqlWriter sqlSelect("SELECT dv_Id FROM MIVA_DB.`DataSources` WHERE Del = 0");
- sqlSelect << sqlRet;
- for(auto &line : sqlRet){
- std::string str = "/FACEREC-DEVICE/deviceConfig/findByDvId?";
- str += "authToken=" + this->authToken;
- str += "&authUid=" + this->account;
- str += "&dvId=" + line[0];
-
- if (auto res = m_httpClient->Get(str.c_str())){
- if (res->status == 200) {
- NettyHttpResultMsg<NettyHttpDeviceConfig,NettyHttpNull> nettyHttpResultMsg;
- // 解析数据
- if(nettyHttpResultMsg.jsonToObject(res->body)){
- vector<NettyHttpDeviceConfig>::iterator iter;
- for(iter=nettyHttpResultMsg.datas.begin(); iter!= nettyHttpResultMsg.datas.end(); iter++){
- if(iter->algoType == 1){
- if(!iter->NettyClientDataSync()){
- cout << "NettyHttpDeviceConfig 数据同步失败" << endl;
- }
- }
- }
- }else{
- cout << "NettyHttpDeviceConfig 数据解析失败" << endl;
- return ERR;
- }
- }else {
- // 有可能是token是失效
- this->authToken = "";
- }
- }else{
- auto err = res.error();
- return ERR;
- }
- }
- return OK;
- }
- /**
- * @description: 获取时间
- * @param {char} *ctime
- * @return {*}
- */
- void HttpClient::getDataTime(char *ctime)
- {
- time_t rawtime;
- struct tm *info;
- time(&rawtime);
- info = localtime(&rawtime);
- strftime(ctime, 80, "%Y-%m-%d %H:%M:%S", info);
- }
-
- /**
- * @description: 判断是否获取到Token
- * @param {*}
- * @return {*}
- */
- bool HttpClient::alive(){
- return this->authToken == "" ? false : true;
- }
-
- } // namespace MIVA
|