//Ham boc tach du lieu theo yeu cau
        public void RequestData(string mesg)
        {
            try
            {
                Database mydatabase = new Database();
                DataSensor(mesg, ref temperature, ref humidity, ref energy);
                string _mac = mesg.Substring(8, 2);
                string time1 = DateTime.Now.ToString();
                try
                {
                    int timenow = DateTime.Now.Hour * 3600 + DateTime.Now.Minute * 60 + DateTime.Now.Second;
                    int dapung = timenow - (int)timeDapung[_mac];
                    timeDapung.Remove(_mac);
                    DisplayData("(" + DateTime.Now + "): Du lieu yeu cau (Dap ung: " + dapung + " giay): \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);
                }
                catch
                {
                    DisplayData("(" + DateTime.Now + "): Du lieu yeu cau: \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);
 
                }
                mydatabase.updateSensor(_mac, mesg.Substring(4, 4), Temperature, Humidity, Energy, time1);
                showdata(_mac, mesg.Substring(4, 4), Temperature, Humidity, Energy, mytext);
                if (_mac[0] == '3')
                {
                    mydatabase.SaveDataDB(Temperature, Humidity);
                }
                else {
                    int iIndex = Int32.Parse(_mac, System.Globalization.NumberStyles.HexNumber);
                    timePing[iIndex] = 0;
                }
            }
            catch
            {
                DisplayData("Error du lieu yeu cau", tb);
            }
        }
Exemplo n.º 2
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 { }
 }