Commit 71f792d0 authored by 刘乐's avatar 刘乐

1, 追踪分析接口封装

parent 10eb3705
......@@ -13,9 +13,13 @@ int main(int argc, char* argv[])
const char* uri = "host=192.168.19.100 port=5432 dbname=JinXian user=postgres password=admin";
char* findUri = const_cast<char*>(uri);
char* result = (char*)malloc(2048);
int succ = hdyrSimulation(findUri);
char sn[32];
strcpy_s(sn, "JD00000680");
int succ = trackingSimulation(findUri, sn,2, result);
std::cout << succ << std::endl;
free(result);
/*const char* date = "2020-07-02";
char* dates = const_cast<char*>(date);
......
......@@ -66,7 +66,7 @@ bool DLLEXPORT getDataByInterval(char* uri, char* date, char* interval)
#include "CivPgConn.h"
#include "CivHydrSimulation.h"
int DLLEXPORT hdyrSimulation(char* uri)
bool DLLEXPORT hdyrSimulation(char* uri)
{
// 创建数据库连接
CivDbConn* pgConn = new CivPgConn(uri);
......@@ -83,7 +83,7 @@ int DLLEXPORT hdyrSimulation(char* uri)
return flag;
}
int DLLEXPORT qualitySimulation(char* uri)
bool DLLEXPORT qualitySimulation(char* uri)
{
// 创建数据库连接
CivDbConn* pgConn = new CivPgConn(uri);
......@@ -101,7 +101,7 @@ int DLLEXPORT qualitySimulation(char* uri)
return flag;
}
int DLLEXPORT trackingSimulation(char* uri, char* sN, int hours, char* result)
bool DLLEXPORT trackingSimulation(char* uri, char* sN, int hours, char* result)
{
// 创建数据库连接
......@@ -112,13 +112,14 @@ int DLLEXPORT trackingSimulation(char* uri, char* sN, int hours, char* result)
mHyDr->registDb(pgConn);
//开始模拟
int flag = mHyDr->trackingSimulation(sN, hours);
bool flag = mHyDr->trackingSimulation(sN, hours);
if (flag)
{
mHyDr->getTrackingResult(hours, result);
std::string strResult;
mHyDr->getTrackingResult(hours, strResult);
result = const_cast<char*>(strResult.c_str());
}
std::cout << result << std::endl;
delete pgConn;
delete mHyDr;
......
......@@ -11,14 +11,14 @@ extern "C" {
*@uri:数据库连接地址
*@return 1:成功,其他值失败
*/
int DLLEXPORT hdyrSimulation(char* uri);
bool DLLEXPORT hdyrSimulation(char* uri);
/**
*@brief 水质计算
*@uri:数据库连接地址
*@return 1:成功,其他值失败
*/
int DLLEXPORT qualitySimulation(char* uri);
bool DLLEXPORT qualitySimulation(char* uri);
/**
*@brief 水质追踪分析,扩散分析
*@uri:数据库连接地址
......@@ -27,7 +27,7 @@ extern "C" {
*@result:输出结果:json字符串
*@return 1:成功,其他值失败
*/
int DLLEXPORT trackingSimulation(char* uri, char* sN, int hours, char* result);
bool DLLEXPORT trackingSimulation(char* uri, char* sN, int hours, char* result);
/**
*@brief 模拟计算
......
......@@ -63,15 +63,6 @@ void CivHydrSimulation::setBinFile(const std::string& binFile)
void CivHydrSimulation::registDb(CivDbConn* dbConn)
{
if (dbConn == nullptr)
return;
if (dbConn != nullptr)
{
dbConn->close();
delete mDbConn;
mDbConn = nullptr;
}
mDbConn = dbConn;
}
......@@ -102,7 +93,7 @@ bool CivHydrSimulation::convertInp(const QualityAnalyType analyType, const std::
return true;
}
int CivHydrSimulation::hdyrSimulation()
bool CivHydrSimulation::hdyrSimulation()
{
CivSysLog::getInstance()->error("开始水力分析计算", "CivHydrSimulation", __FUNCTION__);
if (!convertInp(ANALYSIS_RESERVE,""))
......@@ -148,7 +139,6 @@ int CivHydrSimulation::hdyrSimulation()
float dHStep;
ENGetVal(4, &dHStep);
bool isFirst = true;
// 计算
do
{
......@@ -169,37 +159,70 @@ int CivHydrSimulation::hdyrSimulation()
}
ENnextH(&tstep);
if (isFirst)
{
mDbConn->deleteByField(NODERESULTTABLE, "日期", CurrentDate);
mDbConn->deleteByField(PIPERESULTTABLE, "日期", CurrentDate);
isFirst = false;
}
// 获取当前节点和管段数据
getNodeResult(iTime);
getLinkResult(iTime);
iTime++;
} while (tstep > 0);
ENcloseH();
ENclose();
//模拟数据刷新
if (!brushingResult())
{
CivSysLog::getInstance()->error("水力分析结果存储失败", "CivHydrCalc", __FUNCTION__);
return false;
}
return true;
}
bool CivHydrSimulation::brushingResult()
{
clearSamePeriodValue(mDbConn, CurrentDate);
// 更新数据
if (!mResultCache.updateToDb(mDbConn))
{
CivSysLog::getInstance()->error("水力分析结果存储失败", "CivHydrCalc", __FUNCTION__);
return false;
}
// 更新管网数据库数据
(void)mDbConn->updateNodeByInterval(CurrentDate, "0");
(void)mDbConn->updateLinkByInterval(CurrentDate, "0");
updateLastestDataToGisNet(mDbConn, "0");
return true;
}
bool CivHydrSimulation::clearSamePeriodValue(CivDbConn* dbConn, const std::string& date)
{
if (dbConn == nullptr)
return false;
dbConn->deleteByField(NODERESULTTABLE, "日期", date);
dbConn->deleteByField(PIPERESULTTABLE, "日期", date);
return true;
}
bool CivHydrSimulation::updateLastestDataToGisNet(CivDbConn* dbConn, const std::string& time)
{
if (dbConn == nullptr)
return false;
dbConn->updateNodeByInterval(CurrentDate, time);
dbConn->updateLinkByInterval(CurrentDate, time);
ENcloseH();
ENclose();
return true;
}
int CivHydrSimulation::qualitySimulation()
bool CivHydrSimulation::qualitySimulation()
{
CivSysLog::getInstance()->error("开始水质分析计算", "qualitySimulation", __FUNCTION__);
if (!convertInp(ANALYSIS_RESERVE, ""))
if (!convertInp(ANALYSIS_AGE, ""))
{
CivSysLog::getInstance()->error("写入inp文件失败", "qualitySimulation", __FUNCTION__);
return false;
......@@ -207,12 +230,22 @@ int CivHydrSimulation::qualitySimulation()
CivSysLog::getInstance()->info("写入inp成功", "qualitySimulation", __FUNCTION__);
// 水质计算
bool flag = qualityCompute();
if (!qualityCompute())
{
CivSysLog::getInstance()->error("水质分析计算失败", "CivHydrCalc", __FUNCTION__);
return false;
}
return flag;
//模拟数据刷新
if (!brushingResult())
{
CivSysLog::getInstance()->error("水质分析结果存储失败", "CivHydrCalc", __FUNCTION__);
return false;
}
return true;
}
int CivHydrSimulation::trackingSimulation(char* snNode, int hour)
bool CivHydrSimulation::trackingSimulation(char* snNode, int hour)
{
CivSysLog::getInstance()->error("开始水质分析计算", "qualitySimulation", __FUNCTION__);
if (!convertInp(ANALYSIS_TRACE, snNode))
......@@ -223,9 +256,20 @@ int CivHydrSimulation::trackingSimulation(char* snNode, int hour)
CivSysLog::getInstance()->info("写入inp成功", "qualitySimulation", __FUNCTION__);
// 水质计算
bool flag = qualityCompute();
if (!qualityCompute())
{
CivSysLog::getInstance()->error("追踪分析计算失败", "CivHydrCalc", __FUNCTION__);
return false;
}
//模拟数据刷新
if (!brushingResult())
{
CivSysLog::getInstance()->error("追踪分析计算失败", "CivHydrCalc", __FUNCTION__);
return false;
}
return flag;
return true;
}
bool CivHydrSimulation::qualityCompute()
......@@ -236,7 +280,7 @@ bool CivHydrSimulation::qualityCompute()
int errcode = 0;
char* fileName = getBinFile();
char* fileName = getInpFile();
char* rptFile = getRptFile();
char* binOutFile = getBinFile();
......@@ -262,7 +306,7 @@ bool CivHydrSimulation::qualityCompute()
return false;
}
CivSysLog::getInstance()->info("ENinitH inp 成功", "CivHydrCalc", __FUNCTION__);
bool isFirst = true;
do
{
int nLinkCount;
......@@ -282,15 +326,11 @@ bool CivHydrSimulation::qualityCompute()
ENclose();
return false;
}
if (isFirst)
{
mDbConn->deleteByField(NODERESULTTABLE, "日期", CurrentDate);
mDbConn->deleteByField(PIPERESULTTABLE, "日期", CurrentDate);
isFirst = false;
}
// 获取当前节点和管段数据
getNodeResult(iTime);
getLinkResult(iTime);
ENnextH(&tstep);
iTime++;
......@@ -330,6 +370,9 @@ bool CivHydrSimulation::qualityCompute()
ENcloseQ();
ENclose();
// 清除当前之前的模拟的数据
clearSamePeriodValue(mDbConn, CurrentDate);
// 更新数据
if (!mResultCache.updateToDb(mDbConn))
{
......@@ -339,17 +382,69 @@ bool CivHydrSimulation::qualityCompute()
CivSysLog::getInstance()->info("水质分析结果存储成功", "CivHydrCalc", __FUNCTION__);
// 更新管网数据库数据
(void)mDbConn->updateNodeByInterval(CurrentDate, "0");
(void)mDbConn->updateLinkByInterval(CurrentDate, "0");
updateLastestDataToGisNet(mDbConn, "0");
CivSysLog::getInstance()->info("结束水质分析计算", "CivHydrCalc", __FUNCTION__);
return false;
return true;
}
void CivHydrSimulation::getTrackingResult(int hours,char* result)
void CivHydrSimulation::getTrackingResult(int hours, std::string& result)
{
result.append("[");
for (int i = 0; i < hours; i++)
{
NodeResultItems nodeItemsMap;
LinkResultItems linkItemsMap;
mResultCache.getResultByTime(i, nodeItemsMap, linkItemsMap);
std::string nodeTempText;
nodeTempText.append("[");
// 查找节点追踪的节点编号
auto nodeIter = nodeItemsMap.begin();
size_t nodeTotal = nodeItemsMap.size();
while (nodeIter != nodeItemsMap.end())
{
if (nodeIter->second.dQuality < 0.8)
{
nodeTempText.append(nodeIter->first.c_str());
nodeTempText.append(",");
}
nodeIter++;
}
nodeTempText.append("]");
// 追踪管段编号
auto linkIter = linkItemsMap.begin();
size_t total = linkItemsMap.size();
int index = 1;
std::string linkTempText;
linkTempText.append("[");
while (linkIter != linkItemsMap.end())
{
if (linkIter->second.dQuality > 0)
{
linkTempText.append(linkIter->first.c_str());
if (index != total)
{
linkTempText.append(",");
}
}
linkIter++;
index++;
}
linkTempText.append("]");
/* result.append(tempText);
if (i <(hours-1))
{
result.append("|");
}*/
}
result.append("]");
}
void CivHydrSimulation::getNodeResult(short time)
......
......@@ -22,19 +22,19 @@ public:
/**
*@brief 水力模拟
*/
int hdyrSimulation();
bool hdyrSimulation();
/**
*@brief 水质模拟,水龄,化学物质
*/
int qualitySimulation();
bool qualitySimulation();
/**
*@brief 追踪分析,本质仍然式水质分析
*@snNode:追踪的节点号
*@hours: 小时数
*/
int trackingSimulation(char* snNode, int hours);
bool trackingSimulation(char* snNode, int hours);
/**
*@brief 导出inp文件
......@@ -47,8 +47,26 @@ public:
*@brief 获取追踪结果值
*@hours:追踪的小时数
*/
void getTrackingResult(int hours, char* result);
void getTrackingResult(int hours, std::string& result);
/**
*@brief 同一天只保留最后一次模拟的结果值,须清除当前之前模拟的结果值
*@dbConn: 数据库连接对象指针
*@date: 日期
*/
bool clearSamePeriodValue(CivDbConn* dbConn, const std::string& date);
/**
*@brief 及时更新最新模拟的数据到gis管网模型中,默认更新零时刻
*@dbConn: 数据库连接对象指针
*@time: 时段
*/
bool updateLastestDataToGisNet(CivDbConn* dbConn,const std::string& time);
/**
*@brief 刷新最新的模拟的数据
*/
bool brushingResult();
/*
inp文件名,输出文件名,二进制文件名设置和获取
*/
......
......@@ -63,7 +63,6 @@ bool CivSimulResultCache::updateToDb(CivDbConn* dbConn)
}
}
CivSysLog::getInstance()->error("写入数据库完成", "CivSimulResultCache", __FUNCTION__);
clear();
return true;
}
......@@ -99,6 +98,31 @@ bool CivSimulResultCache::updateToDb(CivDbConnection* dbConn)
return true;
}
void CivSimulResultCache::getResultByTime(int time, NodeResultItems& nodeItems, LinkResultItems& linkItems)
{
nodeItems.clear();
linkItems.clear();
auto nodeIter = mNodeItemsMap.find(time);
auto linkIter = mLinkItemsMap.find(time);
if (nodeIter == mNodeItemsMap.end() || linkIter == mLinkItemsMap.end())
{
CivSysLog::getInstance()->error("存储数据出错了,找不到该对应时段的值", "CivSimulResultCache", __FUNCTION__);
return;
}
// 避免发生异常
try
{
nodeItems = nodeIter->second;
linkItems = linkIter->second;
}
catch (const std::exception& e)
{
CivSysLog::getInstance()->error(e.what(), "CivSimulResultCache", __FUNCTION__);
}
}
void CivSimulResultCache::clear()
{
mLinkItemsMap.clear();
......
......@@ -52,6 +52,15 @@ public:
bool updateToDb(CivDbConnection* dbConn);
bool updateToDb(CivDbConn* dbConn);
/**
*@brief 根据时段获取
*@param time 时段
*@param nodeItems 节点值集合
*@param linkItems 管线值集合
*/
void getResultByTime(int time, NodeResultItems& nodeItems, LinkResultItems& linkItems);
private:
std::map<int, NodeResultItems> mNodeItemsMap; // 模拟
std::map<int, LinkResultItems> mLinkItemsMap; // 计算的
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment