Forráskód Böngészése

1、删除无用的通信结构体;
2、Login和UpdateDevices由测试数据转成正式数据接收;
3、完善Login函数;
4、完善通信逻辑;

scbc.sat2 5 éve
szülő
commit
2aa205bdf1

+ 30 - 23
SATService/SATService/SATExecutor.cpp

@@ -123,59 +123,65 @@ SATParameters::STCase* CSATExecutor::ExecuteFreeCaseScript(SATParameters::STTask
 	return pCase;
 }
 
-bool CSATExecutor::Login()
+bool CSATExecutor::Login(std::string user, std::string password, bool bLogin /*= true*/)
 {
-#ifdef _DEBUG
 	std::string url = Global::g_stSATConfig.szExecuteServer;
 	url.append("/ajaxInteractiveManage!executeLogin.action");
 	// 示例值;
-	m_stLoginReq.strUserName = "superAdmin";
-	m_stLoginReq.strStatus = "0";   // 0表示登录;
+	m_stLoginReq.strUserName = user;
+	m_stLoginReq.strStatus = bLogin ? "0" : "1";   // 0表示登录;
 	m_stLoginReq.strDeleteStatus = "";
-	m_stLoginReq.strIP = "10.118.158.175";
+	m_stLoginReq.strIP = Global::GetLocalAddress();//"10.118.158.175";
 	m_stLoginReq.strStorage = "";
 	m_stLoginReq.strConnectTime = "";
 	m_stLoginReq.strExecuteName = "";
-	m_stLoginReq.strMAC = "40:16:7e:23:10:53";
+	//m_stLoginReq.strMAC = Global::g_strMacs;//"40:16:7e:23:10:53";
+	if ( Global::g_vtMac.size() )
+		m_stLoginReq.strMAC = Global::g_vtMac[0].szMacAddress; // 取第一个MAC地址;
+	else
+		m_stLoginReq.strMAC = "";
 	m_stLoginReq.strDisconnectTime = "";
-	m_stLoginReq.strPassword = "123456";
+	m_stLoginReq.strPassword = password;
 	m_stLoginReq.strCPU = "";
 
 	if ( ::Login(url, m_stLoginReq, m_stLoginResp) )
 	{
+#ifdef _DEBUG
+	OutputDebugString("登录成功\n");
+#endif
 		m_bLogin = TRUE;
 		return true;
 	}
 
-	return false;
-#else
-
+#ifdef _DEBUG
+	OutputDebugString("登录失败\n");
 #endif
+
+	return false;
 }
 
-void CSATExecutor::Logout()
+bool CSATExecutor::Logout(std::string user, std::string password)
 {
-
+	return Login(user, password, false);
 }
 
 bool CSATExecutor::UpdateDevice()
 {
-#ifdef _DEBUG
 	SATParameters::STDevice stDevice;
-	stDevice.strStatus = "0";
+	stDevice.strStatus = "0";	// 0表示设备空闲; 1表示设备忙碌
 	stDevice.strDeviceSerial = "192.168.1.119:5555";
-	stDevice.strHardwareVersion = "MS6488";
-	stDevice.strManu = "MStar";
+	stDevice.strHardwareVersion = "SATHardwareVersion";
+	stDevice.strManu = "SATManu";
 	stDevice.strDeviceMac = "";
 	stDevice.strLastJobStartTime = "";
 	stDevice.strLastTimeBreak = "";
 	stDevice.strComments = "";
 	stDevice.strCPU = "";
-	stDevice.strSoftwareVersion = "1522659174";
+	stDevice.strSoftwareVersion = "0123456789";
 	stDevice.strPhoneNumber = "";
 	stDevice.strLastJobFinishTime = "";
 	stDevice.strMemory = "";
-	stDevice.strModel = "Generic Linux on tcl_ms6488_cn_64_n";
+	stDevice.strModel = "SATModel";
 	stDevice.strLastJob = "";
 	stDevice.strLastTimeConnected = "2019-12-16 10:16:01";
 	stDevice.strElectric = "";
@@ -186,11 +192,15 @@ bool CSATExecutor::UpdateDevice()
 	url.append("/ajaxInteractiveManage!updateDeviceMessage.action");
 	
 	stUpdateDeviceReq.strStatus = "0";
-	stUpdateDeviceReq.strUserName = "superAdmin";		
+	stUpdateDeviceReq.strUserName = m_stLoginReq.strUserName;		
 	stUpdateDeviceReq.strIP = "10.118.158.175";
 	stUpdateDeviceReq.strStorage = "228092536KB";
 	stUpdateDeviceReq.strRunnerName = "SAT-Admin";
-	stUpdateDeviceReq.strMAC = "40:16:7e:23:10:53";
+	//stUpdateDeviceReq.strMAC = "40:16:7e:23:10:53";
+	if ( Global::g_vtMac.size() )
+		stUpdateDeviceReq.strMAC = Global::g_vtMac[0].szMacAddress; // 取第一个MAC地址;
+	else
+		stUpdateDeviceReq.strMAC = "";
 	stUpdateDeviceReq.strReportType = "1";
 	stUpdateDeviceReq.strStartTime = "2019-12-16 19:15:30";
 	stUpdateDeviceReq.strMemory = "8938544KB";
@@ -204,9 +214,6 @@ bool CSATExecutor::UpdateDevice()
 	}
 	
 	return true;
-#else
-
-#endif
 }
 
 bool CSATExecutor::NotifyTaskStart(SATParameters::STTask* pTask)

+ 2 - 2
SATService/SATService/SATExecutor.h

@@ -68,9 +68,9 @@ protected:
 	SATParameters::STCase* ExecuteFreeCaseScript(SATParameters::STTask* pTask);
 public:
 	// 登录;
-	bool Login();
+	bool Login(std::string user, std::string password, bool bLogin = true);
 	// 登出;
-	void Logout();
+	bool Logout(std::string user, std::string password);
 	// 更新设备信息;
 	bool UpdateDevice();
 	// 通知SAT任务开始;

+ 28 - 36
SATService/SATService/SATProtocol.h

@@ -1,61 +1,53 @@
-#ifndef __PROTOCOL__
-#define __PROTOCOL__
+#ifndef __SAT_PROTOCOL__
+#define __SAT_PROTOCOL__
 
 #pragma once
 
+//////////////////////////////////////////////////////////////////////////
+// 야竟葵累寧즈;
+enum ProtocolCMD
+{
+	// 되쩌、되놔;
+	CMD_LOGIN					= 0,
+	CMD_LOGOUT					= 1,
+	// �구;
+	CMD_ADD_DEVICE				= 2,
+	CMD_DEL_DEVICE				= 3,
+	CMD_QUERY_DEVICES			= 4,
+};
+
 #pragma pack(push)
 #pragma pack(1)
 
 typedef struct _DATAHEADER_ {
 	// 葵累깃街륜;
-	byte protocol;
-	// 뎠품繫斤관돨낀똑;// header낀똑+buf낀똑;
-	unsigned int len;
+	byte			protocol;
+	// 뎠품繫斤관돨낀똑=header낀똑+buf낀똑;
+	unsigned int	len;
 	// 츱즈잚謹;
-	byte cmd;
+	byte			cmd;
 }DataHeader, *pDataHeader;
 
 // 헝헹관
 typedef struct _PACKAGE_
 {
-	DataHeader header;
-	byte	buf[4]; // 야竟코휭(寧濾뒈囹);
+	DataHeader	header;
+	byte		buf[4]; // 야竟코휭(寧濾뒈囹);
 }Package;
-#pragma pack(pop)
 
-// 야竟葵累寧즈;
-enum ProtocolCMD
+// 칵훰럿쀼令;
+typedef struct __TPRESPONSE__
 {
-	// 되쩌、되놔;
-	CMD_LOGIN					= 0,
-	CMD_LOGOUT					= 1,
-	// �구;
-	CMD_ADD_DEVICE				= 2,
-	CMD_DEL_DEVICE				= 3,
-	CMD_QUERY_DEVICES			= 4,
-};
+	bool bResult;
+}TPResponse, *pTPResponse;
 
+// 痰黨되쩌/되놔;
 typedef struct __TPLOGIN__
 {
 	char		szUserName[MAX_PATH];
 	char		szPassword[MAX_PATH];
 }TPLogin, *pTPLogin;
 
-typedef struct _REQUEST_JSON_
-{
-	int			device_id;
-	std::string device_name;
-	std::string device_cmd;
-	int device_timeout; 
-}RequestJson;
-
-
-typedef struct _RESPONSE_JSON_
-{
-	int			device_id;
-	std::string device_name;
-	std::string device_cmd_result;	// 츱즈獵契써벎;
-}ResponseJson;
-
+#pragma pack(pop)
 
-#endif // __PROTOCOL__
+#endif // __SAT_PROTOCOL__

+ 3 - 2
SATService/SATService/SATService.cpp

@@ -6,7 +6,7 @@
 #include "MainProcess.h"
 #include "SATExecutor.h"
 #include "SATDevices.h"
-#include "IOCPModel.h"
+#include "SATTCPServer.h"
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
@@ -26,7 +26,7 @@ void CALLBACK WorkStart()
 	WindowsService::GetDebugPriv();
 
 #ifdef _DEBUG
-	CSATExecutor::GetInstance()->Login();
+	CSATExecutor::GetInstance()->Login("superAdmin", "123456");
 	CSATExecutor::GetInstance()->UpdateDevice();
 #endif
 	CSATExecutor::GetInstance()->StartWork();
@@ -59,6 +59,7 @@ int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
 	{
 		// TODO: 在此处为应用程序的行为编写代码。
 		Global::GetIniInfo();
+		Global::GetMacAddress();
 #ifdef _DEBUG
 		//WorkStart();
 		//CSATDevices::GetInstance()->StartWork();

+ 3 - 1
SATService/SATService/SATService.vcproj

@@ -90,6 +90,7 @@
 			/>
 			<Tool
 				Name="VCPostBuildEventTool"
+				CommandLine="copy $(ProjectDir)SATProtocol.h ..\..\SATHelper\SATHelper\SATProtocol.h /y/a"
 			/>
 		</Configuration>
 		<Configuration
@@ -169,6 +170,7 @@
 			/>
 			<Tool
 				Name="VCPostBuildEventTool"
+				CommandLine="copy $(ProjectDir)SATProtocol.h ..\..\SATHelper\SATHelper\SATProtocol.h /y/a"
 			/>
 		</Configuration>
 	</Configurations>
@@ -361,7 +363,7 @@
 			Name="TCP"
 			>
 			<File
-				RelativePath=".\protocol.h"
+				RelativePath=".\SATProtocol.h"
 				>
 			</File>
 			<File

+ 38 - 13
SATService/SATService/SATTCPServer.cpp

@@ -1,6 +1,7 @@
 #include "StdAfx.h"
 #include "SATTCPServer.h"
 //#include "MainDlg.h"
+#include "SATExecutor.h"
 
 // 每一个处理器上产生多少个线程(为了最大限度的提升服务器性能,详见配套文档)
 #define WORKER_THREADS_PER_PROCESSOR 2
@@ -892,23 +893,47 @@ void CSATTCPServer::_RecvProcess(PER_SOCKET_CONTEXT* pSocketContext, PER_IO_CONT
 
 void CSATTCPServer::_TaskProcess(PER_IO_CONTEXT* pIoContext, Package* pak)
 {
-	cJSON* pJson = cJSON_Parse((const char*)&pak->buf);
-	if ( pJson == NULL )
-	{
+	DataHeader *pHeader = &pak->header;
+	if ( !pHeader ) 
 		return;
-	}
 
-	RequestJson reqj;
-	reqj.device_id = cJSON_GetObjectItem(pJson, "device_id") ? cJSON_GetObjectItem(pJson, "device_id")->valueint : 0;
-	reqj.device_name = cJSON_GetObjectItem(pJson, "device_name") ? cJSON_GetObjectItem(pJson, "device_name")->valuestring : "";
-	reqj.device_cmd = cJSON_GetObjectItem(pJson, "device_cmd") ? cJSON_GetObjectItem(pJson, "device_cmd")->valuestring : "";
-	reqj.device_timeout = cJSON_GetObjectItem(pJson, "device_timeout") ? cJSON_GetObjectItem(pJson, "device_timeout")->valueint : 300;
+	if ( pHeader->protocol == 0xAA ) {
+		if ( pHeader->cmd == CMD_LOGIN ) {// 登录;
+			TPLogin *pLogin = (TPLogin*)pak->buf;
+			bool bRet = CSATExecutor::GetInstance()->Login(pLogin->szUserName, pLogin->szPassword);
+
+			// 计算数据包长度;
+			long len = sizeof(DataHeader)+sizeof(TPResponse);
+			byte *pbuff = new byte[len];
+			Package *pSendPak = (Package *)pbuff;
+			pSendPak->header.protocol = 0xAA;
+			pSendPak->header.cmd = CMD_LOGIN;
+			pSendPak->header.len = len;
+			// 复制内容到指针中;
+			memcpy(pSendPak->buf, &bRet, sizeof(bool));
+
+			int ret = send(pIoContext->m_sockAccept, (const char*)pbuff, len, 0);
+		}
+		else if ( pHeader->cmd == CMD_LOGOUT ) {// 登出;
 
-	if ( pJson )
-	{
-		cJSON_Delete(pJson);
-		pJson = NULL;
+		}
+		else if ( pHeader->cmd == CMD_ADD_DEVICE ) {// 添加设备;
+
+		}
+		else if ( pHeader->cmd == CMD_DEL_DEVICE ) {// 删除设备;
+
+		}
+		else if ( pHeader->cmd == CMD_QUERY_DEVICES ) {// 查询设备;
+
+		}
+		else {// 无效指令;
+
+		}
 	}
+	else if ( pHeader->protocol == 0xAB )
+	{
+
+	}	
 }
 
 /////////////////////////////////////////////////////////////////////

+ 1 - 1
SATService/SATService/SATTCPServer.h

@@ -5,7 +5,7 @@
 #include <MSWSock.h>
 //#include <ws2tcpip.h>
 #pragma comment(lib,"ws2_32.lib")
-#include "protocol.h"
+#include "SATProtocol.h"
 
 // 缓冲区长度 (1024*8)
 // 之所以为什么设置8K,也是一个江湖上的经验值