private void btCreate_Click(object sender, EventArgs e)
        {
            try
            {
                Database my_Database = new Database();
                if (btCreate.Text == "Create Node")
                {
                        if (cbSelectNode.SelectedIndex == 0)
                        {
                            if (txtmac.Text != "" && tbLatitude.Text != "" && tbLongitude.Text != "" && tbActor.Text != "")
                            {
                                string mac = txtmac.Text;
                                int latitude = int.Parse(tbLatitude.Text);
                                int longitude = int.Parse(tbLongitude.Text);
                                string actor = tbActor.Text;
                                if (my_Database.CheckSensor(mac) == "true")
                                {
                                    int a = my_Database.setSensorPixel_x(mac, latitude);
                                    int b = my_Database.setSensorPixel_y(mac, longitude);
                                    int c = my_Database.setVanSensor(mac, actor);
                                    if (a == 1 && b == 1 && c == 1)
                                    {
                                        MessageBox.Show("Saved succeessful! ", "Informations");
                                    }
                                }
                                else
                                {
                                    int d = my_Database.setSensor(mac, latitude, longitude, actor);
                                    if (d == 1)
                                    {
                                        MessageBox.Show("Saved succeessful! ", "Informations");
                                    }
                                }
                            }
                            else
                            {
                                MessageBox.Show("Not enough informations!", "Error");
                            }
                        }
                        else
                        {
                            if (txtmac.Text != "" && tbLatitude.Text != "" && tbLongitude.Text != "")
                            {
                                string mac = txtmac.Text;
                                int latitude = int.Parse(tbLatitude.Text);
                                int longitude = int.Parse(tbLongitude.Text);
                                string actor = tbActor.Text;
                                if (my_Database.CheckActor(mac) == "true")
                                {
                                    int a = my_Database.setActorPixel_x(mac, latitude);
                                    int b = my_Database.setActorPixel_y(mac, longitude);
                                    if (a == 1 && b == 1)
                                    {
                                        MessageBox.Show("Saved succeessful! ", "Informations");
                                    }
                                }
                                else
                                {
                                    int d = my_Database.setActor(mac, latitude, longitude);
                                    if (d == 1)
                                    {
                                        MessageBox.Show("Saved succeessful! ", "Informations");
                                    }
                                }
                            }
                            else
                            {
                                MessageBox.Show("Not enough informations!", "Error");
                            }
                        }
                    }
                if (btCreate.Text == "Delete Node")
                {
                    if (cbmac.SelectedIndex != -1)
                    {
                        string mac = cbmac.Text;
                        DialogResult result = MessageBox.Show("Do you want to delete " + cbSelectNode.Text + " " + cbmac.Text + "?", "Warring",MessageBoxButtons.OKCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1);
                        if (result == DialogResult.OK)
                        {

                            if (cbSelectNode.SelectedIndex == 0)
                            {
                                int totalSensor = my_Database.getTotalSensor();
                                int a = my_Database.deleteSensor(mac);
                                if (a == 1)
                                {
                                    totalSensor--;
                                    my_Database.setTotalSensor(totalSensor);
                                    MessageBox.Show("Deleted succeessful! ", "Informations");
                                }
                                else
                                {
                                    MessageBox.Show("Deleted unsucceessful! ", "Informations");
                                }
                            }
                            else
                            {
                                int totalActor = my_Database.getTotalActor();
                                int a = my_Database.deleteActor(mac);
                                if (a == 1)
                                {
                                    totalActor--;
                                    my_Database.setTotalActor(totalActor);
                                    MessageBox.Show("Deleted succeessful!", "Informations");
                                }
                                else
                                {
                                    MessageBox.Show("Deleted unsucceessful!", "Informations");
                                }
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("You must select node or MAC address!", "Error");
                    }
                }
                if (btCreate.Text == "Edit Node")
                {
                    if (cbSelectNode.SelectedIndex == 0)
                    {
                        if (cbmac.SelectedIndex != -1 && tbLatitude.Text != "" && tbLongitude.Text != "" && tbActor.Text != "")
                        {
                            string mac = cbmac.Text;
                            int latitude = int.Parse(tbLatitude.Text);
                            int longitude = int.Parse(tbLongitude.Text);
                            string actor = tbActor.Text;
                            int a = my_Database.setSensorPixel_x(mac, latitude);
                            int b = my_Database.setSensorPixel_y(mac, longitude);
                            int c = my_Database.setVanSensor(mac, actor);
                            if (a == 1 && b == 1 && c == 1)
                            {
                               MessageBox.Show("Saved succeessful! ", "Informations");
                             }
                        }
                        else
                        {
                            MessageBox.Show("Not enough informations!", "Error");
                        }
                    }
                    else
                    {
                        if (cbmac.SelectedIndex != -1 && tbLatitude.Text != "" && tbLongitude.Text != "")
                        {
                            string mac = cbmac.Text;
                            int latitude = int.Parse(tbLatitude.Text);
                            int longitude = int.Parse(tbLongitude.Text);
                            string actor = tbActor.Text;
                            int a = my_Database.setActorPixel_x(mac, latitude);
                            int b = my_Database.setActorPixel_y(mac, longitude);
                            if (a == 1 && b == 1)
                            {
                                MessageBox.Show("Saved succeessful! ", "Informations");
                             }
                        }
                        else
                        {
                            MessageBox.Show("Not enough informations!", "Error");
                        }
                    }
                }
                comPort.reload(comPort.pictureBox);
            }
            catch { }
        }
 //Ham boc tach thong so khi join mang
 public void JoinMang(string mesg)
 {
     try
     {
         Database mydatabase = new Database();
         string check = mesg.Substring(8, 2);
         int chck = int.Parse(check, System.Globalization.NumberStyles.HexNumber);
         if (chck < 160 && chck > 0)
         {
             DisplayData("(" + DateTime.Now + "): Thong tin gia nhap mang: \r\n Sensor " + mesg.Substring(4, 4) + " (" + check + ") " + " : \r\n Da gia nhap vao mang !!!\r\n", tb);
             if (check[0] == '3')
             {
                 if (mydatabase.CheckSensorBC(check) == "true")
                 {
                     mydatabase.setNetworkIpSensorBC(check, mesg.Substring(4, 4));
                     mydatabase.setStatusSensorBC(check, true);
                 }
                 else
                 {
                     mydatabase.setSensor_bc(check, mesg.Substring(4, 4), true);
                 }
             }
             else
             {
                 timePing[chck] = 0;
                 if (mydatabase.CheckSensor(check) == "true")
                 {
                     mydatabase.setNetworkIpSensor(check, mesg.Substring(4, 4));
                     mydatabase.setActiveSensor(check, true);
                 }
                 else
                 {
                     mydatabase.setNodeSensor(check, mesg.Substring(4, 4), true);
                 }
             }
         }
         if ((160 < chck && chck < 255) || chck == 0)
         {
             DisplayData("(" + DateTime.Now + "): Thong tin trang thai Actor: \r\n Actor " + mesg.Substring(4, 4) + " (" + check + ") " + " : \r\n Van hoat dong trong mang !!!\r\n", tb);
             if (mydatabase.CheckActor(check) == "true")
             {
                 mydatabase.setNetworkIpActor(check, mesg.Substring(4, 4));
                 mydatabase.setStatusActor(check, true);
             }
             else
             {
                 mydatabase.setNodeActor(check, mesg.Substring(4, 4), true);
             }
         }
     }
     catch
     {
         DisplayData("Error Join mang", tb);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Boc tach du lieu cua ban tin gia nhap mang
        /// </summary>
        /// <param name="data"></param>
        public void convertDataJoinNetwork(string data)
        {
            try
            {
                string mac = null;
                string ip = null;
                db = new Database();
                mac = data.Substring(8, 2);
                ip = data.Substring(4, 4);
                if (mac == "00" || mac == "B1")
                {

                    checkSensor = false;
                    actor.Mac = mac;
                    actor.Ip = ip;
                    if (db.CheckActor(mac) == "true")
                    {
                        db.setNetworkIpActor(mac, ip);
                        db.setStatusActor(mac, true);
                    }
                    else
                    {
                        db.setNodeActor(mac, ip, true);
                    }
                }
                else if (mac[0] == '3')
                {
                    checkSensor = true;
                    sensor.Mac = mac;
                    sensor.Ip = ip;
                    if (db.CheckSensorBC(mac) == "true")
                    {
                        db.setNetworkIpSensorBC(mac, ip);
                        db.setStatusSensorBC(mac, true);
                    }
                    else
                    {
                        db.setSensor_bc(mac, ip, true);
                    }
                }
                else
                {
                    checkSensor = true;
                    sensor.Mac = mac;
                    sensor.Ip = ip;
                    if (db.CheckSensor(mac) == "true")
                    {
                        db.setNetworkIpSensor(sensor.Mac, sensor.Ip);
                        db.setActiveSensor(sensor.Mac, true);
                    }
                    else
                    {
                        db.setNodeSensor(sensor.Mac, sensor.Ip, true);
                    }
                }
            }
            catch (Exception ex)
            {
                ERR = ex.Message;
            }
        }