using Microsoft.AspNetCore.Mvc; using ServiceCenter; using System.Net.NetworkInformation; using System.Text; using WCS.Core; using WCS.Entity.Protocol.SRM; using WCS.WorkEngineering.Systems; namespace WCS.WorkEngineering.WebApi.Controllers { /// /// WCS相关接口控制器 /// [ApiController] [Route("api/[controller]/[action]")] public class WcsController : ControllerBase, IDeviceWriter { /// /// 设备信息写入接口 /// /// 需要写入信息的设备类型 /// 设备编号 /// 设备协议类名 /// 写入字段名 /// 值 [HttpPost] public void Write(DeviceTypeEnum deviceType, string devCode, string protocol, string propName, string value) { World.GetSystemInstance().Invoke(new DeviceWriteInfo { DeviceType = deviceType, Code = devCode, Protocol = protocol, Property = propName, Value = value }); } /// /// 获取设备配置信息接口 /// /// [HttpGet] public List GetDeviceList() { return Device.All.ToList(); } /// /// 获取设备信息 /// /// 设备名称 /// [HttpGet] public object GetDeviceInfo(string name) { var obj = World.GetSystemInstance().Invoke(name); return obj; } /// /// 堆垛机测试 /// /// 堆垛机编号 /// 任务类型 /// 起始行 /// 起始列 /// 起始层 /// 目标行 /// 目标列 /// 目标层 [HttpPost] public void SrmDeBug(string srmcod, SrmTaskTypeEnum typeEnum, short value1, short value2, short value3, short value4, short value5, short value6) { World.GetSystemInstance().Invoke(new SrmDebugInfo { SrmCode = srmcod, srmTaskType = typeEnum, SLine = value1, SCol = value2, SLayer = value3, ELine = value4, ECol = value5, ELayer = value6 }); } #region 设备IP相关 /// /// 获取设备Ip集合 /// /// 设备Ip集合 [HttpGet] public List GetDeviceIpList() { if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip"); return ServiceHub.DeviceIPList; } /// /// 获取设备IP检测结果 /// /// 设备IP检测结果 [HttpGet] public List DeviceIpTest() { if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip"); List deviceIpTestResults = new List(); ServiceHub.DeviceIPList.ForEach(ip => { deviceIpTestResults.Add(new DeviceIpTestResults { Ip = ip, Result = PingIpOrDomainName(ip) }); }); return deviceIpTestResults; } /// /// 检查Ip是否正常联通 /// /// 输入参数,表示IP地址或域名 /// 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); } /// /// 设备Ip通讯检测结构 /// public class DeviceIpTestResults { public string Ip { get; set; } public bool Result { get; set; } } }