DIOS.Dev.DAP.VACUE130DAP.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #include "stdafx.h"
  2. #include <assert.h>
  3. #include <functional>
  4. using namespace std::placeholders;
  5. #pragma comment(lib, "version.lib")
  6. //#include "ResDataObject.h"
  7. #include "DIOS.Dev.DAP.VACUE130DAP.h"
  8. #include "Helper.JSON.hpp"
  9. using namespace DIOS::Dev::Detail::DAP;
  10. namespace nsDAP = DIOS::Dev::Detail::DAP;
  11. #pragma warning (disable:4244) // warning C4244: “初始化”: 从“double”转换到“float”,可能丢失数据
  12. #pragma warning (disable:4305) // warning C4305: “参数”: 从“double”到“float”截断
  13. #pragma warning (disable:4267) // warning C4267 : “初始化”: 从“size_t”转换到“int”,可能丢失数据
  14. static const int msTimeOut_Lock = 500;
  15. #ifdef _WIN64
  16. #ifdef _DEBUG
  17. static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64D.dll";
  18. #else
  19. static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64.dll";
  20. #endif
  21. #endif
  22. #ifdef _WIN64
  23. #ifdef _DEBUG
  24. static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64D.dll";
  25. #else
  26. static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64.dll";
  27. #endif
  28. #endif
  29. Log4CPP::Logger* mLog::gLogger = nullptr;
  30. //-----------------------------------------------------------------------------
  31. // VACUE130DAP
  32. //-----------------------------------------------------------------------------
  33. nsDAP::VACUE130DAP::VACUE130DAP (std::shared_ptr <IOEventCenter> center, nsSCF::SCF SCF, string configfile) : super (center, SCF)
  34. {
  35. assert (EventCenter);
  36. ResDataObject temp;
  37. temp.loadFile(configfile.c_str());
  38. m_DapConfig = temp["CONFIGURATION"];
  39. TransJsonText(m_DapConfig);
  40. string version;
  41. if (GetVersion(version, hMyModule))
  42. mLog::FINFO("\n===============log begin : module version:{$} ===================\n", version.c_str());
  43. else
  44. mLog::FINFO("\n===============log begin : version:0.0.0.0 ===================\n");
  45. //获取delta动态库版本号
  46. string strPath;
  47. strPath = GetDynamicLibraryPath() + "\\DIOS.Dev.DAP.VACUE130DAP64.dll";
  48. string strVersion;
  49. strVersion = GetFileVersion(strPath);
  50. mLog::FINFO("=====================log begin :delta dll version{$}===strPath {$}====================", strVersion, strPath);
  51. m_MSGUnit.reset(new nsDetail::MSGUnit(center, nsDAP::DAPUnitType));
  52. GetConfData();
  53. Register();
  54. }
  55. nsDAP::VACUE130DAP::~VACUE130DAP ()
  56. {
  57. }
  58. std::string nsDAP::VACUE130DAP::GetGUID() const
  59. {
  60. printf("\n===============GetGUID : %s ===================\n", DAPUnitType);
  61. return DeviceDriverType;
  62. }
  63. void nsDAP::VACUE130DAP::Register()
  64. {
  65. auto Disp = &Dispatch;
  66. m_DAPUnit.m_DAP.reset(new DOSEMould(0.0, 1.0, 5000.0, 0.1));
  67. m_DAPUnit.m_ConnectionStatus.reset(new ConnectionStatusMould(0, 0, 1, 1));
  68. superGen::Register (Disp);
  69. Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; });
  70. }
  71. RET_STATUS nsDAP::VACUE130DAP::HWSend (char* strCommand, int nTimeOut)
  72. {
  73. if (!m_SCF) return RET_STATUS::RET_FAILED;
  74. unsigned char strSendCommand[100] = { 0 };
  75. int len = strlen(strCommand);
  76. memcpy(strSendCommand, strCommand, len);
  77. strSendCommand[len] = 0x0D;
  78. strSendCommand[len + 1] = 0x0A;
  79. mLog::FINFO("==OUT==: %s \n", strSendCommand);
  80. int retLength;
  81. m_SCF.Lock(msTimeOut_Lock)
  82. .SendPacket((char*)strSendCommand, strlen((char*)strSendCommand), nTimeOut, retLength);
  83. Sleep(nTimeOut);
  84. return RET_STATUS::RET_SUCCEED;
  85. }
  86. RET_STATUS nsDAP::VACUE130DAP::ClearDAP()
  87. {
  88. return RET_STATUS::RET_SUCCEED;
  89. }
  90. RET_STATUS nsDAP::VACUE130DAP::QueryDAP(float& value)
  91. {
  92. HWSend("M1");
  93. return RET_STATUS::RET_SUCCEED;
  94. }
  95. //-----------------------------------------------------------------------------
  96. // ProcessCmd
  97. //-----------------------------------------------------------------------------
  98. void nsDAP::VACUE130DAP::FireNotify (std::string key, std::string content)
  99. {
  100. EventCenter->OnNotify (1, key, content);
  101. }
  102. // 收到硬件的通知, 有包到达
  103. // 按照设计, 仅当数据有变化时, 才发通知到上层, 因此里面有大量的 if (..) FireNotify (..)
  104. std::string extractBetweenCommas(std::string& str) {
  105. size_t start = str.find(',');
  106. if (start == std::string::npos) return ""; // 没有逗号
  107. size_t end = str.find(',', start + 1);
  108. if (end == std::string::npos) return ""; // 只有一个逗号
  109. return str.substr(start + 1, end - start - 1);
  110. }
  111. void nsDAP::VACUE130DAP::OnCallBack (const char* strPackage, DWORD Length)
  112. {
  113. unsigned char strSendCommand[100] = { 0 };
  114. int len = strlen(strPackage);
  115. memcpy(strSendCommand, strPackage, len);
  116. mLog::FINFO("==OUT==: {$} \n", strSendCommand);
  117. if (Length < 18)
  118. {
  119. return;
  120. }
  121. if (m_DAPUnit.m_ConnectionStatus->Update(true))
  122. FireNotify(m_DAPUnit.m_ConnectionStatus->GetKey(), m_DAPUnit.m_ConnectionStatus->JSGet());
  123. float dapvalue = 0.0;
  124. string temp = strPackage;
  125. if (strPackage[1] == 'M')
  126. {
  127. dapvalue = atof(extractBetweenCommas(temp).c_str());
  128. m_DAPUnit.m_DAP->Update(dapvalue);
  129. FireNotify(m_DAPUnit.m_DAP->GetKey(), m_DAPUnit.m_DAP->JSGet());
  130. }
  131. }
  132. void DIOS::Dev::Detail::DAP::VACUE130DAP::SubscribeSelf(dios_mqtt_connection* conn)
  133. {
  134. mLog::FINFO("SubScribeSelf");
  135. //子系统当前状态
  136. SubscribeTopic(conn, "DIOS/DEVICE/Subsystem/Notify/CurrentSMState");
  137. //发生器属性
  138. SubscribeTopic(conn, "DIOS/DEVICE/Generator/Notify/POSTKV");
  139. SubscribeTopic(conn, "DIOS/DEVICE/Generator/Notify/POSTMA");
  140. SubscribeTopic(conn, "DIOS/DEVICE/Generator/Notify/POSTMS");
  141. SubscribeTopic(conn, "DIOS/DEVICE/Generator/Notify/POSTMAS");
  142. //遮光器属性
  143. SubscribeTopic(conn, "DIOS/DEVICE/Collimator/Notify/SID");
  144. SubscribeTopic(conn, "DIOS/DEVICE/Collimator/Notify/XSIZE");
  145. SubscribeTopic(conn, "DIOS/DEVICE/Collimator/Notify/YSIZE");
  146. }
  147. //-----------------------------------------------------------------------------
  148. // DAPDriver
  149. //-----------------------------------------------------------------------------
  150. std::string nsDAP::VACUE130DAP::GetDynamicLibraryPath()
  151. {
  152. HMODULE hModule = GetModuleHandle("DIOS.Dev.DAP.VACUE130DAP64.dll");
  153. char path[MAX_PATH];
  154. GetModuleFileName(hModule, path, MAX_PATH);
  155. std::string fullPath(path);
  156. size_t pos = fullPath.find_last_of("\\/");
  157. mLog::FINFO("Delta dll fullPath {$}", fullPath);
  158. return fullPath.substr(0, pos);
  159. }
  160. std::string nsDAP::VACUE130DAP::GetFileVersion(std::string strFilePathName)
  161. {
  162. DWORD dwVerSize = GetFileVersionInfoSize(strFilePathName.c_str(), NULL);
  163. if (dwVerSize == 0)
  164. {
  165. return false;
  166. }
  167. LPVOID pVersionBuffer = malloc(dwVerSize);
  168. GetFileVersionInfo(strFilePathName.c_str(), 0, dwVerSize, pVersionBuffer);
  169. VS_FIXEDFILEINFO* pInfo;
  170. UINT nInfoLen;
  171. std::string strVersion = "";
  172. if (VerQueryValue(pVersionBuffer, "\\", (void**)&pInfo, &nInfoLen))
  173. {
  174. strVersion = to_string(HIWORD(pInfo->dwFileVersionMS)) + "." + to_string(LOWORD(pInfo->dwFileVersionMS)) + "."
  175. + to_string(HIWORD(pInfo->dwFileVersionLS)) + "." + to_string(LOWORD(pInfo->dwFileVersionLS));
  176. }
  177. return strVersion;
  178. }
  179. void nsDAP::VACUE130DAP::GetConfData()
  180. {
  181. ResDataObject Connection = m_DapConfig["connections"][0];
  182. m_strCommunicateType = (string)Connection["type"];
  183. mLog::FINFO("CommunicateType {$}" m_strCommunicateType);
  184. }
  185. void nsDAP::VACUE130DAP::ResetDap()
  186. {
  187. HWSend("R1");
  188. }
  189. void DIOS::Dev::Detail::DAP::VACUE130DAP::TestDAP()
  190. {
  191. HWSend("t");
  192. }
  193. void DIOS::Dev::Detail::DAP::VACUE130DAP::QueryStatus()
  194. {
  195. HWSend("z");
  196. }
  197. void DIOS::Dev::Detail::DAP::VACUE130DAP::SetProtocol()
  198. {
  199. HWSend("c=2");
  200. }
  201. void DIOS::Dev::Detail::DAP::VACUE130DAP::GetMeanValue()
  202. {
  203. HWSend("f");
  204. }
  205. nsDAP::DAPDriver::DAPDriver ()
  206. {
  207. }
  208. nsDAP::DAPDriver::~DAPDriver ()
  209. {
  210. }
  211. auto nsDAP::DAPDriver::CreateDevice (int index) -> std::unique_ptr <IODevice>
  212. {
  213. if (!m_SCF.isConnected())
  214. return nullptr;
  215. m_pDevice = new VACUE130DAP(EventCenter, m_SCF, m_ConfigFileName);
  216. auto dev = std::unique_ptr <IODevice>(new IODevice(m_pDevice));
  217. return dev;
  218. }
  219. void nsDAP::DAPDriver::FireNotify (int code, std::string key, std::string content)
  220. {
  221. EventCenter->OnNotify (code, key, content);
  222. }
  223. void nsDAP::DAPDriver::Prepare ()
  224. {
  225. string strLogPath = GetProcessDirectory() + R"(\OEMDrivers\Generator\Conf\Log4CPP.Config.DAP.xml)";
  226. Log4CPP::GlobalContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "DAP.VACUE130");
  227. auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str());
  228. mLog::gLogger = Log4CPP::LogManager::GetLogger("DAP.VACUE130");
  229. m_SCFDllName = GetConnectDLL(m_ConfigFileName);
  230. super::Prepare();
  231. }
  232. bool DATA_ACTION nsDAP::DAPDriver::Connect ()
  233. {
  234. ResDataObject Connection = GetConnectParam(m_ConfigFileName);
  235. mLog::FINFO("connections:{$}", Connection.encode());
  236. auto erCode = m_SCF.Connect(Connection.encode(), &nsDAP::DAPDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000);
  237. if (erCode != SCF_ERR::SCF_SUCCEED)
  238. return erCode;
  239. auto rc = super::Connect();
  240. if (!rc)
  241. return 0;
  242. return SCF_ERR::SCF_SUCCEED;
  243. }
  244. void nsDAP::DAPDriver::Disconnect()
  245. {
  246. super::Disconnect();
  247. m_SCF.Disconnect();
  248. }
  249. bool nsDAP::DAPDriver::isConnected() const
  250. {
  251. return super::isConnected();
  252. }
  253. std::string nsDAP::DAPDriver::DriverProbe ()
  254. {
  255. ResDataObject r_config, HardwareInfo;
  256. if (r_config.loadFile (m_ConfigFileName.c_str ()))
  257. {
  258. HardwareInfo.add ("MajorID", r_config ["CONFIGURATION"] ["MajorID"]);
  259. HardwareInfo.add ("MinorID", r_config ["CONFIGURATION"] ["MinorID"]);
  260. HardwareInfo.add ("VendorID", r_config ["CONFIGURATION"] ["VendorID"]);
  261. HardwareInfo.add ("ProductID", r_config ["CONFIGURATION"] ["ProductID"]);
  262. HardwareInfo.add ("SerialID", r_config ["CONFIGURATION"] ["SerialID"]);
  263. }
  264. else
  265. {
  266. HardwareInfo.add ("MajorID", "DAP");
  267. HardwareInfo.add ("MinorID", "Dr");
  268. HardwareInfo.add ("VendorID", "PTW");
  269. HardwareInfo.add ("ProductID", "HF");
  270. HardwareInfo.add ("SerialID", "Drv");
  271. }
  272. string ret = HardwareInfo.encode ();
  273. return ret;
  274. }
  275. std::string nsDAP::DAPDriver::GetResource ()
  276. {
  277. ResDataObject temp;
  278. if (!temp.loadFile(m_ConfigFileName.c_str()))
  279. return std::string();
  280. auto r_config = temp["CONFIGURATION"];
  281. for (auto& Item : m_ConfigInfo)
  282. {
  283. string key = Item.GetKey();
  284. if (key == ConfKey::DiosType)
  285. {
  286. Item.SetCurrentValue(((string)r_config["VendorID"]).c_str());
  287. }
  288. else if (key == ConfKey::DiosModel)
  289. {
  290. Item.SetCurrentValue(((string)r_config["ProductID"]).c_str());
  291. }
  292. else if (key == ConfKey::DiosSCFType)
  293. {
  294. Item.SetCurrentValue(((string)r_config["connections"][0]["type"]).c_str());
  295. }
  296. else if (key == ConfKey::DiosSCFPort || key == ConfKey::DiosSCFBaudrate || key == ConfKey::DiosSCFBytesize
  297. || key == ConfKey::DiosSCFParity || key == ConfKey::DiosSCFStopbits || key == ConfKey::DiosSCFIP)
  298. {
  299. if (r_config["connections"][0].GetFirstOf(key.c_str()) >= 0)
  300. {
  301. Item.SetCurrentValue(((string)r_config["connections"][0][key.c_str()]).c_str());
  302. }
  303. }
  304. }
  305. ResDataObject resAttr, resDescription;
  306. for (auto Item : m_ConfigInfo)
  307. {
  308. resAttr.add(Item.GetKey(), Item.GetCurrentValue());
  309. resDescription.add(Item.GetKey(), Item.GetDescription());
  310. }
  311. ResDataObject resDeviceResource;
  312. resDeviceResource.add(ConfKey::DiosAttribute, resAttr);
  313. resDeviceResource.add(ConfKey::DiosDescription, resDescription);
  314. string res = resDeviceResource.encode();
  315. printf("resDeviceResource :%s \n", resDeviceResource.encode());
  316. return res;
  317. }
  318. std::string nsDAP::DAPDriver::DeviceProbe ()
  319. {
  320. ResDataObject r_config, HardwareInfo;
  321. if (r_config.loadFile (m_ConfigFileName.c_str ()))
  322. {
  323. HardwareInfo.add ("MajorID", r_config ["CONFIGURATION"] ["MajorID"]);
  324. HardwareInfo.add ("MinorID", r_config ["CONFIGURATION"] ["MinorID"]);
  325. HardwareInfo.add ("VendorID", r_config ["CONFIGURATION"] ["VendorID"]);
  326. HardwareInfo.add ("ProductID", r_config ["CONFIGURATION"] ["ProductID"]);
  327. HardwareInfo.add ("SerialID", r_config ["CONFIGURATION"] ["SerialID"]);
  328. }
  329. else
  330. {
  331. HardwareInfo.add ("MajorID", "DAP");
  332. HardwareInfo.add ("MinorID", "Dr");
  333. HardwareInfo.add ("VendorID", "PTW");
  334. HardwareInfo.add ("ProductID", "HF");
  335. HardwareInfo.add ("SerialID", "1234");
  336. }
  337. string ret = HardwareInfo.encode ();
  338. return ret;
  339. }
  340. void nsDAP::DAPDriver::Dequeue (const char * Packet, DWORD Length)
  341. {
  342. m_pDevice->OnCallBack(Packet, Length);
  343. }
  344. PACKET_RET nsDAP::DAPDriver::callbackPackageProcess (const char * RecData, DWORD nLength, DWORD& PacketLength)
  345. {
  346. mLog::FINFO("nLength < 1, nLength=={$} PacketLength {$}", nLength, PacketLength);
  347. if (nLength < 1)
  348. {
  349. return PACKET_USELESS;
  350. }
  351. if (RecData[0] == 0x0D && RecData[nLength - 1] == 0x0A)
  352. {
  353. PacketLength = nLength + 2;
  354. char strtemp[100] = { 0 };
  355. memcpy(strtemp, RecData, nLength);
  356. strtemp[PacketLength + 1] = 0;
  357. mLog::FINFO("==IN ==:{$}", strtemp);
  358. return PACKET_ISPACKET;
  359. }
  360. return PACKET_NOPACKET;
  361. }
  362. //-----------------------------------------------------------------------------
  363. // GetIODriver & CreateIODriver
  364. //-----------------------------------------------------------------------------
  365. static nsDAP::DAPDriver gIODriver;
  366. extern "C" DIOS::Dev::IODriver * __cdecl GetIODriver () // 返回静态对象的引用, 调用者不能删除 !
  367. {
  368. return &gIODriver;
  369. }
  370. extern "C" DIOS::Dev::IODriver * __cdecl CreateIODriver () // 返回新对象, 调用者必须自行删除此对象 !
  371. {
  372. return new nsDAP::DAPDriver ();
  373. }
  374. #if 0
  375. #endif
  376. RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetGenCurrentSMState(std::string value)
  377. {
  378. return RET_STATUS();
  379. }
  380. RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetGenPostKV(const std::string value)
  381. {
  382. m_fPostKV = atof(value.c_str());
  383. mLog::FINFO("{$}PostKV", m_fPostKV);
  384. return RET_STATUS();
  385. }
  386. RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetGenPostMAS(const std::string value)
  387. {
  388. m_fPostMAS = atof(value.c_str());
  389. mLog::FINFO("{$}PostMAS", m_fPostMAS);
  390. return RET_STATUS();
  391. }
  392. RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetGenPostMA(const std::string value)
  393. {
  394. m_fPostMA = atof(value.c_str());
  395. mLog::FINFO("{$}PostMA", m_fPostMA);
  396. return RET_STATUS();
  397. }
  398. RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetGenPostMS(const std::string value)
  399. {
  400. m_fPostMS = atof(value.c_str());
  401. mLog::FINFO("{$}PostMS", m_fPostMS);
  402. return RET_STATUS();
  403. }
  404. RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetCollSID(const std::string value)
  405. {
  406. m_fSID = atof(value.c_str());
  407. mLog::FINFO("SID{$}", m_fSID);
  408. return RET_STATUS();
  409. }
  410. RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetCollXSIZE(const std::string value)
  411. {
  412. m_fXsize = atof(value.c_str());
  413. mLog::FINFO("Xsize{$}", m_fXsize);
  414. return RET_STATUS();
  415. }
  416. RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetCollYSIZE(const std::string value)
  417. {
  418. m_fYsize = atof(value.c_str());
  419. mLog::FINFO("Ysize{$}", m_fYsize);
  420. return RET_STATUS();
  421. }