Skip to content

Commit

Permalink
修改读取传感器类型从配置文件读取的方法。
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoweihan committed Sep 15, 2017
1 parent 5add96a commit d466c14
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 22 deletions.
1 change: 1 addition & 0 deletions Src/Edislab/Bin_x64/Setup.iss
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Source:"*.dll";Excludes:"*d.dll,*d100.dll";DestDir:"{app}"
;Source:"*.ocx";DestDir:"{app}"
Source:"UnInstall.ico";DestDir:"{app}"
Source:"SensorConfig.json";DestDir:"{app}"
Source:"SensorType.json";DestDir:"{app}"
Source:"Edislab Pro.ico";DestDir:"{app}";Source:"版本变更内容.txt";DestDir:{app};

[Icons]
Expand Down
20 changes: 10 additions & 10 deletions Src/Edislab/Edislab Pro/DlgSensorChoose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "SensorConfig.h"
#include "SerialPortService.h"
#include "GridColumnGroupManager.h"
#include "SensorDataManager.h"
// CDlgSensorChoose 对话框

IMPLEMENT_DYNAMIC(CDlgSensorChoose, CBaseDialog)
Expand Down Expand Up @@ -194,12 +195,15 @@ void CDlgSensorChoose::OnBnClickedBtnAdd()
{
std::string strSensorUnit = iter->second.SensorRangeInfoArray[0].strUnitName;

std::string strTempSensorName = CSensorTypeTable::CreateInstance().QuerySensorNameByID(iter->second.nSensorID);
std::string strTempSensorName = CSensorTypeTable::CreateInstance().QuerySensorNameByID(iter->second.nSensorID).strSensorTypeName;

std::string strSensorName = strTempSensorName;//strTempSensorName + std::string("[") + strSensorUnit + std::string("]");

CSensorManager::CreateInstance().RegisterSensor(iter->second.nSensorID,0);

//添加对应SensorID的数据
CSensorDataManager::CreateInstance().AddSensorData(SENSOR_TYPE_KEY(iter->second.nSensorID,0));

str.Format(_T("数据列:%s(%s)"), CString(iter->second.strSensorSymbol.c_str()), CString(iter->second.strSensorName.c_str()));

// 拼凑添加项描述文字
Expand Down Expand Up @@ -278,10 +282,10 @@ void CDlgSensorChoose::OnBnClickedBtnDelete()
// 删除传感器信息
std::string strSensorUnit = element.SensorRangeInfoArray[0].strUnitName;

std::string strSensorName = element.strSensorName + std::string("[") + strSensorUnit + std::string("]");
std::string strSensorName = CSensorTypeTable::CreateInstance().QuerySensorNameByID(element.nSensorID).strSensorTypeName;//element.strSensorName; //+ std::string("[") + strSensorUnit + std::string("]");

CSensorDataManager::CreateInstance().DelSensorData(SENSOR_TYPE_KEY(element.nSensorID,0));
//根据ID

CSensorManager::CreateInstance().UnRegisterSensor(element.nSensorID,0);
// 删除表格数据列
CString strColumnName(strSensorName.c_str());
Expand Down Expand Up @@ -329,8 +333,9 @@ void CDlgSensorChoose::OnBnClickedBtnDeleteAll()
// 删除传感器信息
std::string strSensorUnit = element.SensorRangeInfoArray[0].strUnitName;

std::string strSensorName = element.strSensorName + std::string("[") + strSensorUnit + std::string("]");
std::string strSensorName = CSensorTypeTable::CreateInstance().QuerySensorNameByID(element.nSensorID).strSensorTypeName;//element.strSensorName; //+ std::string("[") + strSensorUnit + std::string("]");
CSensorManager::CreateInstance().UnRegisterSensor(element.nSensorID,0);
CSensorDataManager::CreateInstance().DelSensorData(SENSOR_TYPE_KEY(element.nSensorID,0));
// 删除表格数据列
CString strColumnName(strSensorName.c_str());
CGridColumnGroupManager::CreateInstance().RemoveColumnInfo(_T("当前"), strColumnName);
Expand All @@ -344,11 +349,6 @@ void CDlgSensorChoose::OnBnClickedBtnDeleteAll()
{
pWnd->PostMessage(WM_NOTIFY_GRID_GROUP_INFO_CHANGE,0,0);
}





m_mapChooseSensor.clear();
m_ListChoosedSensor.CleanUp();
m_setChooseSensorID.clear();
Expand Down Expand Up @@ -425,7 +425,7 @@ void CDlgSensorChoose::RefreshSensorList()
std::string strSensorUnit = sensor.SensorRangeInfoArray[0].strUnitName;

//根据传感器ID获取传感器名称
sensor.strSensorName = CSensorTypeTable::CreateInstance().QuerySensorNameByID(sensor.nSensorID);
sensor.strSensorName = CSensorTypeTable::CreateInstance().QuerySensorNameByID(sensor.nSensorID).strSensorTypeName;

//std::string strSensorName = sensor.strSensorName + std::string("[") + strSensorUnit + std::string("]");

Expand Down
8 changes: 7 additions & 1 deletion Src/Edislab/Edislab Pro/Edislab Pro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "SensorConfig.h"
#include "SensorDataManager.h"
#include "SensorData.h"
//#include "SensorIDGenerator.h"
#include "SensorTypeTable.h"
#include "SensorManager.h"
using std::string;
#ifdef _DEBUG
Expand Down Expand Up @@ -213,6 +213,12 @@ void CEdislabProApp::Init( void )
CDumpFileSwitch::CreateInstance().SetDumpFilePath(strDumpFilePath.c_str());
CDumpFileSwitch::CreateInstance().OpenSwitch();

//加载传感器类型配置文件
if (!CSensorTypeTable::CreateInstance().LoadSensorTypeListFromFile())
{
ERROR_LOG("LoadSensorType failed!");
}

//加载传感器配置文件
if (!CSensorConfig::CreateInstance().LoadSensorConfig())
{
Expand Down
4 changes: 2 additions & 2 deletions Src/Edislab/Edislab Pro/SensorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ bool CSensorManager::RegisterSensor(int nSensorTypeID,int nSensorSerialID)
{
using namespace std;
//根据传感器类型的ID查询传感器的名称
string strSensorName = CSensorTypeTable::CreateInstance().QuerySensorNameByID(nSensorTypeID);
return RegisterSensor(SENSOR_TYPE_INFO_ELEMENT(strSensorName,nSensorTypeID,nSensorSerialID));
auto strSensorName = CSensorTypeTable::CreateInstance().QuerySensorNameByID(nSensorTypeID);
return RegisterSensor(SENSOR_TYPE_INFO_ELEMENT(strSensorName.strSensorTypeName,nSensorTypeID,nSensorSerialID));
}

/*********************************************************
Expand Down
146 changes: 142 additions & 4 deletions Src/Edislab/Edislab Pro/SensorTypeTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ Version:1.0
#include <boost/thread/lock_factories.hpp>
#include <boost/assign.hpp>
#include <string>
#include <boost/scope_exit.hpp>
#include <rapidjson/document.h>
#include <rapidjson/filereadstream.h>
#include <rapidjson/encodedstream.h>
#include <boost/filesystem.hpp>
#include "Utility.h"
#include "Log.h"
CSensorTypeTable::CSensorTypeTable(void)
{
m_SensorTypeMap.clear();
InitSensorTypeTable();
//InitSensorTypeTable();
}


Expand All @@ -25,6 +32,7 @@ CSensorTypeTable::~CSensorTypeTable(void)
m_SensorTypeMap.clear();
}

#if 0
void CSensorTypeTable::InitSensorTypeTable(void)
{
int nSensorID = 0;
Expand Down Expand Up @@ -214,24 +222,154 @@ void CSensorTypeTable::InitSensorTypeTable(void)
strSensorName = "电流/电压合体传感器";
m_SensorTypeMap.emplace(nSensorID,strSensorName);
}
#endif

CSensorTypeTable& CSensorTypeTable::CreateInstance(void)
{
return s_obj;
}

std::string CSensorTypeTable::QuerySensorNameByID(int nSensorTypeID)

CSensorTypeTable::SENSOR_TYPE_VALUE CSensorTypeTable::QuerySensorNameByID(int nSensorTypeID)
{
SENSOR_TYPE_VALUE Result;

auto Lock = boost::make_unique_lock(m_Lock);

auto Iter = m_SensorTypeMap.find(nSensorTypeID);

if (Iter != m_SensorTypeMap.end())
{
return Iter->second;
Result = Iter->second;
}

return Result;
}

bool CSensorTypeTable::LoadSensorTypeListFromFile(void)
{
using namespace std;
using namespace rapidjson;
std::string strConfigPath = Utility::GetExeDirecory() + std::string("\\SensorType.json");

//判断文件是否存在
boost::filesystem::path ConfigPath(strConfigPath);

try
{
//判断配置文件是否存在
if (!boost::filesystem::exists(ConfigPath))
{
ERROR_LOG("the [%s] is not exist!",strConfigPath.c_str());
return false;
}
//开始进行json文件解析
FILE* fp = fopen(strConfigPath.c_str(),"rb");

if (nullptr == fp)
{
ERROR_LOG("open [%s] file failed.", strConfigPath.c_str());
return false;
}
BOOST_SCOPE_EXIT(&fp)
{
if (nullptr != fp)
{
fclose(fp);
fp = nullptr;
}

}BOOST_SCOPE_EXIT_END
//获取文件的长度
fseek(fp,0,SEEK_END);
int nFileLength = (int)ftell(fp);
if (nFileLength < 0)
{
ERROR_LOG("get [%s] file length failed.", strConfigPath.c_str());
return false;
}
//申请内存
char* pBuffer = new char[nFileLength];
if (nullptr == pBuffer)
{
ERROR_LOG("Allocate memory failed.");
return false;
}
BOOST_SCOPE_EXIT(&pBuffer)
{
if (nullptr != pBuffer)
{
delete []pBuffer;
}
}BOOST_SCOPE_EXIT_END
ZeroMemory(pBuffer,nFileLength);
fseek(fp,0,SEEK_SET);
FileReadStream bis(fp,pBuffer,nFileLength);
AutoUTFInputStream<unsigned, FileReadStream> eis(bis);
Document Parser;
Parser.ParseStream<0, AutoUTF<unsigned> >(eis);
if (Parser.HasParseError())
{
ERROR_LOG("Parse json file [%s] failed,the error [%d].", strConfigPath.c_str(),Parser.GetParseError());
return false;
}

//加载传感器类型列表
if (!LoadSensorTypeList(Parser))
{
return false;
}
}
catch (boost::filesystem::filesystem_error& e)
{
ERROR_LOG("oh,shit,this is something wrong in filesystem,the error is [%s].",e.what());

return false;
}
return true;
}

return "";
bool CSensorTypeTable::LoadSensorTypeList(rapidjson::Document& Parser)
{
const rapidjson::Value& SensorTypeArray = Parser["SensorTypeList"];

if (!SensorTypeArray.IsArray())
{
return false;
}


for (auto Iter = SensorTypeArray.Begin(); Iter != SensorTypeArray.End(); ++Iter)
{
if (Iter->IsObject())
{
SENSOR_TYPE_VALUE SensorElement;
// 传感器ID
if (!Iter->HasMember("SensorTypeID") || !(*Iter)["SensorTypeID"].IsInt())
{
continue;
}

SensorElement.nSensorTypeID = (*Iter)["SensorTypeID"].GetInt();

// 传感器名
if (!Iter->HasMember("SensorTypeName") || !(*Iter)["SensorTypeName"].IsString())
{
continue;
}
SensorElement.strSensorTypeName = Utility::ConverUTF8ToGB2312((*Iter)["SensorTypeName"].GetString());

// 传感器单位
if (!Iter->HasMember("SensorTypeUnit") || !(*Iter)["SensorTypeUnit"].IsString())
{
continue;
}
SensorElement.strSensorUnit = (*Iter)["SensorTypeUnit"].GetString();

m_SensorTypeMap.emplace(SensorElement.nSensorTypeID,SensorElement);
}
}
return true;
}

CSensorTypeTable CSensorTypeTable::s_obj;
32 changes: 28 additions & 4 deletions Src/Edislab/Edislab Pro/SensorTypeTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,48 @@ Version:1.0
#include <boost/thread/mutex.hpp>
#include <boost/unordered_map.hpp>
#include <string>
#include <rapidjson/document.h>
class CSensorTypeTable
{

public:
typedef struct _sensor_type_value
{
int nSensorTypeID;
std::string strSensorTypeName;
std::string strSensorUnit;

_sensor_type_value()
{
nSensorTypeID = -1;
}

_sensor_type_value(int nSensorTypeID,const std::string& strSensorTypeName,const std::string& strSensorUnit)
{
this->nSensorTypeID = nSensorTypeID;
this->strSensorTypeName = strSensorTypeName;
this->strSensorUnit = strSensorUnit;
}

}SENSOR_TYPE_VALUE,* LP_SENSOR_TYPE_VALUE;

public:
static CSensorTypeTable& CreateInstance(void);
private:
CSensorTypeTable(void);
~CSensorTypeTable(void);

public:
std::string QuerySensorNameByID(int nSensorTypeID);

SENSOR_TYPE_VALUE QuerySensorNameByID(int nSensorTypeID);
//从配置文件加载传感器类型
bool LoadSensorTypeListFromFile(void);
private:
void InitSensorTypeTable(void);
//void InitSensorTypeTable(void);
bool LoadSensorTypeList(rapidjson::Document& Parser);
private:
boost::mutex m_Lock;

boost::unordered_map<int,std::string> m_SensorTypeMap;
boost::unordered_map<int,SENSOR_TYPE_VALUE> m_SensorTypeMap;

static CSensorTypeTable s_obj;
};
Expand Down
4 changes: 3 additions & 1 deletion Src/Edislab/Edislab Pro/SerialPortService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ int CSerialPortService::HandlerData(BYTE* pData, int nDataLength)
if (nSensorSeqID >= 0 && nSensorTypeID >= 0)
{
//根据设备ID获取设备名称
std::string strSensorName = CSensorTypeTable::CreateInstance().QuerySensorNameByID(nSensorTypeID);
auto SensorName = CSensorTypeTable::CreateInstance().QuerySensorNameByID(nSensorTypeID);

std::string strSensorName = SensorName.strSensorTypeName;

//组装设备名称
boost::format FT("%1%_%2%");
Expand Down

0 comments on commit d466c14

Please sign in to comment.