IRControl.cpp 12 KB

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