Răsfoiți Sursa

删除SATQuerier,新设备adb处理类:CSATDevices

scbc.sat2 5 ani în urmă
părinte
comite
ac01f5c2f9

+ 2 - 2
SATService/SATService/IOCPModel.h

@@ -35,7 +35,7 @@ typedef struct _PACKAGE_
 typedef struct _REQUEST_JSON_
 {
 	int			device_id;
-	std::string		device_name;
+	std::string device_name;
 	std::string device_cmd;
 	int	device_timeout;
 }RequesJson;
@@ -43,7 +43,7 @@ typedef struct _REQUEST_JSON_
 typedef struct _RESPONSE_JSON_
 {
 	int			device_id;
-	std::string		device_name;
+	std::string device_name;
 	std::string device_cmd_result;	// ÃüÁîÖ´Ðнá¹û;
 }ResponseJson;
 

+ 287 - 0
SATService/SATService/SATDevices.cpp

@@ -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);
+	}
+}

+ 87 - 0
SATService/SATService/SATDevices.h

@@ -0,0 +1,87 @@
+#ifndef __SAT_QUERIER__
+#define __SAT_QUERIER__
+
+#pragma once
+
+#include "CritSection.h"
+
+enum DEVICE_TYPE{
+	Virtual,
+	Usb,
+	Reticle
+};
+
+// 安卓设备adb连接:USB连接、网线连接;
+typedef struct __ANDROID__ 
+{
+	int 			nType;		// 0=虚拟, 1=usb, 2=网线;
+	std::string 	strName;	// 设备名称(IP:端口号);
+	/* 
+		离线时间,如果离线时间超过10分钟,删除该设备:
+		因为线程会一直尝试连接掉线设备,如果在指定时间内都未连接上,
+		认为设备被拨或IP变了, 只对网线连接Android有效;
+	*/
+	ULONGLONG		ulOfflineTime;
+}STAndroid, *pSTAndroid;
+
+class CSATDevices
+{
+	CSATDevices(void);
+public:
+	// 单例对象模式;
+	static CSATDevices* GetInstance()
+	{
+		static CSATDevices* pInstance = NULL;
+		if ( pInstance == NULL )
+		{
+			pInstance = new CSATDevices;
+		}
+
+		return pInstance;
+	}
+
+	~CSATDevices(void);
+
+protected:
+	// 线程控制句柄;
+	HANDLE  m_hEvent;
+	// 线程句柄;
+	HANDLE	m_hWorkThread;
+
+	// 是否启动安卓设备;
+	static bool s_bEnableAndroid;
+	// 离线擦除时长,单位毫秒;
+	static ULONGLONG s_ulEraseDuration;	
+	// 线程锁;
+	static ThreadSection s_ThreadSection;
+	// 安卓设备列表;
+	static std::vector<STAndroid> s_vtAndroids;	
+
+	// 其他接口;
+public:
+	// 工作开始函数;	
+	void StartWork();
+	// 工作结束函数;
+	void EndofWork();
+
+	// 工作线程;
+	static DWORD WINAPI WorkThread(LPVOID lpVoid);
+
+
+	// 添加网线连接设备;
+	static void AddReticleDevices(std::string ip);
+	// 设备是否存在;
+	static bool IsDeviceExist(STAndroid &stAndroid);
+	// 设备是否下线;
+	static bool IsAndroidOffline(STAndroid &stAndroid, std::vector<STAndroid> &vtAndroids );
+	static bool IsAndroidOffline(std::string strAndroidName, std::vector<STAndroid> &vtAndroids );
+	// 是否新设备;
+	static bool IsNewAndroid(STAndroid &stAndroid);
+	// 获取当前设备列表;
+	static void GetCurrentDevices(std::vector<STAndroid> &vtAndroids);
+	// 重连所有设备;
+	static void ReConnectAllDevices();
+
+};
+
+#endif // __SAT_QUERIER__

+ 0 - 10
SATService/SATService/SATQuerier.cpp

@@ -1,10 +0,0 @@
-#include "StdAfx.h"
-#include "SATQuerier.h"
-
-CSATQuerier::CSATQuerier(void)
-{
-}
-
-CSATQuerier::~CSATQuerier(void)
-{
-}

+ 0 - 46
SATService/SATService/SATQuerier.h

@@ -1,46 +0,0 @@
-#ifndef __SAT_QUERIER__
-#define __SAT_QUERIER__
-
-#pragma once
-
-class CSATQuerier
-{
-	CSATQuerier(void);
-public:
-	// 单例对象模式;
-	static CSATQuerier* GetInstance()
-	{
-		static CSATQuerier* pInstance = NULL;
-		if ( pInstance == NULL )
-		{
-			pInstance = new CSATQuerier;
-		}
-
-		return pInstance;
-	}
-
-	~CSATQuerier(void);
-
-protected:
-	// 线程控制句柄;
-	HANDLE  m_hEvent1;
-	// 检查服务线程句柄;
-	HANDLE	m_hCheckServices;
-	// 线程控制句柄;
-	HANDLE  m_hEvent2;
-	// 备份数据库线程句柄;
-	HANDLE	m_hBackupDatabase;
-
-	// 其他接口;
-public:
-	// 工作开始函数;	
-	void StartWork();
-	// 工作结束函数;
-	void EndofWork();
-
-	// 检测服务在线线程;
-	static DWORD WINAPI CheckSvcStatusThread(LPVOID lpVoid);
-	static DWORD WINAPI BackupDatabaseThread(LPVOID lpVoid);
-};
-
-#endif // __SAT_QUERIER__

+ 2 - 2
SATService/SATService/SATService.cpp

@@ -5,6 +5,7 @@
 #include "SATService.h"
 #include "MainProcess.h"
 #include "SATExecutor.h"
+#include "SATDevices.h"
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
@@ -24,7 +25,6 @@ void CALLBACK WorkStart()
 	WindowsService::GetDebugPriv();
 
 #ifdef _DEBUG
-	//Sleep(20000);
 	CSATExecutor::GetInstance()->Login();
 	CSATExecutor::GetInstance()->UpdateDevice();
 #endif
@@ -58,7 +58,7 @@ int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
 	{
 		// TODO: 在此处为应用程序的行为编写代码。
 		Global::GetIniInfo();
-#if 1
+#ifdef _DEBUG
 		WorkStart();
 		system("pause");
 		return 0;

+ 4 - 4
SATService/SATService/SATService.vcproj

@@ -381,19 +381,19 @@
 				>
 			</File>
 			<File
-				RelativePath=".\SATExecutor.cpp"
+				RelativePath=".\SATDevices.cpp"
 				>
 			</File>
 			<File
-				RelativePath=".\SATExecutor.h"
+				RelativePath=".\SATDevices.h"
 				>
 			</File>
 			<File
-				RelativePath=".\SATQuerier.cpp"
+				RelativePath=".\SATExecutor.cpp"
 				>
 			</File>
 			<File
-				RelativePath=".\SATQuerier.h"
+				RelativePath=".\SATExecutor.h"
 				>
 			</File>
 			<File