using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Configuration;
using QMAPP.FJC.BLL.Basic;
using QMAPP.FJC.Entity.Basic;
using System.Data;

namespace AutoFileCopyNet
{
    public class NetworkMonitor
    {
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
        //检测本机是否连接到互联网
        private bool IsConnected()
        {
            int I = 0;
            bool state = InternetGetConnectedState(out I, 0);
            return state;
        }
        private string PingResult(string ip)
        {
            try
            {
                System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
                System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
                options.DontFragment = true;
                string data = "12345678901234567890123456789012";
                byte[] buffer = Encoding.ASCII.GetBytes(data);
                int timeout = 1000;
                System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
                //PingTime = int.Parse(reply.RoundtripTime.ToString());
                return reply.Status.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
       // public int MonitorLever;
        public  void MonitoringStart()
        {
            try
            {
                lock (this)
                {
                    //MonitorLever = int.Parse(ConfigurationSettings.AppSettings["MonitorLever"].ToString());
                    MachineInfoBLL bllFisOrder = new MachineInfoBLL();
                   List<MachineInfo> list =  bllFisOrder.GetAllList(new MachineInfo());

                   foreach (MachineInfo row in list)
                    {

                        string strip = row.IPADDRESS.ToString();
                        string strname = row.MACHINENAME.ToString();
                        string constatus = PingResult(strip);
                        if (constatus != "Success")
                        {
                            string eeror = strname + "(" + strip + ")" + "网络ping不通";
                            //插入报警信息

                            //DataTable wtb = bllFisOrder.QueryWaring(strip);
                            //if (wtb.Rows.Count == 0)
                            //{
                            //    bllFisOrder.InsertWaring(eeror, strip);
                            //}
                            //else
                            //{
                            //    bllFisOrder.UpdateWaring(strip);
                            //}
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
            }
        }
    }

}