#include "stdafx.h" #include #include "Helper.JSON.hpp" #include "DIOS.Dev.DME.Mould.hpp" #include "EasyJSONEncoder.hpp" #include "DIOSDICOMInfo.h" using namespace DIOS::Dev::Detail::DME; namespace nsDME = DIOS::Dev::Detail::DME; //template //inline _Container& operator << (_Container& ar, const T& t) //{ // ar.push_back(t); // return ar; //} string nsDME::GetProcessDirectory() { string ret = ""; char szFilename[MAX_PATH] = { 0 }; DWORD res = GetModuleFileNameA(0, szFilename, MAX_PATH); if (res == 0) { return ret; } string fullpath = szFilename; string::size_type firstHit = fullpath.find_last_of('\\'); if (firstHit == string::npos || firstHit == 0) { return ret; } ret = fullpath.substr(0, firstHit);//kick last \ return ret; } //获取配置文件中指定模块的版本号 bool nsDME::GetVersion(string& version, HMODULE hMyModule) { try { char filename[MAX_PATH + 1] = { 0 }; if (GetModuleFileName(hMyModule, filename, MAX_PATH) == 0) { printf("dll HMODULEis null\n"); //mLog::Error("dll HMODULEis null\n"); return false; } printf("get Generator path:{$}\n", filename); //mLog::Info("get Generator path:{$}\n", filename); DWORD dummy; DWORD size = GetFileVersionInfoSize(filename, &dummy); if (size == 0) { return false; } auto data = make_unique(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(&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 Generator Mould version failed\n"); } return false; } bool nsDME::GetVersion(string& version, ResDataObject& config) { try { string procdir = ""; char filename[MAX_PATH + 1] = { 0 }; procdir = nsDME::GetProcessDirectory(); if (procdir.empty()) { if (GetModuleFileName(nullptr, filename, MAX_PATH) == 0) { return false; } } else { string oemdrvpath = (const char*)config["oemdriver"]; { 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; } } strncpy_s(filename, oemdrvpath.c_str(), MAX_PATH); } printf("get Generator path:{$}\n", filename); //mLog::Info("get Generator path:{$}\n", filename); DWORD dummy; DWORD size = GetFileVersionInfoSize(filename, &dummy); if (size == 0) { return false; } auto data = make_unique(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(&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 Generator Mould version failed\n"); } return false; } bool nsDME::GetVersion(string& version) { try { char filename[MAX_PATH + 1] = { 0 }; if (GetModuleFileName(nullptr, filename, MAX_PATH) == 0) { return false; } printf("get Generator path:{$}\n", filename); //mLog::Info("get Generator path:{$}\n", filename); DWORD dummy; DWORD size = GetFileVersionInfoSize(filename, &dummy); if (size == 0) { return false; } auto data = make_unique(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(&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 Generator Mould version failed\n"); } return false; } void nsDME::TransJsonText(ResDataObject& config) { for (int x = 0; x < config.size(); x++) { //如果有Value if (config[x].GetKeyCount("Value") > 0) { //mLog::FINFO("TRY COVERT [{$}] VALUE {$}", config.GetKey(x), config[x]["Value"].size() > 0 ? config[x]["Value"].encode() : (const char*)config[x]["Value"]); if (config[x]["Value"].size() <= 0) { string va = (const char*)config[x]["Value"]; config[x] = va.c_str(); } else { ResDataObject rest = config[x]["Value"]; config[x] = rest; //mLog::FINFO("convert object [{$}], object {$}", config.GetKey(x), rest.encode()); } } //mLog::FINFO("After Convert {$}", config.encode()); } } //----------------------------------------------------------------------------- // nsGEN::DMEMould //----------------------------------------------------------------------------- nsDME::DMEMould::DMEMould() { } nsDME::DMEMould::~DMEMould() { } void nsDME::DMEMould::Register (Dispatch* Dispatch) { Dispatch->Get.Push(AttrKey::Distance, [this](std::string& out) { auto value = GetDistance(); out = ToJSON(value); return RET_STATUS::RET_SUCCEED; }); Dispatch->Get.Push(AttrKey::LaserState, [this](std::string& out) { auto value = GetLaserState(); out = ToJSON(value); return RET_STATUS::RET_SUCCEED; }); Dispatch->Get.Push(AttrKey::ConnectionStatus, [this](std::string& out) { auto value = GetConnectionStatus(); out = ToJSON(value); return RET_STATUS::RET_SUCCEED; }); Dispatch->Action.Push(ActionKey::SetLaserState, [this](std::string in, std::string& out) { auto value = JSONTo (in); return SetLaserState(value); }); Dispatch->Action.Push(ActionKey::ShutDown, [this](std::string in, std::string& out) { return ShutDown(); }); Dispatch->Action.Push(ActionKey::FetchDistance, [this](std::string in, std::string& out) { return FetchDistance(); }); Dispatch->Update.Push(AttrKey::CalibrateValue, [this](std::string in, std::string& out) { SetCalibrateValue(atoi(in.c_str())); return RET_STATUS::RET_SUCCEED; }); Dispatch->Update.Push(AttrKey::MeasurementInterval, [this](std::string in, std::string& out) { SetMeasurementInterval(atoi(in.c_str())); return RET_STATUS::RET_SUCCEED; }); Dispatch->Update.Push(AttrKey::Position, [this](std::string in, std::string& out) { SetStartEndPoints(atoi(in.c_str())); return RET_STATUS::RET_SUCCEED; }); Dispatch->Update.Push(AttrKey::Range, [this](std::string in, std::string& out) { SetRange(atoi(in.c_str())); return RET_STATUS::RET_SUCCEED; }); Dispatch->Update.Push(AttrKey::Frequency, [this](std::string in, std::string& out) { SetFrequency(atoi(in.c_str())); return RET_STATUS::RET_SUCCEED; }); Dispatch->Update.Push(AttrKey::Resolution, [this](std::string in, std::string& out) { SetResolution(atoi(in.c_str())); return RET_STATUS::RET_SUCCEED; }); Dispatch->Update.Push(AttrKey::MeasureSID, [this](std::string in, std::string& out) { SetMeasureSID(atoi(in.c_str())); return RET_STATUS::RET_SUCCEED; }); Dispatch->Update.Push(AttrKey::MeasureAngle, [this](std::string in, std::string& out) { SetMeasureAngle(atoi(in.c_str())); return RET_STATUS::RET_SUCCEED; }); } //----------------------------------------------------------------------------- // 所有的 SetXX 函数 //----------------------------------------------------------------------------- RET_STATUS nsDME::DMEMould::SetCalibrateValue(int pParams) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsDME::DMEMould::SetMeasurementInterval(int pParams) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsDME::DMEMould::SetStartEndPoints(int pParams) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsDME::DMEMould::SetRange(int pParams) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsDME::DMEMould::SetFrequency(int pParams) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsDME::DMEMould::SetResolution(int pParams) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsDME::DMEMould::SetLaserState(int pParams) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsDME::DMEMould::SetMeasureSID(int pParams) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsDME::DMEMould::SetMeasureAngle(int pParams) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsDME::DMEMould::ShutDown() { return RET_STATUS::RET_SUCCEED; } //----------------------------------------------------------------------------- // 所有的 GetXX 函数 //----------------------------------------------------------------------------- int nsDME::DMEMould::GetDistance() { return m_DMEUnit.m_Distance->Get(); } int nsDME::DMEMould::GetLaserState(){ return m_DMEUnit.m_LaserState->Get();} int nsDME::DMEMould::GetConnectionStatus() { return m_DMEUnit.m_ConnectionStatus->Get(); } //----------------------------------------------------------------------------- // nsGEN::DriverMould //----------------------------------------------------------------------------- nsDME::DriverMould::DriverMould () { ConfigInfo Vender (ConfKey::DiosType, "string", "R", "TRUE", ""); m_ConfigInfo.push_back(Vender); ConfigInfo Model (ConfKey::DiosModel, "string", "R", "TRUE", ""); m_ConfigInfo.push_back(Model); EasyJSONEncoder synList; synList.Set ("0", "1"); synList.Set ("1", "2"); ConfigInfo GenIsConnect (ConfKey::DiosIsConnect, "int", "RW", "FALSE", ""); GenIsConnect.SetList (synList.ToString ().c_str ()); m_ConfigInfo.push_back(GenIsConnect); ConfigInfo SCFType (ConfKey::DiosSCFType, "string", "R", "TRUE", ""); m_ConfigInfo.push_back(SCFType); EasyJSONEncoder COMList; COMList.Set ("0", "COM1"); COMList.Set ("1", "COM2"); COMList.Set ("2", "COM3"); COMList.Set ("3", "COM4"); COMList.Set ("4", "COM5"); COMList.Set ("5", "COM6"); COMList.Set ("6", "COM7"); COMList.Set ("7", "COM8"); COMList.Set ("8", "COM9"); COMList.Set ("9", "COM10"); COMList.Set ("10", "COM11"); COMList.Set ("11", "COM12"); ConfigInfo SCFPort (ConfKey::DiosSCFPort, "string", "RW", "TRUE", ""); SCFPort.SetList (COMList.ToString ().c_str ()); m_ConfigInfo.push_back(SCFPort); ConfigInfo SCFBaudrate (ConfKey::DiosSCFBaudrate, "int", "R", "FALSE", ""); m_ConfigInfo.push_back(SCFBaudrate); ConfigInfo SCFBytesize (ConfKey::DiosSCFBytesize, "int", "R", "FALSE", ""); m_ConfigInfo.push_back(SCFBytesize); ConfigInfo SCFParity (ConfKey::DiosSCFParity, "int", "R", "FALSE", ""); m_ConfigInfo.push_back(SCFParity); ConfigInfo SCFStopbits (ConfKey::DiosSCFStopbits, "int", "R", "FALSE", ""); m_ConfigInfo.push_back(SCFStopbits); } nsDME::DriverMould::~DriverMould () { } #ifdef _WIN64 #ifdef _DEBUG static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64D.dll"; static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64D.dll"; #else static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64.dll"; static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64.dll"; #endif #endif std::string nsDME::DriverMould::GetGUID () const { return DeviceDriverType; } string nsDME::DriverMould::GetConnectDLL(string& ConfigFileName) { string ConnectDLL = COM_SCFDllName; ResDataObject r_config, Connection; if (!r_config.loadFile(ConfigFileName.c_str())) return ConnectDLL; if (r_config["CONFIGURATION"]["connections"].GetKeyCount("Value") > 0) { Connection = r_config["CONFIGURATION"]["connections"]["Value"]; } else { Connection = r_config["CONFIGURATION"]["connections"][0]; } if ((string)Connection["type"] == "COM") ConnectDLL = COM_SCFDllName; else ConnectDLL = TCP_SCFDllName; return ConnectDLL; } ResDataObject nsDME::DriverMould::GetConnectParam(string& ConfigFileName) { ResDataObject r_config, Connection; if (!r_config.loadFile(ConfigFileName.c_str())) return Connection; if (r_config["CONFIGURATION"]["connections"].GetKeyCount("Value") > 0) { Connection = r_config["CONFIGURATION"]["connections"]["Value"]; } else { Connection = r_config["CONFIGURATION"]["connections"][0]; } return Connection; }