//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 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); } }
/// <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 { } }