瀏覽代碼

SATDevices修改:网络adb也有offline的情况,此情况只能使用adb reconnect ip才能重连成功,使用adb connect据说是不行的。

scbc.sat2 5 年之前
父節點
當前提交
a7fb5cbd58

+ 69 - 81
SATService/SATService/SATDevices.cpp

@@ -111,7 +111,22 @@ void CSATDevices::StartWork()
 
 void CSATDevices::EndofWork()
 {
+	// 设置事件有信号;
+	if ( m_hEvent )
+		SetEvent(m_hEvent);
+
+	// 等待线程结束;
+	if ( m_hWorkThread ) {
+		WaitForSingleObject(m_hWorkThread, INFINITE);
+		CloseHandle(m_hWorkThread);
+		m_hWorkThread = NULL;
+	}
 
+	// 关闭事件句柄;
+	if ( m_hEvent ) {
+		CloseHandle(m_hEvent);
+		m_hEvent = NULL;
+	}
 }
 
 DWORD CSATDevices::WorkThread(LPVOID lpVoid)
@@ -124,7 +139,9 @@ DWORD CSATDevices::WorkThread(LPVOID lpVoid)
 
 		// 刷新当前设备列表;
 		std::vector<STDevice> vtDevices;
+#ifdef _DEBUG
 		printf("GetCurrentDevices\n");
+#endif
 		// 获取当前设备列表;
 		GetCurrentDevices(vtDevices);
 
@@ -136,17 +153,26 @@ DWORD CSATDevices::WorkThread(LPVOID lpVoid)
 				continue;
 			}
 
-			if ( IsAndroidOffline(*it, vtDevices) ) {
-				printf("IsAndroidOffline\n");
+			if ( IsDevicesOffline(*it, vtDevices) ) {
+#ifdef _DEBUG
+				printf("IsDevicesOffline\n");
+#endif
 				if ( GetTickCount64() - it->ulOfflineTime > s_ulEraseDuration ) {
 					// 移除设备(需要线程加锁);
 					it = s_vtDevices.erase(it);
 				} else {
 					if ( it->nType == Reticle ) {
-						// 设备下线,尝试重新连接;
-						std::string str = "adb connect ";
-						str.append(it->strName);
-						WinExec(str.c_str(), SW_HIDE);
+						if ( it->nStatus == Offline ) {
+							// offline只能reconnect才能重连;
+							std::string str = "adb reconnect ";
+							str.append(it->strName);
+							WinExec(str.c_str(), SW_HIDE);
+						} 
+						else if ( it->nStatus == Dropline ) {
+							std::string str = "adb connect ";
+							str.append(it->strName);
+							WinExec(str.c_str(), SW_HIDE);
+						}
 					} else if ( it->nType == Usb ) {
 						// 如果是usb的话,可能要kill-server,再全部重连接;
 						WinExec("adb kill-server", SW_HIDE);
@@ -192,7 +218,8 @@ void CSATDevices::AddReticleDevices(std::string ip)
 	stDevice.nType = Reticle;
 	stDevice.strName = ip;
 	stDevice.ulOfflineTime = 0;
-	stDevice.nStatus = 0;
+	stDevice.nStatus = Online;
+	stDevice.nUsageState = Idle;
 
 	AutoThreadSection ats(&s_ThreadSection);
 	if (!IsDeviceExist(stDevice))
@@ -205,7 +232,8 @@ void CSATDevices::AddVirtualDevices(std::string name)
 	stDevice.nType = Virtual;
 	stDevice.strName = name;
 	stDevice.ulOfflineTime = 0;
-	stDevice.nStatus = 0;
+	stDevice.nStatus = Online;
+	stDevice.nUsageState = Idle;
 
 	AutoThreadSection ats(&s_ThreadSection);
 	if (!IsDeviceExist(stDevice))
@@ -226,14 +254,18 @@ bool CSATDevices::IsDeviceExist(STDevice &stDevice)
 	return bExist;
 }
 
-bool CSATDevices::IsAndroidOffline(STDevice &stDevice, std::vector<STDevice> &vtDevices )
+bool CSATDevices::IsDevicesOffline(STDevice &stDevice, std::vector<STDevice> &vtDevices )
 {
 	bool bOffline = true;
 	// 在当前设备列表查询;
 	std::vector<STDevice>::iterator it = vtDevices.begin();
 	for(; it != vtDevices.end(); it++ ) {
 		if ( _tcscmp(it->strName.c_str(), stDevice.strName.c_str()) == 0 ) {
-			bOffline = false;
+			// 同步状态;
+			stDevice.nStatus = it->nStatus;
+			// 设备是否offline或dropline;
+			if ( it->nStatus != Offline && it->nStatus != Dropline)
+				bOffline = false;
 			break;
 		}
 	}
@@ -241,11 +273,14 @@ bool CSATDevices::IsAndroidOffline(STDevice &stDevice, std::vector<STDevice> &vt
 	if ( bOffline ) {
 		if ( stDevice.ulOfflineTime == 0 ) {
 			// 首次检测到离线;
+			if ( stDevice.nStatus != Offline )
+				stDevice.nStatus = Dropline;
 			stDevice.ulOfflineTime = GetTickCount64();
 		}
 	} else {
 		// 若重新连接,重置离线时间;
 		if ( stDevice.ulOfflineTime ) {
+			stDevice.nStatus = Online;
 			stDevice.ulOfflineTime = 0;
 		}
 	}	
@@ -253,76 +288,24 @@ bool CSATDevices::IsAndroidOffline(STDevice &stDevice, std::vector<STDevice> &vt
 	return bOffline;
 }
 
-bool CSATDevices::IsAndroidOffline(std::string strDeviceName, std::vector<STDevice> &vtDevices )
-{
-	bool bOffline = true;
-	// 在当前设备列表查询;
-	std::vector<STDevice>::iterator it = vtDevices.begin();
-	for(; it != vtDevices.end(); it++ ) {
-		if ( _tcscmp(it->strName.c_str(), strDeviceName.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(STDevice &stDevice)
-{
-	bool bNewAndroid = true;
-	std::vector<STDevice>::iterator it = s_vtDevices.begin();
-	for(; it != s_vtDevices.end(); it++ ) {
-		if ( _tcscmp(it->strName.c_str(), stDevice.strName.c_str()) == 0 ) {
-			bNewAndroid = false;
-			break;
-		}
-	}
-
-	// 如果是新设备(一般是usb连接)
-	if ( bNewAndroid ) {
-		AutoThreadSection ats(&s_ThreadSection);
-		s_vtDevices.push_back(stDevice);
-	}
-
-	return bNewAndroid;
-}
-
-bool CSATDevices::IsNewUsbAndroid(STDevice &stDevice)
+bool CSATDevices::IsNewDevices(STDevice &stDevice)
 {
-	if ( stDevice.nType != Usb )
-		return false;
-
-	bool bNewAndroid = true;
+	bool bNewDevices = true;
 	std::vector<STDevice>::iterator it = s_vtDevices.begin();
 	for(; it != s_vtDevices.end(); it++ ) {
-		if ( it->nType != Usb )
-			continue;
 		if ( _tcscmp(it->strName.c_str(), stDevice.strName.c_str()) == 0 ) {
-			bNewAndroid = false;
+			bNewDevices = false;
 			break;
 		}
 	}
 
 	// 如果是新设备(一般是usb连接)
-	if ( bNewAndroid ) {
+	if ( bNewDevices ) {
 		AutoThreadSection ats(&s_ThreadSection);
 		s_vtDevices.push_back(stDevice);
 	}
 
-	return bNewAndroid;
+	return bNewDevices;
 }
 
 void CSATDevices::GetCurrentDevices(std::vector<STDevice> &vtDevices)
@@ -331,21 +314,26 @@ void CSATDevices::GetCurrentDevices(std::vector<STDevice> &vtDevices)
 	std::vector<std::string> vtLine;
 	Split(vtLine, strLines, "\r\n");
 	int npos = -1;
-	// 需要在此处过滤掉offline的usb设备,不要加入vt中;
+	// offline设备也要加入,不区分usb或reticle;
 	for ( std::vector<std::string>::iterator it = vtLine.begin(); it != vtLine.end(); it++ ) {
-		// 只获取在线设备,离线设备过滤;
-		if ( (npos = it->find("	device")) != std::string::npos ) {
-			STDevice stDevice;
-			if ( it->find(":5555") == std::string::npos )
-				stDevice.nType = Usb;
-			else
-				stDevice.nType = Reticle;
-			stDevice.ulOfflineTime = 0;
-			stDevice.strName = it->substr(0, npos);
-			// 新设备否(一般用于usb设备)
-			IsNewAndroid(stDevice);
-			vtDevices.push_back(stDevice);
-		}
+		STDevice stDevice;
+		// 设备类型;
+		if ( it->find(":5555") == std::string::npos )
+			stDevice.nType = Usb;
+		else
+			stDevice.nType = Reticle;
+		stDevice.ulOfflineTime = 0;
+		// 设备状态;
+		if ( (npos = it->find("	device")) != std::string::npos )
+			stDevice.nStatus = Online;
+		else if ( (npos = it->find("	offline")) != std::string::npos )
+			stDevice.nStatus = Offline;
+		// 获取设备名;
+		stDevice.strName = it->substr(0, npos);
+		// 新设备否(一般用于usb设备)
+		IsNewDevices(stDevice);
+		// 压入容器保存;
+		vtDevices.push_back(stDevice);
 	}
 }
 

+ 17 - 7
SATService/SATService/SATDevices.h

@@ -12,13 +12,25 @@ enum DEVICE_TYPE{
 	Reticle
 };
 
+enum DEVICE_STATUS{
+	Online,
+	Offline,
+	Dropline
+};
+
+enum DEVICE_USAGE_STATUS{
+	Idle,
+	InUse
+};
+
 typedef struct __DEVICE__
 {
-	int 			nType;		// 0=虚拟, 1=usb, 2=网线;
-	std::string 	strName;	// 设备名称(IP:端口号);
+	int 			nType;			// 0=虚拟, 1=usb, 2=网线;
+	std::string 	strName;		// 设备名称(IP:端口号);
 	// 离线时刻:超过指定时长删除该设备,线程实时刷新离线时刻;
 	ULONGLONG		ulOfflineTime;
-	int				nStatus;		// 设备状态:0表示空闲,1表示繁忙;
+	int				nStatus;		// 设备状态:0=在线,1=offline, 2=掉线(drop line);
+	int				nUsageState;	// 设备使用状态:0=空闲,1=使用;
 }STDevice, *pSTDevice;
 
 // 安卓设备adb连接:USB连接、网线连接;
@@ -86,11 +98,9 @@ public:
 	// 设备是否存在;
 	static bool IsDeviceExist(STDevice &stDevice);
 	// 设备是否下线;
-	static bool IsAndroidOffline(STDevice &stDevice, std::vector<STDevice> &vtDevices);
-	static bool IsAndroidOffline(std::string strDeviceName, std::vector<STDevice> &vtDevices );
+	static bool IsDevicesOffline(STDevice &stDevice, std::vector<STDevice> &vtDevices);
 	// 是否新设备;
-	static bool IsNewAndroid(STDevice &stDevice);
-	static bool IsNewUsbAndroid(STDevice &stDevice);
+	static bool IsNewDevices(STDevice &stDevice);
 	// 获取当前设备列表;
 	static void GetCurrentDevices(std::vector<STDevice> &vtDevices);
 	// 重连所有设备;

+ 20 - 23
SATService/SATService/SATExecutor.cpp

@@ -2,6 +2,7 @@
 #include "SATExecutor.h"
 #include "ScriptExecutor.h"
 #include "CharEncoding.h"
+#include "SATDevices.h"
 
 CSATExecutor::CSATExecutor(void)
 {
@@ -20,11 +21,9 @@ bool CSATExecutor::IsTaskExist(SATParameters::STTask &task)
 {
 	bool found = false;
 	std::list<SATParameters::STTask>::iterator it = m_vtTask.begin();
-	for ( ; it != m_vtTask.end(); it++ )
-	{
+	for ( ; it != m_vtTask.end(); it++ ) {
 		// 不要使用nTaskId,这个值没啥用;
-		if ( it->Id == task.Id )
-		{
+		if ( it->Id == task.Id ) {
 			found = true;
 			break;
 		}
@@ -36,10 +35,8 @@ bool CSATExecutor::IsTaskExist(SATParameters::STTask &task)
 SATParameters::STTask* CSATExecutor::IsThereATaskInProcess()
 {
 	std::list<SATParameters::STTask>::iterator it = m_vtTask.begin();
-	for ( ; it != m_vtTask.end(); it++ )
-	{
-		if ( it->_nExecutionState == 1 )
-		{
+	for ( ; it != m_vtTask.end(); it++ ) {
+		if ( it->_nExecutionState == 1 ) {
 			return &(*it);
 		}
 	}
@@ -50,10 +47,8 @@ SATParameters::STTask* CSATExecutor::IsThereATaskInProcess()
 SATParameters::STTask* CSATExecutor::GetFreeTask()
 {
 	std::list<SATParameters::STTask>::iterator it = m_vtTask.begin();
-	for ( ; it != m_vtTask.end(); it++ )
-	{
-		if ( it->_nExecutionState == 0 )
-		{
+	for ( ; it != m_vtTask.end(); it++ ) {
+		if ( it->_nExecutionState == 0 ) {
 			return &(*it);
 		}
 	}
@@ -64,10 +59,8 @@ SATParameters::STTask* CSATExecutor::GetFreeTask()
 SATParameters::STCase* CSATExecutor::IsCaseScriptProcess(std::vector<SATParameters::STCase> &vtCases)
 {
 	std::vector<SATParameters::STCase>::iterator it = vtCases.begin();
-	for ( ; it != vtCases.end(); it++)
-	{
-		if ( it->_nExecutionState == 1 )
-		{
+	for ( ; it != vtCases.end(); it++) {
+		if ( it->_nExecutionState == 1 ) {
 			return &(*it);
 		}
 	}
@@ -78,10 +71,8 @@ SATParameters::STCase* CSATExecutor::IsCaseScriptProcess(std::vector<SATParamete
 SATParameters::STCase* CSATExecutor::GetFreeCaseScript(std::vector<SATParameters::STCase> &vtCases)
 {
 	std::vector<SATParameters::STCase>::iterator it = vtCases.begin();
-	for ( ; it != vtCases.end(); it++)
-	{
-		if ( it->_nExecutionState == 0 )
-		{
+	for ( ; it != vtCases.end(); it++) {
+		if ( it->_nExecutionState == 0 ) {
 			return &(*it);
 		}
 	}
@@ -127,20 +118,26 @@ bool CSATExecutor::Login(std::string user, std::string password, std::string act
 {
 	std::string url = Global::g_stSATConfig.szExecuteServer;
 	url.append("/ajaxInteractiveManage!executeLogin.action");
-	// 示例值;
+	// 填充数据;
 	m_stLoginReq.strUserName = user;
 	m_stLoginReq.strStatus = bLogin ? "0" : "1";   // 0表示登录;
 	m_stLoginReq.strDeleteStatus = "";
 	m_stLoginReq.strIP = Global::GetLocalAddress();//"10.118.158.175";
 	m_stLoginReq.strStorage = "";
-	m_stLoginReq.strConnectTime = "";
+	if ( bLogin ) {
+		m_stLoginReq.strDisconnectTime = "";
+		m_stLoginReq.strConnectTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));		
+	} else { 
+		m_stLoginReq.strConnectTime = "";
+		m_stLoginReq.strDisconnectTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
+	}
 	// 执行器名称;
 	m_stLoginReq.strExecuteName = m_strActuatorName = actuator;
 	if ( Global::g_vtMac.size() )
 		m_stLoginReq.strMAC = Global::g_vtMac[0].szMacAddress; // 取第一个MAC地址;
 	else
 		m_stLoginReq.strMAC = "";
-	m_stLoginReq.strDisconnectTime = "";
+	
 	m_stLoginReq.strPassword = password;
 	m_stLoginReq.strCPU = "";
 

+ 3 - 1
SATService/SATService/SATProtocol.h

@@ -13,7 +13,9 @@ namespace SATProtocol{
 		// É豸;
 		CMD_ADD_DEVICE,
 		CMD_DEL_DEVICE,
-		CMD_QUERY_DEVICES		
+		CMD_QUERY_DEVICES,
+		// ²éѯÈÎÎñ;
+		CMD_QUERY_TASK
 	};
 
 	//////////////////////////////////////////////////////////////////////////

+ 6 - 3
SATService/SATService/SATService.cpp

@@ -83,7 +83,7 @@ int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
 #if 1
 		// 启动SAT TCP服务端;
 		CSATTCPServer::GetInstance()->LoadSocketLib();
-		CSATTCPServer::GetInstance()->Start(5599);
+		CSATTCPServer::GetInstance()->Start(5588);
 #endif
 
 #if 1
@@ -92,12 +92,15 @@ int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
 
 		// 指定时间停止;
 		ULONGLONG ulTickCount = GetTickCount64();
-		while (GetTickCount64() - ulTickCount < 1800000 )
+		//while (GetTickCount64() - ulTickCount < 1800000 )
+		while(1)
 		{
 			//CSATExecutor::GetInstance()->Login("superAdmin", "123456");
 			//printf("倒计时:%ld\n",GetTickCount64() - ulTickCount);
-			Sleep(100);
+			Sleep(1000);
 		}
+
+		CSATTCPServer::GetInstance()->Stop();
 		
 		return 0;
 #endif

+ 3 - 3
SATService/tcp_client.py

@@ -10,7 +10,7 @@ import binascii
 #创建ProHeader数据类型
 # 定义:
 DataHeader = np.dtype({'names': ['protocol', 'len', 'cmd'], 'formats': ['u1', 'u4', 'u1']}, align=False)
-UserInfo = np.dtype({'names': ['userName', 'password'], 'formats': ['|S260', '|S260']}, align=False)
+UserInfo = np.dtype({'names': ['userName', 'password', 'actuator'], 'formats': ['|S260', '|S260', '|S260']}, align=False)
 LoginResp = np.dtype({'names': ['bStatus', 'szMessage'], 'formats': ['?', '|S260']}, align=False)
 
 class BaseClient(object):
@@ -115,7 +115,7 @@ class SATClient(object):
         '''通信sock'''
         self.sock = None
         '''通信端口号'''
-        self.port = 5588
+        self.port = 5599
         '''通信状态'''
         self.constate = False
 
@@ -144,7 +144,7 @@ class SATClient(object):
                 return None
 
         '''拼装要发送的数据包'''
-        userinfo = np.array([("superAdmin", "123456")], dtype=UserInfo)
+        userinfo = np.array([("superAdmin", "123456", "SAT-Admin")], dtype=UserInfo)
 
         '''发送请求数据'''
         try: