318 lines
8.1 KiB
C++
318 lines
8.1 KiB
C++
#include "cimparserbatch.h"
|
||
#include "derivedqthread.h"
|
||
#include <QtDebug>
|
||
int callmain(char *arg);
|
||
CIMParserBatch::CIMParserBatch()
|
||
{
|
||
}
|
||
|
||
void CIMParserBatch::parser(const QString& dir, const QString &scadaDataFilePath)
|
||
{
|
||
QDir qdir(dir);
|
||
QStringList filter;
|
||
filter<<"*";
|
||
this->mdirList=qdir.entryInfoList(filter,QDir::NoDotAndDotDot | QDir::AllDirs);
|
||
this->mite=this->mdirList.constBegin();//后面会用到,就是dir文件下的所有文件
|
||
this->ReadscadaData(scadaDataFilePath);
|
||
//this->ExecuteParserApp(NULL);
|
||
//this->GenerateBatchFile("c:/parser.bat");
|
||
this->CallFunParser();
|
||
}
|
||
|
||
QString CIMParserBatch::FindXML(const QString& dir)
|
||
{
|
||
|
||
QDir qdir(dir);
|
||
QStringList filter;
|
||
filter<<"*.xml";
|
||
QFileInfoList xmlList;
|
||
xmlList=qdir.entryInfoList(filter);
|
||
if(xmlList.size()>1)
|
||
{
|
||
std::cout<<dir.toStdString()<<"has more than one xml file."<<std::endl;
|
||
return "";
|
||
}
|
||
if(xmlList.size()==0)
|
||
{
|
||
std::cout<<"could not find any xml in "<<dir.toStdString()<<std::endl;
|
||
return "";
|
||
}
|
||
return xmlList.at(0).absoluteFilePath();
|
||
}
|
||
|
||
void CIMParserBatch::ExecuteParserApp(QThread *thread)
|
||
{
|
||
if(thread!=NULL)
|
||
{
|
||
thread->deleteLater();
|
||
//先把上一次解析得到的文件拷回去
|
||
QFileInfoList fileList=this->FindiPsoPrefixedFile("c:/parser");
|
||
QFileInfo info;
|
||
info=*(this->mite);//原始文件的路径
|
||
if(fileList.size()<6)
|
||
{
|
||
|
||
std::cout<<info.baseName().toStdString()<<"parsed, but got fewer files."<<std::endl;
|
||
}
|
||
QString fileName;
|
||
QString desFilePath;
|
||
fileName=info.fileName();//原始文件的文件名,其实是文件夹名
|
||
desFilePath=info.absoluteFilePath();
|
||
desFilePath.replace('/','\\');
|
||
QString parseFileName;
|
||
QFileInfo parseFileInfo;
|
||
QString transNoLoad;
|
||
QString parsedFile;//计算文件
|
||
for(QFileInfoList::const_iterator ite=fileList.constBegin();
|
||
ite!=fileList.constEnd();
|
||
ite++
|
||
)
|
||
{
|
||
parseFileInfo=*ite;
|
||
parseFileName=parseFileInfo.fileName();
|
||
|
||
if(parseFileName.contains("TransformerNoLoad"))
|
||
{
|
||
transNoLoad=desFilePath+"\\iPso_"+fileName+"_TransformerNoLoad.txt";
|
||
QFile::remove(transNoLoad);
|
||
QFile::copy(parseFileInfo.absoluteFilePath(),transNoLoad);
|
||
continue;
|
||
}
|
||
if(parseFileName.contains("TransformerNoResistance"))
|
||
{
|
||
continue;
|
||
}
|
||
if(parseFileName.contains("TransformerInfor"))
|
||
{
|
||
continue;
|
||
}
|
||
if(parseFileName.contains("LineInfor"))
|
||
{
|
||
continue;
|
||
}
|
||
if(parseFileName.contains("lineZeroResistance"))
|
||
{
|
||
continue;
|
||
}
|
||
|
||
parsedFile=desFilePath+"\\iPso_"+fileName+".txt";
|
||
QFile::remove(parsedFile);
|
||
QFile::copy(parseFileInfo.absoluteFilePath(),parsedFile);//TODO:这里会多拷贝一次
|
||
//开始安放pqi.txt文件
|
||
QFile::remove(desFilePath+"\\pqi.txt");
|
||
QFile pqi(desFilePath+"\\pqi.txt");
|
||
QString lineName;
|
||
if(pqi.open(QIODevice::WriteOnly))
|
||
{
|
||
lineName=fileName.section('_',0,0);
|
||
|
||
if(this->mScada.contains(lineName))
|
||
{
|
||
QTextStream stream(&pqi);
|
||
FeederData feederData=this->mScada[lineName];
|
||
stream<<feederData.p<<" "<<feederData.q<<" "<<0<<endl;
|
||
}
|
||
else
|
||
{
|
||
std::cout<<"pqi of "<<lineName.toStdString()<<" not found."<<std::endl;
|
||
}
|
||
pqi.close();
|
||
}
|
||
|
||
}
|
||
this->mite++;
|
||
|
||
|
||
}
|
||
if(this->mite==this->mdirList.constEnd())
|
||
{
|
||
std::cout<<"job finished."<<std::endl;
|
||
return;
|
||
}
|
||
QString xmlFile;
|
||
QString dir;//有xml文件的dir
|
||
QFileInfo fileInfo=*(this->mite);
|
||
dir=fileInfo.absoluteFilePath();
|
||
xmlFile=this->FindXML(dir);
|
||
if(xmlFile=="")
|
||
{
|
||
return;
|
||
}
|
||
QFile::remove("c:/parser/parser.xml");
|
||
QFile::copy(xmlFile,"c:/parser/parser.xml");
|
||
std::cout<<"prepare to parse "<<xmlFile.toStdString()<<std::endl;
|
||
QFileInfo sizeInfo;
|
||
DerivedQThread *_thread;
|
||
sizeInfo=QFileInfo(xmlFile);
|
||
_thread=new DerivedQThread("E:/lineloss/console/run.bat c:/parser/parser.xml",this);
|
||
_thread->connect(_thread,SIGNAL(finishedptr(QThread*)),this,SLOT(ExecuteParserApp(QThread*)));
|
||
_thread->start();
|
||
|
||
|
||
}
|
||
|
||
QFileInfoList CIMParserBatch::FindiPsoPrefixedFile(const QString& dir)
|
||
{
|
||
QDir qdir(dir);
|
||
QStringList filter;
|
||
filter<<"iPso_*";
|
||
return qdir.entryInfoList(filter);//如果找不到iPso开头的文件,返回的QFileInfoList长度为0。
|
||
}
|
||
void CIMParserBatch::ReadscadaData(const QString& dataPath)
|
||
{
|
||
QString line;
|
||
QStringList sep;
|
||
QFile file(dataPath);
|
||
if(file.open(QIODevice::ReadOnly))
|
||
{
|
||
QTextStream stream(&file);
|
||
while(!stream.atEnd())
|
||
{
|
||
line=stream.readLine();
|
||
line=line.simplified();
|
||
line.trimmed();
|
||
sep=line.split(' ');
|
||
if(sep.size()<3)
|
||
{
|
||
std::cout<<"scada file format error!"<<std::endl;
|
||
qApp->exit();
|
||
}
|
||
FeederData feederData;
|
||
feederData.p=sep.at(1).toDouble();
|
||
feederData.q=sep.at(2).toDouble();
|
||
this->mScada.insert(sep.at(0),feederData);
|
||
|
||
}
|
||
file.close();
|
||
}
|
||
else
|
||
{
|
||
std::cout<<"scada File not open. This file may not exist."<<std::endl;
|
||
}
|
||
}
|
||
|
||
void CIMParserBatch::GenerateBatchFile(const QString& batFilePath)
|
||
{
|
||
QFile bat(batFilePath);
|
||
QString fullFilePath;
|
||
QFileInfo info;
|
||
QString xmlFile;
|
||
if(bat.open(QIODevice::WriteOnly))
|
||
{
|
||
QTextStream stream(&bat);
|
||
while(this->mite!=this->mdirList.constEnd())
|
||
{
|
||
info=*(this->mite);
|
||
fullFilePath=info.absoluteFilePath();
|
||
xmlFile=this->FindXML(fullFilePath);
|
||
//stream<<"call ";
|
||
stream<<"E:/lineloss/console/readxml1.exe ";
|
||
stream<<xmlFile<<"\n\r";
|
||
this->mite++;
|
||
}
|
||
bat.close();
|
||
}
|
||
std::cout<<"job finished."<<std::endl;
|
||
}
|
||
|
||
void CIMParserBatch::CallFunParser()
|
||
{
|
||
while(this->mite!=this->mdirList.constEnd())
|
||
{
|
||
|
||
QString xmlFile;
|
||
QString dir;//有xml文件的dir
|
||
QFileInfo fileInfo=*(this->mite);
|
||
dir=fileInfo.absoluteFilePath();
|
||
xmlFile=this->FindXML(dir);
|
||
if(xmlFile=="")
|
||
{
|
||
return;
|
||
}
|
||
//QFile::remove("c:/parser/parser.xml");
|
||
//QFile::copy(xmlFile,"c:/parser/parser.xml");
|
||
std::cout<<"prepare to parse "<<xmlFile.toLocal8Bit().data()<<std::endl;
|
||
//qDebug()<<xmlFile<<"\n";
|
||
callmain(const_cast<char *>(xmlFile.toStdString().c_str()));
|
||
//先把上一次解析得到的文件拷回去
|
||
//QFileInfoList fileList=this->FindiPsoPrefixedFile("c:/parser");
|
||
QFileInfo info(xmlFile);
|
||
// info=*(this->mite);//原始文件的路径
|
||
// if(fileList.size()<6)
|
||
// {
|
||
|
||
// std::cout<<info.baseName().toStdString()<<"parserd, but got fewer files."<<std::endl;
|
||
// }
|
||
// QString fileName;
|
||
QString desFilePath;
|
||
desFilePath=info.absolutePath();
|
||
// fileName=info.fileName();//原始文件的文件名,其实是文件夹名
|
||
// desFilePath=info.absoluteFilePath();
|
||
desFilePath.replace('/','\\');
|
||
// QString parseFileName;
|
||
// QFileInfo parseFileInfo;
|
||
// QString transNoLoad;
|
||
// QString parsedFile;//计算文件
|
||
// for(QFileInfoList::const_iterator ite=fileList.constBegin();
|
||
// ite!=fileList.constEnd();
|
||
// ite++
|
||
// )
|
||
// {
|
||
// parseFileInfo=*ite;
|
||
// parseFileName=parseFileInfo.fileName();
|
||
|
||
// if(parseFileName.contains("变压器无负载"))
|
||
// {
|
||
// transNoLoad=desFilePath+"\\iPso_"+fileName+"_变压器无负载.txt";
|
||
// QFile::remove(transNoLoad);
|
||
// QFile::copy(parseFileInfo.absoluteFilePath(),transNoLoad);
|
||
// continue;
|
||
// }
|
||
// if(parseFileName.contains("变压器无阻抗"))
|
||
// {
|
||
// continue;
|
||
// }
|
||
// if(parseFileName.contains("变压器信息"))
|
||
// {
|
||
// continue;
|
||
// }
|
||
// if(parseFileName.contains("线路信息"))
|
||
// {
|
||
// continue;
|
||
// }
|
||
// if(parseFileName.contains("线路零阻抗"))
|
||
// {
|
||
// continue;
|
||
// }
|
||
|
||
// parsedFile=desFilePath+"\\iPso_"+fileName+".txt";
|
||
// QFile::remove(parsedFile);
|
||
// QFile::copy(parseFileInfo.absoluteFilePath(),parsedFile);//TODO:这里会多拷贝一次
|
||
//开始安放pqi.txt文件
|
||
QFile::remove(desFilePath+"\\pqi.txt");
|
||
QFile pqi(desFilePath+"\\pqi.txt");
|
||
QString lineName;
|
||
lineName=info.fileName();
|
||
if(pqi.open(QIODevice::WriteOnly))
|
||
{
|
||
lineName=lineName.section('_',0,0);
|
||
|
||
if(this->mScada.contains(lineName))
|
||
{
|
||
QTextStream stream(&pqi);
|
||
FeederData feederData=this->mScada[lineName];
|
||
stream<<feederData.p<<" "<<feederData.q<<" "<<0<<endl;
|
||
}
|
||
else
|
||
{
|
||
std::cout<<"pqi of "<<lineName.toStdString()<<" not found."<<std::endl;
|
||
}
|
||
pqi.close();
|
||
}
|
||
this->mite++;
|
||
}
|
||
|
||
|
||
std::cout<<"job finished."<<std::endl;
|
||
}
|