using LangGuan.Command.Model.EntityModel; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Net.NetworkInformation; using System.Threading; using System.Threading.Tasks; namespace LangGuan.Services { /// /// ping service /// public class PingService { private DeviceSerive _deviceSerive; /// /// /// /// public PingService(DeviceSerive deviceSerive) { _deviceSerive = deviceSerive; } /// /// /// public void CreatTask() { //5分钟执行一次 Timer myTimer = new Timer(new TimerCallback(Execute), null, 2000, 300000); // Timer myTimer = new Timer(new TimerCallback(Execute), "ping service", 2000, 10000); } /// /// /// /// public async void Execute(object b) { var deviceList = await _deviceSerive.GetAsync(); if (deviceList.Any()) { foreach (var item in deviceList) { if (!string.IsNullOrEmpty(item.deviceIp)) { var ip = item.deviceIp.Split(":")[0]; var res = await PingIp(ip); item.state = res == true ? 1 : 0; await _deviceSerive.UpdateState(item); } } } Console.WriteLine("{0} ping running.", (string)b); } /// /// ping /// /// /// public async Task PingIp(string ip) { try { //Ping 实例对象; Ping pingSender = new Ping(); PingReply pingReply = await pingSender.SendPingAsync(ip); if (pingReply.Status == IPStatus.Success) { return true; } } catch (Exception) { return false; } return false; } } }