private void btapplythreshold_Click(object sender, System.EventArgs e)
 {
     try
     {
         if (txtTemThresh.Text != "" && txtHumiThresh.Text != "" && txtPhoneThresh.Text != "")
         {
             Database myDatabase = new Database();
             int id = cbSelectVan.SelectedIndex + 1;
             int a = myDatabase.setTempVan(id,float.Parse(txtTemThresh.Text));
             int b = myDatabase.setHumiVan(id,float.Parse(txtHumiThresh.Text));
             int c = myDatabase.setPhoneNumber(txtPhoneThresh.Text);
             if(a == 1 && b == 1 && c == 1)
             {
                 MessageBox.Show("Save successful", "Imformation");
             }
             else
             {
                 MessageBox.Show("Not successful","Imformation");
             }
         }
         else
         {
             MessageBox.Show("Not enough imformation", "Error");
         }
     }
     catch { }
 }
 public void Request()
 {
     try
     {
         string[] url = connection.Confix();
         string uriCom = url[4];
         while (true)
         {
             //comReadWeb = getdata_from_server(uriCom);
             comReadWeb = getDataServer(uriCom);
             if (comReadWeb != "")
             {
                 if(comReadWeb[0] == '#')
                 {
                     Database myDatabase = new Database();
                     int van = int.Parse(comReadWeb.Substring(1,1));
                     float temp = float.Parse(comReadWeb.Substring(2,2));
                     float humi = float.Parse(comReadWeb.Substring(4,2));
                     myDatabase.setHumiVan(van,humi);
                     myDatabase.setTempVan(van,temp);
                     DisplayData("(" + DateTime.Now + "): Cai dat nguong tu WEB:", tb);
                     DisplayData("Van so : " + van, tb);
                     DisplayData("Nhiet do : " + temp + "°C", tb);
                     DisplayData("Do am : " + humi+ "%", tb);
                 }
                 else
                 { 
                     DisplayData("(" + DateTime.Now + "): Lenh gui tu WEB:", tb);
                     DisplayData("Ma lenh :" + comReadWeb, tb);
                     byte[] commandWeb = ConvertTobyte(comReadWeb);
                     WriteData(commandWeb);
                 }
                 comReadWeb = "";
             }
             Thread.Sleep(1000);
         }
     }
     catch
     {
         //DisplayData(MessageType.Error, "Khong the lay du lieu tu WEB", tb);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Ham doc du lieu lien tuc tu web
        /// </summary>
        public void Request()
        {
            
            try
            {
              //  DisplayData("Request len web :", txtShowData);
                string[] url = connection.Confix();
                string uriCom = url[4];
                //string a = null;
                while (true)
                {
                    web.DataReceiveFromWeb = web.receiveDataFromWeb(uriCom);
                    //MessageBox.Show("Dữ liệu truoc : "+ web.DataReceiveFromWeb+" nhe");
                    if (web.DataReceiveFromWeb.Length > 0)
                    {
                        MessageBox.Show("length >0");
                        if (web.DataReceiveFromWeb[0] == '#')
                        {
                            MessageBox.Show("Dữ liệu sau : " + web.DataReceiveFromWeb.ToString() + " nhe");

                            Database myDatabase = new Database();
                            int van = int.Parse(web.DataReceiveFromWeb.Substring(1, 1));
                            float temp = float.Parse(web.DataReceiveFromWeb.Substring(2, 2));
                            float humi = float.Parse(web.DataReceiveFromWeb.Substring(4, 2));
                            myDatabase.setHumiVan(van, humi);
                            myDatabase.setTempVan(van, temp);
                            DisplayData("(" + showTime() + "): Cai dat nguong tu WEB:", txtShowData);
                            DisplayData("Van so : " + van, txtShowData);
                            DisplayData("Nhiet do : " + temp + "°C", txtShowData);
                            DisplayData("Do am : " + humi + "%", txtShowData);
                        }
                        else
                        {
                            DisplayData("(" + showTime() + "): Lenh gui tu WEB:", txtShowData);
                            string mac = web.DataReceiveFromWeb.Substring(0,2);
                            int time_web = DateTime.Now.Hour * 3600 + DateTime.Now.Minute * 60 + DateTime.Now.Second;
                            InformationNode.timeDapUng.Remove(mac);
                           // MessageBox.Show("qua dap ung mac");
                            InformationNode.timeDapUng.Add(mac,time_web);
                           // MessageBox.Show("qua dap ung mac 1 :" + web.DataReceiveFromWeb);
                            web.DataReceiveFromWeb = web.DataReceiveFromWeb.Substring(0,8);
                            //a = web.DataReceiveFromWeb.Substring(2, 6);
                            //web.DataReceiveFromWeb = a;
                          //  MessageBox.Show("test :" + web.DataReceiveFromWeb);
                            DisplayData("Ma lenh :" + web.DataReceiveFromWeb, txtShowData);
                            byte[] commandWeb = ConvertTobyte(web.DataReceiveFromWeb);
                            writeByteData(commandWeb);
                        }
                        web.DataReceiveFromWeb = null;
                    }
                    Thread.Sleep(500);
                }
            }
            catch(Exception ex)
            {
               DisplayData(ex.Message.ToString()+ " Khong the lay du lieu tu WEB", txtShowData);
                
            }
        }