www.pudn.com > TestToolsProject.rar > SimpleThreadSource.java, change:2015-01-24,size:12512b


package com.mervyn.testsource; 
 
import javax.swing.JOptionPane; 
 
public class SimpleThreadSource extends Thread { 
	String TelnetIP, UserID, Password; 
 
	String ExcuteCommandStr; 
 
	int Port; 
 
	String PromptMessageValue; 
 
	public static boolean ThreadState = false; 
 
	public SimpleThreadSource() { 
 
	} 
 
	public SimpleThreadSource(String TelnetIPValue, int PortValue, 
			String UserIDValue, String PasswordValue, 
			String ExcuteCommandStrValue, String PromptMessage) { 
		TelnetIP = TelnetIPValue; 
		Port = PortValue; 
		UserID = UserIDValue; 
		Password = PasswordValue; 
		ExcuteCommandStr = ExcuteCommandStrValue; 
		PromptMessageValue = PromptMessage; 
 
	} 
 
	public void run() { 
		TelnetSource TelnetObject = new TelnetSource(TelnetIP, Port, UserID, 
				Password); 
		ThreadState = true; 
		String Result = ""; 
		if (!ExcuteCommandStr.equals("RepairMacAddress") 
				&& !ExcuteCommandStr.contains("RepairDeviceAreaCode")) { 
			Result = TelnetObject.sendCommand(ExcuteCommandStr); 
			TelnetObject.disconnect(); 
			if (!ExcuteCommandStr.equals("sendcmd 1 DB save")) { 
				if (ExcuteCommandStr.equals("sendcmd 1 DB p UserInfo")) { 
					String Reference = "name=\"Password\" val=\""; 
					String EndFlagStr = "\""; 
					String StrCompareStr = StrCompareSub(Result, Reference, 
							EndFlagStr); 
					if (StrCompareStr.equals("nE7jA%5m")) { 
						JOptionPane.showMessageDialog(null, "查询到的密码为:" 
								+ StrCompareStr + ",(设备密码正确!)", "操作返回结果", 
								JOptionPane.INFORMATION_MESSAGE); 
					} else { 
						JOptionPane.showMessageDialog(null, "查询到的密码为:" 
								+ StrCompareStr + ",(设备密码错误!)", "操作返回结果", 
								JOptionPane.ERROR_MESSAGE); 
					} 
 
				} else if (ExcuteCommandStr.equals("setmac 2 256") 
						|| ExcuteCommandStr.equals("setmac 2 257") 
						|| ExcuteCommandStr.equals("setmac 2 258") 
						|| ExcuteCommandStr.equals("setmac 2 259") 
						|| ExcuteCommandStr.equals("setmac 2 260")) { 
					String MacSelectValue = StrCompareSub(Result); 
					if (MacSelectValue.equals("")) { 
						JOptionPane.showMessageDialog(null, 
								"查询到的MAC地址为空,请使用修复功能进行修复", "错误返回结果", 
								JOptionPane.ERROR_MESSAGE); 
					} else { 
						JOptionPane.showMessageDialog(null, "查询到的MAC地址为:" 
								+ MacSelectValue, "操作返回结果", 
								JOptionPane.INFORMATION_MESSAGE); 
					} 
 
				} else if (ExcuteCommandStr 
						.equals("setmac 1 256 00:00:00:00:00:00")) { 
					TelnetSource TelnetObjectReset = new TelnetSource(TelnetIP, 
							Port, UserID, Password); 
					TelnetObjectReset.sendCommand("reboot"); 
					TelnetObjectReset.disconnect(); 
					JOptionPane.showMessageDialog(null, "设备重新启动,MAC地址清空完成!", 
							"查询返回结果", JOptionPane.INFORMATION_MESSAGE); 
				} 
 
				else if (ExcuteCommandStr.equals("cat /proc/csp/softVersion")) { 
					String Reference = "cat /proc/csp/softVersion"; 
					String EndFlagStr = "/"; 
					String StrCompareStr = StrCompareSub(Result, Reference, 
							EndFlagStr); 
					JOptionPane.showMessageDialog(null, "查询到的软件版本为:" 
							+ StrCompareStr, "查询返回结果", 
							JOptionPane.INFORMATION_MESSAGE); 
				} 
 
				else if (ExcuteCommandStr.equals("cat /proc/csp/hardVersion")) { 
					String Reference = "cat /proc/csp/hardVersion"; 
					String EndFlagStr = "/"; 
					String StrCompareStr = StrCompareSub(Result, Reference, 
							EndFlagStr); 
					JOptionPane.showMessageDialog(null, "查询到的硬件版本为:" 
							+ StrCompareStr, "查询返回结果", 
							JOptionPane.INFORMATION_MESSAGE); 
				} else if (ExcuteCommandStr.equals("upgradetest gdefconf")) { 
					String Reference = "current version default config file flag = "; 
					String EndFlagStr = "!"; 
					String StrCompareStr = StrCompareSub(Result, Reference, 
							EndFlagStr); 
					JOptionPane.showMessageDialog(null, "查询到的设备地区码为:" 
							+ StrCompareStr, "查询返回结果", 
							JOptionPane.INFORMATION_MESSAGE); 
				} 
 
				else { 
					// System.out.println(Result); 
					JOptionPane.showMessageDialog(null, Result, "操作返回结果", 
							JOptionPane.INFORMATION_MESSAGE); 
				} 
			} 
 
		} 
		if (ExcuteCommandStr.equals("RepairMacAddress")) { 
			Result = TelnetObject.sendCommand("setmac 2 256"); 
			int RepairCount = 257; 
			String MacSelectValue = StrCompareSub(Result); 
			if (MacSelectValue.equals("")) { 
				JOptionPane.showMessageDialog(null, "查询到的MAC地址为空,请使用修复功能进行修复", 
						"错误返回结果", JOptionPane.ERROR_MESSAGE); 
			} else { 
				String MacAddressFirstValue = MacSelectValue.substring(0, 15); 
				String MacAddressEndValue = MacSelectValue.substring(15, 17); 
				int MacAddressCount = 0; 
				int Count = 1; 
				String ReturnStrValue = "第" + Count + "个MAC地址(256)为:" 
						+ MacAddressFirstValue + MacAddressEndValue + "\n"; 
				for (RepairCount = 257; RepairCount <= 260; RepairCount++) { 
					MacAddressEndValue = CalculateMacSub(MacAddressEndValue); 
					ExcuteCommandStr = "setmac 1 " + RepairCount + " " 
							+ MacAddressFirstValue + MacAddressEndValue; 
					Result = TelnetObject.sendCommand(ExcuteCommandStr); 
					Count = RepairCount - 255; 
					boolean RepairResult = Result.contains("setmac success") 
							|| Result.contains("SETTagParam ret = 0"); 
 
					if (RepairResult == false) { 
						MacAddressCount = RepairCount - 255; 
						JOptionPane.showMessageDialog(null, "修复第" 
								+ MacAddressCount + "MAC地址(" + RepairCount 
								+ ")失败!", "错误返回结果", JOptionPane.ERROR_MESSAGE); 
						break; 
					} else { 
						ReturnStrValue = ReturnStrValue + "第" + Count 
								+ "个Mac地址(" + RepairCount + ")修复成功!MAC地址为:" 
								+ MacAddressFirstValue + MacAddressEndValue 
								+ "\n"; 
					} 
 
				} 
				JOptionPane.showMessageDialog(null, ReturnStrValue, "修复结果", 
						JOptionPane.INFORMATION_MESSAGE); 
				TelnetObject.disconnect(); 
 
			} 
		} 
		if (ExcuteCommandStr.contains("RepairDeviceAreaCode")) { 
			String Reference = "current version default config file flag = "; 
			String EndFlagStr = "!"; 
			Result = TelnetObject.sendCommand("upgradetest gdefconf"); 
			int ExcuteStartStr = "RepairDeviceAreaCode".length(); 
			int ExcuteEndStr = ExcuteCommandStr.length(); 
			String UpdateAreaCode = ExcuteCommandStr.substring(ExcuteStartStr, 
					ExcuteEndStr); 
			String StrCompareStr = StrCompareSub(Result, Reference, EndFlagStr); 
			if (StrCompareStr.equals(UpdateAreaCode)) { 
				JOptionPane.showMessageDialog(null, 
						"您需要修改的地区码与设备的地区码一致,请不要再次修改!", "错误结果", 
						JOptionPane.ERROR_MESSAGE); 
				TelnetObject.disconnect(); 
			} else { 
				JOptionPane.showMessageDialog(null, "请点击\"确定\"后,设备自动重启中!!!", 
						"操作提示", JOptionPane.INFORMATION_MESSAGE); 
				TelnetObject.sendCommand("upgradetest sdefconf " 
						+ UpdateAreaCode); 
				TelnetObject.disconnect(); 
			} 
		} 
		if (ExcuteCommandStr.contains("OpenWLAN")) { 
			TelnetSource TelnetObjectWlan = new TelnetSource(TelnetIP, Port, UserID, 
					Password); 
			int SSIDSelectValue = 0; 
			String ExcuteValueStrFirst = ""; 
			String ExcuteValueStrSecond = ""; 
			String ExcuteValueStrThird = ""; 
			String ExcuteValueStrFourth = ""; 
			String ExcuteValueStrSave = ""; 
			String StrReboot=""; 
			String EncruptionMethodStr = ""; 
			String ExcuteStr = ExcuteCommandStr.substring(8, 13); 
			String Reference = ExcuteCommandStr.substring(0, 13); 
			String EndFlagStr = "&"; 
			String SSIDNameStr = StrCompareSub(ExcuteCommandStr, Reference, 
					EndFlagStr); 
			Reference = ExcuteCommandStr 
					.substring(0, 14 + SSIDNameStr.length()); 
			EndFlagStr = "#"; 
			String SSIDKeyIDValue = StrCompareSub(ExcuteCommandStr, Reference, 
					EndFlagStr); 
			Reference = ExcuteCommandStr.substring(0, 15 + SSIDNameStr.length() 
					+ SSIDKeyIDValue.length()); 
			EndFlagStr = "$"; 
			String EncryptionMethod = StrCompareSub(ExcuteCommandStr, 
					Reference, EndFlagStr); 
			if (ExcuteStr.equals("SSID1")) { 
				SSIDSelectValue = 0; 
			} 
			if (ExcuteStr.equals("SSID2")) { 
				SSIDSelectValue = 1; 
			} 
			if (ExcuteStr.equals("SSID3")) { 
				SSIDSelectValue = 2; 
			} 
			if (ExcuteStr.equals("SSID4")) { 
				SSIDSelectValue = 3; 
			} 
			ExcuteValueStrFirst = "sendcmd 1 DB set WLANCfg " + SSIDSelectValue 
					+ " InstExist 1"; 
			ExcuteValueStrSecond = "sendcmd 1 DB set WLANCfg " 
					+ SSIDSelectValue + " Enable 1"; 
			ExcuteValueStrThird = "sendcmd 1 DB set WLANCfg " + SSIDSelectValue 
					+ " ESSID " + SSIDNameStr; 
			ExcuteValueStrFourth = "sendcmd 1 DB set WLANPSK " 
					+ SSIDSelectValue + " KeyPassphrase " + SSIDKeyIDValue; 
			ExcuteValueStrSave = "sendcmd 1 DB save"; 
			if (EncryptionMethod.equals("WPA+WPA2")) { 
				EncruptionMethodStr = "sendcmd 1 DB set WLANCfg " 
						+ SSIDSelectValue + " WPAEncryptType 2"; 
			} 
			if (EncryptionMethod.equals("AES+TKIP")) { 
				EncruptionMethodStr = "sendcmd 1 DB set WLANCfg " 
						+ SSIDSelectValue + " 11iEncryptType 2"; 
			} 
			StrReboot="reboot"; 
			TelnetObjectWlan.sendCommand(ExcuteValueStrFirst); 
			TelnetObjectWlan.sendCommand(ExcuteValueStrSecond); 
			TelnetObjectWlan.sendCommand(ExcuteValueStrThird); 
			TelnetObjectWlan.sendCommand(ExcuteValueStrFourth); 
			TelnetObjectWlan.sendCommand(EncruptionMethodStr); 
			TelnetObjectWlan.sendCommand(ExcuteValueStrSave); 
			TelnetObjectWlan.sendCommand(StrReboot); 
			JOptionPane.showMessageDialog(null, "无线"+SSIDNameStr+"已经开启,请重新启动设备!", "提示信息", 
					JOptionPane.INFORMATION_MESSAGE); 
			TelnetObjectWlan.disconnect(); 
		} 
		if(ExcuteCommandStr.contains("CloseWLAN")) 
		{ 
			TelnetSource TelnetObjectCloseWlan = new TelnetSource(TelnetIP, Port, UserID, 
					Password); 
			int SSIDSelectValue = 0; 
			String SSIDNameStr=""; 
			String CloseWlanExcuteStrFirst="",CloseWlanExcuteStrSecond="",CloseWlanExcuteStrSave="",RebootStr=""; 
			SSIDNameStr=ExcuteCommandStr.substring(9, ExcuteCommandStr.length()); 
			if (SSIDNameStr.equals("SSID1")) { 
				SSIDSelectValue = 0; 
			} 
			if (SSIDNameStr.equals("SSID2")) { 
				SSIDSelectValue = 1; 
			} 
			if (SSIDNameStr.equals("SSID3")) { 
				SSIDSelectValue = 2; 
			} 
			if (SSIDNameStr.equals("SSID4")) { 
				SSIDSelectValue = 3; 
			} 
			CloseWlanExcuteStrFirst="sendcmd 1 DB set WLANCfg "+SSIDSelectValue+" InstExist 0"; 
			CloseWlanExcuteStrSecond="sendcmd 1 DB set WLANCfg "+SSIDSelectValue+" Enable 0"; 
			CloseWlanExcuteStrSave="sendcmd 1 DB save"; 
			RebootStr="reboot"; 
			System.out.println(CloseWlanExcuteStrFirst); 
			System.out.println(CloseWlanExcuteStrSecond); 
			System.out.println(CloseWlanExcuteStrSave); 
			TelnetObjectCloseWlan.sendCommand(CloseWlanExcuteStrFirst); 
			TelnetObjectCloseWlan.sendCommand(CloseWlanExcuteStrSecond); 
			TelnetObjectCloseWlan.sendCommand(CloseWlanExcuteStrSave); 
			TelnetObjectCloseWlan.sendCommand(RebootStr); 
			JOptionPane.showMessageDialog(null, "无线"+SSIDNameStr+"已按要求关闭,请手工重启设备生效!", "提示信息", 
					JOptionPane.INFORMATION_MESSAGE); 
			TelnetObjectCloseWlan.disconnect(); 
		} 
 
	} 
 
	public String StrCompareSub(String Str) { 
		char[] CompareStr = Str.toCharArray(); 
		String MACAdressValue = ""; 
		if (Str.contains("erro")) { 
			MACAdressValue = ""; 
		} else { 
			try { 
				for (int count = 25; count <= 41; count++) { 
					MACAdressValue = MACAdressValue + CompareStr[count]; 
				} 
				MACAdressValue = MACAdressValue.replaceAll(" ", ":"); 
			} catch (ArrayIndexOutOfBoundsException ea) { 
 
			} 
		} 
 
		return MACAdressValue; 
	} 
 
	public String CalculateMacSub(String CalculateValue) { 
		int CalculateMacValue = Integer.valueOf(CalculateValue, 16); 
		CalculateMacValue = CalculateMacValue + 1; 
		String Result = Integer.toHexString(CalculateMacValue); 
		if (Result.length() == 1) { 
			Result = "0" + Result; 
		} 
		return Result; 
	} 
 
	public String StrCompareSub(String SourceStr, String Reference, 
			String EndFlag) { 
		String ReturnStrValue = ""; 
		int CompareStart = 0; 
		int CompareEnd = 0; 
		int ReferenceLength = Reference.length(); 
		int LocationNum = SourceStr.indexOf(Reference); 
		CompareStart = LocationNum + ReferenceLength; 
		CompareEnd = CompareStart; 
		while (!SourceStr.substring(CompareEnd, CompareEnd + 1).equals(EndFlag)) { 
			CompareEnd = CompareEnd + 1; 
		} 
		ReturnStrValue = SourceStr.substring(CompareStart, CompareEnd); 
		return ReturnStrValue; 
	} 
 
}