DIOS.Dev.SYNBOX.Mould.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. //
  2. #include "stdafx.h"
  3. #include <assert.h>
  4. #include "ResDataObject.h"
  5. #include "Helper.JSON.hpp"
  6. #include "DIOS.Dev.SYNBOX.Mould.hpp"
  7. #include "EasyJSONEncoder.hpp"
  8. using namespace DIOS::Dev::Detail::SYNBOX;
  9. namespace nsSYN = DIOS::Dev::Detail::SYNBOX;
  10. //template <typename _Container, typename T>
  11. //inline _Container& operator << (_Container& ar, const T& t)
  12. //{
  13. // ar.push_back(t);
  14. // return ar;
  15. //}
  16. string nsSYN::GetProcessDirectory()
  17. {
  18. string ret = "";
  19. char szFilename[MAX_PATH] = { 0 };
  20. DWORD res = GetModuleFileNameA(0, szFilename, MAX_PATH);
  21. if (res == 0)
  22. {
  23. return ret;
  24. }
  25. string fullpath = szFilename;
  26. string::size_type firstHit = fullpath.find_last_of('\\');
  27. if (firstHit == string::npos || firstHit == 0)
  28. {
  29. return ret;
  30. }
  31. ret = fullpath.substr(0, firstHit);//kick last \
  32. return ret;
  33. }
  34. //获取配置文件中指定模块的版本号
  35. bool nsSYN::GetVersion(string& version, HMODULE hMyModule)
  36. {
  37. try {
  38. char filename[MAX_PATH + 1] = { 0 };
  39. if (GetModuleFileName(hMyModule, filename, MAX_PATH) == 0)
  40. {
  41. printf("dll HMODULEis null\n");
  42. //mLog::Error("dll HMODULEis null\n");
  43. return false;
  44. }
  45. printf("get SYNBox path:{$}\n", filename);
  46. //mLog::Info("get SYNBox path:{$}\n", filename);
  47. DWORD dummy;
  48. DWORD size = GetFileVersionInfoSize(filename, &dummy);
  49. if (size == 0)
  50. {
  51. return false;
  52. }
  53. auto data = make_unique<BYTE[]>(size);
  54. if (!GetFileVersionInfo(filename, 0, size, &data[0]))
  55. {
  56. return false;
  57. }
  58. UINT32 len = 0;
  59. VS_FIXEDFILEINFO* fixed_file_info = 0;
  60. if (!VerQueryValue(&data[0], TEXT("\\"), reinterpret_cast<void**>(&fixed_file_info), &len))
  61. {
  62. return false;
  63. }
  64. // version 为版本号
  65. UINT32 vBitNumber = 0;
  66. vBitNumber = HIWORD(fixed_file_info->dwProductVersionMS);
  67. version += to_string(vBitNumber);
  68. version += ".";
  69. vBitNumber = LOWORD(fixed_file_info->dwProductVersionMS);
  70. version += to_string(vBitNumber);
  71. version += ".";
  72. vBitNumber = HIWORD(fixed_file_info->dwProductVersionLS);
  73. version += to_string(vBitNumber);
  74. version += ".";
  75. vBitNumber = LOWORD(fixed_file_info->dwProductVersionLS);
  76. version += to_string(vBitNumber);
  77. return true;
  78. }
  79. catch (...)
  80. {
  81. printf("get SYNBox Mould version failed");
  82. //mLog::Error("get SYNBox Mould version failed\n");
  83. }
  84. return false;
  85. }
  86. bool nsSYN::GetVersion(string& version, ResDataObject& config)
  87. {
  88. try {
  89. string procdir = "";
  90. char filename[MAX_PATH + 1] = { 0 };
  91. procdir = nsSYN::GetProcessDirectory();
  92. if (procdir.empty())
  93. {
  94. if (GetModuleFileName(nullptr, filename, MAX_PATH) == 0)
  95. {
  96. return false;
  97. }
  98. }
  99. else
  100. {
  101. string oemdrvpath = (const char*)config["oemdriver"];
  102. {
  103. string keystr = "%ROOT%";
  104. string::size_type pos = 0;
  105. string::size_type keylen = keystr.size();
  106. string::size_type replen = procdir.size();
  107. while ((pos = oemdrvpath.find(keystr, pos)) != string::npos)
  108. {
  109. oemdrvpath.replace(pos, keylen, procdir);
  110. pos += replen;
  111. }
  112. }
  113. strncpy_s(filename, oemdrvpath.c_str(), MAX_PATH);
  114. }
  115. printf("get SYNBox path:{$}\n", filename);
  116. //mLog::Info("get SYNBox path:{$}\n", filename);
  117. DWORD dummy;
  118. DWORD size = GetFileVersionInfoSize(filename, &dummy);
  119. if (size == 0)
  120. {
  121. return false;
  122. }
  123. auto data = make_unique<BYTE[]>(size);
  124. if (!GetFileVersionInfo(filename, 0, size, &data[0]))
  125. {
  126. return false;
  127. }
  128. UINT32 len = 0;
  129. VS_FIXEDFILEINFO* fixed_file_info = 0;
  130. if (!VerQueryValue(&data[0], TEXT("\\"), reinterpret_cast<void**>(&fixed_file_info), &len))
  131. {
  132. return false;
  133. }
  134. // version 为版本号
  135. UINT32 vBitNumber = 0;
  136. vBitNumber = HIWORD(fixed_file_info->dwProductVersionMS);
  137. version += to_string(vBitNumber);
  138. version += ".";
  139. vBitNumber = LOWORD(fixed_file_info->dwProductVersionMS);
  140. version += to_string(vBitNumber);
  141. version += ".";
  142. vBitNumber = HIWORD(fixed_file_info->dwProductVersionLS);
  143. version += to_string(vBitNumber);
  144. version += ".";
  145. vBitNumber = LOWORD(fixed_file_info->dwProductVersionLS);
  146. version += to_string(vBitNumber);
  147. return true;
  148. }
  149. catch (...)
  150. {
  151. printf("get SYNBox Mould version failed");
  152. //mLog::Error("get SYNBox Mould version failed\n");
  153. }
  154. return false;
  155. }
  156. bool nsSYN::GetVersion(string& version)
  157. {
  158. try {
  159. char filename[MAX_PATH + 1] = { 0 };
  160. if (GetModuleFileName(nullptr, filename, MAX_PATH) == 0)
  161. {
  162. return false;
  163. }
  164. printf("get SYNBox path:{$}\n", filename);
  165. //mLog::Info("get SYNBox path:{$}\n", filename);
  166. DWORD dummy;
  167. DWORD size = GetFileVersionInfoSize(filename, &dummy);
  168. if (size == 0)
  169. {
  170. return false;
  171. }
  172. auto data = make_unique<BYTE[]>(size);
  173. if (!GetFileVersionInfo(filename, 0, size, &data[0]))
  174. {
  175. return false;
  176. }
  177. UINT32 len = 0;
  178. VS_FIXEDFILEINFO* fixed_file_info = 0;
  179. if (!VerQueryValue(&data[0], TEXT("\\"), reinterpret_cast<void**>(&fixed_file_info), &len))
  180. {
  181. return false;
  182. }
  183. // version 为版本号
  184. UINT32 vBitNumber = 0;
  185. vBitNumber = HIWORD(fixed_file_info->dwProductVersionMS);
  186. version += to_string(vBitNumber);
  187. version += ".";
  188. vBitNumber = LOWORD(fixed_file_info->dwProductVersionMS);
  189. version += to_string(vBitNumber);
  190. version += ".";
  191. vBitNumber = HIWORD(fixed_file_info->dwProductVersionLS);
  192. version += to_string(vBitNumber);
  193. version += ".";
  194. vBitNumber = LOWORD(fixed_file_info->dwProductVersionLS);
  195. version += to_string(vBitNumber);
  196. return true;
  197. }
  198. catch (...)
  199. {
  200. printf("get SYNBox Mould version failed");
  201. //mLog::Error("get SYNBox Mould version failed\n");
  202. }
  203. return false;
  204. }
  205. void nsSYN::TransJsonText(ResDataObject& config)
  206. {
  207. for (int x = 0; x < config.size(); x++)
  208. {
  209. //如果有Value
  210. if (config[x].GetKeyCount("Value") > 0)
  211. {
  212. //mLog::FINFO("TRY COVERT [{$}] VALUE {$}", config.GetKey(x), config[x]["Value"].size() > 0 ? config[x]["Value"].encode() : (const char*)config[x]["Value"]);
  213. if (config[x]["Value"].size() <= 0)
  214. {
  215. string va = (const char*)config[x]["Value"];
  216. config[x] = va.c_str();
  217. }
  218. else
  219. {
  220. ResDataObject rest = config[x]["Value"];
  221. config[x] = rest;
  222. //mLog::FINFO("convert object [{$}], object {$}", config.GetKey(x), rest.encode());
  223. }
  224. }
  225. //mLog::FINFO("After Convert {$}", config.encode());
  226. }
  227. }
  228. //-----------------------------------------------------------------------------
  229. // nsSYN::SYNBOXMould
  230. //-----------------------------------------------------------------------------
  231. nsSYN::SYNBOXMould::SYNBOXMould()
  232. {
  233. }
  234. nsSYN::SYNBOXMould::~SYNBOXMould()
  235. {
  236. }
  237. void nsSYN::SYNBOXMould::Register (Dispatch* Dispatch)
  238. {
  239. //同步盒内容
  240. Dispatch->Get.Push(AttrKey::SYNSTATE, [this](std::string& out) {
  241. if (m_SynBoxUnit.m_SynBoxState)
  242. out = m_SynBoxUnit.m_SynBoxState->JSGet();
  243. return RET_STATUS::RET_SUCCEED; });
  244. Dispatch->Get.Push(AttrKey::TOTALEXPSURENUMBER, [this](std::string& out) {
  245. if (m_SynBoxUnit.m_TotalExpNumber)
  246. out = m_SynBoxUnit.m_TotalExpNumber->JSGet();
  247. return RET_STATUS::RET_SUCCEED; });
  248. Dispatch->Get.Push(AttrKey::CURRENTEXPOSUREBNUMBER, [this](std::string& out) {
  249. if (m_SynBoxUnit.m_CurrentExpNumber)
  250. out = m_SynBoxUnit.m_CurrentExpNumber->JSGet();
  251. return RET_STATUS::RET_SUCCEED; });
  252. Dispatch->Set.Push(AttrKey::WORKSTATION, [this](std::string in) {
  253. ResDataObject json;
  254. json.decode(in.c_str());
  255. auto value = (string)json[0];
  256. return SetWS(value); });
  257. Dispatch->Action.Push(ActionKey::SetValue_WORKSTATION, [this](std::string in, std::string& out) {
  258. ResDataObject json;
  259. json.decode(in.c_str());
  260. auto value = (string)json[0];
  261. return SetWS(value); });
  262. Dispatch->Set.Push(AttrKey::EXPMODE, [this](std::string in) {
  263. ResDataObject json;
  264. json.decode(in.c_str());
  265. auto value = (string)json[0];
  266. SetExpMode(value);
  267. try
  268. {
  269. //解析出曝光次数
  270. auto exptimes = (int)json[1];
  271. SetExposureTimes(exptimes);
  272. }
  273. catch (...)
  274. {
  275. printf("get SYNBox Mould default exptimes failed");
  276. }
  277. try
  278. {
  279. //解析出来framerate
  280. auto framerate = (int)json[2];
  281. SetFrameRate(framerate);
  282. }
  283. catch (...)
  284. {
  285. printf("get SYNBox Mould default framerate failed");
  286. }
  287. return RET_STATUS::RET_SUCCEED; });
  288. Dispatch->Action.Push(ActionKey::SetExpMode, [this](std::string in, std::string& out) {
  289. ResDataObject json;
  290. json.decode(in.c_str());
  291. auto value = (string)json[0];
  292. SetExpMode(value);
  293. if (json.size() >= 2)
  294. {
  295. try
  296. {
  297. //解析出曝光次数
  298. auto exptimes = (int)json[1];
  299. SetExposureTimes(exptimes);
  300. }
  301. catch (...)
  302. {
  303. printf("get SYNBox Mould default exptimes failed");
  304. }
  305. }
  306. //add by wxx 暂时不需要,由上层根据探测器反馈自行设置
  307. //if (json.size() >= 3)
  308. //{
  309. // try
  310. // {
  311. // //解析出帧率
  312. // auto pps = (float)json[2];
  313. // SetFrameRate(pps);
  314. // }
  315. // catch (...)
  316. // {
  317. // printf("get SYNBox Mould default PPS failed");
  318. // }
  319. //}
  320. return RET_STATUS::RET_SUCCEED; });
  321. Dispatch->Action.Push(ActionKey::SetExpEnable, [this](std::string in, std::string& out) {
  322. return SetExpEnable(); });
  323. Dispatch->Action.Push(ActionKey::SetExpDisable, [this](std::string in, std::string& out) {
  324. return SetExpDisable(); });
  325. Dispatch->Set.Push(AttrKey::EXPTIMES, [this](std::string in) {
  326. auto value = JSONTo <int>(in);
  327. return SetExposureTimes(value); });
  328. Dispatch->Action.Push(ActionKey::SetExposureTimes, [this](std::string in, std::string& out) {
  329. auto value = JSONTo <int>(in);
  330. return SetExposureTimes(value); });
  331. Dispatch->Action.Push(ActionKey::SetGenAECSignal, [this](std::string in, std::string& out) {
  332. auto value = JSONTo <int>(in);
  333. return SetGenAECSignal(value); });
  334. Dispatch->Action.Push(ActionKey::SimulateFootSwitchSignal, [this](std::string in, std::string& out) {
  335. auto value = JSONTo <int>(in);
  336. return SimulateFootSwitchSignal(value); });
  337. Dispatch->Action.Push(ActionKey::SimulateHandSwitchSignal, [this](std::string in, std::string& out) {
  338. auto value = JSONTo <int>(in);
  339. return SimulateHandSwitchSignal(value); });
  340. Dispatch->Action.Push(ActionKey::SetDirectSignal, [this](std::string in, std::string& out) {
  341. ResDataObject json;
  342. json.decode(in.c_str());
  343. int paramCount = json.size();
  344. for (int i = 0; i < paramCount; i++)
  345. {
  346. string strName = json.GetKey(i);
  347. string strValue = (string)json[i];
  348. SetDirectSignal(strName, atoi(strValue.c_str()));
  349. }
  350. return RET_STATUS::RET_SUCCEED; });
  351. //重置
  352. Dispatch->Action.Push(ActionKey::Reset, [this](std::string in, std::string& out) {
  353. return Reset(); });
  354. //同步模式
  355. Dispatch->Action.Push(ActionKey::ActiveSyncMode, [this](std::string in, std::string& out) {
  356. ResDataObject json;
  357. json.decode(in.c_str());
  358. _tSyncModeArgs t{ };
  359. if (json.size() >= 3)
  360. {
  361. t.strWS = (string)json[0];
  362. t.strSyncMode = (string)json[1];
  363. t.strSyncValue = (string)json[2];
  364. return ActiveSyncMode(t);
  365. }
  366. else
  367. {
  368. return RET_STATUS::RET_FAILED;
  369. }});
  370. //发生器内容
  371. Dispatch->Get.Push(AttrKey::DiosHandSwitch, [this](std::string& out) {
  372. if (m_SynBoxUnit.m_HandSwitchState)
  373. out = m_SynBoxUnit.m_HandSwitchState->JSGet();
  374. return RET_STATUS::RET_SUCCEED; });
  375. Dispatch->Get.Push(AttrKey::DiosGeneratorSync, [this](std::string& out) {
  376. if (m_SynBoxUnit.m_GenSynState)
  377. out = m_SynBoxUnit.m_GenSynState->JSGet();
  378. return RET_STATUS::RET_SUCCEED; });
  379. Dispatch->Set.Push(AttrKey::DiosGeneratorSync, [this](std::string in) {
  380. auto value = JSONTo <int>(in);
  381. return SetGeneratortoSyncStatus(value); });
  382. Dispatch->Action.Push(ActionKey::SetGeneratortoSyncStatus, [this](std::string in, std::string& out) {
  383. auto value = JSONTo <int>(in);
  384. return SetGeneratortoSyncStatus(value); });
  385. Dispatch->Set.Push(AttrKey::EXPFRAMERAT, [this](std::string in) {
  386. auto value = JSONTo <float>(in);
  387. return SetFrameRate(value); });
  388. Dispatch->Action.Push(ActionKey::SetFrameRate, [this](std::string in, std::string& out) {
  389. auto value = JSONTo <float>(in);
  390. return SetFrameRate(value); });
  391. Dispatch->Action.Push(ActionKey::SetValue_PPS, [this](std::string in, std::string& out) {
  392. auto value = JSONTo <float>(in);
  393. return SetValue_PPS(value); });
  394. //探测器内容
  395. Dispatch->Get.Push(AttrKey::DiosDetectorStatus, [this](std::string& out) {
  396. if (m_SynBoxUnit.m_DetectorState)
  397. out = m_SynBoxUnit.m_DetectorState->JSGet();
  398. return RET_STATUS::RET_SUCCEED; });
  399. Dispatch->Get.Push(AttrKey::DiosXwindowStatus, [this](std::string& out) {
  400. if (m_SynBoxUnit.m_DetectorWindowState)
  401. out = m_SynBoxUnit.m_DetectorWindowState->JSGet();
  402. return RET_STATUS::RET_SUCCEED; });
  403. Dispatch->Action.Push(ActionKey::PrepareAcquisition, [this](std::string in, std::string& out) {
  404. return PrepareAcquisition(); });
  405. Dispatch->Action.Push(ActionKey::StartWindowRequest, [this](std::string in, std::string& out) {
  406. return StartWindowRequest(); });
  407. Dispatch->Action.Push(ActionKey::StopWindowRequest, [this](std::string in, std::string& out) {
  408. return StopWindowRequest(); });
  409. //CAN口内容
  410. Dispatch->Get.Push(AttrKey::DiosGRIDSync, [this](std::string& out) {
  411. if (m_SynBoxUnit.m_GridStatue)
  412. out = m_SynBoxUnit.m_GridStatue->JSGet();
  413. return RET_STATUS::RET_SUCCEED; });
  414. }
  415. //-----------------------------------------------------------------------------
  416. // 所有的消息响应函数
  417. //-----------------------------------------------------------------------------
  418. //-----------------------------------------------------------------------------
  419. // 所有的 GetXX 函数
  420. //-----------------------------------------------------------------------------
  421. //-----------------------------------------------------------------------------
  422. // nsSYN::DriverMould
  423. //-----------------------------------------------------------------------------
  424. nsSYN::DriverMould::DriverMould ()
  425. {
  426. ConfigInfo Vender (ConfKey::DiosType, "string", "R", "TRUE", "");
  427. m_ConfigInfo.push_back(Vender);
  428. ConfigInfo Model (ConfKey::DiosModel, "string", "R", "TRUE", "");
  429. m_ConfigInfo.push_back(Model);
  430. EasyJSONEncoder synList;
  431. synList.Set ("0", "1");
  432. synList.Set ("1", "2");
  433. ConfigInfo GenIsConnect (ConfKey::DiosIsConnect, "int", "RW", "FALSE", "");
  434. GenIsConnect.SetList (synList.ToString ().c_str ());
  435. m_ConfigInfo.push_back(GenIsConnect);
  436. ConfigInfo SCFType (ConfKey::DiosSCFType, "string", "R", "TRUE", "");
  437. m_ConfigInfo.push_back(SCFType);
  438. EasyJSONEncoder COMList;
  439. COMList.Set ("0", "COM1");
  440. COMList.Set ("1", "COM2");
  441. COMList.Set ("2", "COM3");
  442. COMList.Set ("3", "COM4");
  443. COMList.Set ("4", "COM5");
  444. COMList.Set ("5", "COM6");
  445. COMList.Set ("6", "COM7");
  446. COMList.Set ("7", "COM8");
  447. COMList.Set ("8", "COM9");
  448. COMList.Set ("9", "COM10");
  449. COMList.Set ("10", "COM11");
  450. COMList.Set ("11", "COM12");
  451. ConfigInfo SCFPort (ConfKey::DiosSCFPort, "string", "RW", "TRUE", "");
  452. SCFPort.SetList (COMList.ToString ().c_str ());
  453. m_ConfigInfo.push_back(SCFPort);
  454. ConfigInfo SCFBaudrate (ConfKey::DiosSCFBaudrate, "int", "R", "FALSE", "");
  455. m_ConfigInfo.push_back(SCFBaudrate);
  456. ConfigInfo SCFBytesize (ConfKey::DiosSCFBytesize, "int", "R", "FALSE", "");
  457. m_ConfigInfo.push_back(SCFBytesize);
  458. ConfigInfo SCFParity (ConfKey::DiosSCFParity, "int", "R", "FALSE", "");
  459. m_ConfigInfo.push_back(SCFParity);
  460. ConfigInfo SCFStopbits (ConfKey::DiosSCFStopbits, "int", "R", "FALSE", "");
  461. m_ConfigInfo.push_back(SCFStopbits);
  462. }
  463. nsSYN::DriverMould::~DriverMould ()
  464. {
  465. }
  466. std::string nsSYN::DriverMould::GetGUID () const
  467. {
  468. return DeviceDriverType;
  469. }
  470. #ifdef _WIN64
  471. #ifdef _DEBUG
  472. static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64D.dll";
  473. static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64D.dll";
  474. #else
  475. static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64.dll";
  476. static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64.dll";
  477. #endif
  478. #endif
  479. string nsSYN::DriverMould::GetConnectDLL(string& ConfigFileName)
  480. {
  481. string ConnectDLL = COM_SCFDllName;
  482. ResDataObject r_config, Connection;
  483. if (!r_config.loadFile(ConfigFileName.c_str()))
  484. return ConnectDLL;
  485. if (r_config["CONFIGURATION"]["connections"].GetKeyCount("Value") > 0)
  486. {
  487. Connection = r_config["CONFIGURATION"]["connections"]["Value"];
  488. }
  489. else
  490. {
  491. Connection = r_config["CONFIGURATION"]["connections"][0];
  492. }
  493. if ((string)Connection["type"] == "COM")
  494. ConnectDLL = COM_SCFDllName;
  495. else
  496. ConnectDLL = TCP_SCFDllName;
  497. return ConnectDLL;
  498. }
  499. ResDataObject nsSYN::DriverMould::GetConnectParam(string& ConfigFileName)
  500. {
  501. ResDataObject r_config, Connection;
  502. if (!r_config.loadFile(ConfigFileName.c_str()))
  503. return Connection;
  504. if (r_config["CONFIGURATION"]["connections"].GetKeyCount("Value") > 0)
  505. {
  506. Connection = r_config["CONFIGURATION"]["connections"]["Value"];
  507. }
  508. else
  509. {
  510. Connection = r_config["CONFIGURATION"]["connections"][0];
  511. }
  512. return Connection;
  513. }