Selaa lähdekoodia

1、CSATDevices新增函数:DelDevices、AttachDeviceName2Buffer
2、登录接口,需要再多一参数:执行器名称;

scbc.sat2 5 vuotta sitten
vanhempi
commit
1dde641f96

+ 37 - 0
SATService/SATService/SATDevices.cpp

@@ -166,6 +166,26 @@ DWORD CSATDevices::WorkThread(LPVOID lpVoid)
 	return 0;
 }
 
+void CSATDevices::DelDevices(std::string name)
+{
+	std::string str;
+	AutoThreadSection ats(&s_ThreadSection);
+	std::vector<STDevice>::iterator it = s_vtDevices.begin();
+	for(; it != s_vtDevices.end(); it++ ) {
+		if ( _tcscmp(it->strName.c_str(), name.c_str()) == 0 ) {
+			// usb设备没办法删除,此处忽略;
+			if ( it->nType != Usb) {
+				// 只有网络设备才能删除;
+				str = "adb disconnect ";
+				str.append(name);
+				WinExec(str.c_str(), SW_HIDE);
+				s_vtDevices.erase(it);
+			}
+			break;
+		}
+	}
+}
+
 void CSATDevices::AddReticleDevices(std::string ip)
 {
 	STDevice stDevice;
@@ -338,4 +358,21 @@ void CSATDevices::ReConnectAllDevices()
 			WinExec(str.c_str(), SW_HIDE);
 		}
 	}
+}
+
+int CSATDevices::AttachDeviceName2Buffer(char (&pbuff)[30][MAX_PATH])
+{
+	int count = 0;
+	if ( pbuff ) {
+		std::string str;
+		std::vector<STDevice>::iterator it = s_vtDevices.begin();
+		for ( ;it != s_vtDevices.end(); it++ ) {
+			memcpy_s(pbuff[count++], MAX_PATH, it->strName.c_str(), it->strName.size());
+			// 超过30退出;
+			if ( count == 30)
+				break;
+		}
+	}
+
+	return count;
 }

+ 5 - 0
SATService/SATService/SATDevices.h

@@ -76,6 +76,8 @@ public:
 	static DWORD WINAPI WorkThread(LPVOID lpVoid);
 
 
+	// 删除设备;
+	static void DelDevices(std::string name);
 	// 添加网线连接设备;
 	static void AddReticleDevices(std::string ip);
 	static void AddVirtualDevices(std::string name);
@@ -92,6 +94,9 @@ public:
 	// 重连所有设备;
 	static void ReConnectAllDevices();
 
+	// 附加设备名到缓存中;
+	static int AttachDeviceName2Buffer(char (&pbuff)[30][MAX_PATH]);
+
 };
 
 #endif // __SAT_QUERIER__

+ 3 - 2
SATService/SATService/SATExecutor.cpp

@@ -123,7 +123,7 @@ SATParameters::STCase* CSATExecutor::ExecuteFreeCaseScript(SATParameters::STTask
 	return pCase;
 }
 
-bool CSATExecutor::Login(std::string user, std::string password, bool bLogin /*= true*/)
+bool CSATExecutor::Login(std::string user, std::string password, std::string actuator, bool bLogin /*= true*/)
 {
 	std::string url = Global::g_stSATConfig.szExecuteServer;
 	url.append("/ajaxInteractiveManage!executeLogin.action");
@@ -134,7 +134,8 @@ bool CSATExecutor::Login(std::string user, std::string password, bool bLogin /*=
 	m_stLoginReq.strIP = Global::GetLocalAddress();//"10.118.158.175";
 	m_stLoginReq.strStorage = "";
 	m_stLoginReq.strConnectTime = "";
-	m_stLoginReq.strExecuteName = "";
+	// 执行器名称;
+	m_stLoginReq.strExecuteName = m_strActuatorName;
 	if ( Global::g_vtMac.size() )
 		m_stLoginReq.strMAC = Global::g_vtMac[0].szMacAddress; // 取第一个MAC地址;
 	else

+ 5 - 1
SATService/SATService/SATExecutor.h

@@ -52,6 +52,8 @@ protected:
 	std::vector<SATParameters::STDevice> m_vtDevice;
 	SATParameters::STLoginReq	m_stLoginReq;
 	SATParameters::STLoginResp	m_stLoginResp;
+	// 执行器名称;
+	std::string m_strActuatorName;
 
 	// 任务是否存在;
 	bool IsTaskExist(SATParameters::STTask &task);
@@ -68,10 +70,12 @@ protected:
 	SATParameters::STCase* ExecuteFreeCaseScript(SATParameters::STTask* pTask);
 public:
 	// 登录;
-	bool Login(std::string user, std::string password, bool bLogin = true);
+	bool Login(std::string user, std::string password, std::string actuator, bool bLogin = true);
 	// 登出;
 	bool Logout(std::string user, std::string password);
 	const SATParameters::STLoginResp *GetLoginResp() const{ return &m_stLoginResp;};
+	// 设置执行器名称;
+	void SetActuatorName(const char *pszActuator){ if (pszActuator) m_strActuatorName = pszActuator;};
 	// 更新设备信息;
 	bool UpdateDevice();
 	// 通知SAT任务开始;

+ 14 - 6
SATService/SATService/SATProtocol.h

@@ -9,11 +9,11 @@ namespace SATProtocol{
 	enum ProtocolCMD {
 		// 登录、登出;
 		CMD_LOGIN					= 0,
-		CMD_LOGOUT					= 1,
+		CMD_LOGOUT,
 		// 设备;
-		CMD_ADD_DEVICE				= 2,
-		CMD_DEL_DEVICE				= 3,
-		CMD_QUERY_DEVICES			= 4,
+		CMD_ADD_DEVICE,
+		CMD_DEL_DEVICE,
+		CMD_QUERY_DEVICES		
 	};
 
 	//////////////////////////////////////////////////////////////////////////
@@ -37,10 +37,10 @@ namespace SATProtocol{
 	}Package;
 
 	// 用于登录/登出;
-	typedef struct __USERINFO__
-	{
+	typedef struct __USERINFO__ {
 		char		szUserName[MAX_PATH];
 		char		szPassword[MAX_PATH];
+		char		szActuatorName[MAX_PATH];
 	}UserInfo, *pUserInfo;
 
 	// 登录返回;
@@ -51,6 +51,14 @@ namespace SATProtocol{
 		char		szMessage[MAX_PATH];
 	}LoginResp, *pLoginResp;
 
+	// 查询设备返回;
+	typedef struct __QUERY_DEVICES_RESP__ {
+		// 设备数量;
+		int			nSize;
+		// 最多只能返回30个设备;
+		char		szDevs[30][MAX_PATH];
+	}DeviceResp, *pDeviceResp;
+
 #pragma pack(pop)
 }
 

+ 1 - 1
SATService/SATService/SATService.cpp

@@ -26,7 +26,7 @@ void CALLBACK WorkStart()
 	WindowsService::GetDebugPriv();
 
 #ifdef _DEBUG
-	CSATExecutor::GetInstance()->Login("superAdmin", "123456");
+	CSATExecutor::GetInstance()->Login("superAdmin", "123456", "SAT-Admin");
 	CSATExecutor::GetInstance()->UpdateDevice();
 #endif
 	// ÆôÓÃSATÖ´ÐÐÆ÷;

+ 38 - 2
SATService/SATService/SATTCPServer.cpp

@@ -2,6 +2,7 @@
 #include "SATTCPServer.h"
 //#include "MainDlg.h"
 #include "SATExecutor.h"
+#include "SATDevices.h"
 
 // 每一个处理器上产生多少个线程(为了最大限度的提升服务器性能,详见配套文档)
 #define WORKER_THREADS_PER_PROCESSOR 2
@@ -896,7 +897,7 @@ void CSATTCPServer::_TaskProcess(PER_IO_CONTEXT* pIoContext, SATProtocol::Packag
 					// 登录;
 					SATProtocol::UserInfo *pLogin = (SATProtocol::UserInfo*)pak->buf;
 					Global::WriteTextLog(_T("User=%s, psw=%s"), pLogin->szUserName, pLogin->szPassword);
-					bool bRet = CSATExecutor::GetInstance()->Login(pLogin->szUserName, pLogin->szPassword, !pHeader->cmd);
+					bool bRet = CSATExecutor::GetInstance()->Login(pLogin->szUserName, pLogin->szPassword, pLogin->szActuatorName, !pHeader->cmd);
 
 					// 计算数据包长度;
 					long len = sizeof(SATProtocol::DataHeader)+sizeof(SATProtocol::LoginResp);
@@ -932,10 +933,45 @@ void CSATTCPServer::_TaskProcess(PER_IO_CONTEXT* pIoContext, SATProtocol::Packag
 				}
 				break;
 			case SATProtocol::CMD_ADD_DEVICE:
-				break;
 			case SATProtocol::CMD_DEL_DEVICE:
+				{
+					std::string ip = (char*)pak->buf;
+					if (pHeader->cmd == SATProtocol::CMD_DEL_DEVICE ) 
+						CSATDevices::DelDevices(ip);
+					else 
+						CSATDevices::AddReticleDevices(ip);
+
+					SATProtocol::DataHeader header;
+					header.cmd = pHeader->cmd;
+					header.len = sizeof(SATProtocol::DataHeader);
+					header.protocol = 0xAA;
+
+					send(pIoContext->m_sockAccept, (const char*)&header, sizeof(SATProtocol::DataHeader), 0);
+				}
 				break;
 			case SATProtocol::CMD_QUERY_DEVICES:
+				{
+					// 计算数据包长度;
+					long len = sizeof(SATProtocol::DataHeader)+sizeof(SATProtocol::LoginResp);
+					byte *pbuff = new byte[len];
+					memset(pbuff, 0, len);
+					SATProtocol::Package *pSendPak = (SATProtocol::Package *)pbuff;
+					pSendPak->header.protocol = 0xAA;
+					pSendPak->header.cmd = pHeader->cmd;
+					pSendPak->header.len = len;
+					// 转换pak->buf为结构体;
+					SATProtocol::DeviceResp *pDevResp = (SATProtocol::DeviceResp*)(pbuff+sizeof(SATProtocol::DataHeader));
+					memset(pDevResp->szDevs, 0, 30*MAX_PATH);
+					// 获取设备列表;
+					pDevResp->nSize = CSATDevices::AttachDeviceName2Buffer(pDevResp->szDevs);
+
+					// 返回给客户端;
+					send(pIoContext->m_sockAccept, (const char*)pbuff, len, 0);
+
+					// 释放内存;
+					delete []pbuff;
+					pbuff = NULL;
+				}
 				break;
 			default:
 				break;