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