From d466c14a611325ed7c9e245e80cb5ed32d4edeaa Mon Sep 17 00:00:00 2001 From: xiaowei_han Date: Fri, 15 Sep 2017 13:15:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AF=BB=E5=8F=96=E4=BC=A0?= =?UTF-8?q?=E6=84=9F=E5=99=A8=E7=B1=BB=E5=9E=8B=E4=BB=8E=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=AF=BB=E5=8F=96=E7=9A=84=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Src/Edislab/Bin_x64/Setup.iss | 1 + Src/Edislab/Edislab Pro/DlgSensorChoose.cpp | 20 +-- Src/Edislab/Edislab Pro/Edislab Pro.cpp | 8 +- Src/Edislab/Edislab Pro/SensorManager.cpp | 4 +- Src/Edislab/Edislab Pro/SensorTypeTable.cpp | 146 +++++++++++++++++- Src/Edislab/Edislab Pro/SensorTypeTable.h | 32 +++- Src/Edislab/Edislab Pro/SerialPortService.cpp | 4 +- 7 files changed, 193 insertions(+), 22 deletions(-) diff --git a/Src/Edislab/Bin_x64/Setup.iss b/Src/Edislab/Bin_x64/Setup.iss index 13cb696..97b712f 100644 --- a/Src/Edislab/Bin_x64/Setup.iss +++ b/Src/Edislab/Bin_x64/Setup.iss @@ -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] diff --git a/Src/Edislab/Edislab Pro/DlgSensorChoose.cpp b/Src/Edislab/Edislab Pro/DlgSensorChoose.cpp index 89db558..8630141 100644 --- a/Src/Edislab/Edislab Pro/DlgSensorChoose.cpp +++ b/Src/Edislab/Edislab Pro/DlgSensorChoose.cpp @@ -12,6 +12,7 @@ #include "SensorConfig.h" #include "SerialPortService.h" #include "GridColumnGroupManager.h" +#include "SensorDataManager.h" // CDlgSensorChoose 对话框 IMPLEMENT_DYNAMIC(CDlgSensorChoose, CBaseDialog) @@ -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())); // 拼凑添加项描述文字 @@ -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()); @@ -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); @@ -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(); @@ -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("]"); diff --git a/Src/Edislab/Edislab Pro/Edislab Pro.cpp b/Src/Edislab/Edislab Pro/Edislab Pro.cpp index d17363e..0faae9d 100644 --- a/Src/Edislab/Edislab Pro/Edislab Pro.cpp +++ b/Src/Edislab/Edislab Pro/Edislab Pro.cpp @@ -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 @@ -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()) { diff --git a/Src/Edislab/Edislab Pro/SensorManager.cpp b/Src/Edislab/Edislab Pro/SensorManager.cpp index a3c1b9c..849b5aa 100644 --- a/Src/Edislab/Edislab Pro/SensorManager.cpp +++ b/Src/Edislab/Edislab Pro/SensorManager.cpp @@ -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)); } /********************************************************* diff --git a/Src/Edislab/Edislab Pro/SensorTypeTable.cpp b/Src/Edislab/Edislab Pro/SensorTypeTable.cpp index 9b6537a..bb661f8 100644 --- a/Src/Edislab/Edislab Pro/SensorTypeTable.cpp +++ b/Src/Edislab/Edislab Pro/SensorTypeTable.cpp @@ -13,10 +13,17 @@ Version:1.0 #include #include #include +#include +#include +#include +#include +#include +#include "Utility.h" +#include "Log.h" CSensorTypeTable::CSensorTypeTable(void) { m_SensorTypeMap.clear(); - InitSensorTypeTable(); + //InitSensorTypeTable(); } @@ -25,6 +32,7 @@ CSensorTypeTable::~CSensorTypeTable(void) m_SensorTypeMap.clear(); } +#if 0 void CSensorTypeTable::InitSensorTypeTable(void) { int nSensorID = 0; @@ -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 eis(bis); + Document Parser; + Parser.ParseStream<0, AutoUTF >(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; diff --git a/Src/Edislab/Edislab Pro/SensorTypeTable.h b/Src/Edislab/Edislab Pro/SensorTypeTable.h index 4b23c7e..88ecfe9 100644 --- a/Src/Edislab/Edislab Pro/SensorTypeTable.h +++ b/Src/Edislab/Edislab Pro/SensorTypeTable.h @@ -12,9 +12,31 @@ Version:1.0 #include #include #include +#include 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: @@ -22,14 +44,16 @@ class CSensorTypeTable ~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 m_SensorTypeMap; + boost::unordered_map m_SensorTypeMap; static CSensorTypeTable s_obj; }; diff --git a/Src/Edislab/Edislab Pro/SerialPortService.cpp b/Src/Edislab/Edislab Pro/SerialPortService.cpp index 6b35b4b..d2cd274 100644 --- a/Src/Edislab/Edislab Pro/SerialPortService.cpp +++ b/Src/Edislab/Edislab Pro/SerialPortService.cpp @@ -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%");