215 lines
7.8 KiB
C++
215 lines
7.8 KiB
C++
#include "DealRfidEngine.h"
|
||
|
||
|
||
using namespace ai_matrix;
|
||
|
||
DealRfidEngine::DealRfidEngine() {}
|
||
|
||
DealRfidEngine::~DealRfidEngine() {}
|
||
|
||
APP_ERROR DealRfidEngine::Init()
|
||
{
|
||
strPort0_ = engineName_ + "_" + std::to_string(engineId_) + "_0";
|
||
strPort1_ = engineName_ + "_" + std::to_string(engineId_) + "_1";
|
||
strPort2_ = engineName_ + "_" + std::to_string(engineId_) + "_2";
|
||
|
||
this->baseConfig_ = Config::GetIns()->getBaseConfig();
|
||
|
||
this->initParam();
|
||
|
||
LogInfo << "DealRfidEngine Init ok";
|
||
return APP_ERR_OK;
|
||
}
|
||
|
||
APP_ERROR DealRfidEngine::DeInit()
|
||
{
|
||
LogInfo << "DealRfidEngine DeInit ok";
|
||
return APP_ERR_OK;
|
||
}
|
||
|
||
APP_ERROR DealRfidEngine::Process()
|
||
{
|
||
int iRet = APP_ERR_OK;
|
||
while (!isStop_)
|
||
{
|
||
//pop端口0
|
||
std::shared_ptr<void> pVoidData0 = nullptr;
|
||
iRet = inputQueMap_[strPort0_]->pop(pVoidData0);
|
||
if (nullptr == pVoidData0)
|
||
{
|
||
usleep(1000);
|
||
continue;
|
||
}
|
||
|
||
std::shared_ptr<RfidInfo> pRfidData = std::static_pointer_cast<RfidInfo>(pVoidData0);
|
||
|
||
this->tmpRfid_.append(pRfidData->strRfid);
|
||
|
||
LogDebug << "原文:" << this->tmpRfid_;
|
||
if (this->tmpRfid_.substr(this->tmpRfid_.size()-1,1) != "&")
|
||
{
|
||
continue;
|
||
}
|
||
|
||
this->tmpRfid_ = StringUtil::getins()->replace_all_distinct(this->tmpRfid_, "&", "");
|
||
|
||
std::vector<std::string> vecRfidSub = StringUtil::getins()->split(this->tmpRfid_, "@");
|
||
|
||
// LogDebug << "--" << vecRfidSub.size();
|
||
this->tmpRfid_ = "";
|
||
|
||
auto split_lambda = [=](std::string strFirst, std::string strOther){
|
||
if (strFirst == "0")
|
||
{
|
||
this->rfidHasTrain_ = false;
|
||
LogInfo << "RFID-火车离开了";
|
||
// 关功放信号
|
||
// 需要结合 “来车状态”广播消息,确认是否真的车离开了;
|
||
this->identifyFlagUpdate();
|
||
}
|
||
else
|
||
{
|
||
// 注意: 此处按照4个磁钢来配置,可能存在一定限制
|
||
if (strOther == "000000") // 第一次检测来车时(前提是上一次功放已关闭)
|
||
{
|
||
if (strFirst == this->baseConfig_.strMagnetSteelOrder.substr(0, 1)
|
||
|| strFirst == this->baseConfig_.strMagnetSteelOrder.substr(1, 1))
|
||
{
|
||
this->rfidHasTrain_ = true;
|
||
LogInfo << "RFID-来车了";
|
||
this->identifyFlagUpdate();
|
||
}
|
||
else
|
||
{
|
||
LogInfo << "RFID-列车出厂";
|
||
this->rfidHasTrain_ = false;
|
||
}
|
||
}
|
||
}
|
||
};
|
||
|
||
for (const auto & i : vecRfidSub)
|
||
{
|
||
if (i.size() == 26 || i.size() == 24)
|
||
{
|
||
// 车厢信息
|
||
std::string strTrainInfo = i.substr(0, 14);
|
||
|
||
if (this->vecTrain_.empty() || !Utils::getins()->contains_vec(this->vecTrain_, strTrainInfo))
|
||
{
|
||
bool isNoCarriage = false;
|
||
int carT = StringUtil::getins()->string2int(strTrainInfo.substr(3, 2), isNoCarriage);
|
||
|
||
if (isNoCarriage && carT >= 10) continue;
|
||
// 因信号不稳定 增加一行过滤 出现文字中带有字母及数字以外的字符时,过滤
|
||
if (StringUtil::getins()->containsNonAlphaNum(strTrainInfo)) continue;
|
||
// 因信号不稳定 增加一行过滤 出现读到的数据里 空格替代了实际字符的情况
|
||
if (StringUtil::getins()->trim(strTrainInfo.substr(7, 7)).size() < 7) continue;
|
||
vecTrain_.emplace_back(strTrainInfo);
|
||
int train_order = vecTrain_.size();
|
||
LogInfo << "第" << train_order << "节 " << strTrainInfo;
|
||
// 将接收到的RFID使用@符拼接起来,以待后续上传web
|
||
this->rfidSourceInfo_.append("@" + i);
|
||
std::string rfidinfo = this->rfidSourceInfo_;
|
||
this->rfidSourceInfo_ = "";
|
||
|
||
std::shared_ptr<TrainInfo> pTrainInfo = std::make_shared<TrainInfo>();
|
||
this->makeTrainInfo(rfidinfo, pTrainInfo);
|
||
|
||
if (this->needIdentify_) {
|
||
// 上传识别结果
|
||
iRet = outputQueMap_[strPort1_]->push(std::static_pointer_cast<void>(pTrainInfo));
|
||
}
|
||
// 存储识别结果
|
||
iRet = outputQueMap_[strPort2_]->push(std::static_pointer_cast<void>(pTrainInfo));
|
||
}
|
||
}
|
||
else if (i.size() == 7)
|
||
{
|
||
// 不使用磁钢的情况下 过滤掉磁钢信号
|
||
if (!this->baseConfig_.bHaveMagnetSteel) continue;
|
||
// 磁钢信号
|
||
std::string strFirst = i.substr(0,1);
|
||
std::string strOther = i.substr(1, i.size() - 1);
|
||
// 将接收到的RFID使用@符拼接起来,以待后续上传web
|
||
this->rfidSourceInfo_.append("@" + i);
|
||
split_lambda(strFirst, strOther);
|
||
}
|
||
else if (i.size() == 14) // 特殊情况,来车信号少了&符
|
||
{
|
||
// 不使用磁钢的情况下 过滤掉磁钢信号
|
||
if (!this->baseConfig_.bHaveMagnetSteel) continue;
|
||
for (int j = 0; j < i.size(); j+=7) {
|
||
std::string rfid_cg = i.substr(j, 7);
|
||
std::string strFirst = rfid_cg.substr(0, 1);
|
||
std::string strOther = rfid_cg.substr(1, 6);
|
||
// 将接收到的RFID使用@符拼接起来,以待后续上传web
|
||
this->rfidSourceInfo_.append("@" + rfid_cg);
|
||
split_lambda(strFirst, strOther);
|
||
}
|
||
}
|
||
pRfidData->strTrainTime = this->trainTime_;
|
||
// 存储原始数据
|
||
iRet = outputQueMap_[strPort0_]->push(std::static_pointer_cast<void>(pRfidData));
|
||
}
|
||
}
|
||
return APP_ERR_OK;
|
||
}
|
||
|
||
/**
|
||
* 参数初始化
|
||
*/
|
||
void DealRfidEngine::initParam() {
|
||
this->rfidSourceInfo_ = "";
|
||
this->trainTime_ = "";
|
||
this->vecTrain_.clear();
|
||
this->tmpRfid_ = "";
|
||
// 识别方向正确标志
|
||
this->needIdentify_ = false;
|
||
// 视频识别有火车标志(使用socket服务时才有用)
|
||
this->videoHasTrain_ = false;
|
||
// RFID识别有火车标志
|
||
this->rfidHasTrain_ = false;
|
||
}
|
||
|
||
/**
|
||
* 修改识别状态
|
||
*/
|
||
void DealRfidEngine::identifyFlagUpdate() {
|
||
if (this->rfidHasTrain_ || this->videoHasTrain_)
|
||
{
|
||
if (!this->needIdentify_)
|
||
{
|
||
LogInfo << "--- 来车了 ---";
|
||
// this->mkRfidLog();
|
||
this->vecTrain_.clear();
|
||
this->needIdentify_ = true;
|
||
this->trainTime_ = TimeUtil::getins()->getDateTime();
|
||
}
|
||
}
|
||
else if (!this->rfidHasTrain_ && !this->videoHasTrain_)
|
||
{
|
||
this->initParam();
|
||
LogInfo << "--- 火车离开了 ---";
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 组织识别数据
|
||
* @param rfidinfo
|
||
* @param pTrainInfo
|
||
*/
|
||
void DealRfidEngine::makeTrainInfo(const std::string rfidinfo, std::shared_ptr<TrainInfo> pTrainInfo) {
|
||
std::string info = this->vecTrain_.back();
|
||
int order = this->vecTrain_.size();
|
||
|
||
info = info.substr(1,info.size() - 1);
|
||
|
||
if (this->trainTime_.empty()) this->trainTime_ = TimeUtil::getins()->getDateTime();
|
||
pTrainInfo->strCarriageType = StringUtil::getins()->trim(info.substr(0, 6));
|
||
pTrainInfo->strCarriageNum = StringUtil::getins()->trim(info.substr(6, info.size() - 6));
|
||
pTrainInfo->strOrder = std::to_string(order);
|
||
pTrainInfo->strTrainTime = this->trainTime_;
|
||
pTrainInfo->strRfidInfo = rfidinfo;
|
||
pTrainInfo->strNowTime = TimeUtil::getins()->getDateTime_usec();
|
||
} |