123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- #include "stdafx.h"
- #include <assert.h>
- #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 <typename _Container, typename T>
- //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<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 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<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 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<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 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 <int>(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;
- }
|