12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145 |
- // pch.cpp: 与预编译标头对应的源文件
- #include "pch.h"
- #include "resource.h" // 主符号
- // 当使用预编译的头时,需要使用此源文件,编译才能成功。
- //主窗口句柄
- HWND gal_hTopWnd = nullptr;
- //输出日志:可供下层模块调用(下发至各个窗口)
- Log4CPP::Logger* mLog::gLogger = nullptr;
- //输出日志:可供下层模块调用
- logFun gal_pLogFun = nullptr;
- void ToolsDlgLog(CString strlog)
- {
- if (gal_pLogFun != nullptr)
- {
- gal_pLogFun(strlog);
- }
- }
- //输出消息:可供下层模块调用
- msgFun gal_pMSGFun = nullptr;
- void ToolsDlgMSG(CString strKey, CString strText)
- {
- if (gal_pMSGFun != nullptr)
- {
- gal_pMSGFun(strKey, strText);
- }
- }
- //全局设备集合对象
- CDeviceSET CDeviceSET::glo_CDeviceSET;
- _DeviceMape CDeviceSET::m_CDeviceMap;
- //********************************公共方法定义********************************
- //获取进程路径
- string GetProcessDirectory(bool withPlatformModule)
- {
- string ret = "";
- char szFilename[MAX_PATH] = { 0 };
- DWORD res = GetModuleFileNameA(0, szFilename, MAX_PATH);
- if (res == 0)
- {
- return ret;
- }
- string fullpath = szFilename;
- if (withPlatformModule)
- {
- string::size_type firstHit = fullpath.find_last_of('\\');
- if (firstHit != string::npos && firstHit != 0)
- {
- ret = fullpath.substr(0, firstHit);//kick last
- }
- }
- else
- {
- string V3_1_Key("\\");
- V3_1_Key += UnitTool_PlatformModule;
- string::size_type firstHit = fullpath.find(V3_1_Key);
- if (firstHit != string::npos)
- {
- ret = fullpath.substr(0, firstHit);
- }
- else
- {
- firstHit = fullpath.find_last_of('\\');
- if (firstHit != string::npos && firstHit != 0)
- {
- ret = fullpath.substr(0, firstHit);//kick last
- }
- }
- }
- //mLog::Info("get device ProcessDirectory:{$}\n", ret.c_str());
- return ret;
- }
- //获取模块版本
- bool GetVersion(string& version)
- {
- try {
- char filename[MAX_PATH + 1] = { 0 };
- if (GetModuleFileName(nullptr, filename, MAX_PATH) == 0)
- {
- return false;
- }
- printf("get Generator path:%s\n", filename);
- mLog::Info("get device path:{$}\n", filename);
- DWORD dummy;
- DWORD size = GetFileVersionInfoSize(filename, &dummy);
- if (size == 0)
- {
- return false;
- }
- auto data = make_unique<BYTE[]>(size);
- if (!GetFileVersionInfo(filename, 0, size, &data[0]))
- {
- return false;
- }
- UINT32 len = 0;
- VS_FIXEDFILEINFO* fixed_file_info = 0;
- if (!VerQueryValue(&data[0], TEXT("\\"), reinterpret_cast<void**>(&fixed_file_info), &len))
- {
- return false;
- }
- // version 为版本号
- UINT32 vBitNumber = 0;
- vBitNumber = HIWORD(fixed_file_info->dwProductVersionMS);
- version += to_string(vBitNumber);
- version += ".";
- vBitNumber = LOWORD(fixed_file_info->dwProductVersionMS);
- version += to_string(vBitNumber);
- version += ".";
- vBitNumber = HIWORD(fixed_file_info->dwProductVersionLS);
- version += to_string(vBitNumber);
- version += ".";
- vBitNumber = LOWORD(fixed_file_info->dwProductVersionLS);
- version += to_string(vBitNumber);
- return true;
- }
- catch (...)
- {
- printf("get Generator Mould version failed");
- mLog::Error("get device Mould version failed");
- }
- return false;
- }
- //获取时间
- CString GetTimeStr()
- {
- CTime tm = CTime::GetCurrentTime();
- SYSTEMTIME st;
- GetSystemTime(&st);
- CString strTime;
- strTime.Format("[%02d:%02d:%02d,%03d]", tm.GetHour(), tm.GetMinute(), tm.GetSecond(), st.wMilliseconds);
- return strTime;
- }
- //读取文件夹下的所有文件路径
- void GetAllCFGFiles(string path, vector<string>& files)
- {
- CFileFind find;
- BOOL IsFind = find.FindFile(CString(path.c_str()) + _T("/*.xml"));
- while (IsFind)
- {
- IsFind = find.FindNextFile();
- if (find.IsDots())
- {
- continue;
- }
- else
- {
- files.push_back(string(find.GetFileName()));
- }
- }
- IsFind = find.FindFile(CString(path.c_str()) + _T("/*.json"));
- while (IsFind)
- {
- IsFind = find.FindNextFile();
- if (find.IsDots())
- {
- continue;
- }
- else
- {
- files.push_back(string(find.GetFileName()));
- }
- }
- }
- void GetAllLibrary(string path, vector<string>& dlls, string nameKey)
- {
- CFileFind find;
- BOOL IsFind = find.FindFile(CString(path.c_str()) + _T("/*.dll"));
- while (IsFind)
- {
- IsFind = find.FindNextFile();
- if (find.IsDots())
- {
- continue;
- }
- else
- {
- string itemName = find.GetFileName();
- if(!nameKey.empty() && itemName.find(nameKey) != string::npos)
- dlls.push_back(itemName);
- }
- }
- }
- //设置、删除临时环境变量
- bool AddEnvPath(const char* pPath)
- {
- DWORD PathLen = 32768;
- char* pszPath = new char[PathLen];
- DWORD Len = GetEnvironmentVariable("Path", pszPath, PathLen);
- if (Len > PathLen)
- {
- mLog::Error("AddEnvPath:EnvironmentVariable too long[{$}]", (int)Len);
- delete[]pszPath;
- return false;
- }
- string::size_type pos = 0;
- string EnvPath = pszPath;
- delete[]pszPath;
- //EnvPath.find(pPath, 0);
- string NewPath;
- if ((pos = EnvPath.find(pPath, pos)) == string::npos)
- {
- NewPath = EnvPath;
- NewPath += ";";
- NewPath += pPath;
- if (SetEnvironmentVariable("Path", NewPath.c_str()) == FALSE)
- {
- mLog::Error("AddEnvPath:SetEnvironmentVariable failed");
- return false;
- }
- }
- mLog::Debug("AddEnvPath:SetEnvironmentVariable[{$}]successed", NewPath.c_str());
- return true;
- }
- bool DelEnvPath(const char* pPath)
- {
- DWORD PathLen = 32768;
- char* pszPath = new char[PathLen];
- DWORD Len = GetEnvironmentVariable("Path", pszPath, PathLen);
- if (Len > PathLen)
- {
- delete[]pszPath;
- return false;
- }
- string::size_type pos = 0;
- string EnvPath = pszPath;
- delete[]pszPath;
- //EnvPath.find(pPath, 0);
- if ((pos = EnvPath.find(pPath, pos)) > 0)
- {
- string NewPath = EnvPath;
- //NewPath += ";";
- //NewPath += pPath;
- NewPath.erase(pos, pos + strlen(pPath) + 1);
- if (SetEnvironmentVariable("Path", NewPath.c_str()) == FALSE)
- {
- return false;
- }
- }
- return true;
- }
- //********************************指令查找表********************************
- tFrameMapItem::tFrameMapItem()
- {
- m_fFun = NULL;
- }
- tFrameMapItem::tFrameMapItem(cmdFun f)
- {
- m_fFun = f;
- }
- tFrameMapItem& tFrameMapItem::operator =(const tFrameMapItem& value)
- {
- m_fFun = value.m_fFun;
- return *this;
- }
- //********************************通用模块类********************************
- Device::Device(Dev_Type type)
- {
- CString log;
- string name = UnitTool_DevType_Null;
- string logTitle = UnitTool_logKey_Null;
- string cfgLib;
- string cfgFile;
- switch (type)
- {
- case Dev_Generator:
- {
- logTitle = UnitTool_logKey_Gen;
- name = UnitTool_DevType_Gen;
- cfgLib = UnitTool_GenLibDLL;
- cfgFile = UnitTool_GenFileName;
- }break;
- case Dev_Mechanical:
- {
- logTitle = UnitTool_logKey_Mech;
- name = UnitTool_DevType_Mech;
- cfgLib = UnitTool_MechLibDLL;
- cfgFile = UnitTool_MechFileName;
- }break;
- case Dev_Collimator:
- {
- logTitle = UnitTool_logKey_Col;
- name = UnitTool_DevType_Col;
- cfgLib = UnitTool_ColLibDLL;
- cfgFile = UnitTool_ColFileName;
- }break;
- case Dev_SynBox:
- {
- logTitle = UnitTool_logKey_Syn;
- name = UnitTool_DevType_Syn;
- cfgLib = UnitTool_SynLibDLL;
- cfgFile = UnitTool_SynFileName;
- }break;
- case Dev_Detector:
- {
- logTitle = UnitTool_logKey_FPD;
- name = UnitTool_DevType_FPD;
- cfgLib = UnitTool_FPDLibDLL;
- cfgFile = UnitTool_FPDFileName;
- }break;
- case Dev_SMachine:
- {
- logTitle = UnitTool_logKey_SM;
- name = UnitTool_DevType_SM;
- cfgLib = UnitTool_SMLibDLL;
- cfgFile = UnitTool_SMFileName;
- }break;
- case Dev_TypeALL:
- {
- logTitle = UnitTool_logKey_UI;
- name = UnitTool_DevType_UI;
- }break;
- default:
- log.Format("unknown DevType[%d]", (int)type);
- ToolsDlgLog(log);
- break;
- }
- m_enuType = type;
- m_strName = name;
- m_strLogTitle = logTitle;
- m_strLibTitle = cfgLib;
- m_strFileTitle = cfgFile;
- }
- bool Device::CreatMode(bool act)
- {
- CString log;
- try
- {
- if (act)
- {
- if (m_pDevice)
- {
- mLog::Debug("CreatModel: Device type[{$}] aiready create", (int)m_enuType);
- return true;
- }
- mLog::Debug("{$}AppInitial:Initial", m_strLogTitle.c_str());
- string strCFGPath("");
- string strDLLPath("");
- if (!OpenCFGAndGetDllPath(strCFGPath, strDLLPath))
- {
- log.Format("%s OpenCFGAndGetDllPath failed", m_strLogTitle.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- return false;
- }
- if (m_hDevLib != NULL)
- {
- FreeLibrary(m_hDevLib);
- m_hDevLib = NULL;
- }
- #if true //优先加载DLL所在目录
- m_hDevLib = LoadLibraryEx(strDLLPath.c_str(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
- #else
- m_hDevLib = LoadLibrary(strDLLPath.c_str());
- #endif // 1
- if (m_hDevLib == NULL)
- {
- DWORD ret = GetLastError();
- log.Format("%s Failed to load library ret = %d", m_strLogTitle.c_str(), ret);
- mLog::Error(log);
- ToolsDlgLog(log);
- return false;
- }
- Sleep(100);
- mLog::Debug("{$}get module entry function", m_strLogTitle.c_str());
- auto inletFun = GetProcAddress(m_hDevLib, "CreateIODriver");//GetIODriver
- if (!inletFun)
- {
- log.Format("%s entry function get failed", m_strLogTitle.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- return false;
- }
- shared_ptr<nsDEV::IODriver> tempDriver(reinterpret_cast <nsDEV::IODriver*> (inletFun()));
- if (!tempDriver)
- {
- log.Format("%s entry function conversion failed", m_strLogTitle.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- return false;
- }
- Sleep(100);
- mLog::Debug("{$}Module driver execution DriverEntry", m_strLogTitle.c_str());
- tempDriver->DriverEntry(strCFGPath);
- Sleep(100);
- mLog::Debug("{$}Module driver execution Prepare", m_strLogTitle.c_str());
- tempDriver->Prepare();
- Sleep(100);
- mLog::Debug("{$}Module driver execution Connect", m_strLogTitle.c_str());
- tempDriver->Connect();
- auto bConnected = tempDriver->isConnected();
- if (!bConnected)
- {
- log.Format("%s Module failed to connect to device", m_strLogTitle.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- return false;
- }
- bConnected = tempDriver->isConnected();
- Sleep(100);
- //bConnected = tempDriver->isConnected();
- int devNumber = 1;
- if (m_DeviceConfig[UnitTool_CONFIGURATION].GetKeyCount(UnitTool_DeviceNumber) > 0)
- {
- devNumber = m_DeviceConfig[UnitTool_CONFIGURATION][UnitTool_DeviceNumber];
- }
- mLog::Debug("{$}Module Driver Create [{$}]Logical Device Object", m_strLogTitle.c_str(), devNumber);
- for (int i = 0; i < devNumber; i++)
- {
- unique_ptr<nsDEV::IODevice> tempDevice = tempDriver->CreateDevice(i);
- if (tempDevice->Prepare())
- {
- Dev_Type tempDevType = CDeviceSET::GetInstance()->CheckDeviceType(tempDevice->GetGUID());
- mLog::Debug("{$}Logical Device type[{$}]execute Prepare", m_strLogTitle.c_str(), (int)tempDevType);
- auto findItem = CDeviceSET::GetInstance()->m_CDeviceMap.find(tempDevType);
- if (findItem != CDeviceSET::GetInstance()->m_CDeviceMap.end())
- {
- findItem->second->m_pDriver = tempDriver;
- findItem->second->m_BelongToOtherDriver = m_enuType;
- findItem->second->m_pDevice = std::move(tempDevice);
- findItem->second->m_pDevice->EventCenter->OnNotify.Push([this](int NotifyCode, string key, string context) { OnDiosNotify(NotifyCode, key, context); });
- mLog::Debug("{$}Device[{$}:{$}] Create success", m_strLogTitle.c_str(), i, findItem->second->m_strName.c_str());
- if (gal_hTopWnd && tempDevType != m_enuType)
- {
- ::PostMessage(gal_hTopWnd, MSG_Dios_Notify_ModeInit, tempDevType, 1);
- }
- }
- else
- {
- log.Format("%s Unknow Device type[%d]", m_strLogTitle.c_str(), (int)tempDevType);
- mLog::Error(log);
- ToolsDlgLog(log);
- }
- }
- else
- {
- log.Format("%s Logical Device num[%d]execute Prepare failed", m_strLogTitle.c_str(), i);
- mLog::Error(log);
- ToolsDlgLog(log);
- }
- }
- Sleep(100);
- }
- else
- {
- if (!m_pDevice)
- {
- mLog::Debug("CreatModel: Device type[{$}] already Delete", (int)m_enuType);
- return true;
- }
- shared_ptr<nsDEV::IODriver> tempDriver = m_pDriver;
- for (auto findDevcie = CDeviceSET::GetInstance()->m_CDeviceMap.begin(); findDevcie != CDeviceSET::GetInstance()->m_CDeviceMap.end(); findDevcie++)
- {
- if (findDevcie->second->m_BelongToOtherDriver == m_enuType)
- {
- mLog::Debug("{$}AppInitial:Exit[{$}]", m_strLogTitle.c_str(), findDevcie->second->m_strName.c_str());
- if (gal_hTopWnd && findDevcie->second->m_enuType != m_enuType)
- {
- ::SendMessage(gal_hTopWnd, MSG_Dios_Notify_ModeInit, findDevcie->second->m_enuType, 0);
- Sleep(100);
- }
- if (nullptr != findDevcie->second->m_pDevice)
- {
- findDevcie->second->m_pDevice.reset();
- }
- if (nullptr != findDevcie->second->m_pDriver)
- {
- findDevcie->second->m_pDriver.reset();
- }
- }
- }
- if (tempDriver != nullptr)
- {
- tempDriver->Disconnect();
- mLog::Debug("{$}try release dirver[{$}] count[{$}]", m_strLogTitle.c_str(), m_strName.c_str(), tempDriver.use_count()-1);
- tempDriver.reset();
- if (m_hDevLib != NULL)
- {
- FreeLibrary(m_hDevLib);
- m_hDevLib = NULL;
- }
- }
- mLog::Debug("{$}exit success", m_strLogTitle.c_str());
- }
- }
- catch (const ResDataObjectExption&)
- {
- log.Format("%s open file failed", m_strLogTitle.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- return false;
- }
- catch (...)
- {
- log.Format("%s AppInitial failed", m_strLogTitle.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- return false;
- }
- return true;
- }
- void Device::SetMagMap()
- {}
- string Device::DevActionValue(string action, initializer_list<string> args)
- {
- CString log;
- string rsp{ "error" };
- try {
- ResDataObject json;
- if (0 == args.size())
- {
- return rsp;
- }
- char key[8]{ 0 };
- int num{ 0 };
- for (auto item : args)
- {
- sprintf(key,"P%1d", num);
- json.add(key, item.c_str());
- num++;
- }
- //mLog::Debug("act[{$}],param[{$}]", action.c_str(), json.encode());
- RET_STATUS rc0;
- if (m_pDevice != nullptr)
- rc0 = m_pDevice->Action(action, json.encode(), rsp);
- if (rc0 == RET_STATUS::RET_SUCCEED)
- {
- mLog::Debug("{$}[{$}] success", m_strLogTitle.c_str(), action.c_str());
- }
- else
- {
- log.Format("%s[%s] fail", m_strLogTitle.c_str(), action.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- rsp.clear();
- }
- }
- catch (...)
- {
- log.Format("%s action[%s] exe failed", m_strLogTitle.c_str(), action.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- }
- return rsp;
- }
- string Device::DevActionKeyValue(string action, map<string, string>& args, bool withP0)
- {
- CString log;
- string rsp{ "error" };
- try {
- ResDataObject json;
- if (0 == args.size())
- {
- return rsp;
- }
- ResDataObject tempParam;
- for (auto& item : args)
- {
- tempParam.add(item.first.c_str(), item.second.c_str());
- }
- if (withP0)
- {
- json.add("P0", tempParam);
- }
- else
- {
- json = tempParam;
- }
- //mLog::Debug("act[{$}],param[{$}]", action.c_str(), json.encode());
- RET_STATUS rc0;
- if (m_pDevice != nullptr)
- rc0 = m_pDevice->Action(action, json.encode(), rsp);
- if (rc0 == RET_STATUS::RET_SUCCEED)
- {
- mLog::Debug("{$}[{$}] success", m_strLogTitle.c_str(), action.c_str());
- }
- else
- {
- log.Format("%s[%s] fail", m_strLogTitle.c_str(), action.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- rsp.clear();
- }
- }
- catch (...)
- {
- log.Format("%s action[%s] exe failed", m_strLogTitle.c_str(), action.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- }
- return rsp;
- }
- string Device::DevAction(string action)
- {
- CString log;
- string strLogKey = UnitTool_logKey_Null;
- string rsp;
- RET_STATUS rc0;
- if (m_pDevice != nullptr)
- rc0 = m_pDevice->Action(action, "", rsp);
- if (rc0 == RET_STATUS::RET_SUCCEED)
- {
- mLog::Debug("{$}[{$}] success", m_strLogTitle.c_str(), action.c_str());
- }
- else
- {
- log.Format("%s[%s] fail", m_strLogTitle.c_str(), action.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- rsp.clear();
- }
- return rsp;
- }
- string Device::DevGetValue(string action)
- {
- CString log;
- string strLogKey = UnitTool_logKey_Null;
- string rsp{ "error" };
- try {
- RET_STATUS rc0;
- if (m_pDevice != nullptr)
- rc0 = m_pDevice->Get(action, rsp);
- if (rc0 == RET_STATUS::RET_SUCCEED)
- {
- mLog::Debug("{$}[{$}] success", m_strLogTitle.c_str(), action.c_str());
- }
- else
- {
- log.Format("%s[%s] fail", m_strLogTitle.c_str(), action.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- rsp.clear();
- }
- }
- catch (...)
- {
- log.Format("%s action[%s] exe failed", m_strLogTitle.c_str(), action.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- }
- return rsp;
- }
- void Device::OnDiosNotify(int& NotifyCode, string& key, string& context)
- {
- CString strNotify;
- mLog::Debug("OnNotify: code[{$}] key[{$}] text[{$}]", NotifyCode, key.c_str(), context.c_str());
- auto found = m_mapFrame.find(key);//此处pr用来在arFrame中找到对于的包头
- if (found == m_mapFrame.end())
- {
- mLog::Warn("OnNotify: UnKnown key");
- }
- else
- {
- found->second.m_fFun(NotifyCode, key, context);
- }
- }
- bool Device::OpenCFGAndGetDllPath(string& CFGPath, string& DLLPath)
- {
- CString log;
- switch (m_enuType)
- {
- case Dev_Generator:
- case Dev_Mechanical:
- case Dev_Collimator:
- case Dev_SynBox:
- case Dev_Detector:
- {
- string procdir = GetProcessDirectory();
- if (!m_strCfgFile.empty())
- {
- CFGPath = procdir + "\\" + UnitTool_DriverConfigPath + "\\" + m_strCfgFile;
- mLog::Debug("{$}Read module config file:{$}", m_strLogTitle.c_str(), CFGPath.c_str());
- if (!(m_DeviceConfig.loadFile(CFGPath.c_str())))
- {
- log.Format("%s Load reading module config file[%s]failed", m_strLogTitle.c_str(), CFGPath.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- return false;
- }
- if (m_DeviceConfig[UnitTool_CONFIGURATION].GetKeyCount(UnitTool_oemdriver) > 0)
- {
- DLLPath = (string)m_DeviceConfig[UnitTool_CONFIGURATION][UnitTool_oemdriver];
- }
- if (!(DLLPath.empty()))
- {
- string oemdrvpath = DLLPath;
- {
- string keystr = "%ROOT%";
- string::size_type pos = 0;
- string::size_type keylen = keystr.size();
- string::size_type replen = procdir.size();
- while ((pos = oemdrvpath.find(keystr, pos)) != string::npos)
- {
- oemdrvpath.replace(pos, keylen, procdir);
- pos += replen;
- }
- }
- DLLPath = oemdrvpath;
- }
- }
- else
- {
- log.Format("%s Read module config file is null", m_strLogTitle.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- return false;
- }
- }break;
- case Dev_SMachine:
- {
- string procdir = GetProcessDirectory(true);
- DLLPath = procdir + "\\" + m_strCfgLib;
- mLog::Debug("{$}get SMachine dll:{$}", m_strLogTitle.c_str(), DLLPath.c_str());
- CFGPath = m_strCfgFile;
- mLog::Debug("{$}Read module config file:{$}", m_strLogTitle.c_str(), CFGPath.c_str());
- }break;
- case Dev_TypeALL:
- {}break;
- default:
- log.Format("unknown DevType[%d]", (int)m_enuType);
- ToolsDlgLog(log);
- break;
- }
-
- #if UnitTool_UseJson
- string strMajorID, strVendorID, strProductID, strSerialID;
- if (m_DeviceConfig[UnitTool_CONFIGURATION].GetKeyCount(UnitTool_MajorID) > 0)
- {
- strMajorID = (string)m_DeviceConfig[UnitTool_CONFIGURATION][UnitTool_MajorID];
- }
- if (m_DeviceConfig[UnitTool_CONFIGURATION].GetKeyCount(UnitTool_VendorID) > 0)
- {
- strVendorID = (string)m_DeviceConfig[UnitTool_CONFIGURATION][UnitTool_VendorID];
- }
- if (m_DeviceConfig[UnitTool_CONFIGURATION].GetKeyCount(UnitTool_ProductID) > 0)
- {
- strProductID = (string)m_DeviceConfig[UnitTool_CONFIGURATION][UnitTool_ProductID];
- }
- if (m_DeviceConfig[UnitTool_CONFIGURATION].GetKeyCount(UnitTool_SerialID) > 0)
- {
- strSerialID = (string)m_DeviceConfig[UnitTool_CONFIGURATION][UnitTool_SerialID];
- }
- CFGPath = strMajorID + "_" + strVendorID + "_" + strProductID + "_" + strSerialID + ".json";
- #endif
- mLog::Debug("{$}Load cfgFile:{$}, library:{$}", m_strLogTitle.c_str(), CFGPath.c_str(), DLLPath.c_str());
- return true;
- }
- //********************************通用对话框基类********************************
- BOOL CDiosBasicDlg::OnInitDialog()
- {
- CDialogEx::OnInitDialog();
- //鼠标悬停
- m_Ttc.Create(this); //必需
- EnableToolTips(true); //设置是否启用提示
- m_Ttc.SetDelayTime(0); //设置延迟,如果为0则不等待
- m_Ttc.SetTipTextColor(RGB(0, 0, 255)); //设置提示文本的颜色
- m_Ttc.SetTipBkColor(RGB(255, 255, 255)); //设置提示框的背景颜色
- m_Ttc.SetMaxTipWidth(600);//设置提示文本框的最大宽度
- m_Ttc.Activate(TRUE); //设置是否启用提示
- SetToolTipsMsgMap();//封装的函数
- return TRUE;
- }
- void CDiosBasicDlg::InitPageElements(bool act)
- {}
- void CDiosBasicDlg::ShowPageElements(bool init, bool exit, bool cfg, bool general)
- {
- //init
- GetDlgItem(IDC_BUTTON1)->EnableWindow(init);
- //exit
- GetDlgItem(IDC_BUTTON2)->EnableWindow(exit);
- //config
- GetDlgItem(IDC_BUTTON3)->EnableWindow(cfg);
- }
- void CDiosBasicDlg::OnOK()
- {
- }
- void CDiosBasicDlg::OnCancel()
- {
- }
- BOOL CDiosBasicDlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
- {
- if ((GetStyle() & WS_VSCROLL) == WS_VSCROLL)
- {
- SCROLLINFO scinfo = { 0 };
- scinfo.cbSize = sizeof(SCROLLINFO);
- GetScrollInfo(SB_VERT, &scinfo, SIF_ALL);
- // 滚动条最大滚动位置
- int nPosMax = scinfo.nMax - (int)scinfo.nPage;
- // 旧窗体位置(窗体尺寸及滚动位置根据 SCROLLINFO::nMax、nPage、nPos 反算,省去增加并维护相关成员变量的代码)
- int nWndPosOld = scinfo.nPos * scinfo.nMax / nPosMax;
- // 移动滚动条
- scinfo.fMask = SIF_POS;
- scinfo.nPos = min(max(scinfo.nPos - zDelta, 0), nPosMax);
- SetScrollInfo(SB_VERT, &scinfo);
- // 新窗体位置(窗体尺寸及滚动位置根据 SCROLLINFO::nMax、nPage、nPos 反算,省去增加并维护相关成员变量的代码)
- int nWndPosNew = scinfo.nPos * scinfo.nMax / nPosMax;
- // 滚动窗体
- ScrollWindow(0, nWndPosOld - nWndPosNew);
- }
- else if ((GetStyle() & WS_HSCROLL) == WS_HSCROLL)
- {
- SCROLLINFO scinfo = { 0 };
- scinfo.cbSize = sizeof(SCROLLINFO);
- GetScrollInfo(SB_HORZ, &scinfo, SIF_ALL);
- // 滚动条最大滚动位置
- int nPosMax = scinfo.nMax - (int)scinfo.nPage;
- // 旧窗体位置(窗体尺寸及滚动位置根据 SCROLLINFO::nMax、nPage、nPos 反算,省去增加并维护相关成员变量的代码)
- int nWndPosOld = scinfo.nPos * scinfo.nMax / nPosMax;
- // 移动滚动条
- scinfo.fMask = SIF_POS;
- scinfo.nPos = min(max(scinfo.nPos - zDelta, 0), nPosMax);
- SetScrollInfo(SB_HORZ, &scinfo);
- // 新窗体位置(窗体尺寸及滚动位置根据 SCROLLINFO::nMax、nPage、nPos 反算,省去增加并维护相关成员变量的代码)
- int nWndPosNew = scinfo.nPos * scinfo.nMax / nPosMax;
- // 滚动窗体
- ScrollWindow(nWndPosOld - nWndPosNew, 0);
- }
- return CDialogEx::OnMouseWheel(nFlags, zDelta, pt);
- }
- void CDiosBasicDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- if ((GetStyle() & WS_VSCROLL) == WS_VSCROLL)
- {
- SCROLLINFO scinfo = { 0 };
- scinfo.cbSize = sizeof(SCROLLINFO);
- GetScrollInfo(SB_VERT, &scinfo, SIF_ALL);
- // 滚动条最大滚动位置
- int nPosMax = scinfo.nMax - (int)scinfo.nPage;
- // 旧窗体位置(窗体尺寸及滚动位置根据 SCROLLINFO::nMax、nPage、nPos 反算,省去增加并维护相关成员变量的代码)
- int nWndPosOld = scinfo.nPos * scinfo.nMax / nPosMax;
- switch (nSBCode)
- {
- case SB_TOP: scinfo.nPos = 0; break; // 滑块滚动到最顶部
- case SB_BOTTOM: scinfo.nPos = nPosMax; break; // 滑块滚动到最底部
- case SB_LINEUP: scinfo.nPos -= nPosMax / 10; break; // 单击上箭头(10 次结束,可视需求修改)
- case SB_LINEDOWN: scinfo.nPos += nPosMax / 10; break; // 单击下箭头(10 次结束,可视需求修改)
- case SB_PAGEUP: scinfo.nPos -= nPosMax / 2; break; // 单击滑块上方空白区域(2 次结束,可视需求修改)
- case SB_PAGEDOWN: scinfo.nPos += nPosMax / 2; break; // 单击滑块下方空白区域(2 次结束,可视需求修改)
- case SB_THUMBTRACK: scinfo.nPos = nPos; break; // 拖动滑块
- }
- // 移动滚动条
- scinfo.fMask = SIF_POS;
- scinfo.nPos = min(max(scinfo.nPos, 0), nPosMax);
- SetScrollInfo(SB_VERT, &scinfo);
- // 新窗体位置(窗体尺寸及滚动位置根据 SCROLLINFO::nMax、nPage、nPos 反算,省去增加并维护相关成员变量的代码)
- int nWndPosNew = scinfo.nPos * scinfo.nMax / nPosMax;
- // 滚动窗体
- ScrollWindow(0, nWndPosOld - nWndPosNew);
- }
- CDialogEx::OnVScroll(nSBCode, nPos, pScrollBar);
- }
- void CDiosBasicDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- if ((GetStyle() & WS_HSCROLL) == WS_HSCROLL)
- {
- SCROLLINFO scinfo = { 0 };
- scinfo.cbSize = sizeof(SCROLLINFO);
- GetScrollInfo(SB_HORZ, &scinfo, SIF_ALL);
- // 滚动条最大滚动位置
- int nPosMax = scinfo.nMax - (int)scinfo.nPage;
- // 旧窗体位置(窗体尺寸及滚动位置根据 SCROLLINFO::nMax、nPage、nPos 反算,省去增加并维护相关成员变量的代码)
- int nWndPosOld = scinfo.nPos * scinfo.nMax / nPosMax;
- switch (nSBCode)
- {
- case SB_LEFT: scinfo.nPos = 0; break; // 滑块滚动到最左部
- case SB_RIGHT: scinfo.nPos = nPosMax; break; // 滑块滚动到最右部
- case SB_LINELEFT: scinfo.nPos -= nPosMax / 10; break; // 单击左箭头(10 次结束,可视需求修改)
- case SB_LINERIGHT: scinfo.nPos += nPosMax / 10; break; // 单击右箭头(10 次结束,可视需求修改)
- case SB_PAGELEFT: scinfo.nPos -= nPosMax / 2; break; // 单击滑块左空白区域(2 次结束,可视需求修改)
- case SB_PAGERIGHT: scinfo.nPos += nPosMax / 2; break; // 单击滑块右空白区域(2 次结束,可视需求修改)
- case SB_THUMBTRACK: scinfo.nPos = nPos; break; // 拖动滑块
- }
- // 移动滚动条
- scinfo.fMask = SIF_POS;
- scinfo.nPos = min(max(scinfo.nPos, 0), nPosMax);
- SetScrollInfo(SB_HORZ, &scinfo);
- // 新窗体位置(窗体尺寸及滚动位置根据 SCROLLINFO::nMax、nPage、nPos 反算,省去增加并维护相关成员变量的代码)
- int nWndPosNew = scinfo.nPos * scinfo.nMax / nPosMax;
- // 滚动窗体
- ScrollWindow(nWndPosOld - nWndPosNew, 0);
- }
- CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
- }
- void CDiosBasicDlg::UpdateScrollInfo(int nWidthNeed, int nLengthNeed)
- {
- //<summary>更新对话框滚动条(未改变原滚动位置)</summary>
- //<param name="nWidthNeed">需要滚动的区域宽度</param>
- //<param name="nHeightNeed">需要滚动的区域高度</param>
-
- //ASSERT(nWidthNeed > 0);
- //ASSERT(nLengthNeed > 0);
- int nCxVsCroll = ::GetSystemMetrics(SM_CXVSCROLL); // 系统垂直滚动条宽度
- int nCyHsCroll = ::GetSystemMetrics(SM_CYHSCROLL); // 系统水平滚动条高度
- // 可用客户区是去掉滚动条的客户区,计算滚动条参数时需要知道更新前和后滚动条是否存在,以在计算时减去或加上滚动条的尺寸
- // 当前客户区,这是不含滚动条的尺寸
- CRect rcClient;
- GetClientRect(rcClient);
- SCROLLINFO scinfo = { 0 };
- scinfo.cbSize = sizeof(SCROLLINFO);
- scinfo.fMask = SIF_RANGE | SIF_PAGE; // 不改变原滚动位置
- // 如果当前有垂直滚动条,则可用客户区宽度加上垂直滚动条宽度,计算结果是原始可用客户区
- if ((GetStyle() & WS_VSCROLL) == WS_VSCROLL) rcClient.right += nCxVsCroll;
- // 如果当前有水平滚动条,则可用客户区高度加上水平滚动条高度,计算结果是原始可用客户区
- if ((GetStyle() & WS_HSCROLL) == WS_HSCROLL) rcClient.bottom += nCyHsCroll;
- // 1、计算是否需要添加垂直滚动条
- BOOL bVsCrollAdd = nLengthNeed > rcClient.Height();
- // 2、计算是否需要添加水平滚动条
- BOOL bHsCrollAdd = nWidthNeed > rcClient.Width();
- // 3、如果需要水平滚动条并且前面计算不需要垂直滚动条,则由于可用客户区高度已变化,需要重新计算是否需要垂直滚动条
- if (bHsCrollAdd && !bVsCrollAdd)
- {
- bVsCrollAdd = nLengthNeed > rcClient.Height();
- // 如果需要垂直滚动条,客户区宽度减去垂直滚动条宽度
- if (bVsCrollAdd)
- rcClient.right -= nCxVsCroll;
- }
- // 如果需要垂直滚动条,客户区宽度减去垂直滚动条宽度
- if (bVsCrollAdd)
- {
- rcClient.right -= nCxVsCroll;
- // 更新垂直滚动条(不改变原滚动位置)
- scinfo.nMax = max(0, nLengthNeed - rcClient.Height()); // 垂直滚动最大值 = 需求高度 - 客户区高度
- scinfo.nPage = scinfo.nMax * rcClient.Height() / nLengthNeed; // 垂直滚动条内的滚动块高度
- SetScrollInfo(SB_VERT, &scinfo);
- }
- // 如果需要水平滚动条,客户区高度减去水平滚动条宽度
- if (bHsCrollAdd)
- {
- rcClient.bottom -= nCyHsCroll;
- // 更新水平滚动条(不改变原滚动位置)
- scinfo.nMax = max(0, nWidthNeed - rcClient.Width()); // 水平滚动最大值 = 需求宽度 - 客户区宽度
- scinfo.nPage = scinfo.nMax * rcClient.Width() / nWidthNeed; // 水平滚动条内的滚动块长度
- SetScrollInfo(SB_HORZ, &scinfo);
- }
- }
- BOOL CDiosBasicDlg::PreTranslateMessage(MSG* pMsg)
- {
- if (pMsg->message == WM_MOUSEMOVE)
- m_Ttc.RelayEvent(pMsg);
- return CDialog::PreTranslateMessage(pMsg);
- }
- void CDiosBasicDlg::SetToolTipsMsgMap()
- {
- //m_Ttc.AddTool(GetDlgItem(控件ID), _T("提示内容"));//绑定控件ID 显示提示内容
- }
- //********************************模块集合类********************************
- CDeviceSET* CDeviceSET::GetInstance()
- {
- return &glo_CDeviceSET;
- }
- Dev_Type CDeviceSET::CheckDeviceType(string GUID)
- {
- CString log;
- Dev_Type tempType = Dev_TypeNULL;
- if (GUID == UnitTool_GenGUID)//创建发生器
- {
- tempType = Dev_Generator;
- }
- else if (GUID == UnitTool_Mech1GUID ||
- GUID == UnitTool_Mech2GUID ||
- GUID == UnitTool_Mech3GUID)//创建机架
- {
- tempType = Dev_Mechanical;
- }
- else if (GUID == UnitTool_Col1GUID ||
- GUID == UnitTool_Col2GUID)//创建遮光器
- {
- tempType = Dev_Collimator;
- }
- else if (GUID == UnitTool_SynGUID)//创建同步盒
- {
- tempType = Dev_SynBox;
- }
- else if (GUID == UnitTool_FPDGUID)//创建探测器
- {
- tempType = Dev_Detector;
- }
- else
- {
- log.Format("can not deal with GUID[%s]", GUID.c_str());
- mLog::Error(log);
- ToolsDlgLog(log);
- }
- return tempType;
- }
- void CDeviceSET::SetToolDefaultConfig()
- {
- bool needSaveFlag = false;
- ResDataObject tempConfig;
- try
- {
- string strCFGPath = GetProcessDirectory() + UnitTool_configPath;
- if (tempConfig.loadFile(strCFGPath.c_str()))
- {
- for (int i = Dev_TypeNULL + 1; i < Dev_TypeALL; i++)
- {
- string FileName,cfgFile = CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strFileTitle;
- string Lib,cfgLib = CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strLibTitle;
- if (tempConfig[UnitTool_CONFIGURATION].GetKeyCount(cfgFile.c_str()) > 0)
- {
- FileName = (string)tempConfig[UnitTool_CONFIGURATION][cfgFile.c_str()];
- if (FileName != CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strCfgFile)
- {
- tempConfig[UnitTool_CONFIGURATION][cfgFile.c_str()] = CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strCfgFile.c_str();
- needSaveFlag = true;
- }
- }
- else
- {
- tempConfig[UnitTool_CONFIGURATION].add(cfgFile.c_str(), CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strCfgFile.c_str());
- needSaveFlag = true;
- }
- if (tempConfig[UnitTool_CONFIGURATION].GetKeyCount(cfgLib.c_str()) > 0)
- {
- Lib = (string)tempConfig[UnitTool_CONFIGURATION][cfgLib.c_str()];
- if (Lib != CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strCfgLib)
- {
- tempConfig[UnitTool_CONFIGURATION][cfgLib.c_str()] = CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strCfgLib.c_str();
- needSaveFlag = true;
- }
- }
- else
- {
- tempConfig[UnitTool_CONFIGURATION].add(cfgLib.c_str(), CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strCfgLib.c_str());
- needSaveFlag = true;
- }
- }
- if (needSaveFlag)
- {
- tempConfig.SaveFile(strCFGPath.c_str());
- }
- }
- }
- catch (...)
- {
- mLog::Error("open UnitToolconfig.xml catch");
- return;
- }
- }
- void CDeviceSET::GetToolDefaultConfig()
- {
- ResDataObject tempConfig;
- try
- {
- string strCFGPath = GetProcessDirectory() + UnitTool_configPath;
- if (tempConfig.loadFile(strCFGPath.c_str()))
- {
- for (int i = Dev_TypeNULL+1; i < Dev_TypeALL; i++)
- {
- bool isHasCfg{ false };
- string cfgLib = CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strLibTitle;
- string cfgFile = CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strFileTitle;
- if (tempConfig[UnitTool_CONFIGURATION].GetKeyCount(cfgFile.c_str()) > 0)
- {
- CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strCfgFile = (string)tempConfig[UnitTool_CONFIGURATION][cfgFile.c_str()];
- if(!CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strCfgFile.empty())
- isHasCfg = true;
- }
- if (tempConfig[UnitTool_CONFIGURATION].GetKeyCount(cfgLib.c_str()) > 0)
- {
- CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strCfgLib = (string)tempConfig[UnitTool_CONFIGURATION][cfgLib.c_str()];
- if (!CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->m_strCfgLib.empty())
- isHasCfg = true;
- }
- if (isHasCfg)
- {
- CDeviceSET::GetInstance()->m_CDeviceMap[(Dev_Type)i]->ShowPageElements(TRUE, FALSE, TRUE, FALSE);
- }
- }
- }
- }
- catch (...)
- {
- mLog::Error("open UnitToolconfig.xml catch");
- return;
- }
- }
|