import java.awt.*;
import java.awt.event.*;
import ZNetComC.*;

/**
 * Sample application using Frame.
 *
 * @author 
 * @version 1.00 05/04/19
 */
public class TestconfigFrame extends Frame implements ActionListener {
    
    ZNetCom m_netcom;
    
   	private Button m_BtnBroadSearch;
   	private Button m_BtnSearchIP;
   	private Button m_BtnGetInfo;
   	private Button m_BtnModifyCom;
   	private Button m_BtnModifyWorkPort;
   	private Button m_BtnResetDev;
   	private Button m_BtnGetIO;
   	private Button m_BtnSetIO;
   	private Button m_BtnGetADC;
	private TextArea m_TextAreaInfo;
	private TextField m_TextIP;
	private TextField m_TextWorkPort;
	private TextField m_TextPwd;
	private TextField m_TextIOPort;
	private TextField m_TextIO0;
	private TextField m_TextIO1;

    /**
     * The constructor.
     */  
     public TestconfigFrame() {
                
        MenuBar menuBar = new MenuBar();
        Menu menuFile = new Menu();
        MenuItem menuFileExit = new MenuItem();
        
        menuFile.setLabel("File");
        menuFileExit.setLabel("Exit");
        
        // Add action listener.for the menu button
        menuFileExit.addActionListener
        (
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    TestconfigFrame.this.windowClosed();
                }
            }
        ); 
        menuFile.add(menuFileExit);
        menuBar.add(menuFile);
        
        setTitle("Testconfig");
        setMenuBar(menuBar);
        setSize(new Dimension(400, 400));
        
        // Add window listener.
        this.addWindowListener
        (
            new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    TestconfigFrame.this.windowClosed();
                }
            }
        );  
        
        
        init();
    }
    
	protected Button makebutton(String name, GridBagLayout gridbag,GridBagConstraints c) 
	{
		Button btn = new Button(name);
		gridbag.setConstraints(btn, c);
		add(btn);
		return btn;
	}
 
	protected void makelabel(String name, GridBagLayout gridbag,GridBagConstraints c) 
	{
		Label label= new Label(name,Label.LEFT);
		gridbag.setConstraints(label, c);
		add(label);
	}
	protected TextField makeTextField(String name,int len, GridBagLayout gridbag,GridBagConstraints c) 
	{
		TextField field;
		field= new TextField(name,len);
		gridbag.setConstraints(field, c);
		add(field);
		return field;
	}
	protected TextArea makeTextArea(String name,int lenx,int leny, GridBagLayout gridbag,GridBagConstraints c) 
	{
		TextArea field;
		field= new TextArea(name,lenx,leny,TextArea.SCROLLBARS_VERTICAL_ONLY);
		gridbag.setConstraints(field, c);
		add(field);
		return field;
	}    
    
    public void init() 
	{
		m_netcom=new ZNetCom(0);
		
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		setLayout(gridbag);
		c.anchor=GridBagConstraints.NORTHWEST;
		c.insets=new Insets(1,1,1,1);
		c.gridwidth=GridBagConstraints.REMAINDER;
		m_BtnBroadSearch=makebutton("广播搜索",gridbag,c);
		m_BtnBroadSearch.addActionListener(this);
		m_BtnBroadSearch.setActionCommand("bSearch");

		c.gridwidth=1;
		m_BtnSearchIP=makebutton("指定IP搜索",gridbag,c);
		m_BtnSearchIP.addActionListener(this);
		m_BtnSearchIP.setActionCommand("SSearch");
		c.gridwidth=GridBagConstraints.REMAINDER;
		m_TextIP=makeTextField("192.168.0.100",13,gridbag,c);

		c.gridwidth=GridBagConstraints.REMAINDER;
		m_BtnGetInfo=makebutton("获取指定设备信息",gridbag,c);
		m_BtnGetInfo.addActionListener(this);
		m_BtnGetInfo.setActionCommand("GetInfo");
		

		c.gridwidth=1;
		m_BtnModifyCom=makebutton("更改串口参数为",gridbag,c);
		m_BtnModifyCom.addActionListener(this);
		m_BtnModifyCom.setActionCommand("MoCom");
		c.gridwidth=GridBagConstraints.REMAINDER;
		makelabel("波特率:9600,数据位:8,停止位:1,校验位:无",gridbag, c);

		c.gridwidth=1;
		m_BtnModifyWorkPort=makebutton("更改工作端口",gridbag,c);
		m_BtnModifyWorkPort.addActionListener(this);
		m_BtnModifyWorkPort.setActionCommand("MoWorkPort");
		m_TextWorkPort=makeTextField("4001",8,gridbag,c);
		makelabel("验证密码:",gridbag, c);
		c.gridwidth=GridBagConstraints.REMAINDER;
		m_TextPwd=makeTextField("",8,gridbag,c);


		c.gridwidth=GridBagConstraints.REMAINDER;
		m_BtnResetDev=makebutton("复位设备",gridbag,c);
		m_BtnResetDev.addActionListener(this);
		m_BtnResetDev.setActionCommand("ResetDev");

		c.gridwidth=1;
		m_BtnGetIO=makebutton("获取IO状态",gridbag,c);
		m_BtnGetIO.addActionListener(this);
		m_BtnGetIO.setActionCommand("GetIO");
		makelabel("IO端口:",gridbag, c);
		c.gridwidth=GridBagConstraints.REMAINDER;
		m_TextIOPort=makeTextField("3003",8,gridbag,c);
		
		c.gridwidth=1;
		m_BtnSetIO=makebutton("设置IO状态",gridbag,c);
		m_BtnSetIO.addActionListener(this);
		m_BtnSetIO.setActionCommand("SetIO");
		makelabel("IO0:",gridbag, c);
		m_TextIO0=makeTextField("00",3,gridbag,c);
		makelabel("IO1:",gridbag, c);
		c.gridwidth=GridBagConstraints.REMAINDER;
		m_TextIO1=makeTextField("ff",3,gridbag,c);

		c.gridwidth=GridBagConstraints.REMAINDER;
		m_BtnGetADC=makebutton("获取ADC状态",gridbag,c);
		m_BtnGetADC.addActionListener(this);
		m_BtnGetADC.setActionCommand("GetADC");

		m_TextAreaInfo=makeTextArea("信息显示区",5,50,gridbag,c);
		
	
    }
    
    public void actionPerformed(ActionEvent e) 
    {
		if(e.getActionCommand()=="bSearch")
		{
			DEVINFO info[]=new DEVINFO[20];
			for(int j=0;j<20;j++)
				info[j]=new DEVINFO();
		    m_netcom.ZNET_SearchBroadcast();
		    int reclen=m_netcom.ZNET_RecDevice(info,20);
		    m_TextAreaInfo.setText("");
		    for(int i=0;i<reclen;i++)
		    {
		    	String str1;
		    	str1="MAC地址:"+info[i].szmacaddr+"  IP地址："+info[i].szip+"\n";
		    	m_TextAreaInfo.append(str1);
		    }
		}
		else if(e.getActionCommand()=="GetInfo")
		{
			DEVINFO info[]=new DEVINFO[1];
			for(int j=0;j<1;j++)
				info[j]=new DEVINFO();
		    if(m_netcom.ZNET_GetDeviceInformation(m_TextIP.getText(),info)==1)
		    {
			    m_TextAreaInfo.setText("设备信息:\n");
		    	String str1;
		    	str1="设备名称:"+info[0].szname;
		    	m_TextAreaInfo.append(str1);
		    	str1=" 设备IP:"+info[0].szip+
	    		" 网关:"+info[0].szgateip+" 子网掩码:"+info[0].szmask+" MAC地址:"+info[0].szmacaddr+" 工作端口:"+info[0].workport+"\n";
		    	m_TextAreaInfo.append(str1);
		    }
		    else
			    m_TextAreaInfo.setText("获取设备信息失败");
		    	
		}
		else if(e.getActionCommand()=="SSearch")
		{
			DEVINFO info[]=new DEVINFO[1];
			for(int j=0;j<1;j++)
				info[j]=new DEVINFO();
		    m_netcom.ZNET_SearchIP(m_TextIP.getText());
		    int reclen=m_netcom.ZNET_RecDevice(info,1);
		    m_TextAreaInfo.setText("");
		    for(int i=0;i<reclen;i++)
		    {
		    	String str1;
		    	str1="MAC地址:"+info[i].szmacaddr+"  IP地址："+info[i].szip+"\n";
		    	m_TextAreaInfo.append(str1);
		    }
		}
		else if(e.getActionCommand()=="MoCom")
		{
			ZNE100INFO info=new ZNE100INFO();
			info.baud=5;info.databits=8;info.stopbits=1;
			info.checkbits=0;
			m_netcom.ZNET_ModifyComCfg(m_TextIP.getText(),info);
		}
		else if(e.getActionCommand()=="MoWorkPort")
		{
			DEVINFO devinfo[]=new DEVINFO[1];
			devinfo[0]=new DEVINFO();
		    m_TextAreaInfo.setText("");
			if(m_netcom.ZNET_GetDeviceInformation(m_TextIP.getText(),devinfo)==1)
			{
				devinfo[0].workport=Integer.parseInt(m_TextWorkPort.getText());
				if(m_netcom.ZNET_ModifyDevice(m_TextIP.getText(),m_TextPwd.getText(),
					devinfo)==1)
				{
			    	m_TextAreaInfo.append("更改工作端口成功\n");
				}
				else
				{
			    	m_TextAreaInfo.append("更改工作端口失败\n");
				}
			}
			else
			{
		    	m_TextAreaInfo.append("更改工作端口失败\n");
			}
		}
  		else if(e.getActionCommand()=="ResetDev")
		{
			m_netcom.ZNET_ResetDevice(m_TextIP.getText(),m_TextPwd.getText());
			//m_netcom.ZNET_ResetDevTCP(m_TextIP.getText(),
			//		Integer.parseInt(m_TextIOPort.getText()));
		}
  		else if(e.getActionCommand()=="GetIO")
		{
			byte ioarr[]=new byte[2];
			
		    m_TextAreaInfo.setText("");
			if(m_netcom.ZNET_GetIOStat(m_TextIP.getText(),
					Integer.parseInt(m_TextIOPort.getText()),ioarr)==1)
			{
				String str1;
				str1="IO状态:"+"0x"+Integer.toHexString(((int)ioarr[0])&0xff)+
					"  0x"+Integer.toHexString(((int)ioarr[1])&0xff)+"\n";
		    	m_TextAreaInfo.append(str1);
			}			
		}
  		else if(e.getActionCommand()=="SetIO")
		{
			byte ioarr[]=new byte[2];
			
		    m_TextAreaInfo.setText("");
		   	ioarr[0]=(byte)Integer.parseInt(m_TextIO0.getText(),16);
		   	ioarr[1]=(byte)Integer.parseInt(m_TextIO1.getText(),16);
			if(m_netcom.ZNET_SetIOStat(m_TextIP.getText(),
				Integer.parseInt(m_TextIOPort.getText()),(byte)2,ioarr)==1)
			{
				String str1;
				str1="IO状态:"+"0x"+Integer.toHexString(((int)ioarr[0])&0xff)+
					"  0x"+Integer.toHexString(((int)ioarr[1])&0xff)+"\n";
		    	m_TextAreaInfo.append(str1);
			}			
		}
  		else if(e.getActionCommand()=="GetADC")
		{
			short ioarr[]=new short[2];
			
		    m_TextAreaInfo.setText("");
			if(m_netcom.ZNET_GetADC(m_TextIP.getText(),
					Integer.parseInt(m_TextIOPort.getText()),ioarr)==1)
			{
				String str1;
				str1="ADC状态:"+"0x"+Integer.toHexString(((int)ioarr[0])&0xffff)+
					"  0x"+Integer.toHexString(((int)ioarr[1])&0xffff)+"\n";
		    	m_TextAreaInfo.append(str1);
			}			
		}
  }
    
    /**
     * Shutdown procedure when run as an application.
     */
    protected void windowClosed() {
    	
    	// TODO: Check if it is safe to close the application
    	
        // Exit application.
        System.exit(0);
    }
}
