Train_RFID_Linux/engine/VideoAuxiliaryEngine/VideoAuxiliaryEngine.cpp

145 lines
4.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "VideoAuxiliaryEngine.h"
using namespace ai_matrix;
VideoAuxiliaryEngine::VideoAuxiliaryEngine() {}
VideoAuxiliaryEngine::~VideoAuxiliaryEngine() {}
APP_ERROR VideoAuxiliaryEngine::Init()
{
strPort0_ = engineName_ + "_" + std::to_string(engineId_) + "_0";
this->videoAuxiliaryConfig_ = Config::GetIns()->getVideoAuxiliaryConfig();
this->carType_ = "0";
LogInfo << "VideoAuxiliaryEngine Init ok";
return APP_ERR_OK;
}
APP_ERROR VideoAuxiliaryEngine::DeInit()
{
this->baseScoket_.Close();
LogInfo << "VideoAuxiliaryEngine DeInit ok";
return APP_ERR_OK;
}
APP_ERROR VideoAuxiliaryEngine::Process()
{
int iRet = APP_ERR_OK;
if (!this->videoAuxiliaryConfig_.bUseVideoAuxiliary)
{
return APP_ERR_OK;
}
this->baseScoket_.Open();
this->baseScoket_.SetPort(this->videoAuxiliaryConfig_.iPort, this->videoAuxiliaryConfig_.strIp);
this->baseScoket_.Init();
LogInfo << "视频辅助 服务端IP" << this->videoAuxiliaryConfig_.strIp << " 端口:" << this->videoAuxiliaryConfig_.iPort;
LogInfo << "开启通讯...";
while (!isStop_)
{
char chbuffer[AEI_COMM_BUFFER_SIZE] = {0};
int nRevSize = 0;
memset(chbuffer, 0x00, sizeof(chbuffer));
if (!this->baseScoket_.bRuning())
{
LogError << "TCP 连接失败,重启中 ...";
usleep(20 * 1000 * 1000); // 20秒后重试
this->baseScoket_.Close();
this->baseScoket_.Open();
this->baseScoket_.Init();
continue;
}
nRevSize = this->baseScoket_.Read();
if (nRevSize <= 0)
{
this->baseScoket_.Monitor();
continue;
}
else
{
memcpy(chbuffer, this->baseScoket_.chbuffer, nRevSize);
}
this->dealSocketInfo(chbuffer);
}
return APP_ERR_OK;
}
bool VideoAuxiliaryEngine::getOrder(const std::string &recv, Json::Value &order)
{
Json::CharReaderBuilder readerBuilder;
std::shared_ptr<Json::CharReader> reader(readerBuilder.newCharReader());
JSONCPP_STRING errs;
if (!reader->parse(recv.data(), recv.data() + recv.size(), &order, &errs))
return false;
if (order.isArray()) {
if (!order.empty()) order = order[0];
return true;
}
return true;
}
void VideoAuxiliaryEngine::dealSocketInfo(const std::string &info) {
std::string vedioInfo = info;
StringUtil::getins()->replace_all_distinct(vedioInfo, "}{", "}&{");
std::vector<std::string> vecJson = StringUtil::getins()->split(vedioInfo, "&");
for (int i = 0; i < vecJson.size(); ++i)
{
Json::Value feedBack;
if (!this->getOrder(vecJson[i], feedBack))
{
LogWarn << "接收到视频识别反馈的来车状态信息未能解析成JSON格式。";
continue;
}
std::shared_ptr<ComeTrainInfo> pComeTrainInfo = std::make_shared<ComeTrainInfo>();
std::string tmpCarType = feedBack.get("type", "").asString();
int iDirection = feedBack.get("direction", 0).asInt();
if (tmpCarType != this->carType_)
{
if (this->carType_ == "0")
{
// 来车
pComeTrainInfo->bComeTrain = true;
pComeTrainInfo->iDirection = 0;
if (iDirection != 0) pComeTrainInfo->iDirection = iDirection;
outputQueMap_[strPort0_]->push(pComeTrainInfo);
this->carType_ = tmpCarType;
}
else if (this->carType_ == "1")
{
// 无车
pComeTrainInfo->bComeTrain = false;
pComeTrainInfo->iDirection = 0;
outputQueMap_[strPort0_]->push(pComeTrainInfo);
this->carType_ = tmpCarType;
}
else
{
LogWarn << "视频辅助检测来车状态,反馈的来车状态无法识别";
continue;
}
if (tmpCarType == this->carType_ && iDirection != 0)
{
pComeTrainInfo->bComeTrain = true;
pComeTrainInfo->iDirection = iDirection;
outputQueMap_[strPort0_]->push(pComeTrainInfo);
}
}
}
}