//Ham boc tach du lieu khi nhan du lieu dinh ky
 public void AutoData(string mesg)
 {
     try
     {
         Database mydatabase = new Database();
         string mac = mesg.Substring(8, 2);
         DataSensor(mesg, ref temperature, ref humidity, ref energy);
         DisplayData("(" + DateTime.Now + "): Du lieu dinh ky :\r\n Sensor " + mesg.Substring(4, 4) + "(" + mac + "): \r\n     Nhiet do: " + Temperature + "\r\n     Do am: " + Humidity + "\r\n     Nang luong : " + Energy + "\r\n", tb);
         string time = DateTime.Now.ToString();
         if (mac[0] == '3')
         {
             if (mydatabase.CheckSensorBC(mac) == "true")
             {
                 mydatabase.setNetworkIpSensorBC(mac, mesg.Substring(4, 4));
                 mydatabase.setStatusSensorBC(mac, true);
             }
             else
             {
                 mydatabase.setSensor_bc(mac, mesg.Substring(4, 4), true);
             }
             mydatabase.SaveDataDB(Temperature, Humidity);
         }
         else
         {
             int iIndex = int.Parse(mac,System.Globalization.NumberStyles.HexNumber);
             timePing[iIndex] = 0;
             if (mydatabase.CheckSensor(mac) == "true")
             {
                 mydatabase.setNetworkIpSensor(mac, mesg.Substring(4, 4));
                 mydatabase.setActiveSensor(mac, true);
             }
             else
             {
                 mydatabase.setNodeSensor(mac, mesg.Substring(4, 4), true);
             }
             string Val = mydatabase.getVanSensor(mac);
             int van = Int32.Parse(Val.Substring(1,1));
             float tempmax = mydatabase.getTempVan(van);
             float humimax = mydatabase.getHumiVan(van);
             bool test = ((Temperature > tempmax) && (Humidity < humimax));
             if (test) //Dieu kien thoa man
             {
             if (DateTime.Now.Hour >= mydatabase.getTimeStart() && DateTime.Now.Hour < mydatabase.getTimeFinish())
              {
              #if USE_CANBANGTAI
                 
                  DisplayData("Thoa man dieu kien bat van (" + mydatabase.getTimeStart() + " h - " + mydatabase.getTimeFinish() + " h)", tb);
                 if(Val == "V6")
                 {
                     DisplayData("Thoa man dieu kien bat van 6 (" + mydatabase.getTimeStart() + " h - " + mydatabase.getTimeFinish() + " h)", tb);
                     AutoBatVan(Val, mesg);
                 }
                 else if (Val == "V5")
                 {
                     int tvan4 = mydatabase.getTimeVan(4);
                     int tvan5 = mydatabase.getTimeVan(5);
                     if (tvan4 > 60)
                     {
                         int tv4phut = tvan4 / 60;
                         int tv4giay = tvan4 - tv4phut * 60;
                         DisplayData("Thoi gian da bat van 4 la:" + tv4phut + " phut "+ tv4giay + " giay", tb);
                     }
                     else
                     {
                         DisplayData("Thoi gian da bat van 4 la:" + tvan4 + " giay", tb);
                     }
                     if (tvan5 > 60)
                     {
                         int tv5phut = tvan5 / 60;
                         int tv5giay = tvan5 - tv5phut * 60;
                         DisplayData("Thoi gian da bat van 5 la:" + tv5phut + " phut " + tv5giay + " giay", tb);
                     }
                     else
                     {
                         DisplayData("Thoi gian da bat van 5 la:" + tvan5 + " giay", tb);
                     }
                     if (tvan4 > tvan5)
                     {
                         DisplayData("Quyet dinh ban van 5:", tb);
                         AutoBatVan("V5", mesg);
                     }
                     else
                     {
                         DisplayData("Quyet dinh ban van 4:", tb);
                         AutoBatVan("V4", mesg);
                         Val = "V4";
                     }
                 }
                 else if (Val == "V1")
                 {
                     int tvan1 = mydatabase.getTimeVan(1);
                     int tvan2 = mydatabase.getTimeVan(2);
                     if (tvan1 > 60)
                     {
                         int tv1phut = tvan1 / 60;
                         int tv1giay = tvan1 - tv1phut * 60;
                         DisplayData("Thoi gian da bat van 1 la:" + tv1phut + " phut " + tv1giay + " giay", tb);
                     }
                     else
                     {
                         DisplayData("Thoi gian da bat van 1 la:" + tvan1 + " giay", tb);
                     }
                     if (tvan2 > 60)
                     {
                         int tv2phut = tvan2 / 60;
                         int tv2giay = tvan2 - tv2phut * 60;
                         DisplayData("Thoi gian da bat van 2 la:" + tv2phut + " phut " + tv2giay + " giay", tb);
                     }
                     else
                     {
                         DisplayData("Thoi gian da bat van 2 la:" + tvan2 + " giay", tb);
                     }
                     if (tvan1 > tvan2)
                     {
                         DisplayData("Quyet dinh ban van 2:", tb);
                         AutoBatVan("V2", mesg);
                         Val = "V2";
                     }
                     else
                     {
                         DisplayData("Quyet dinh ban van 1:", tb);
                         AutoBatVan("V1", mesg);
                     }
                 }
                 else
                 {
                     string val = Val.Substring(1,1);
                     //int van = int.Parse(val);
                     int vantruoc = van - 1;
                     int vansau = van + 1;
                     int timevan = mydatabase.getTimeVan(van);
                     int timevanTruoc = mydatabase.getTimeVan(vantruoc);
                     int timevanSau = mydatabase.getTimeVan(vansau);
                     if (timevan > 60)
                     {
                         int tvphut = timevan / 60;
                         int tvgiay = timevan - tvphut * 60;
                         DisplayData("Thoi gian da bat van "+van+" la:" + tvphut + " phut " + tvgiay + " giay", tb);
                     }
                     else
                     {
                         DisplayData("Thoi gian da bat van " + van+" la:" + timevan + " giay", tb);
                     }
                     if (timevanTruoc > 60)
                     {
                         int tvTphut = timevanTruoc / 60;
                         int tvTgiay = timevanTruoc - tvTphut * 60;
                         DisplayData("Thoi gian da bat van " + vantruoc + " la:" + tvTphut + " phut " + tvTgiay + " giay", tb);
                     }
                     else
                     {
                         DisplayData("Thoi gian da bat van "+ vantruoc +" la:" + timevanTruoc + " giay", tb);
                     }
                     if (timevanSau > 60)
                     {
                         int tvSphut = timevanSau / 60;
                         int tvSgiay = timevanSau - tvSphut * 60;
                         DisplayData("Thoi gian da bat van " + vansau + " la:" + tvSphut + " phut " + tvSgiay + " giay", tb);
                     }
                     else
                     {
                         DisplayData("Thoi gian da bat van " + vansau + " la:" + timevanSau + " giay", tb);
                     }
                     if(timevan > timevanTruoc)
                     {
                         if(timevanTruoc > timevanSau)
                         {
                             DisplayData("Quyet dinh bat van"+vansau+":",tb);
                             AutoBatVan("V"+vansau,mesg);
                             Val = "V" + vansau;
                         }
                         else
                         {
                             DisplayData("Quyet dinh bat van" + vantruoc + ":", tb);
                             AutoBatVan("V" + vantruoc, mesg);
                             Val = "V" + vantruoc;
                         }
                     }
                     else
                     {
                         if (timevan > timevanSau)
                         {
                             DisplayData("Quyet dinh bat van" + vansau + ":", tb);
                             AutoBatVan("V" + vansau, mesg);
                             Val = "V" + vansau;
                         }
                         else
                         {
                             DisplayData("Quyet dinh bat van" + van + ":", tb);
                             AutoBatVan("V" + van, mesg);
                             Val = "V" + van;
                         }
                     }
                 }
                 int timenow = DateTime.Now.Hour * 3600 + DateTime.Now.Minute * 60 + DateTime.Now.Second - 1;
                 timeDapung.Remove(Val);
                 timeDapung.Add(Val, timenow);
              #endif
              #if NO_CANBANGTAI
                 DisplayData("Thoa man dieu kien bat van "+id+" (" + mydatabase.getTimeStart() + " h - " + mydatabase.getTimeFinish() + " h)", tb);
                 AutoBatVan(Val, mesg);
             #endif
              }
              else
              {
                    DisplayData("Khong phai khoang thoi gian bat bom (" + mydatabase.getTimeStart() + " h - "+mydatabase.getTimeFinish()+" h)",tb);
              }
             }
         }
     }
     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);
     }
 }
  //Hamf boc tach ban tin ping node mang
 public void PingNode(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 ping node mang: \r\n Sensor " + mesg.Substring(4, 4) + " (" + check + ") " + " : \r\n Van hoat dong trong mang !!!\r\n", tb);
             if (check[0] == '3')
             {
                  mydatabase.setNetworkIpSensorBC(check, mesg.Substring(4, 4));
                  mydatabase.setStatusSensorBC(check, true);
             }
             else
             {
                 timePing[chck] = 0;
                 checkPing[chck] = false;
                 countTime[chck] = 0;
                 mydatabase.setNetworkIpSensor(check, mesg.Substring(4, 4));
                 mydatabase.setActiveSensor(check, true);
             }
         }
     }
     catch
     {
         DisplayData("Error Ping node", tb);
     }
 }
 /******************************************
  * Giai ma ban tin tra ve tu cong COM
  * Tham so dau vao: Chuoi du lieu nhan tu cong COM
  * Kieu tra ve : void
  * Dinh dang ban tin tra ve:
  * #JN:NNNN MM  Ban tin join mang
  * #AD:NNNN MM D1D2D3D4 D5D6D7D8 E1E2E3E4 Ban tin du lieu dinh ky
  * #RD:NNNN MM D1D2D3D4 D5D6D7D8 E1E2E3E4 Ban tin du lieu theo yeu cau
  * #VL:MM Ban tin thong bao ngu cua mot sensor
  * #OK:NNNN MM SS Ban tin thong bao trang thai actor
  * #SN: NNNN MM SS  Ban tin thong bao trang thai sensor
  * ***************************************/
 public void Converter(string mesg)
 {
     try
     {
         Database myDatabase = new Database();
         switch (mesg[1])
         {
             case 'J':
                 JoinMang(mesg);
                 reload(pictureBox);
                 break;
             case 'A':
                 // VD #AD: NNNN MM D1D2D3D4 D5D6D7D8 EEEE
                 string mac = mesg.Substring(8,2);
                 string status = "";
                 if (mac[0] == '0')
                 {
                     status = myDatabase.getStatusSensor(mac);
                 }
                 AutoData(mesg);
                 if (status != "true" && status != "True")
                 {
                     reload(pictureBox);
                 }
                 break;
             case 'S':
                 // VD: #SN: NNNN MM SS
                 TrangThaiNode(mesg);
                 break;
             case 'V':
                 try
                 {
                     DisplayData("(" + DateTime.Now + "): Sensor " + mesg.Substring(4, 2) + " da vao che do ngu\r\n", tb);
                     myDatabase.setActiveSensor(mesg.Substring(4, 2), false);
                     reload(pictureBox);
                 }
                 catch
                 {
                     DisplayData("Error", tb);
                 }
                 break;
             case 'O':
                 // VD: #OK:NNNN MM SS
                 ThongTinActor(mesg);
                 reload(pictureBox);
                 break;
             case 'R':
                 // VD #RD: NNNN MM D1D2D3D4 D5D6D7D8 EEEE
                 RequestData(mesg);
                 break;
             case 'P':
                 PingNode(mesg);
                 break;
         }
     }
     catch
     {
         DisplayData("Khong the giai ma du lieu", tb);
     }
 }
        public void TuoiCayVal1(object sender, EventArgs e)
        {
            #if ACTOR_COOR
            for (int i = 1; i < 13; i++)
            {
                timePing[i]++;
                if (timePing[i] == 120)
                {
                    Database myDatabase = new Database();
                    byte[] bCommand = new byte[4];
                    string sHex = i.ToString("x").ToUpper();
                    sHex = "0" + sHex;
                    if (myDatabase.getStatusSensor(sHex) == "true" || myDatabase.getStatusSensor(sHex) == "True")
                    {
                        string sIp = myDatabase.getNetworkIpSensor(sHex);
                        int byte0 = int.Parse(sIp.Substring(0,2), System.Globalization.NumberStyles.HexNumber);
                        int byte1 = int.Parse(sIp.Substring(2,2), System.Globalization.NumberStyles.HexNumber);
                        bCommand[0] = (byte)byte0;
                        bCommand[1] = (byte)byte1;
                        bCommand[2] = (byte)255;
                        bCommand[3] = (byte)36;
                        DisplayData("Gui lenh ping node: "+ sHex,tb);
                        WriteData(bCommand);
                        checkPing[i] = true;
                    }
                    
                }

                if (checkPing[i] == true)
                {
                    countTime[i]++;
                    if (countTime[i] == 12)
                    {
                        Database myDatabase = new Database();
                        string sHex = i.ToString("x").ToUpper();
                        sHex = "0" + sHex;
                        myDatabase.setActiveSensor(sHex, false);
                        DisplayData("Sensor "+sHex+" khong con trong mang",tb);
                        countTime[i] = 0;
                        checkPing[i] = false;
                        reload(pictureBox);
                    }
                }
            }
            #endif
            count++;
            #if BC_COOR
            count_BC++;
            if (count_BC > Time_alarm * 12)
            {
                AutoSendCanhBao();
                count_BC = 0;
            }
            #endif
            #if ACTOR_COOR
            if (onofVal1 == true)
            {
                try
                {
                    time_count_val1++;
                    if(time_count_val1 > Time_control *12)
                    {
                        //gui lenh tat van 1
                        Database mydatabase = new Database();
                        string command = mydatabase.getNetworkIpActor("00");

                        command += "010$";
                        byte[] com = ConvertTobyte(command);
                        WriteData(com);

                        onofVal1 = false;
                    }
                }
                catch
                {
                    DisplayData("Khong gui duoc lenh tat van so 1 " + DateTime.Now, tb);
                }
            }
            if (onofVal2 == true)
            {
                try
                {
                    time_count_val2++;
                    if(time_count_val2 > Time_control *12)
                    {
                        //gui lenh tat van 2
                        Database mydatabase = new Database();
                        string command = mydatabase.getNetworkIpActor("00");

                        command += "020$";
                        byte[] com = ConvertTobyte(command);
                        WriteData(com);

                        onofVal2 = false;
                    }
                }
                catch
                {
                    DisplayData("Khong gui duoc lenh tat van so 2 " + DateTime.Now, tb);
                }
            }
            if (onofVal3 == true)
            {
                try
                {
                    time_count_val3++;
                    if(time_count_val3 > Time_control * 12)
                    {
                        //gui lenh tat van 3
                        Database mydatabase = new Database();
                        string command = mydatabase.getNetworkIpActor("00");

                        command += "030$";
                        byte[] com = ConvertTobyte(command);
                        WriteData(com);

                        onofVal3 = false;
                    }
                }
                catch
                {
                    DisplayData("Khong gui duoc lenh tat van so 3 " + DateTime.Now, tb);
                }
            }
            if (onofVal4 == true)
            {
                try
                {
                    time_count_val4++;
                    if(time_count_val4 > Time_control * 12)
                    {
                        //gui lenh tat van 4
                        Database mydatabase = new Database();
                        string command = mydatabase.getNetworkIpActor("00");

                        command += "040$";
                        byte[] com = ConvertTobyte(command);
                        WriteData(com);

                        onofVal4 = false;
                    }
                }
                catch
                {
                    DisplayData("Khong gui duoc lenh tat van so 4 " + DateTime.Now, tb);
                }
            }
            if (onofVal5 == true)
            {
                try
                {
                    time_count_val5++;
                    if(time_count_val5 > Time_control *12)
                    {
                        //gui lenh tat van 5
                        Database mydatabase = new Database();
                        string command = mydatabase.getNetworkIpActor("00");

                        command += "050$";
                        byte[] com = ConvertTobyte(command);
                        WriteData(com);

                        onofVal5 = false;
                    }
                }
                catch
                {
                    DisplayData("Khong gui duoc lenh tat van so 5 " + DateTime.Now, tb);
                }
            }
            if (onofVal6 == true)
            {
                try
                {
                    time_count_val6++;
                    if (time_count_val6 > Time_control * 12)
                    {
                        //gui lenh tat van 6
                        Database mydatabase = new Database();
                        string command = mydatabase.getNetworkIpActor("00");

                        command += "060$";
                        byte[] com = ConvertTobyte(command);
                        WriteData(com);

                        onofVal6 = false;
                    }
                }
                catch
                {
                    DisplayData("Khong gui duoc lenh tat van so 6 " + DateTime.Now, tb);
                }
            }
            #endif
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Lu du lieu cac sensor vao CSDL
 /// Neu dia chi mac cua sensor da ton tai thi update lai status sensor  la true
 /// Neu dia chi mac sensor khong ton tai thi tao node moi
 /// </summary>
 /// <param name="mac"></param>
 /// <param name="ip"></param>
 /// <param name="t"></param>
 /// <param name="h"></param>
 /// <param name="e"></param>
 public void saveDataSensor(string mac, string ip, float t, float h, float e)
 {
     try
     {
         db = new Database();
         string time = DateTime.Now.ToString();
         if (mac[0] == '3')
         {
             if (db.CheckSensorBC(mac) == "true")
             {
                 db.setNetworkIpSensorBC(mac, ip);
                 db.setStatusSensorBC(mac, true);
             }
             else
             {
                 db.setSensor_bc(mac, ip, true);
             }
             db.SaveDataDB(t, h);
             db.updateSensorBC(mac, ip, t, h, e, time);
         }
         else
         {
             if (db.CheckSensor(mac) == "true")
             {
                 db.setNetworkIpSensor(mac,ip);
                 db.setActiveSensor(mac, true);
             }
             else
             {
                 db.setNodeSensor(mac, ip, true);
             }
             db.updateSensor(mac, ip, t, h, e, time);
         }
     }
     catch { }
 }
Ejemplo n.º 7
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;
            }
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Boc tach ban tin thuc ngu
 /// </summary>
 /// <param name="data"></param>
 public void convertImformationSleep(string data)
 {
     try
     {
         db = new Database();
         db.setActiveSensor(data.Substring(4, 2), false);
         sensor.Mac = data.Substring(4,2);
     }
     catch (Exception ex)
     {
         ERR = ex.ToString();
     }
 }