void T3000_Default_MainView::OnBnClickedApplybutton()
{
    int m_nListenPort=0;
    BeginWaitCursor();
    //m_nListenPort	
    CString strText;
    m_ListenPort.GetWindowText(strText);
    if(!strText.IsEmpty())
        m_nListenPort=_wtoi(strText);
    if(m_nListenPort>=12767)
    {
        AfxMessageBox(_T("The listen port number is too big, please change it."));
        return;
    }
    if(m_nListenPort<=0)
    {
        AfxMessageBox(_T("The listen port number must be greater than 0!"));
        return;
    }
    write_one(g_tstat_id,75,m_nListenPort);
    product_register_value[75]=m_nListenPort;
    BYTE address1,address2,address3,address4;
    m_ipaddress.GetAddress(address1,address2,address3,address4);

    int sel = m_ip_model.GetCurSel ();
     write_one(g_tstat_id,61,sel);

    int CO2_NET_MODBUS_IP_ADDRESS_GHOST_START = 62 ;
    int n=write_one(g_tstat_id,CO2_NET_MODBUS_IP_ADDRESS_GHOST_START,address1);
    n=write_one(g_tstat_id,CO2_NET_MODBUS_IP_ADDRESS_GHOST_START+1,address2);
    n=write_one(g_tstat_id,CO2_NET_MODBUS_IP_ADDRESS_GHOST_START+2,address3);
    n=write_one(g_tstat_id,CO2_NET_MODBUS_IP_ADDRESS_GHOST_START+3,address4);
    int CO2_NET_MODBUS_SUBNET_MASK_ADDRESS_GHOST_START = 66;
    m_Subnet_Mask.GetAddress(address1,address2,address3,address4);
    n=write_one(g_tstat_id,CO2_NET_MODBUS_SUBNET_MASK_ADDRESS_GHOST_START+0,address1);
    n=write_one(g_tstat_id,CO2_NET_MODBUS_SUBNET_MASK_ADDRESS_GHOST_START+1,address2);
    n=write_one(g_tstat_id,CO2_NET_MODBUS_SUBNET_MASK_ADDRESS_GHOST_START+2,address3);
    n=write_one(g_tstat_id,CO2_NET_MODBUS_SUBNET_MASK_ADDRESS_GHOST_START+3,address4);
    m_GatewayAddress.GetAddress(address1,address2,address3,address4);
    int CO2_NET_MODBUS_GATEWAY_ADDRESS_GHOST_START =   70;
    n=write_one(g_tstat_id,CO2_NET_MODBUS_GATEWAY_ADDRESS_GHOST_START,address1);
    n=write_one(g_tstat_id,CO2_NET_MODBUS_GATEWAY_ADDRESS_GHOST_START+1,address2);
    n=write_one(g_tstat_id,CO2_NET_MODBUS_GATEWAY_ADDRESS_GHOST_START+2,address3);
    n=write_one(g_tstat_id,CO2_NET_MODBUS_GATEWAY_ADDRESS_GHOST_START+3,address4);
    int CO2_NET_MODBUS_ENABLE_GHOST = 76;
    n=write_one(g_tstat_id,CO2_NET_MODBUS_ENABLE_GHOST,1);

    Sleep(5*1000);



    CMainFrame* pPraent=(CMainFrame*)GetParent();

    CString strBuilding,strSubBuilding;
    strBuilding=pPraent->m_strCurMainBuildingName;
    strSubBuilding=pPraent->m_strCurSubBuldingName;
    CString strIP,strPort;
    strPort.Format(_T("%d"),product_register_value[75]);
    // BYTE address1,address2,address3,address4;
    m_ipaddress.GetAddress(address1,address2,address3,address4);
    strIP.Format(_T("%d.%d.%d.%d"),address1,address2,address3,address4);
    if(g_CommunicationType==1)//TCP
    {
       /* int nPort;
        nPort=_wtoi(strPort);*/
        //m_strIP=build_info.strIp; 
        g_CommunicationType=1;
        SetCommunicationType(g_CommunicationType);
        BOOL bOk=FALSE;
        bOk=Open_Socket2(strIP,m_nListenPort);
        CString strInfo;

        strInfo.Format((_T("Open IP:%s successful")),strIP);//prompt info;
        SetPaneString(1,strInfo);
        if(bOk)
        {
            try
			{
			CppSQLite3DB SqliteDBBuilding;
			CppSQLite3Table table;
			CppSQLite3Query q;
			SqliteDBBuilding.open((UTF8MBSTR)g_strCurBuildingDatabasefilePath);


                CString strSql;
                //strSql.Format(_T("update Building set Ip_Address='%s' where Ip_Address='%s'"),strIP,pPraent->m_strIP);
                //t_pCon->Execute(strSql.GetString(),NULL,adCmdText);

                // ¸Änode
                CString strSID;

                strSID.Format(_T("%d"), get_serialnumber());
                strSql.Format(_T("update ALL_NODE set Bautrate='%s',Com_Port='%s' where Serial_ID='%s'"),strIP,strPort,strSID); //bautrate ·ÅIP
                SqliteDBBuilding.execDML((UTF8MBSTR)strSql);

                SqliteDBBuilding.closedb();

            }
            catch(_com_error *e)
            {
                AfxMessageBox(e->ErrorMessage());
            }

            pPraent->m_strIP=strIP;
            pPraent->ScanTstatInDB();

        }
        else
        {

        }



    }
    EndWaitCursor();
}
void CARDDlg::OnBnClickedOk()
{
   CString ip;
   CString strport;
   m_ipaddress.GetWindowText(ip);
   m_porteditor.GetWindowText(strport);
   if (ip.IsEmpty()||strport.IsEmpty())
   {
	   AfxMessageBox(_T("ip or port can not be empty!"));
	   return;
   }
   short port=_wtoi(strport);
   SetCommunicationType(1);
   BOOL is_open=Open_Socket2(ip,port);
   if (is_open)
   {
       //unsigned short data[2];
	   //int ModelID=Read_Multi(209,data,6,2);//  (255,6,20);
	   //CString show;
	   //show.Format(_T("Add=%d,Model=%d"),data[0],data[1]);
	   //ModelID=read_one(data[0],7);
	   //AfxMessageBox(show);
	   //show.Format(_T("Model=%d"),ModelID);
	   //AfxMessageBox(show);
	  unsigned short Data[10];
	  int ret=Read_Multi(255,Data,0,10);
	  if (ret<0)
	  {
	  AfxMessageBox(_T("Can't get the information of your device!\n Please try again."));
	  ret;
	  }

	   CString strSql;
	   CString strMainBuildName,strSubBuildingName,strSID,strFloorName,strRoomName,strProID;
	   CString strProName,strProType,strScreenID,strBaudrate,strGraphicID,strHdVersion,strStVersion,strCom,strEPSize;

	  
	   
	  /* CString temp_str=_T("select * from Building where Default_SubBuilding=-1");
	   m_pRs->Open(_variant_t(temp_str),_variant_t((IDispatch *)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText);	
	   int recordcount=m_pRs->GetRecordCount();
	   if(m_pRs->GetRecordCount()<=0)
	   {
		   AfxMessageBox(_T("There is no default building,please select a building First."));
		   return;
	   }


	   if(m_pRs->State)
		   m_pRs->Close(); 
	   if(m_pCon->State)
           m_pCon->Close();*/
	   CMainFrame* pFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd);
	   strMainBuildName= pFrame->m_strCurMainBuildingName;
	   strSubBuildingName= pFrame->m_strCurSubBuldingName;
	   int SN=Data[3]*256*256*256+Data[2]*256*256+Data[1]*256+Data[0];
       strSID.Format(_T("%d"),SN);
	   strFloorName=_T("floor1");
	   strRoomName=_T("room1");
	   strProName.Format(_T("%s:%d--%d"),GetProductName(Data[7]).GetBuffer(),SN,Data[6]);
	   strProType.Format(_T("%d"),Data[7]);
	   strProID.Format(_T("%d"),Data[6]);

	   strScreenID.Format(_T("Screen(S:%d--%d)"),SN,Data[6]);
	   m_ipaddress.GetWindowText(strBaudrate);
	   strGraphicID=_T("Clicking here to add a image...");
	   strHdVersion=_T("0.0");
	   strStVersion=_T("0.0");
	   m_porteditor.GetWindowText(strCom);
	   strEPSize=_T("0.0");
	   try
	   {
		   CADO ado;
		   ado.OnInitADOConn();
           strSql.Format(_T("Select * from ALL_NODE where Product_name='%s'"),strProName.GetBuffer());
           ado.m_pRecordset=ado.OpenRecordset(strSql);
           if (!ado.m_pRecordset->EndOfFile)//有表但是没有对应序列号的值
           {
            AfxMessageBox(_T("Already Exists!"));
            return;
            //ado.m_pRecordset->Close();
           }
		   strSql.Format(_T("insert into ALL_NODE (MainBuilding_Name,Building_Name,Serial_ID,Floor_name,Room_name,Product_name,Product_class_ID,Product_ID,Screen_Name,Bautrate,Background_imgID,Hardware_Ver,Software_Ver,Com_Port,EPsize) values('"
			   +strMainBuildName+"','"
			   +strSubBuildingName +"','"
			   +strSID+"','"
			   +strFloorName+"','"
			   +strRoomName+"','"
			   +strProName+"','"
			   +strProType+"','"
			   +strProID+"','"
			   +strScreenID+"','"
			   +strBaudrate+"','"
			   +strGraphicID+"','"
			   +strHdVersion+"','"
			   +strStVersion+"','"
			   +strCom+"','"
			   +strEPSize+"')"));
		   ado.m_pRecordset=ado.OpenRecordset(strSql);
		   ado.CloseConn();
		   AfxMessageBox(_T("Add OK!"));
           CDialogEx::OnOK();
	   }
	   
	   catch (CException* e)
	   {
		   AfxMessageBox(_T("Add Fail!"));
	   }
	  
	   
   }
   else
   {
	   AfxMessageBox(_T("Can not Connect"));
   }

}
void CNetworkControllView::OnBnClickedApplybutton()
{
	if(!CheckSettingChanged()) // if setting changed,return TRUE
	{
		return; // unchanged, don't write register
	}

	if(g_NetWorkLevel==1)
		return;
	BeginWaitCursor();
	//m_nListenPort	
	CString strText;
	m_listenPortEdit.GetWindowText(strText);
	if(!strText.IsEmpty())
		m_nListenPort=_wtoi(strText);
	if(m_nListenPort>=12767)
	{
		AfxMessageBox(_T("The listen port number is too big, please change it."));
		return;
	}
	if(m_nListenPort<=0)
	{
		AfxMessageBox(_T("The listen port number must be greater than 0!"));
		return;
	}
	write_one(g_tstat_id,120,m_nListenPort);
	BYTE address1,address2,address3,address4;
	m_ip_addressCtrl.	GetAddress(address1,address2,address3,address4);
	int n=write_one(g_tstat_id,107,address1);
	n=write_one(g_tstat_id,108,address2);
	n=write_one(g_tstat_id,109,address3);
	n=write_one(g_tstat_id,110,address4);
	m_subnet_addressCtrl.GetAddress(address1,address2,address3,address4);
	write_one(g_tstat_id,111,address1);
	write_one(g_tstat_id,112,address2);
	write_one(g_tstat_id,113,address3);
	write_one(g_tstat_id,114,address4);
	m_gateway_addressCtrl.GetAddress(address1,address2,address3,address4);
	write_one(g_tstat_id,115,address1);
	write_one(g_tstat_id,116,address2);
	write_one(g_tstat_id,117,address3);
	write_one(g_tstat_id,118,address4);
	write_one(g_tstat_id,131,1);
	Sleep(1000);//Sleep(10000); // ???
	write_one(g_tstat_id,133,1);
	Sleep(5000);	//Sleep(5000); // wait for nc restart

	CMainFrame* pPraent=(CMainFrame*)GetParent();
	//pPraent->ReFresh();

	/*
	unsigned short variable[13]={0};
	int nRet=Read_Multi(g_tstat_id,variable,106,13,3);/////////////////////////read
	m_ipModelComBox.SetCurSel(variable[0]);
	m_ip_addressCtrl.SetAddress(variable[1],variable[2],variable[3],variable[4]);
	m_subnet_addressCtrl.SetAddress(variable[5],variable[6],variable[7],variable[8]);
	m_gateway_addressCtrl.SetAddress(variable[9],variable[10],variable[11],variable[12]);

*/
	
	CString strBuilding,strSubBuilding;
	strBuilding=pPraent->m_strCurMainBuildingName;
	strSubBuilding=pPraent->m_strCurSubBuldingName;
	CString strIP,strPort;
	strPort.Format(_T("%d"),multi_register_value[120]);
	m_ip_addressCtrl.GetAddress(address1,address2,address3,address4);
	strIP.Format(_T("%d.%d.%d.%d"),address1,address2,address3,address4);
	if(g_CommunicationType==1)//TCP
	{
		int nPort;
		nPort=_wtoi(strPort);
		//m_strIP=build_info.strIp; 
		g_CommunicationType=1;
		SetCommunicationType(g_CommunicationType);
		BOOL bOk=FALSE;
		bOk=Open_Socket2(strIP,nPort);
		CString strInfo;

		strInfo.Format((_T("Open IP:%s successful")),strIP);//prompt info;
		SetPaneString(1,strInfo);
		if(bOk)
		{
			_ConnectionPtr t_pCon;//for ado connection
			t_pCon.CreateInstance(_T("ADODB.Connection"));
			t_pCon->Open(g_strDatabasefilepath.GetString(),_T(""),_T(""),adModeUnknown);
			CString strSql;
			strSql.Format(_T("update Building set Ip_Address='%s' where Ip_Address='%s'"),strIP,pPraent->m_strIP);
			t_pCon->Execute(strSql.GetString(),NULL,adCmdText);

			// 改node
			CString strSID;
			strSID.Format(_T("%d"), m_nSerialNum);
			strSql.Format(_T("update ALL_NODE set Bautrate='%s' where Serial_ID='%s'"),strIP, strSID); //bautrate 放IP
			t_pCon->Execute(strSql.GetString(),NULL,adCmdText);




			if(t_pCon->State)
				t_pCon->Close();
			pPraent->m_strIP=strIP;
			pPraent->ScanTstatInDB();

		}
		else
		{

		}



/*
		_ConnectionPtr t_pCon;//for ado connection
		t_pCon.CreateInstance(_T("ADODB.Connection"));
		t_pCon->Open(g_strDatabasefilepath.GetString(),_T(""),_T(""),adModeUnknown);
		CString strSql;
		strSql.Format(_T("update Building set Ip_Address='%s',Ip_Port='%s' where Main_BuildingName='%s' and Building_Name='%s'"),strIP,strPort,strBuilding,strSubBuilding);
		t_pCon->Execute(strSql.GetString(),NULL,adCmdText);
		if(t_pCon->State)
			t_pCon->Close();
		pPraent->OnConnect();
		*/
	}
	//write_one(g_tstat_id,131,1);
	Fresh();
	EndWaitCursor();

}