using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using PlcSiemens.Core.Extension;
using ServiceCenter;
using ServiceCenter.Attributes;
using ServiceCenter.Extensions;
using ServiceCenter.Logs;
using ServiceCenter.Redis;
using ServiceCenter.SqlSugars;
using System.Net.NetworkInformation;
using System.Text;
using WCS.Core;
using WCS.Entity;
using WCS.Entity.Protocol.DataStructure;
using WCS.Entity.Protocol.SRM;
using WCS.WorkEngineering.Extensions;
using WCS.WorkEngineering.Systems;
using WCS.WorkEngineering.WebApi.Models.AGV.Response;
using WCS.WorkEngineering.WebApi.Models.WCS.Request;
using WCS.WorkEngineering.WebApi.Models.WCS.Response;
using WCS.WorkEngineering.WebApi.Models.WMS.Response;

namespace WCS.WorkEngineering.WebApi.Controllers
{
    /// <summary>
    /// WCS相关接口控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class WcsController : ControllerBase, IDeviceWriter
    {
        /// <summary>
        ///  任务处理接口
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost, Log("任务处理接口")]
        public SRes<HandleTaskResponse> HandleTask([FromBody] HandleTaskRequest req)
        {
            SRes<HandleTaskResponse> response = new SRes<HandleTaskResponse>() { ResCode = ResponseStatusCodeEnum.Sucess, ResDataList = new List<HandleTaskResponse>() };

            //取消任务
            if (req.Type == HandleTaskTypeEnum.取消任务)
            {
                SqlSugarHelper.Do(db =>
                {
                    foreach (var item in req.TaskIds)
                    {
                        var task = db.Default.Queryable<WCS_TaskInfo>().Where(t => t.ID == item).First();
                        if (task != null)
                        {
                            //验证wms是否能取消
                            SRes res = HandleTaskVerify(response, item, 106);
                            if (res == null) continue;

                            switch (task.Type)
                            {
                                case TaskType.SetPlate:
                                    if (task.Status != Entity.TaskStatus.WaitingToExecute)
                                    {
                                        response.ResDataList.Add(new HandleTaskResponse()
                                        {
                                            IsSuccess = false,
                                            TaskNo = item,
                                            Message = $"只能取消待执行状态组盘任务",
                                        });
                                        continue;
                                    }
                                    break;

                                case TaskType.EnterDepot:
                                    if (task.Status > Entity.TaskStatus.WaitingToExecute && task.Status > Entity.TaskStatus.AGVExecution)
                                    {
                                        response.ResDataList.Add(new HandleTaskResponse()
                                        {
                                            IsSuccess = false,
                                            TaskNo = item,
                                            Message = $"只能取消待执行状态入库任务",
                                        });
                                        continue;
                                    }
                                    break;

                                case TaskType.OutDepot:
                                    if (task.Status > Entity.TaskStatus.WaitingToExecute)
                                    {
                                        response.ResDataList.Add(new HandleTaskResponse()
                                        {
                                            IsSuccess = false,
                                            TaskNo = item,
                                            Message = $"只能取消待执行状态出库任务",
                                        });
                                        continue;
                                    }

                                    break;

                                case TaskType.TransferDepot:
                                    if (task.Status > Entity.TaskStatus.WaitingToExecute)
                                    {
                                        response.ResDataList.Add(new HandleTaskResponse()
                                        {
                                            IsSuccess = false,
                                            TaskNo = item,
                                            Message = $"无法取消{task.Status.GetDescription()}的移库任务,只能取消新建/待执行的移库任务",
                                        });
                                        continue;
                                    }
                                    break;

                                case TaskType.Delivery:

                                    break;

                                case TaskType.EmptyInit:
                                    if (task.Status != Entity.TaskStatus.WaitingToExecute)
                                    {
                                        response.ResDataList.Add(new HandleTaskResponse()
                                        {
                                            IsSuccess = false,
                                            TaskNo = item,
                                            Message = $"只能取消待执行状态空轮初始化任务",
                                        });
                                        continue;
                                    }
                                    break;
                            }

                            SRes cancelRes = CarryTaskInfo(response, item, 106);
                            if (cancelRes == null) continue;

                            //找到对应的AGV任务
                            var agv = db.Default.Queryable<WCS_AgvTaskInfo>().Where(v => v.ID == task.AgvTaskID && v.AgvStatus < AGVTaskStatus.MissionCompleted).SplitTable(v => v.Take(2)).First();
                            if (agv != null)
                            {
                                if (!agv.AgvID.IsNullOrEmpty())
                                {
                                    if (task.Floor == 2)//二楼调用海康一键抹除
                                    {
                                        var cancelTaskUpdateRes = DelAgvTask(response, item, agv.AgvID, task.AddWho != "wms" && !string.IsNullOrEmpty(task.AddWho) ? task.AddWho : "");
                                    }
                                    else
                                    {
                                        var cancelTaskUpdateRes = CancelAgvTask(response, item, agv.AgvID);
                                        //if (cancelTaskUpdateRes == null) continue;
                                    }
                                   ;
                                }
                                agv.Status = AGVTaskStatus.Cancel;
                                agv.AgvStatus = AGVTaskStatus.Cancel;
                                db.Default.Updateable(agv).SplitTable(x => x.Take(2)).ExecuteCommand();
                            }
                            //更新任务状态
                            task.Status = Entity.TaskStatus.Cancel;
                            task.EedTime = DateTime.Now;
                            task.EditWho = req.User;
                            task.ManualRemarks = req.ManualRemarks;
                            task.AddWCS_TASK_DTL(db, "未知", "任务取消");
                            db.Default.Updateable(task).ExecuteCommand();
                            task.CompleteOrCancelTasks(db);
                        }
                        else
                        {
                            response.ResDataList.Add(new HandleTaskResponse()
                            {
                                IsSuccess = false,
                                TaskNo = item,
                                Message = $"未找到对应任务{item}"
                            });
                        }
                    }
                });
            }
            // 完成任务
            else if (req.Type == HandleTaskTypeEnum.完成任务)
            {
                SqlSugarHelper.Do(db =>
                {
                    foreach (var item in req.TaskIds)
                    {
                        var task = db.Default.Queryable<WCS_TaskInfo>().Where(t => t.ID == item).First();
                        if (task != null)
                        {
                            if (task.Type == TaskType.OutDepot)
                            {
                                SRes res = HandleTaskVerify(response, item, 99);
                                if (res == null) continue;

                                switch (task.Type)
                                {
                                    case TaskType.OutDepot:
                                        if (task.Status >= Entity.TaskStatus.Finish)
                                        {
                                            response.ResDataList.Add(new HandleTaskResponse()
                                            {
                                                IsSuccess = false,
                                                TaskNo = item,
                                                Message = $"只能完成未完成状态的任务",
                                            });
                                            continue;
                                        }
                                        break;
                                }

                                SRes cancelRes = CarryTaskInfo(response, item, 99);
                                if (cancelRes == null) continue;

                                //找到对应的AGV任务
                                var agv = db.Default.Queryable<WCS_AgvTaskInfo>().Where(v => v.ID == task.AgvTaskID && v.AgvStatus < AGVTaskStatus.MissionCompleted).SplitTable(v => v.Take(2)).First();
                                if (agv != null)
                                {
                                    //if (!agv.AgvID.IsNullOrEmpty())
                                    //{
                                    //    var cancelTaskUpdateRes = CancelAgvTask(response, item, agv.AgvID);
                                    //    if (cancelTaskUpdateRes == null) continue;
                                    //}
                                    agv.Status = AGVTaskStatus.MissionCompleted;
                                    agv.AgvStatus = AGVTaskStatus.MissionCompleted;
                                    db.Default.Updateable(agv).SplitTable(x => x.Take(2)).ExecuteCommand();
                                }
                                //更新任务状态
                                task.Status = Entity.TaskStatus.Finish;
                                task.EedTime = DateTime.Now;
                                task.EditWho = req.User;
                                task.ManualRemarks = req.ManualRemarks;
                                task.AddWCS_TASK_DTL(db, "未知", "任务完成");
                                db.Default.Updateable(task).ExecuteCommand();
                                task.CompleteOrCancelTasks(db);
                            }
                            else if (task.Type == TaskType.EnterDepot)
                            {
                                SRes res = HandleTaskVerify(response, item, 99);
                                if (res == null) continue;

                                switch (task.Type)
                                {
                                    case TaskType.EnterDepot:
                                        if (task.Status >= Entity.TaskStatus.Finish)
                                        {
                                            response.ResDataList.Add(new HandleTaskResponse()
                                            {
                                                IsSuccess = false,
                                                TaskNo = item,
                                                Message = $"只能完成未完成状态的任务",
                                            });
                                            continue;
                                        }
                                        if (task.AddrTo.Length < 6 && task.TaskGroupKey != "1")
                                        {
                                            response.ResDataList.Add(new HandleTaskResponse()
                                            {
                                                IsSuccess = false,
                                                TaskNo = item,
                                                Message = $"只能完成已分配货位的任务",
                                            });
                                            continue;
                                        }
                                        break;
                                }

                                SRes cancelRes = CarryTaskInfo(response, item, 99);
                                if (cancelRes == null) continue;

                                //找到对应的AGV任务
                                var agv = db.Default.Queryable<WCS_AgvTaskInfo>().Where(v => v.ID == task.AgvTaskID && v.AgvStatus < AGVTaskStatus.MissionCompleted).SplitTable(v => v.Take(2)).First();
                                if (agv != null)
                                {
                                    //if (!agv.AgvID.IsNullOrEmpty())
                                    //{
                                    //    var cancelTaskUpdateRes = CancelAgvTask(response, item, agv.AgvID);
                                    //    if (cancelTaskUpdateRes == null) continue;
                                    //}
                                    agv.Status = AGVTaskStatus.MissionCompleted;
                                    agv.AgvStatus = AGVTaskStatus.MissionCompleted;
                                    db.Default.Updateable(agv).SplitTable(x => x.Take(2)).ExecuteCommand();
                                }
                                //更新任务状态
                                task.Status = Entity.TaskStatus.Finish;
                                task.EedTime = DateTime.Now;
                                task.EditWho = req.User;
                                task.ManualRemarks = req.ManualRemarks;
                                task.AddWCS_TASK_DTL(db, "未知", "任务完成");
                                db.Default.Updateable(task).ExecuteCommand();
                                task.CompleteOrCancelTasks(db);
                            }
                            else if (task.Type == TaskType.TransferDepot)
                            {
                                SRes res = HandleTaskVerify(response, item, 99);
                                if (res == null) continue;

                                switch (task.Type)
                                {
                                    case TaskType.TransferDepot:
                                        //if (task.Status >= Entity.TaskStatus.Finish)
                                        //{
                                        //    response.ResDataList.Add(new HandleTaskResponse()
                                        //    {
                                        //        IsSuccess = false,
                                        //        TaskNo = item,
                                        //        Message = $"只能完成未完成状态的任务",
                                        //    });
                                        //    continue;
                                        //}
                                        if (task.AddrTo.Length < 6)
                                        {
                                            response.ResDataList.Add(new HandleTaskResponse()
                                            {
                                                IsSuccess = false,
                                                TaskNo = item,
                                                Message = $"只能完成已分配货位的任务",
                                            });
                                            continue;
                                        }
                                        break;
                                }
                                SRes cancelRes = CarryTaskInfo(response, item, 99);
                                if (cancelRes == null) continue;

                                //更新任务状态
                                task.Status = Entity.TaskStatus.Finish;
                                task.EedTime = DateTime.Now;
                                task.EditWho = req.User;
                                task.ManualRemarks = req.ManualRemarks;
                                task.AddWCS_TASK_DTL(db, "未知", "任务完成");
                                db.Default.Updateable(task).ExecuteCommand();
                                task.CompleteOrCancelTasks(db);
                            }
                            else if (task.Type == TaskType.Delivery)
                            {
                                //找到对应的AGV任务
                                var agv = db.Default.Queryable<WCS_AgvTaskInfo>().Where(v => v.ID == task.AgvTaskID && v.AgvStatus < AGVTaskStatus.MissionCompleted).SplitTable(v => v.Take(2)).First();
                                if (agv != null)
                                {                                  
                                    agv.Status = AGVTaskStatus.MissionCompleted;
                                    agv.AgvStatus = AGVTaskStatus.MissionCompleted;
                                    db.Default.Updateable(agv).SplitTable(x => x.Take(2)).ExecuteCommand();
                                }
                                //更新任务状态
                                task.Status = Entity.TaskStatus.Finish;
                                task.EedTime = DateTime.Now;
                                task.EditWho = req.User;
                                task.ManualRemarks = req.ManualRemarks;
                                task.AddWCS_TASK_DTL(db, "未知", "任务完成");
                                db.Default.Updateable(task).ExecuteCommand();
                                task.CompleteOrCancelTasks(db);
                            }
                            else
                            {
                                response.ResDataList.Add(new HandleTaskResponse()
                                {
                                    IsSuccess = false,
                                    TaskNo = item,
                                    Message = $"未找到对应任务{item}"
                                });
                            }
                        }
                    }
                });
            }
            else if (req.Type == HandleTaskTypeEnum.重新下发AGV任务)
            {
                SqlSugarHelper.Do(db =>
                {
                    foreach (var item in req.TaskIds)
                    {
                        var task = db.Default.Queryable<WCS_TaskInfo>().Where(t => t.ID == item).First();
                        if (task != null)
                        {
                            if (task.Type == TaskType.SetPlate) //组盘任务
                            {
                                response.ResDataList.Add(new HandleTaskResponse()
                                {
                                    IsSuccess = false,
                                    TaskNo = item,
                                    Message = $"组盘任务无AGV执行流程",
                                });
                                continue;
                            }
                            else if (task.Type == TaskType.EnterDepot) //入库任务
                            {
                                if (task.Floor == 1) //一楼入库
                                {
                                }
                                else if (task.Floor == 2) //二楼入库
                                {
                                    response.ResDataList.Add(new HandleTaskResponse()
                                    {
                                        IsSuccess = false,
                                        TaskNo = item,
                                        Message = $"二楼入库任务重新下发AGV未实现",
                                    });
                                    continue;
                                }
                            }
                            else if (task.Type == TaskType.OutDepot) //出库
                            {
                            }
                            else if (task.Type == TaskType.TransferDepot) //移库
                            {
                                response.ResDataList.Add(new HandleTaskResponse()
                                {
                                    IsSuccess = false,
                                    TaskNo = item,
                                    Message = $"组盘任务无AGV执行流程",
                                });
                                continue;
                            }
                            else if (task.Type == TaskType.Delivery) //搬运
                            {
                                if (task.Floor == 1)
                                {
                                }
                                else if (task.Floor == 2)
                                {
                                    response.ResDataList.Add(new HandleTaskResponse()
                                    {
                                        IsSuccess = false,
                                        TaskNo = item,
                                        Message = $"二楼搬运任务重新下发AGV未实现",
                                    });
                                    continue;
                                }
                            }
                            else if (task.Type == TaskType.EmptyInit) //空轮初始化
                            {
                                response.ResDataList.Add(new HandleTaskResponse()
                                {
                                    IsSuccess = false,
                                    TaskNo = item,
                                    Message = $"空轮初始化无AGV执行流程",
                                });
                                continue;
                            }

                            //找到对应的AGV任务
                            var agv = db.Default.Queryable<WCS_AgvTaskInfo>().Where(v => v.ID == task.AgvTaskID).SplitTable(v => v.Take(2)).First();
                            if (agv != null)
                            {
                                agv.Status = AGVTaskStatus.NewBuild;
                                agv.AgvStatus = AGVTaskStatus.NewBuild;
                                db.Default.Updateable(agv).SplitTable(x => x.Take(2)).ExecuteCommand();
                            }
                            if (task.Floor == 1)
                            {
                                task.Status = Entity.TaskStatus.WaitingToExecute;
                            }
                            else if (task.Floor == 2)
                            {
                                task.Status = Entity.TaskStatus.ConveyorExecution;
                            }

                            task.AddWCS_TASK_DTL(db, "AGV", "重新下发AGV任务");
                            db.Default.Updateable(task).ExecuteCommand();
                        }
                        else
                        {
                            response.ResDataList.Add(new HandleTaskResponse()
                            {
                                IsSuccess = false,
                                TaskNo = item,
                                Message = $"未找到对应任务{item}"
                            });
                        }
                    }
                });
            }
            return response;
        }

        /// <summary>
        ///  WMS完成或取消任务验证
        /// </summary>
        /// <param name="sRes"></param>
        /// <param name="id"></param>
        /// <param name="agvTask">需要取消任务的AGV任务号</param>
        /// <returns></returns>
        [HttpGet]
        public CancelTaskResponse? CancelAgvTask(SRes<HandleTaskResponse> sRes, int id, string agvTask)
        {
            try
            {
                var res = AgvApi.CancelAgvTask(agvTask);
                return res;
            }
            catch (Exception ex)
            {
                sRes.ResDataList.Add(new HandleTaskResponse()
                {
                    IsSuccess = false,
                    TaskNo = id,
                    Message = $"AGV错误:{ex.Message}",
                });
                return null;
            }
        }
        /// <summary>
        ///  一键抹除海康任务
        /// </summary>
        /// <param name="sRes"></param>
        /// <param name="id"></param>
        /// <param name="agvTask">需要取消任务的AGV任务号</param>
        /// <param name="reqCode">请求编号</param>
        /// <returns></returns>
        [HttpGet]
        public CancelTaskResponse? DelAgvTask(SRes<HandleTaskResponse> sRes, int id, string agvTask, string reqCode)
        {
            try
            {
                var res = AgvApi.DelAgvTask(agvTask, reqCode);
                return res;
            }
            catch (Exception ex)
            {
                sRes.ResDataList.Add(new HandleTaskResponse()
                {
                    IsSuccess = false,
                    TaskNo = id,
                    Message = $"AGV错误:{ex.Message}",
                });
                return null;
            }
        }

        /// <summary>
        ///  WMS完成或取消任务验证
        /// </summary>
        /// <param name="sRes"></param>
        /// <param name="id"></param>
        /// <param name="type">99完成,106取消</param>
        /// <returns></returns>
        [HttpGet]
        public SRes? HandleTaskVerify(SRes<HandleTaskResponse> sRes, int id, int type)
        {
            try
            {
                var res = WmsApi.HandleTaskVerify(new List<int>() { id }, type);
                return res;
            }
            catch (Exception ex)
            {
                sRes.ResDataList.Add(new HandleTaskResponse()
                {
                    IsSuccess = false,
                    TaskNo = id,
                    Message = $"WMS错误:{ex.Message}",
                });
                return null;
            }
        }

        /// <summary>
        ///  WMS完成或取消任务执行
        /// </summary>
        /// <param name="sRes"></param>
        /// <param name="id"></param>
        /// <param name="type">99完成,106取消</param>
        /// <returns></returns>
        [HttpGet]
        public SRes? CarryTaskInfo(SRes<HandleTaskResponse> sRes, int id, int type)
        {
            try
            {
                var res = WmsApi.CarryTaskInfo(new List<int>() { id }, type);
                return res;
            }
            catch (Exception ex)
            {
                sRes.ResDataList.Add(new HandleTaskResponse()
                {
                    IsSuccess = false,
                    TaskNo = id,
                    Message = $"WMS错误:{ex.Message}",
                });
                return null;
            }
        }

        /// <summary>
        ///  
        /// </summary>
        /// <param name="sRes"></param>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet]
        public SRes? CancelAgvTaskUpdate(SRes<HandleTaskResponse> sRes, int id, int type)
        {
            try
            {
                var res = WmsApi.CarryTaskInfo(new List<int>() { id }, type);
                return res;
            }
            catch (Exception ex)
            {
                sRes.ResDataList.Add(new HandleTaskResponse()
                {
                    IsSuccess = false,
                    TaskNo = id,
                    Message = ex.Message,
                });
                return null;
            }
        }

        /// <summary>
        ///  设备信息写入接口
        /// </summary>
        /// <param name="deviceType">需要写入信息的设备类型</param>
        /// <param name="devCode">设备编号</param>
        /// <param name="protocol">设备协议类名</param>
        /// <param name="propName">写入字段名</param>
        /// <param name="value">值</param>
        [HttpPost]
        public void Write(DeviceTypeEnum deviceType, string devCode, string protocol, string propName, string value)
        {
            World.GetSystemInstance<DeviceWriteSystem>().Invoke(new DeviceWriteInfo
            {
                DeviceType = deviceType,
                Code = devCode,
                Protocol = protocol,
                Property = propName,
                Value = value
            });
        }

        /// <summary>
        ///  设备信息写入接口
        /// </summary>
        /// <param name="deviceType">需要写入信息的设备类型</param>
        /// <param name="devCode">设备编号</param>
        /// <param name="protocol">设备协议类名</param>
        /// <param name="propName">写入字段名</param>
        /// <param name="value">值</param>
        [HttpPost]
        public void GetDevList()
        {
            var a = RedisHub.Monitor.LRange("Packs", 0, 80000);
            List<DeviceDataPack> packs = new List<DeviceDataPack>();
            foreach (var item in a)
            {
                packs.Add(JsonConvert.DeserializeObject<DeviceDataPack>(item));
            }
        }

        /// <summary>
        ///  获取设备配置信息接口
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<Device> GetDeviceList()
        {
            return Device.All.ToList();
        }

        /// <summary>
        ///  获取设备信息
        /// </summary>
        /// <param name="name">设备名称</param>
        /// <returns></returns>
        [HttpGet]
        public object GetDeviceInfo(string name)
        {
            var remoteIpAddress = HttpContext.Connection.RemoteIpAddress.ToString();
            LogHub.InterfacePublish(nameof(GetDeviceInfo), $"IP:{remoteIpAddress}--传入参数--{JsonConvert.SerializeObject(name)}");
            var obj = World.GetSystemInstance<GetDeviceSystem>().Invoke(name);
            LogHub.InterfacePublish(nameof(GetDeviceInfo), $"IP:{remoteIpAddress}--返回结果--{JsonConvert.SerializeObject(obj)}");
            return obj;
        }

        /// <summary>
        ///  堆垛机测试
        /// </summary>
        /// <param name="srmcod">堆垛机编号</param>
        /// <param name="typeEnum">任务类型</param>
        /// <param name="value1">起始行</param>
        /// <param name="value2">起始列</param>
        /// <param name="value3">起始层</param>
        /// <param name="value4">目标行</param>
        /// <param name="value5">目标列</param>
        /// <param name="value6">目标层</param>
        [HttpPost]
        public void SrmDeBug(string srmcod, SrmTaskTypeEnum typeEnum, short value1, short value2, short value3, short value4, short value5, short value6)
        {
            World.GetSystemInstance<SrmDebugSystem>().Invoke(new SrmDebugInfo
            {
                SrmCode = srmcod,
                srmTaskType = typeEnum,
                SLine = value1,
                SCol = value2,
                SLayer = value3,
                ELine = value4,
                ECol = value5,
                ELayer = value6
            });
        }

        #region 设备IP相关

        /// <summary>
        ///  获取设备Ip集合
        /// </summary>
        /// <returns>设备Ip集合</returns>
        [HttpGet]
        public List<string> GetDeviceIpList()
        {
            if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
            return ServiceHub.DeviceIPList;
        }

        /// <summary>
        ///  获取设备IP检测结果
        /// </summary>
        /// <returns>设备IP检测结果</returns>
        [HttpGet]
        public List<DeviceIpTestResults> DeviceIpTest()
        {
            if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
            List<DeviceIpTestResults> deviceIpTestResults = new List<DeviceIpTestResults>();
            ServiceHub.DeviceIPList.ForEach(ip =>
            {
                deviceIpTestResults.Add(new DeviceIpTestResults
                {
                    Ip = ip,
                    Result = PingIpOrDomainName(ip)
                });
            });

            return deviceIpTestResults;
        }

        /// <summary>
        /// 检查Ip是否正常联通
        /// </summary>
        /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
        /// <returns></returns>
        [HttpGet]
        public static bool PingIpOrDomainName(string strIpOrDName)
        {
            try

            {
                Ping objPingSender = new Ping();

                PingOptions objPinOptions = new PingOptions();

                objPinOptions.DontFragment = true;

                string data = "";

                byte[] buffer = Encoding.UTF8.GetBytes(data);

                int intTimeout = 120;

                PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);

                string strInfo = objPinReply.Status.ToString();

                if (strInfo == "Success")

                {
                    return true;
                }
                else

                {
                    return false;
                }
            }
            catch (Exception)

            {
                return false;
            }
        }

        #endregion 设备IP相关
    }

    public interface IDeviceWriter
    {
        [HttpPost]
        void Write(DeviceTypeEnum deviceType, string devCode, string protocol, string propName, string value);
    }

    /// <summary>
    ///  设备Ip通讯检测结构
    /// </summary>
    public class DeviceIpTestResults
    {
        public string Ip { get; set; }

        public bool Result { get; set; }
    }
}
