DIOS.Dev.DME.Mould.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. #include "stdafx.h"
  2. #include <assert.h>
  3. #include "Helper.JSON.hpp"
  4. #include "DIOS.Dev.DME.Mould.hpp"
  5. #include "EasyJSONEncoder.hpp"
  6. #include "DIOSDICOMInfo.h"
  7. using namespace DIOS::Dev::Detail::DME;
  8. namespace nsDME = DIOS::Dev::Detail::DME;
  9. //template <typename _Container, typename T>
  10. //inline _Container& operator << (_Container& ar, const T& t)
  11. //{
  12. // ar.push_back(t);
  13. // return ar;
  14. //}
  15. string nsDME::GetProcessDirectory()
  16. {
  17. string ret = "";
  18. char szFilename[MAX_PATH] = { 0 };
  19. DWORD res = GetModuleFileNameA(0, szFilename, MAX_PATH);
  20. if (res == 0)
  21. {
  22. return ret;
  23. }
  24. string fullpath = szFilename;
  25. string::size_type firstHit = fullpath.find_last_of('\\');
  26. if (firstHit == string::npos || firstHit == 0)
  27. {
  28. return ret;
  29. }
  30. ret = fullpath.substr(0, firstHit);//kick last \
  31. return ret;
  32. }
  33. //获取配置文件中指定模块的版本号
  34. bool nsDME::GetVersion(string& version, HMODULE hMyModule)
  35. {
  36. try {
  37. char filename[MAX_PATH + 1] = { 0 };
  38. if (GetModuleFileName(hMyModule, filename, MAX_PATH) == 0)
  39. {
  40. printf("dll HMODULEis null\n");
  41. //mLog::Error("dll HMODULEis null\n");
  42. return false;
  43. }
  44. printf("get Generator path:{$}\n", filename);
  45. //mLog::Info("get Generator path:{$}\n", filename);
  46. DWORD dummy;
  47. DWORD size = GetFileVersionInfoSize(filename, &dummy);
  48. if (size == 0)
  49. {
  50. return false;
  51. }
  52. auto data = make_unique<BYTE[]>(size);
  53. if (!GetFileVersionInfo(filename, 0, size, &data[0]))
  54. {
  55. return false;
  56. }
  57. UINT32 len = 0;
  58. VS_FIXEDFILEINFO* fixed_file_info = 0;
  59. if (!VerQueryValue(&data[0], TEXT("\\"), reinterpret_cast<void**>(&fixed_file_info), &len))
  60. {
  61. return false;
  62. }
  63. // version 为版本号
  64. UINT32 vBitNumber = 0;
  65. vBitNumber = HIWORD(fixed_file_info->dwProductVersionMS);
  66. version += to_string(vBitNumber);
  67. version += ".";
  68. vBitNumber = LOWORD(fixed_file_info->dwProductVersionMS);
  69. version += to_string(vBitNumber);
  70. version += ".";
  71. vBitNumber = HIWORD(fixed_file_info->dwProductVersionLS);
  72. version += to_string(vBitNumber);
  73. version += ".";
  74. vBitNumber = LOWORD(fixed_file_info->dwProductVersionLS);
  75. version += to_string(vBitNumber);
  76. return true;
  77. }
  78. catch (...)
  79. {
  80. printf("get Generator Mould version failed");
  81. //mLog::Error("get Generator Mould version failed\n");
  82. }
  83. return false;
  84. }
  85. bool nsDME::GetVersion(string& version, ResDataObject& config)
  86. {
  87. try {
  88. string procdir = "";
  89. char filename[MAX_PATH + 1] = { 0 };
  90. procdir = nsDME::GetProcessDirectory();
  91. if (procdir.empty())
  92. {
  93. if (GetModuleFileName(nullptr, filename, MAX_PATH) == 0)
  94. {
  95. return false;
  96. }
  97. }
  98. else
  99. {
  100. string oemdrvpath = (const char*)config["oemdriver"];
  101. {
  102. string keystr = "%ROOT%";
  103. string::size_type pos = 0;
  104. string::size_type keylen = keystr.size();
  105. string::size_type replen = procdir.size();
  106. while ((pos = oemdrvpath.find(keystr, pos)) != string::npos)
  107. {
  108. oemdrvpath.replace(pos, keylen, procdir);
  109. pos += replen;
  110. }
  111. }
  112. strncpy_s(filename, oemdrvpath.c_str(), MAX_PATH);
  113. }
  114. printf("get Generator path:{$}\n", filename);
  115. //mLog::Info("get Generator path:{$}\n", filename);
  116. DWORD dummy;
  117. DWORD size = GetFileVersionInfoSize(filename, &dummy);
  118. if (size == 0)
  119. {
  120. return false;
  121. }
  122. auto data = make_unique<BYTE[]>(size);
  123. if (!GetFileVersionInfo(filename, 0, size, &data[0]))
  124. {
  125. return false;
  126. }
  127. UINT32 len = 0;
  128. VS_FIXEDFILEINFO* fixed_file_info = 0;
  129. if (!VerQueryValue(&data[0], TEXT("\\"), reinterpret_cast<void**>(&fixed_file_info), &len))
  130. {
  131. return false;
  132. }
  133. // version 为版本号
  134. UINT32 vBitNumber = 0;
  135. vBitNumber = HIWORD(fixed_file_info->dwProductVersionMS);
  136. version += to_string(vBitNumber);
  137. version += ".";
  138. vBitNumber = LOWORD(fixed_file_info->dwProductVersionMS);
  139. version += to_string(vBitNumber);
  140. version += ".";
  141. vBitNumber = HIWORD(fixed_file_info->dwProductVersionLS);
  142. version += to_string(vBitNumber);
  143. version += ".";
  144. vBitNumber = LOWORD(fixed_file_info->dwProductVersionLS);
  145. version += to_string(vBitNumber);
  146. return true;
  147. }
  148. catch (...)
  149. {
  150. printf("get Generator Mould version failed");
  151. //mLog::Error("get Generator Mould version failed\n");
  152. }
  153. return false;
  154. }
  155. bool nsDME::GetVersion(string& version)
  156. {
  157. try {
  158. char filename[MAX_PATH + 1] = { 0 };
  159. if (GetModuleFileName(nullptr, filename, MAX_PATH) == 0)
  160. {
  161. return false;
  162. }
  163. printf("get Generator path:{$}\n", filename);
  164. //mLog::Info("get Generator path:{$}\n", filename);
  165. DWORD dummy;
  166. DWORD size = GetFileVersionInfoSize(filename, &dummy);
  167. if (size == 0)
  168. {
  169. return false;
  170. }
  171. auto data = make_unique<BYTE[]>(size);
  172. if (!GetFileVersionInfo(filename, 0, size, &data[0]))
  173. {
  174. return false;
  175. }
  176. UINT32 len = 0;
  177. VS_FIXEDFILEINFO* fixed_file_info = 0;
  178. if (!VerQueryValue(&data[0], TEXT("\\"), reinterpret_cast<void**>(&fixed_file_info), &len))
  179. {
  180. return false;
  181. }
  182. // version 为版本号
  183. UINT32 vBitNumber = 0;
  184. vBitNumber = HIWORD(fixed_file_info->dwProductVersionMS);
  185. version += to_string(vBitNumber);
  186. version += ".";
  187. vBitNumber = LOWORD(fixed_file_info->dwProductVersionMS);
  188. version += to_string(vBitNumber);
  189. version += ".";
  190. vBitNumber = HIWORD(fixed_file_info->dwProductVersionLS);
  191. version += to_string(vBitNumber);
  192. version += ".";
  193. vBitNumber = LOWORD(fixed_file_info->dwProductVersionLS);
  194. version += to_string(vBitNumber);
  195. return true;
  196. }
  197. catch (...)
  198. {
  199. printf("get Generator Mould version failed");
  200. //mLog::Error("get Generator Mould version failed\n");
  201. }
  202. return false;
  203. }
  204. void nsDME::TransJsonText(ResDataObject& config)
  205. {
  206. for (int x = 0; x < config.size(); x++)
  207. {
  208. //如果有Value
  209. if (config[x].GetKeyCount("Value") > 0)
  210. {
  211. //mLog::FINFO("TRY COVERT [{$}] VALUE {$}", config.GetKey(x), config[x]["Value"].size() > 0 ? config[x]["Value"].encode() : (const char*)config[x]["Value"]);
  212. if (config[x]["Value"].size() <= 0)
  213. {
  214. string va = (const char*)config[x]["Value"];
  215. config[x] = va.c_str();
  216. }
  217. else
  218. {
  219. ResDataObject rest = config[x]["Value"];
  220. config[x] = rest;
  221. //mLog::FINFO("convert object [{$}], object {$}", config.GetKey(x), rest.encode());
  222. }
  223. }
  224. //mLog::FINFO("After Convert {$}", config.encode());
  225. }
  226. }
  227. //-----------------------------------------------------------------------------
  228. // nsGEN::DMEMould
  229. //-----------------------------------------------------------------------------
  230. nsDME::DMEMould::DMEMould()
  231. {
  232. }
  233. nsDME::DMEMould::~DMEMould()
  234. {
  235. }
  236. void nsDME::DMEMould::Register (Dispatch* Dispatch)
  237. {
  238. Dispatch->Get.Push(AttrKey::Distance, [this](std::string& out) {
  239. auto value = GetDistance();
  240. out = ToJSON(value);
  241. return RET_STATUS::RET_SUCCEED; });
  242. Dispatch->Get.Push(AttrKey::LaserState, [this](std::string& out) {
  243. auto value = GetLaserState();
  244. out = ToJSON(value);
  245. return RET_STATUS::RET_SUCCEED; });
  246. Dispatch->Get.Push(AttrKey::ConnectionStatus, [this](std::string& out) {
  247. auto value = GetConnectionStatus();
  248. out = ToJSON(value);
  249. return RET_STATUS::RET_SUCCEED; });
  250. Dispatch->Action.Push(ActionKey::SetLaserState, [this](std::string in, std::string& out) {
  251. auto value = JSONTo <int>(in);
  252. return SetLaserState(value); });
  253. Dispatch->Action.Push(ActionKey::ShutDown, [this](std::string in, std::string& out) {
  254. return ShutDown(); });
  255. Dispatch->Action.Push(ActionKey::FetchDistance, [this](std::string in, std::string& out) {
  256. return FetchDistance(); });
  257. Dispatch->Update.Push(AttrKey::CalibrateValue, [this](std::string in, std::string& out) {
  258. SetCalibrateValue(atoi(in.c_str()));
  259. return RET_STATUS::RET_SUCCEED; });
  260. Dispatch->Update.Push(AttrKey::MeasurementInterval, [this](std::string in, std::string& out) {
  261. SetMeasurementInterval(atoi(in.c_str()));
  262. return RET_STATUS::RET_SUCCEED; });
  263. Dispatch->Update.Push(AttrKey::Position, [this](std::string in, std::string& out) {
  264. SetStartEndPoints(atoi(in.c_str()));
  265. return RET_STATUS::RET_SUCCEED; });
  266. Dispatch->Update.Push(AttrKey::Range, [this](std::string in, std::string& out) {
  267. SetRange(atoi(in.c_str()));
  268. return RET_STATUS::RET_SUCCEED; });
  269. Dispatch->Update.Push(AttrKey::Frequency, [this](std::string in, std::string& out) {
  270. SetFrequency(atoi(in.c_str()));
  271. return RET_STATUS::RET_SUCCEED; });
  272. Dispatch->Update.Push(AttrKey::Resolution, [this](std::string in, std::string& out) {
  273. SetResolution(atoi(in.c_str()));
  274. return RET_STATUS::RET_SUCCEED; });
  275. Dispatch->Update.Push(AttrKey::MeasureSID, [this](std::string in, std::string& out) {
  276. SetMeasureSID(atoi(in.c_str()));
  277. return RET_STATUS::RET_SUCCEED; });
  278. Dispatch->Update.Push(AttrKey::MeasureAngle, [this](std::string in, std::string& out) {
  279. SetMeasureAngle(atoi(in.c_str()));
  280. return RET_STATUS::RET_SUCCEED; });
  281. }
  282. //-----------------------------------------------------------------------------
  283. // 所有的 SetXX 函数
  284. //-----------------------------------------------------------------------------
  285. RET_STATUS nsDME::DMEMould::SetCalibrateValue(int pParams)
  286. {
  287. return RET_STATUS::RET_SUCCEED;
  288. }
  289. RET_STATUS nsDME::DMEMould::SetMeasurementInterval(int pParams)
  290. {
  291. return RET_STATUS::RET_SUCCEED;
  292. }
  293. RET_STATUS nsDME::DMEMould::SetStartEndPoints(int pParams)
  294. {
  295. return RET_STATUS::RET_SUCCEED;
  296. }
  297. RET_STATUS nsDME::DMEMould::SetRange(int pParams)
  298. {
  299. return RET_STATUS::RET_SUCCEED;
  300. }
  301. RET_STATUS nsDME::DMEMould::SetFrequency(int pParams)
  302. {
  303. return RET_STATUS::RET_SUCCEED;
  304. }
  305. RET_STATUS nsDME::DMEMould::SetResolution(int pParams)
  306. {
  307. return RET_STATUS::RET_SUCCEED;
  308. }
  309. RET_STATUS nsDME::DMEMould::SetLaserState(int pParams)
  310. {
  311. return RET_STATUS::RET_SUCCEED;
  312. }
  313. RET_STATUS nsDME::DMEMould::SetMeasureSID(int pParams)
  314. {
  315. return RET_STATUS::RET_SUCCEED;
  316. }
  317. RET_STATUS nsDME::DMEMould::SetMeasureAngle(int pParams)
  318. {
  319. return RET_STATUS::RET_SUCCEED;
  320. }
  321. RET_STATUS nsDME::DMEMould::ShutDown()
  322. {
  323. return RET_STATUS::RET_SUCCEED;
  324. }
  325. //-----------------------------------------------------------------------------
  326. // 所有的 GetXX 函数
  327. //-----------------------------------------------------------------------------
  328. int nsDME::DMEMould::GetDistance() { return m_DMEUnit.m_Distance->Get(); }
  329. int nsDME::DMEMould::GetLaserState(){ return m_DMEUnit.m_LaserState->Get();}
  330. int nsDME::DMEMould::GetConnectionStatus() { return m_DMEUnit.m_ConnectionStatus->Get(); }
  331. //-----------------------------------------------------------------------------
  332. // nsGEN::DriverMould
  333. //-----------------------------------------------------------------------------
  334. nsDME::DriverMould::DriverMould ()
  335. {
  336. ConfigInfo Vender (ConfKey::DiosType, "string", "R", "TRUE", "");
  337. m_ConfigInfo.push_back(Vender);
  338. ConfigInfo Model (ConfKey::DiosModel, "string", "R", "TRUE", "");
  339. m_ConfigInfo.push_back(Model);
  340. EasyJSONEncoder synList;
  341. synList.Set ("0", "1");
  342. synList.Set ("1", "2");
  343. ConfigInfo GenIsConnect (ConfKey::DiosIsConnect, "int", "RW", "FALSE", "");
  344. GenIsConnect.SetList (synList.ToString ().c_str ());
  345. m_ConfigInfo.push_back(GenIsConnect);
  346. ConfigInfo SCFType (ConfKey::DiosSCFType, "string", "R", "TRUE", "");
  347. m_ConfigInfo.push_back(SCFType);
  348. EasyJSONEncoder COMList;
  349. COMList.Set ("0", "COM1");
  350. COMList.Set ("1", "COM2");
  351. COMList.Set ("2", "COM3");
  352. COMList.Set ("3", "COM4");
  353. COMList.Set ("4", "COM5");
  354. COMList.Set ("5", "COM6");
  355. COMList.Set ("6", "COM7");
  356. COMList.Set ("7", "COM8");
  357. COMList.Set ("8", "COM9");
  358. COMList.Set ("9", "COM10");
  359. COMList.Set ("10", "COM11");
  360. COMList.Set ("11", "COM12");
  361. ConfigInfo SCFPort (ConfKey::DiosSCFPort, "string", "RW", "TRUE", "");
  362. SCFPort.SetList (COMList.ToString ().c_str ());
  363. m_ConfigInfo.push_back(SCFPort);
  364. ConfigInfo SCFBaudrate (ConfKey::DiosSCFBaudrate, "int", "R", "FALSE", "");
  365. m_ConfigInfo.push_back(SCFBaudrate);
  366. ConfigInfo SCFBytesize (ConfKey::DiosSCFBytesize, "int", "R", "FALSE", "");
  367. m_ConfigInfo.push_back(SCFBytesize);
  368. ConfigInfo SCFParity (ConfKey::DiosSCFParity, "int", "R", "FALSE", "");
  369. m_ConfigInfo.push_back(SCFParity);
  370. ConfigInfo SCFStopbits (ConfKey::DiosSCFStopbits, "int", "R", "FALSE", "");
  371. m_ConfigInfo.push_back(SCFStopbits);
  372. }
  373. nsDME::DriverMould::~DriverMould ()
  374. {
  375. }
  376. #ifdef _WIN64
  377. #ifdef _DEBUG
  378. static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64D.dll";
  379. static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64D.dll";
  380. #else
  381. static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64.dll";
  382. static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64.dll";
  383. #endif
  384. #endif
  385. std::string nsDME::DriverMould::GetGUID () const
  386. {
  387. return DeviceDriverType;
  388. }
  389. string nsDME::DriverMould::GetConnectDLL(string& ConfigFileName)
  390. {
  391. string ConnectDLL = COM_SCFDllName;
  392. ResDataObject r_config, Connection;
  393. if (!r_config.loadFile(ConfigFileName.c_str()))
  394. return ConnectDLL;
  395. if (r_config["CONFIGURATION"]["connections"].GetKeyCount("Value") > 0)
  396. {
  397. Connection = r_config["CONFIGURATION"]["connections"]["Value"];
  398. }
  399. else
  400. {
  401. Connection = r_config["CONFIGURATION"]["connections"][0];
  402. }
  403. if ((string)Connection["type"] == "COM")
  404. ConnectDLL = COM_SCFDllName;
  405. else
  406. ConnectDLL = TCP_SCFDllName;
  407. return ConnectDLL;
  408. }
  409. ResDataObject nsDME::DriverMould::GetConnectParam(string& ConfigFileName)
  410. {
  411. ResDataObject r_config, Connection;
  412. if (!r_config.loadFile(ConfigFileName.c_str()))
  413. return Connection;
  414. if (r_config["CONFIGURATION"]["connections"].GetKeyCount("Value") > 0)
  415. {
  416. Connection = r_config["CONFIGURATION"]["connections"]["Value"];
  417. }
  418. else
  419. {
  420. Connection = r_config["CONFIGURATION"]["connections"][0];
  421. }
  422. return Connection;
  423. }