Train_RFID_Linux/engine/HttpUpResultEngine/HttpUpResultEngine.cpp

216 lines
7.3 KiB
C++
Raw Normal View History

#include "HttpUpResultEngine.h"
#undef DISABLE_SSH_AGENT
namespace
{
std::map<std::string, std::string> mapAscii = {
{"!", "%21"},
{"#", "%23"},
{"$", "%24"},
{"%", "%25"},
{"&", "%26"},
{"@", "%40"},
{"*", "%2A"}
};
}
HttpUpResultEngine::HttpUpResultEngine() {}
HttpUpResultEngine::~HttpUpResultEngine() {}
APP_ERROR HttpUpResultEngine::Init()
{
strPort0_ = engineName_ + "_" + std::to_string(engineId_) + "_0";
this->baseConfig_ = Config::GetIns()->getBaseConfig();
this->httpServerConfig_ = Config::GetIns()->getHttpServerConfig();
LogInfo << "HttpUpResultEngine Init ok";
return APP_ERR_OK;
}
APP_ERROR HttpUpResultEngine::DeInit()
{
LogInfo << "HttpUpResultEngine DeInit ok";
return APP_ERR_OK;
}
bool HttpUpResultEngine::upWeb(std::shared_ptr<TrainInfo> pTrainInfo)
{
try {
Json::Value arrayObj; //构建对象
arrayObj["comeTime"] = pTrainInfo->strTrainTime;
arrayObj["collectTime"] = pTrainInfo->strNowTime;
arrayObj["carriageNumber"] = pTrainInfo->strCarriageNum;
arrayObj["carriageType"] = pTrainInfo->strCarriageType;
arrayObj["carriageOrder"] = pTrainInfo->strOrder;
arrayObj["steel"] = pTrainInfo->strRfidInfo;
Json::Value trainParams;
trainParams["poundNo"] = this->baseConfig_.strTrackName;
arrayObj["trainParams"] = trainParams;
Json::StreamWriterBuilder writer;
std::string str = Json::writeString(writer, arrayObj);
LogInfo << "" << pTrainInfo->strOrder << "发送web: " << str;
httplib::Client cli(this->httpServerConfig_.strIp, this->httpServerConfig_.iPort);
cli.set_connection_timeout(0, 300 * 1000);
cli.set_read_timeout(3,0);
httplib::Headers header;
httplib::Params params;
header.emplace("blade-auth", this->webToken);
//header.emplace("Content-Type", "application/json");
auto res = cli.Post(this->httpServerConfig_.strUpResultUrl, header, str, "application/json");
if (res)
{
if (res->status == 200)
{
LogInfo << "" << pTrainInfo->strOrder << "web返回: " << res->body;
Json::CharReaderBuilder readerBuilder;
std::istringstream iss(res->body);
Json::Value root;
std::string errs;
bool parsingSuccessful = Json::parseFromStream(readerBuilder, iss, &root, &errs);
if (parsingSuccessful)
{
if (root["success"].asBool())
{
return true;
}
else
{
if (root["msg"].asString() == "请求未授权") {
LogWarn << "" << pTrainInfo->strOrder << "因请求未授权而上传识别结果失败重新请求token。";
if (!this->getToken()) return false;
return this->upWeb(pTrainInfo);
}
LogError << "" << pTrainInfo->strOrder << "节,识别结果上传失败,原因:" << root["msg"].asString();
}
}
else
{
LogError << "" << pTrainInfo->strOrder << "识别结果上传失败返回数据解析异常返回数据非json" + res->body;
}
}
else
{
LogError << "" << pTrainInfo->strOrder << "节,识别结果上传失败,原因:" << res->status << " - " << res->body;
if (res->status == 401) {
LogWarn << "因请求未授权而上传识别结果失败重新请求token。";
this->getToken();
return this->upWeb(pTrainInfo);
}
}
}
else
{
LogError << "" << pTrainInfo->strOrder << "节,上传数据失败,请检查网络,或者上传地址!";
}
}
catch (std::exception &e)
{
LogError << "" << pTrainInfo->strOrder << "节,上传识别结果失败,原因:";
LogError << e.what();
}
return false;
}
/**
* @brief:Http获取授权
* @param
* @return:
*/
bool HttpUpResultEngine::getToken()
{
try
{
httplib::Client cli(this->httpServerConfig_.strIp, this->httpServerConfig_.iPort);
cli.set_connection_timeout(0, 300 * 1000);
cli.set_read_timeout(0,300*1000);
httplib::Headers header;
httplib::Params params;
header.emplace("Authorization", "Basic Y2xpZW50X2VudGVycHJpc2U6Y2xpZW50X2VudGVycHJpc2Vfc2VjcmV0");
params.emplace("username", this->httpServerConfig_.strUserName);
params.emplace("password", this->httpServerConfig_.strPassword);
params.emplace("tenantId", "000000");
params.emplace("grant_type", "password");
auto res = cli.Post(this->httpServerConfig_.strTokenUrl, header, params);
if (res)
{
if (res->status == 200)
{
Json::CharReaderBuilder readerBuilder;
std::istringstream iss(res->body);
Json::Value root;
std::string errs;
bool parsingSuccessful = Json::parseFromStream(readerBuilder, iss, &root, &errs);
if (parsingSuccessful)
{
if (!root.get("token_type", "").asString().empty())
{
this->webToken = root["token_type"].asString();
this->webToken.append(" ");
this->webToken.append(root["access_token"].asString());
LogInfo << "已获取到web token";
return true;
}
else
{
LogError << "获取web token失败原因" << res->body;
}
}
else
{
LogError << "获取web token返回数据解析异常返回数据非json。详细" << res->body;
}
}
}
else
{
auto err = res.error();
// if (err == httplib::Error::Connection) {
// std::cout << " (连接出错)" << std::endl;
// }
LogError << "获取web token失败请检查网络或请求地址。详细" << to_string(err);
}
}
catch (std::exception &e)
{
LogError << "获取授权失败,原因:";
LogError << e.what();
}
return false;
}
APP_ERROR HttpUpResultEngine::Process()
{
int iRet = APP_ERR_OK;
while (!isStop_)
{
std::shared_ptr<void> pVoidData0 = nullptr;
inputQueMap_[strPort0_]->pop(pVoidData0);
if (nullptr == pVoidData0)
{
usleep(1000); //1ms
continue;
}
std::shared_ptr<TrainInfo> pTrainInfo = std::static_pointer_cast<TrainInfo>(pVoidData0);
if (!this->httpServerConfig_.bIsUse) continue;
if (!this->upWeb(pTrainInfo))
{
// LogError << "第" << pTrainInfo->strOrder + "节,识别结果上传失败!";
}
}
return APP_ERR_OK;
}