|
@@ -0,0 +1,287 @@
|
|
|
+#include "StdAfx.h"
|
|
|
+#include "SATDevices.h"
|
|
|
+
|
|
|
+bool CSATDevices::s_bEnableAndroid = false;
|
|
|
+ULONGLONG CSATDevices::s_ulEraseDuration = 300000;
|
|
|
+ThreadSection CSATDevices::s_ThreadSection;
|
|
|
+std::vector<STAndroid> CSATDevices::s_vtAndroids;
|
|
|
+
|
|
|
+void Split(vector<std::string>& vtLine, std::string strLines, const std::string strSplit)
|
|
|
+{
|
|
|
+ if (strLines.size() == 0 || strSplit.size() == 0)
|
|
|
+ return;
|
|
|
+
|
|
|
+ int nIndex = 0;
|
|
|
+ vtLine.clear();
|
|
|
+ std::string strtmp;
|
|
|
+ while (std::string::npos != (nIndex = strLines.find(strSplit))) {
|
|
|
+ strtmp = strLines.substr(0, nIndex);
|
|
|
+ if (strtmp.size())vtLine.push_back(strtmp);
|
|
|
+ strLines = strLines.substr(nIndex + strSplit.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (strLines.size())
|
|
|
+ vtLine.push_back(strLines);
|
|
|
+}
|
|
|
+
|
|
|
+std::string ExecuteCMD(std::string cmd)
|
|
|
+{
|
|
|
+ SECURITY_ATTRIBUTES sa = {0};
|
|
|
+ HANDLE hRead = NULL, hWrite = NULL;
|
|
|
+ sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
+ sa.lpSecurityDescriptor = NULL;
|
|
|
+ sa.bInheritHandle = TRUE;
|
|
|
+
|
|
|
+ // 创建管道;
|
|
|
+ if ( !CreatePipe(&hRead, &hWrite, &sa, 1024) )
|
|
|
+ {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ STARTUPINFO si = {0};
|
|
|
+ PROCESS_INFORMATION pi = {0};
|
|
|
+ si.cb = sizeof(STARTUPINFO);
|
|
|
+ GetStartupInfo(&si);
|
|
|
+
|
|
|
+ si.hStdError = hWrite;
|
|
|
+ si.hStdOutput = hWrite;
|
|
|
+ si.wShowWindow = SW_HIDE;
|
|
|
+ si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
|
|
|
+ // 创建子进程;
|
|
|
+ TCHAR szCommand[MAX_PATH] = {0};
|
|
|
+ _tcscpy_s(szCommand,cmd.c_str());
|
|
|
+ if ( !CreateProcess(NULL, szCommand, NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi) )
|
|
|
+ {
|
|
|
+ CloseHandle(hRead);
|
|
|
+ CloseHandle(hWrite);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 等待进程结束;
|
|
|
+ WaitForSingleObject(pi.hProcess, INFINITE);
|
|
|
+
|
|
|
+ // 释放所有句柄;
|
|
|
+ CloseHandle(pi.hProcess);
|
|
|
+ CloseHandle(pi.hThread);
|
|
|
+ CloseHandle(hWrite);
|
|
|
+
|
|
|
+ // 等待缓冲写入;
|
|
|
+ Sleep(200);
|
|
|
+ // 读取内容;
|
|
|
+ DWORD dwBytesRead;
|
|
|
+ TCHAR szBuffer[1024] = {0};
|
|
|
+ ReadFile(hRead, szBuffer, 1024, &dwBytesRead, NULL);
|
|
|
+ CloseHandle(hRead);
|
|
|
+
|
|
|
+ // 返回结果;
|
|
|
+ return std::string(szBuffer);
|
|
|
+}
|
|
|
+
|
|
|
+CSATDevices::CSATDevices(void)
|
|
|
+{
|
|
|
+ m_hEvent = NULL;
|
|
|
+ m_hWorkThread = NULL;
|
|
|
+}
|
|
|
+
|
|
|
+CSATDevices::~CSATDevices(void)
|
|
|
+{
|
|
|
+}
|
|
|
+
|
|
|
+void CSATDevices::StartWork()
|
|
|
+{
|
|
|
+ m_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
+ if ( m_hEvent == NULL )
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ m_hWorkThread = CreateThread(NULL, 0, WorkThread, this, 0, NULL);
|
|
|
+ if ( m_hWorkThread == NULL)
|
|
|
+ {
|
|
|
+ SetEvent(m_hEvent);
|
|
|
+ CloseHandle(m_hEvent);
|
|
|
+ CloseHandle(m_hWorkThread);
|
|
|
+ m_hEvent = NULL;
|
|
|
+ m_hWorkThread = NULL;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void CSATDevices::EndofWork()
|
|
|
+{
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+DWORD CSATDevices::WorkThread(LPVOID lpVoid)
|
|
|
+{
|
|
|
+ CSATDevices *that = (CSATDevices*)lpVoid;
|
|
|
+ do
|
|
|
+ {
|
|
|
+ if ( !s_bEnableAndroid )
|
|
|
+ continue;
|
|
|
+
|
|
|
+ // 刷新当前设备列表;
|
|
|
+ std::vector<STAndroid> vtAndroids;
|
|
|
+ // 获取当前设备列表;
|
|
|
+ GetCurrentDevices(vtAndroids);
|
|
|
+
|
|
|
+ // 遍历设备列表是否有下线设备;
|
|
|
+ std::vector<STAndroid>::iterator it = s_vtAndroids.begin();
|
|
|
+ for ( ;it != s_vtAndroids.end(); ) {
|
|
|
+ if ( IsAndroidOffline(*it, vtAndroids) ) {
|
|
|
+ if ( GetTickCount64() - it->ulOfflineTime > s_ulEraseDuration ) {
|
|
|
+ // 移除设备(需要线程加锁);
|
|
|
+ it = s_vtAndroids.erase(it);
|
|
|
+ } else {
|
|
|
+ if ( it->nType == 1 ) {
|
|
|
+ // 设备下线,尝试重新连接;
|
|
|
+ //cmd("adb connect"+it->strName);
|
|
|
+ } else {
|
|
|
+ // 如果是usb的话,可能要kill-server,再全部重连接;
|
|
|
+ //cmd("adb kill-server");
|
|
|
+ // 重连所有;
|
|
|
+ ReConnectAllDevices();
|
|
|
+ }
|
|
|
+
|
|
|
+ it++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } while (WaitForSingleObject(that->m_hEvent, 200) == WAIT_TIMEOUT);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void CSATDevices::AddReticleDevices(std::string ip)
|
|
|
+{
|
|
|
+ STAndroid stAndroid;
|
|
|
+ stAndroid.nType = Reticle;
|
|
|
+ stAndroid.strName = ip;
|
|
|
+ stAndroid.ulOfflineTime = 0;
|
|
|
+
|
|
|
+ AutoThreadSection ats(&s_ThreadSection);
|
|
|
+ if (!IsDeviceExist(stAndroid))
|
|
|
+ s_vtAndroids.push_back(stAndroid);
|
|
|
+}
|
|
|
+
|
|
|
+bool CSATDevices::IsDeviceExist(STAndroid &stAndroid)
|
|
|
+{
|
|
|
+ bool bExist = false;
|
|
|
+ std::vector<STAndroid>::iterator it = s_vtAndroids.begin();
|
|
|
+ for(; it != s_vtAndroids.end(); it++ ) {
|
|
|
+ if ( _tcscmp(it->strName.c_str(), stAndroid.strName.c_str()) == 0 ) {
|
|
|
+ bExist = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return bExist;
|
|
|
+}
|
|
|
+
|
|
|
+bool CSATDevices::IsAndroidOffline(STAndroid &stAndroid, std::vector<STAndroid> &vtAndroids )
|
|
|
+{
|
|
|
+ bool bOffline = true;
|
|
|
+ // 在当前设备列表查询;
|
|
|
+ std::vector<STAndroid>::iterator it = vtAndroids.begin();
|
|
|
+ for(; it != vtAndroids.end(); it++ ) {
|
|
|
+ if ( _tcscmp(it->strName.c_str(), stAndroid.strName.c_str()) == 0 ) {
|
|
|
+ bOffline = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( bOffline ) {
|
|
|
+ if ( it->ulOfflineTime == 0 ) {
|
|
|
+ // 首次检测到离线;
|
|
|
+ it->ulOfflineTime = GetTickCount64();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 若重新连接,重置离线时间;
|
|
|
+ if ( it->ulOfflineTime ) {
|
|
|
+ it->ulOfflineTime = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return bOffline;
|
|
|
+}
|
|
|
+
|
|
|
+bool CSATDevices::IsAndroidOffline(std::string strAndroidName, std::vector<STAndroid> &vtAndroids )
|
|
|
+{
|
|
|
+ bool bOffline = true;
|
|
|
+ // 在当前设备列表查询;
|
|
|
+ std::vector<STAndroid>::iterator it = vtAndroids.begin();
|
|
|
+ for(; it != vtAndroids.end(); it++ ) {
|
|
|
+ if ( _tcscmp(it->strName.c_str(), strAndroidName.c_str()) == 0 ) {
|
|
|
+ bOffline = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( bOffline ) {
|
|
|
+ if ( it->ulOfflineTime == 0 ) {
|
|
|
+ // 首次检测到离线;
|
|
|
+ it->ulOfflineTime = GetTickCount64();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 若重新连接,重置离线时间;
|
|
|
+ if ( it->ulOfflineTime ) {
|
|
|
+ it->ulOfflineTime = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return bOffline;
|
|
|
+}
|
|
|
+
|
|
|
+bool CSATDevices::IsNewAndroid(STAndroid &stAndroid)
|
|
|
+{
|
|
|
+ bool bNewAndroid = true;
|
|
|
+ std::vector<STAndroid>::iterator it = s_vtAndroids.begin();
|
|
|
+ for(; it != s_vtAndroids.end(); it++ ) {
|
|
|
+ if ( _tcscmp(it->strName.c_str(), stAndroid.strName.c_str()) == 0 ) {
|
|
|
+ bNewAndroid = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果是新设备(一般是usb连接)
|
|
|
+ if ( bNewAndroid ) {
|
|
|
+ AutoThreadSection ats(&s_ThreadSection);
|
|
|
+ if (!IsDeviceExist(stAndroid))
|
|
|
+ s_vtAndroids.push_back(stAndroid);
|
|
|
+ }
|
|
|
+
|
|
|
+ return bNewAndroid;
|
|
|
+}
|
|
|
+
|
|
|
+void CSATDevices::GetCurrentDevices(std::vector<STAndroid> &vtAndroids)
|
|
|
+{
|
|
|
+ std::string strLines = "";//ExecuteCmd("adb devices");
|
|
|
+ std::vector<std::string> vtLine;
|
|
|
+ Split(vtLine, strLines, "\r\n");
|
|
|
+ int npos = -1;
|
|
|
+ // 需要在此处过滤掉offline的usb设备,不要加入vt中;
|
|
|
+ for ( std::vector<std::string>::iterator it = vtLine.begin(); it != vtLine.end(); it++ ) {
|
|
|
+ // 只获取在线设备,离线设备过滤;
|
|
|
+ if ( (npos = it->find("devices")) != std::string::npos ) {
|
|
|
+ STAndroid stAndroid;
|
|
|
+ if ( it->find(":5555") == std::string::npos )
|
|
|
+ stAndroid.nType = 0;
|
|
|
+ else
|
|
|
+ stAndroid.nType = 1;
|
|
|
+ stAndroid.ulOfflineTime = 0;
|
|
|
+ stAndroid.strName = it->substr(0, npos);
|
|
|
+ // 新设备否(一般用于usb设备)
|
|
|
+ if ( !IsNewAndroid(stAndroid) )
|
|
|
+ vtAndroids.push_back(stAndroid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void CSATDevices::ReConnectAllDevices()
|
|
|
+{
|
|
|
+ std::vector<STAndroid>::iterator it = s_vtAndroids.begin();
|
|
|
+ for ( ;it != s_vtAndroids.end(); it++ ) {
|
|
|
+ if ( it->nType )
|
|
|
+ ;//cmd("adb connect"+it->strName);
|
|
|
+ }
|
|
|
+}
|