#include "analysisvehifno.h"
//#include "baseinfo.h"
#include "commonunits.h"
#include <QJsonDocument>
#include <QDebug>
#include <QFile>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonArray>
#include <QVariant>
#include <QTextCodec>
AnalysisVehInfo::AnalysisVehInfo(QObject *parent) : QObject(parent)
{
loadVehConfig();
m_nIndex = 1;
m_nLocation = 0;
m_nPosErrCount = 0;
m_pCheckTimer = new QTimer(this);
connect(m_pCheckTimer, SIGNAL(timeout()), this, SLOT(onTimeout()));
connect(this, SIGNAL(recvLog(QString)), this, SLOT(recvLineLog(QString)));
m_bFirst = true;
sLastLaspTime = QDateTime::currentDateTime().toString("yyyyMMdd");
m_pCheckLaspTimer = new QTimer(this);
connect(m_pCheckLaspTimer, SIGNAL(timeout()), this, SLOT(onLaspTimeout()));
m_pCheckLaspTimer->start(10*1000);
}
void AnalysisVehInfo::loadVehConfig()
{
QFile file("./conf/VehConfig.json");
if (!file.open(QFile::ReadOnly))
{
//LOGMAIN->error(QString("Load [./conf/VehConfig.json] Err -- %1.").arg(file.errorString()));
return;
}
QByteArray ba = file.readAll();
QVariantMap map = QJsonDocument::fromJson(ba).object().toVariantMap();
for (auto iter = map.constBegin(); iter != map.constEnd(); ++iter)
{
if (iter.key() != "analyse")
{
QList<VehConfig> configList;
QList<QVariant> array = iter.value().toList();
if (!array.isEmpty())
{
for (int i = 0; i < array.count(); ++i)
{
VehConfig config;
QVariantMap tmpMap = array.at(i).toMap();
config.regExp = tmpMap.value("regexp").toString();
config.nMaxLen = tmpMap.value("maxlen").toInt();
config.nMinLen = tmpMap.value("minlen").toInt();
QString sLine = tmpMap.value("reginfo").toString();
if (sLine.size())
{
QStringList strList = sLine.trimmed().split(",");
for (int i = 0; i < strList.size(); ++i)
{
QStringList sTmpList = strList.at(i).split("|");
if (sTmpList.size() > 1)
config.valueList.append(QPair<int, QString>(sTmpList.at(0).toInt(), sTmpList.at(1)));
}
} else
{
continue;
}
configList.append(config);
}
m_VehCofingHash.insert(iter.key(), configList);
}
} else
{
QVariantMap tmp = iter.value().toMap();
for (auto i = tmp.constBegin(); i != tmp.constEnd(); ++i)
{
QList<QVariant> array = i.value().toList();
QList<InfoAnalyze> list;
for (int j = 0; j < array.count(); ++j)
{
InfoAnalyze info;
QVariantMap tmpMap = array.at(j).toMap();
info.nBegin = tmpMap.value("begin").toInt();
info.nEnd = tmpMap.value("end").toInt();
info.sDealType = tmpMap.value("dealType").toString().split("|");
info.sName = tmpMap.value("name").toString();
list.append(info);
}
m_InfoAnalyzeHash.insert(i.key(), list);
}
}
}
#ifdef OUTCONFIG
for (auto iter = m_VehCofingHash.constBegin(); iter != m_VehCofingHash.constEnd(); ++iter)
{
qDebug() << "Name" << iter.key();
for (int i = 0; i < iter.value().size(); ++i)
{
qDebug() << QString("regExp : %1, maxLen %2, minLen : %3").arg(iter.value().at(i).regExp).arg(iter.value().at(i).nMaxLen).arg(iter.value().at(i).nMinLen);
qDebug() << iter.value().at(i).valueList;
}
}
#endif
}
QHash<QString, QString> AnalysisVehInfo::checkInfo(QString sLine, QString sKey)
{
//LOGMAIN->info(QString("checkInfo:sLine:%1;sKey:%2").arg(sLine).arg(sKey));
QHash<QString, QString> rt;
QList<VehConfig> baseConfig = m_VehCofingHash.value(sKey);
foreach (VehConfig config, baseConfig)
{
if (config.nMaxLen > 0 && sLine.size() > config.nMaxLen)
continue;
if (config.nMinLen > 0 && sLine.size() < config.nMinLen)
continue;
QRegExp reg(config.regExp);
if (-1 < reg.indexIn(sLine))
{
for (int i = 0; i < config.valueList.size(); ++i)
{
if (0 == config.valueList.at(i).first)
{
rt.insert(config.valueList.at(i).second, reg.cap(i + 1));
} else if (1 == config.valueList.at(i).first)
{
//递归
QHash<QString, QString> rt1 = checkInfo(reg.cap(i + 1), config.valueList.at(i).second);
for (auto iter = rt1.constBegin(); iter != rt1.constEnd(); ++iter)
rt.insert(iter.key(), iter.value());
} else
{
QString sInfo = reg.cap(i + 1);
QList<InfoAnalyze> list = m_InfoAnalyzeHash.value(config.valueList.at(i).second);
foreach (auto value, list)
{
QString sTmp = sInfo.mid(value.nBegin, value.nEnd - value.nBegin);
QByteArray baTmp = sTmp.toUtf8();
foreach (auto d, value.sDealType)
{
int nType = d.toInt();
switch (nType)
{
case 1: // Hex 2 Raw
baTmp = QByteArray::fromHex(baTmp);
break;
case 2: // Raw 2 Hex
baTmp = baTmp.toHex();
break;
case 3: // GBK 2 Un
{
static QTextCodec *pCodec = QTextCodec::codecForName("GB2312");
baTmp = pCodec->toUnicode(baTmp).toUtf8();
break;
default:
break;
}
}
}
//LOGMAIN->info(QString("checkInfo:value.sName:%1;baTmp:%2").arg(value.sName).arg(QString::fromUtf8(baTmp)));
rt.insert(value.sName, QString::fromUtf8(baTmp));
}
}
}
break;
}
}
return rt;
}
void AnalysisVehInfo::readFile(const QString &sName, int nType)
{
//LOGMAIN->info(QString("readFile:%1;nType:%2").arg(sName).arg(nType));
static QTextCodec *pCodec = QTextCodec::codecForName("GB2312");
if (2 == nType)
{
QString sOldName = sName;
sOldName.replace(".log", "_bak01.log");
//LOGMAIN->info(QString("readFile:sOldName:%1").arg(sOldName));
readFile(sOldName, 0);
m_nLocation = 0;
} else
{
QFile file(sName);
//LOGMAIN->info(QString("readFile:sName:%1").arg(sName));
if (file.exists())
{
if (!file.open(QFile::ReadOnly))
return;
if (file.size() > m_nLocation)
{
m_nPosErrCount = 0;
file.seek(m_nLocation);
while (!file.atEnd())
{
QString str;
if (pCodec)
{
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
QT 实时分析文件内容,解析内容字段;文件一有改变或被创建可以实时监测出来,并进行按行分析;把分析出来的有用信息保存下来。用到QRegExp和QVariantMap类;通过配置文件(.json)可以配置需要解析的字段。可以递归解析文件内容,qt 5.7.0 下可以编译运行
资源推荐
资源详情
资源评论
收起资源包目录
QT 实时分析文件内容,解析内容字段;文件一有改变可以实时监测出来,并进行按行分析;把分析出来的有用信息保存下来 (120个子文件)
q736594722.htm.0 21KB
analysisvehifno.cpp 23KB
moc_analysisvehifno.cpp 8KB
watchcontrol.cpp 6KB
moc_filesystemwatcher.cpp 6KB
moc_watchcontrol.cpp 5KB
moc_DownloadManager.cpp 5KB
DownloadManager.cpp 4KB
commonunits.cpp 4KB
moc_mainwindow.cpp 3KB
filesystemwatcher.cpp 3KB
mainwindow.cpp 1KB
commondef.cpp 632B
main.cpp 187B
Makefile.Debug 102KB
object_script.NetworkReply.Debug 329B
Log4qtd.dll 25.14MB
libeay32.dll 1.2MB
Log4qt.dll 496KB
ssleay32.dll 285KB
NetworkReply.exe 4.76MB
log4qt.h 24KB
commondef.h 15KB
logerror.h 15KB
logger.h 15KB
factory.h 15KB
logmanager.h 12KB
initialisationhelper.h 8KB
loggingevent.h 7KB
ttcclayout.h 6KB
appenderskeleton.h 6KB
fileappender.h 6KB
configuratorhelper.h 6KB
dailyrollingfileappender.h 5KB
datetime.h 5KB
propertyconfigurator.h 5KB
ilogmsg.h 5KB
writerappender.h 5KB
optionconverter.h 4KB
patternformatter.h 4KB
binaryfileappender.h 4KB
rollingfileappender.h 4KB
patternlayout.h 4KB
listappender.h 4KB
level.h 4KB
properties.h 3KB
consoleappender.h 3KB
levelrangefilter.h 3KB
layout.h 3KB
appender.h 3KB
asyncappender.h 3KB
rollingbinaryfileappender.h 3KB
appenderattachable.h 3KB
debugappender.h 3KB
levelmatchfilter.h 3KB
qmllogger.h 3KB
stringmatchfilter.h 3KB
colorconsoleappender.h 3KB
binarylogger.h 3KB
dailyfileappender.h 2KB
mainthreadappender.h 2KB
hierarchy.h 2KB
classlogger.h 2KB
systemlogappender.h 2KB
ui_mainwindow.h 2KB
filter.h 2KB
mdc.h 2KB
simplelayout.h 2KB
commonunits.h 2KB
loggerrepository.h 2KB
ndc.h 2KB
nullappender.h 2KB
binaryloggingevent.h 2KB
denyallfilter.h 2KB
simpletimelayout.h 2KB
analysisvehifno.h 2KB
signalappender.h 2KB
dispatcher.h 2KB
logstream.h 2KB
basicconfigurator.h 1KB
binarylayout.h 1KB
binarywriterappender.h 1KB
watchcontrol.h 994B
DownloadManager.h 921B
log4qtsharedptr.h 878B
filesystemwatcher.h 844B
binaryeventfilter.h 839B
binarytotextlayout.h 817B
mainwindow.h 621B
wdcappender.h 579B
binarylogstream.h 482B
binaryclasslogger.h 475B
log4qtshared.h 400B
xmllayout.h 375B
q736594722.htm 21KB
82917333.html 172KB
Assist.ini 113B
VehConfig.json 7KB
Lane.log 195B
Lane_old.log 177B
共 120 条
- 1
- 2
资源评论
hymaliang1120
- 粉丝: 1
- 资源: 68
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功