WcsController.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using Microsoft.AspNetCore.Mvc;
  2. using ServiceCenter;
  3. using System.Net.NetworkInformation;
  4. using System.Text;
  5. using WCS.Core;
  6. using WCS.Entity.Protocol.SRM;
  7. using WCS.WorkEngineering.Systems;
  8. namespace WCS.WorkEngineering.WebApi.Controllers
  9. {
  10. /// <summary>
  11. /// WCS相关接口控制器
  12. /// </summary>
  13. [ApiController]
  14. [Route("api/[controller]/[action]")]
  15. public class WcsController : ControllerBase, IDeviceWriter
  16. {
  17. /// <summary>
  18. /// 设备信息写入接口
  19. /// </summary>
  20. /// <param name="deviceType">需要写入信息的设备类型</param>
  21. /// <param name="devCode">设备编号</param>
  22. /// <param name="protocol">设备协议类名</param>
  23. /// <param name="propName">写入字段名</param>
  24. /// <param name="value">值</param>
  25. [HttpPost]
  26. public void Write(DeviceTypeEnum deviceType, string devCode, string protocol, string propName, string value)
  27. {
  28. World.GetSystemInstance<DeviceWriteSystem>().Invoke(new DeviceWriteInfo
  29. {
  30. DeviceType = deviceType,
  31. Code = devCode,
  32. Protocol = protocol,
  33. Property = propName,
  34. Value = value
  35. });
  36. }
  37. /// <summary>
  38. /// 获取设备配置信息接口
  39. /// </summary>
  40. /// <returns></returns>
  41. [HttpGet]
  42. public List<Device> GetDeviceList()
  43. {
  44. return Device.All.ToList();
  45. }
  46. /// <summary>
  47. /// 获取设备信息
  48. /// </summary>
  49. /// <param name="name">设备名称</param>
  50. /// <returns></returns>
  51. [HttpGet]
  52. public object GetDeviceInfo(string name)
  53. {
  54. var obj = World.GetSystemInstance<GetDeviceSystem>().Invoke(name);
  55. return obj;
  56. }
  57. /// <summary>
  58. /// 堆垛机测试
  59. /// </summary>
  60. /// <param name="srmcod">堆垛机编号</param>
  61. /// <param name="typeEnum">任务类型</param>
  62. /// <param name="value1">起始行</param>
  63. /// <param name="value2">起始列</param>
  64. /// <param name="value3">起始层</param>
  65. /// <param name="value4">目标行</param>
  66. /// <param name="value5">目标列</param>
  67. /// <param name="value6">目标层</param>
  68. [HttpPost]
  69. public void SrmDeBug(string srmcod, SrmTaskTypeEnum typeEnum, short value1, short value2, short value3, short value4, short value5, short value6)
  70. {
  71. World.GetSystemInstance<SrmDebugSystem>().Invoke(new SrmDebugInfo
  72. {
  73. SrmCode = srmcod,
  74. srmTaskType = typeEnum,
  75. SLine = value1,
  76. SCol = value2,
  77. SLayer = value3,
  78. ELine = value4,
  79. ECol = value5,
  80. ELayer = value6
  81. });
  82. }
  83. #region 设备IP相关
  84. /// <summary>
  85. /// 获取设备Ip集合
  86. /// </summary>
  87. /// <returns>设备Ip集合</returns>
  88. [HttpGet]
  89. public List<string> GetDeviceIpList()
  90. {
  91. if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
  92. return ServiceHub.DeviceIPList;
  93. }
  94. /// <summary>
  95. /// 获取设备IP检测结果
  96. /// </summary>
  97. /// <returns>设备IP检测结果</returns>
  98. [HttpGet]
  99. public List<DeviceIpTestResults> DeviceIpTest()
  100. {
  101. if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
  102. List<DeviceIpTestResults> deviceIpTestResults = new List<DeviceIpTestResults>();
  103. ServiceHub.DeviceIPList.ForEach(ip =>
  104. {
  105. deviceIpTestResults.Add(new DeviceIpTestResults
  106. {
  107. Ip = ip,
  108. Result = PingIpOrDomainName(ip)
  109. });
  110. });
  111. return deviceIpTestResults;
  112. }
  113. /// <summary>
  114. /// 检查Ip是否正常联通
  115. /// </summary>
  116. /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
  117. /// <returns></returns>
  118. public static bool PingIpOrDomainName(string strIpOrDName)
  119. {
  120. try
  121. {
  122. Ping objPingSender = new Ping();
  123. PingOptions objPinOptions = new PingOptions();
  124. objPinOptions.DontFragment = true;
  125. string data = "";
  126. byte[] buffer = Encoding.UTF8.GetBytes(data);
  127. int intTimeout = 120;
  128. PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);
  129. string strInfo = objPinReply.Status.ToString();
  130. if (strInfo == "Success")
  131. {
  132. return true;
  133. }
  134. else
  135. {
  136. return false;
  137. }
  138. }
  139. catch (Exception)
  140. {
  141. return false;
  142. }
  143. }
  144. #endregion 设备IP相关
  145. }
  146. public interface IDeviceWriter
  147. {
  148. [HttpPost]
  149. void Write(DeviceTypeEnum deviceType, string devCode, string protocol, string propName, string value);
  150. }
  151. /// <summary>
  152. /// 设备Ip通讯检测结构
  153. /// </summary>
  154. public class DeviceIpTestResults
  155. {
  156. public string Ip { get; set; }
  157. public bool Result { get; set; }
  158. }
  159. }