private static void DeviceAssist(DeviceMonitorInfomation device) { Ping p = new Ping(); p.PingCompleted += DeviceCallback; string ip = device.IPCount == 1 ? device.IP : device.IP.Split(';')[0]; p.SendAsync(ip, 1000, device); // 超时1秒, 也就是每个DOWN都是1秒,如果DOWN过多,整个Ping就会很耗时 flag.WaitOne(); }
private static void DeviceCallback(object sender, PingCompletedEventArgs e) { DeviceMonitorInfomation device = (DeviceMonitorInfomation)e.UserState; try { device.DeviceNetwork = (e.Reply.Status == IPStatus.Success) ? "UP" : "DOWN"; device.Ping = e.Reply.RoundtripTime.ToString(); if (device.IPCount > 1 && device.DeviceNetwork == "DOWN") { device.IP = device.IP.Split(';')[1]; device.IPCount = device.IPCount - 1; flag.Set(); DeviceAssist(device); } else { device.IP = device.IP.Split(';')[0]; device.RecordTime = DateTime.Now.ToString(); device.PingCompletedFlag[device.I] = "True"; device.DeviceList.Add(device); Console.WriteLine((device.I + 1).ToString() + "/" + device.Total.ToString() + " " + device.StoreNo + ": " + device.DeviceNetwork); flag.Set(); } } catch (Exception ex) { device.DeviceNetwork = "DOWN"; device.Ping = "0"; if (device.IPCount > 1) { device.IP = device.IP.Split(';')[1]; device.IPCount = device.IPCount - 1; flag.Set(); DeviceAssist(device); } else { Console.Write("{0} ", ex.Message); device.RecordTime = DateTime.Now.ToString(); device.PingCompletedFlag[device.I] = "True"; device.DeviceList.Add(device); Console.WriteLine((device.I + 1).ToString() + "/" + device.Total.ToString() + " " + device.StoreNo + ": " + device.DeviceNetwork); flag.Set(); } } }
public static void InsertDeviceMonitorInformation(DeviceMonitorInfomation dmi) { SqlParameter[] paras = { new SqlParameter("@storeNo", dmi.StoreNo), new SqlParameter("@storeRegion", dmi.StoreRegion), new SqlParameter("@storeType", dmi.StoreType), new SqlParameter("@deviceID", dmi.DeviceID), new SqlParameter("@deviceName", dmi.DeviceName), new SqlParameter("@deviceName", dmi.IP), new SqlParameter("@deviceNetwork", dmi.DeviceNetwork), new SqlParameter("@recordTime", dmi.RecordTime.ToString()), new SqlParameter("@ping", dmi.Ping), }; SqlHelper.ExecuteNonQuery("InsertDeviceMonitorInformation", paras); }
// 获取每家门店某个监控设备的IP信息 public static List <DeviceMonitorInfomation> GetStoreMonitorDevices(string deviceID) { List <DeviceMonitorInfomation> list = new List <DeviceMonitorInfomation>(); DataSet ds = SqlHelper.GetStoresDeviceIP(deviceID); // 会去掉禁用该设备ID的门店信息 int rows = ds.Tables[0].Rows.Count; for (int i = 0; i < rows; i++) { DeviceMonitorInfomation storeDevice = new DeviceMonitorInfomation(); storeDevice.StoreNo = ds.Tables[0].Rows[i]["storeNo"].ToString(); storeDevice.StoreRegion = ds.Tables[0].Rows[i]["storeRegion"].ToString(); storeDevice.StoreType = ds.Tables[0].Rows[i]["storeType"].ToString(); storeDevice.DeviceID = ds.Tables[0].Rows[i]["deviceID"].ToString(); storeDevice.DeviceName = ds.Tables[0].Rows[i]["deviceName"].ToString(); storeDevice.IP = ds.Tables[0].Rows[i]["IP"].ToString(); storeDevice.IPCount = ds.Tables[0].Rows[i]["IP"].ToString().Split(';').Length; storeDevice.I = i; list.Add(storeDevice); } return(list); }