RedRatProcess.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. #include "StdAfx.h"
  2. #include "RedRatProcess.h"
  3. #include <tlhelp32.h>
  4. #include <Shlwapi.h>
  5. #include "shellapi.h"
  6. #pragma comment(lib, "shlwapi.lib")
  7. #include "Global.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #endif
  11. #ifndef SEND_SG_FORMAT
  12. #define SEND_SG_FORMAT _T("name=\"%s\",dataset=\"%s\",signal=\"%s\"\n")
  13. #endif
  14. #ifndef SEND_SG_REP_FORMAT
  15. #define SEND_SG_REP_FORMAT _T("name=\"%s\",dataset=\"%s\",signal=\"%s\",repeats=\"%d\"\n")
  16. #endif
  17. #ifndef SEND_SG_DUR_FORMAT
  18. #define SEND_SG_DUR_FORMAT _T("name=\"%s\",dataset=\"%s\",signal=\"%s\",duration=\"%d\"\n")
  19. #endif
  20. #ifndef SEND_SG_BOTH_FORMAT
  21. #define SEND_SG_BOTH_FORMAT _T("name=\"%s\",dataset=\"%s\",signal=\"%s\",repeats=\"%d\",duration=\"%d\"\n")
  22. #endif
  23. #ifndef APP_CMD_LINE
  24. #define APP_CMD_LINE _T("%s --port=%ld")
  25. #endif
  26. #ifndef GET_IRC_DATA
  27. #define GET_IRC_DATA \
  28. if (m_strCurDevice.size() == 0) \
  29. getDeviceNameList(); \
  30. if (m_strCurDevice.size() == 0) \
  31. return false;\
  32. if (m_strCurDataset.size() == 0)\
  33. getDatasetNameList();\
  34. if (m_strCurDataset.size() == 0)\
  35. return false;
  36. #endif
  37. DWORD CRedRatProcess::m_dwCurAppId = 0;
  38. // 获取设备名;
  39. void fun_dev(std::vector<std::string>& vtDev, std::string _dev_) {
  40. int npos = -1;
  41. npos = _dev_.find_last_of(']');
  42. if (npos != std::string::npos)
  43. {
  44. _dev_ = _dev_.substr(npos + 2); // +2 包含空格;
  45. // 去除connected;
  46. npos = _dev_.find(" (connected)");
  47. if (npos != std::string::npos)
  48. {
  49. _dev_ = _dev_.substr(0, npos);
  50. }
  51. vtDev.push_back(_dev_);
  52. }
  53. };
  54. CRedRatProcess::CRedRatProcess(void)
  55. {
  56. }
  57. CRedRatProcess::~CRedRatProcess(void)
  58. {
  59. }
  60. bool CRedRatProcess::checkEOM(std::string& data)
  61. {
  62. if (data.size() < 5)
  63. return false;
  64. if (_tcsicmp("{\n", data.substr(0, 2).c_str()) != 0)
  65. return false;
  66. if (_tcsicmp("\n}\n", data.substr(data.size() - 3, 3).c_str()) != 0)
  67. return false;
  68. // 并返回去掉花括号的内容;
  69. data = data.substr(2, data.size() - 5);
  70. return true;
  71. }
  72. bool CRedRatProcess::StartApp(LPCTSTR lpAppDir, LPCTSTR lpSignalXml, DWORD dwPort)
  73. {
  74. if (!lpAppDir || !PathFileExists(lpAppDir))
  75. return false;
  76. if ( IsAppRunning(lpAppDir) != 0)
  77. return true;
  78. // 启动应用程序;
  79. TCHAR szParameters[256] = {0};
  80. _stprintf_s(szParameters, APP_CMD_LINE, lpSignalXml, dwPort);
  81. //ShellExecute(NULL, "open", pszExePath, NULL, NULL, SW_SHOWNORMAL);
  82. SHELLEXECUTEINFO sei;
  83. memset(&sei, 0, sizeof(SHELLEXECUTEINFO));
  84. sei.cbSize = sizeof(SHELLEXECUTEINFO);
  85. sei.hwnd = NULL;
  86. // 普通打开方式:open;若想以管理员身份运行:runas
  87. sei.lpVerb = _T("runas");
  88. //sei.fMask = SEE_MASK_NOCLOSEPROCESS;//不设置,则使用默认值;
  89. sei.lpFile = lpAppDir;
  90. sei.lpParameters = szParameters;
  91. sei.lpDirectory = NULL;
  92. #ifdef _DEBUG
  93. sei.nShow = SW_SHOWNORMAL;
  94. #else
  95. sei.nShow = SW_HIDE;
  96. #endif
  97. sei.hInstApp = NULL;
  98. if (!ShellExecuteEx(&sei))
  99. {
  100. DWORD dw = GetLastError();
  101. return false;
  102. }
  103. if (sei.hProcess)
  104. CloseHandle(sei.hProcess);
  105. return true;
  106. }
  107. DWORD CRedRatProcess::IsAppRunning(LPCTSTR lpszAppDir)
  108. {
  109. if (!lpszAppDir || !PathFileExists(lpszAppDir))
  110. return 0;
  111. std::string strAppDir = lpszAppDir;
  112. int nIndex = strAppDir.find('/');
  113. while (nIndex != std::string::npos)
  114. {
  115. strAppDir.replace(nIndex, 1, _T("\\"));
  116. nIndex = strAppDir.find('/');
  117. }
  118. nIndex = strAppDir.find_last_of(_T('\\'));
  119. if (nIndex != std::string::npos)
  120. strAppDir = strAppDir.substr(nIndex + 1);
  121. DWORD dwProcessID = 0;
  122. PROCESSENTRY32 pe32 = { 0 };
  123. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  124. if (hProcessSnap == NULL)
  125. {
  126. Global::WriteTextLog(_T("获取进程快照失败:%ld"), GetLastError());
  127. return 0;
  128. }
  129. pe32.dwSize = sizeof(PROCESSENTRY32);
  130. if (Process32First(hProcessSnap, &pe32))
  131. {
  132. do
  133. {
  134. // szExeFile只是文件名,不知道是否只有win10才这样;
  135. if (_tcsicmp(strAppDir.c_str(), pe32.szExeFile) == 0)
  136. {
  137. m_dwCurAppId = dwProcessID = pe32.th32ProcessID;
  138. break;
  139. }
  140. } while (Process32Next(hProcessSnap, &pe32));
  141. }
  142. CloseHandle(hProcessSnap);
  143. return dwProcessID;
  144. }
  145. bool CRedRatProcess::CloseApp(DWORD dwAppId)
  146. {
  147. m_dwCurAppId = GetProcessId();
  148. if (dwAppId == 0)
  149. dwAppId = m_dwCurAppId;
  150. if (dwAppId == 0)
  151. return true;
  152. HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, TRUE, dwAppId);
  153. if (hProcess == NULL)
  154. {
  155. Global::WriteTextLog(_T("打开进程失败:%ld"), GetLastError());
  156. return false;
  157. }
  158. DWORD dwError;
  159. if (!TerminateProcess(hProcess, 0))
  160. {
  161. dwError = GetLastError();
  162. CloseHandle(hProcess);
  163. hProcess = NULL;
  164. return false;
  165. }
  166. // 等待进程结束响应;
  167. if (WAIT_OBJECT_0 != WaitForSingleObject(hProcess, INFINITE))
  168. {
  169. CloseHandle(hProcess);
  170. return false;
  171. }
  172. CloseHandle(hProcess);
  173. hProcess = NULL;
  174. return true;
  175. }
  176. DWORD CRedRatProcess::GetProcessId(std::string strApp)
  177. {
  178. DWORD dwProcessID = 0;
  179. PROCESSENTRY32 pe32 = { 0 };
  180. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  181. if (hProcessSnap == NULL)
  182. {
  183. Global::WriteTextLog(_T("获取进程快照失败:%ld"), GetLastError());
  184. return 0;
  185. }
  186. pe32.dwSize = sizeof(PROCESSENTRY32);
  187. if (Process32First(hProcessSnap, &pe32))
  188. {
  189. do
  190. {
  191. // szExeFile只是文件名,不知道是否只有win10才这样;
  192. if (_tcsicmp(strApp.c_str(), pe32.szExeFile) == 0)
  193. {
  194. m_dwCurAppId = dwProcessID = pe32.th32ProcessID;
  195. break;
  196. }
  197. } while (Process32Next(hProcessSnap, &pe32));
  198. }
  199. CloseHandle(hProcessSnap);
  200. return dwProcessID;
  201. }
  202. bool CRedRatProcess::getDeviceNameList()
  203. {
  204. return getDeviceNameList(m_vtDevices);
  205. }
  206. bool CRedRatProcess::getDeviceNameList(std::vector<std::string>& vtDevice)
  207. {
  208. vtDevice.clear();
  209. std::string data;
  210. if (!Send("hubquery=\"list redrats\"\n", data))
  211. return false;
  212. if (!checkEOM(data))
  213. return false;
  214. std::string dev;
  215. int npos = -1;
  216. while (true)
  217. {
  218. npos = data.find_first_of('\n');
  219. if (npos == std::string::npos)
  220. break;
  221. // 去除方括号;
  222. fun_dev(vtDevice, data.substr(0, npos));
  223. data = data.substr(npos+1);
  224. }
  225. fun_dev(vtDevice, data);
  226. // 第一个设备给当前设备;
  227. if (vtDevice.size())
  228. {
  229. m_strCurDevice = *vtDevice.begin();
  230. }
  231. return true;
  232. }
  233. bool CRedRatProcess::getDatasetNameList()
  234. {
  235. return getDatasetNameList(m_vtDataset);
  236. }
  237. bool CRedRatProcess::getDatasetNameList(std::vector<std::string>& vtDataset)
  238. {
  239. vtDataset.clear();
  240. std::string data;
  241. if (!Send("hubquery=\"list datasets\"\n", data))
  242. return false;
  243. if (!checkEOM(data))
  244. return false;
  245. int npos = -1;
  246. while (true)
  247. {
  248. npos = data.find_first_of('\n');
  249. if (npos == std::string::npos)
  250. break;
  251. vtDataset.push_back(data.substr(0, npos));
  252. data = data.substr(npos + 1);
  253. }
  254. vtDataset.push_back(data);
  255. // 第一个设备给当前设备;
  256. if (vtDataset.size())
  257. {
  258. m_strCurDataset = *vtDataset.begin();
  259. }
  260. return true;
  261. }
  262. bool CRedRatProcess::loadSignalDataSet(std::string file)
  263. {
  264. std::string data;
  265. std::string send_data = "hubquery=\"load ir-file\",file=\"";
  266. send_data.append(file);
  267. send_data.append("\"\n");
  268. // 加载数据集xml文件,以前的会被删除;
  269. if (!Send(send_data, data))
  270. return false;
  271. if (_tcsicmp(data.c_str(), _T("OK\n")) == 0)
  272. {
  273. m_strCurDataset.clear();
  274. getDatasetNameList();
  275. return true;
  276. }
  277. return false;
  278. }
  279. bool CRedRatProcess::addSignalDataSet(std::string file)
  280. {
  281. std::string data;
  282. std::string send_data = "hubquery=\"add ir-file\",file=\"";
  283. send_data.append(file);
  284. send_data.append("\"\n");
  285. // 添加信息数据集,同名会被覆盖;
  286. if (!Send(send_data, data))
  287. return false;
  288. return !_tcsicmp(data.c_str(), _T("OK\n"));
  289. }
  290. //
  291. // bool CRedRatProcess::getSignalsName(std::string dataset)
  292. // {
  293. // return getSignalsName(m_vtSignals, dataset);
  294. // }
  295. bool CRedRatProcess::getSignalsName(std::vector<std::string>& vtSignals, std::string dataset)
  296. {
  297. std::string data;
  298. std::string send_data = "hubquery=\"list signals in dataset\",dataset=\"";
  299. if (dataset.size() == 0)
  300. {// 加载默认;
  301. if (m_strCurDataset.size() == 0)
  302. getDatasetNameList();
  303. dataset = m_strCurDataset;
  304. }
  305. if (dataset.size() == 0)
  306. return false;
  307. send_data.append(dataset);
  308. send_data.append("\"\n");
  309. if (!Send(send_data, data))
  310. return false;
  311. if (!checkEOM(data))
  312. return false;
  313. int npos = -1;
  314. while (true)
  315. {
  316. npos = data.find_first_of('\n');
  317. if (npos == std::string::npos)
  318. break;
  319. vtSignals.push_back(data.substr(0, npos));
  320. data = data.substr(npos + 1);
  321. }
  322. vtSignals.push_back(data);
  323. return true;
  324. }
  325. std::string CRedRatProcess::getSignalsName(std::string dataset /* = "" */)
  326. {
  327. std::string data;
  328. std::string send_data = "hubquery=\"list signals in dataset\",dataset=\"";
  329. if (dataset.size() == 0)
  330. {// 加载默认;
  331. if (m_strCurDataset.size() == 0)
  332. getDatasetNameList();
  333. dataset = m_strCurDataset;
  334. }
  335. if (dataset.size() == 0)
  336. {
  337. return data;
  338. }
  339. send_data.append(dataset);
  340. send_data.append("\"\n");
  341. if (!Send(send_data, data))
  342. {
  343. return std::string();
  344. }
  345. if (!checkEOM(data))
  346. {
  347. return std::string();
  348. }
  349. return data;
  350. }
  351. bool CRedRatProcess::sendSignal(std::string signal, int send_times, int sleep_time)
  352. {
  353. GET_IRC_DATA;
  354. std::string data;
  355. TCHAR szSendSignal[256] = { 0 };
  356. _stprintf_s(szSendSignal, SEND_SG_FORMAT, m_strCurDevice.c_str(), m_strCurDataset.c_str(), signal.c_str());
  357. for (; send_times > 0; send_times--)
  358. {
  359. if (!Send(szSendSignal, data))
  360. return false;
  361. if (_tcsicmp(data.c_str(), _T("OK\n")))
  362. return false;
  363. Sleep(sleep_time);
  364. }
  365. return true;
  366. }
  367. bool CRedRatProcess::sendSignals(const char(*signals)[10], int nCount, int sleep_time)
  368. {
  369. GET_IRC_DATA;
  370. std::string data;
  371. TCHAR szSendSignal[256] = { 0 };
  372. for (int i = 0; i < nCount; i++)
  373. {
  374. //_stprintf_s(szSendSignal, SEND_SG_FORMAT, m_strCurDevice.c_str(), m_strCurDataset.c_str(), signals[i]);
  375. _stprintf_s(szSendSignal, SEND_SG_FORMAT, m_strCurDevice.c_str(), m_strCurDataset.c_str(), signals++);
  376. if (!Send(szSendSignal, data))
  377. return false;
  378. if (_tcsicmp(data.c_str(), _T("OK\n")))
  379. return false;
  380. Sleep(sleep_time);
  381. }
  382. return true;
  383. }
  384. bool CRedRatProcess::sendSignals(std::string signals[], int nCount, int sleep_time)
  385. {
  386. GET_IRC_DATA;
  387. std::string data;
  388. TCHAR szSendSignal[256] = { 0 };
  389. for (int i = 0; i < nCount; i++)
  390. {
  391. //_stprintf_s(szSendSignal, SEND_SG_FORMAT, m_strCurDevice.c_str(), m_strCurDataset.c_str(), signals[i].c_str());
  392. _stprintf_s(szSendSignal, SEND_SG_FORMAT, m_strCurDevice.c_str(), m_strCurDataset.c_str(), (*signals++).c_str());
  393. if (!Send(szSendSignal, data))
  394. return false;
  395. if (_tcsicmp(data.c_str(), _T("OK\n")))
  396. return false;
  397. Sleep(sleep_time);
  398. }
  399. return true;
  400. }
  401. bool CRedRatProcess::sendSignals(std::vector<std::string>& vtSignals, int sleep_time)
  402. {
  403. GET_IRC_DATA;
  404. std::string data;
  405. TCHAR szSendSignal[256] = { 0 };
  406. for (std::vector<std::string>::iterator signal = vtSignals.begin(); signal != vtSignals.end(); signal++)
  407. {
  408. _stprintf_s(szSendSignal, SEND_SG_FORMAT, m_strCurDevice.c_str(), m_strCurDataset.c_str(), signal->c_str());
  409. if (!Send(szSendSignal, data))
  410. return false;
  411. if (_tcsicmp(data.c_str(), _T("OK\n")))
  412. return false;
  413. Sleep(sleep_time);
  414. }
  415. return true;
  416. }
  417. bool CRedRatProcess::sendRepeatsSignal(std::string signal, int repeat_time)
  418. {
  419. GET_IRC_DATA;
  420. std::string data;
  421. TCHAR szSendSignal[256] = { 0 };
  422. _stprintf_s(szSendSignal, SEND_SG_REP_FORMAT, m_strCurDevice.c_str(), m_strCurDataset.c_str(), signal.c_str(), repeat_time);
  423. if (!Send(szSendSignal, data))
  424. {
  425. Global::WriteTextLog(_T("发送重复信号失败:%s"), data.c_str());
  426. return false;
  427. }
  428. if (!_tcsicmp(data.c_str(), _T("OK\n")))
  429. return true;
  430. Global::WriteTextLog(_T("发送重复信号失败:%s"), data.c_str());
  431. return false;
  432. }
  433. bool CRedRatProcess::sendDurationSignal(std::string signal, int duration_time)
  434. {
  435. GET_IRC_DATA;
  436. std::string data;
  437. TCHAR szSendSignal[256] = { 0 };
  438. _stprintf_s(szSendSignal, SEND_SG_DUR_FORMAT, m_strCurDevice.c_str(), m_strCurDataset.c_str(), signal.c_str(), duration_time);
  439. if (!Send(szSendSignal, data))
  440. return false;
  441. return !_tcsicmp(data.c_str(), _T("OK\n"));
  442. }
  443. bool CRedRatProcess::sendBothSignal(std::string signal, int repeat_time, int duration_time)
  444. {
  445. GET_IRC_DATA;
  446. std::string data;
  447. TCHAR szSendSignal[256] = { 0 };
  448. _stprintf_s(szSendSignal, SEND_SG_BOTH_FORMAT, m_strCurDevice.c_str(), m_strCurDataset.c_str(), signal.c_str(), repeat_time, duration_time);
  449. if (!Send(szSendSignal, data))
  450. return false;
  451. return !_tcsicmp(data.c_str(), _T("OK\n"));
  452. }