WcsController.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using Microsoft.AspNetCore.Mvc;
  2. using Newtonsoft.Json;
  3. using ServiceCenter;
  4. using ServiceCenter.Redis;
  5. using ServiceCenter.SqlSugars;
  6. using SqlSugar.Extensions;
  7. using System.Net.NetworkInformation;
  8. using System.Text;
  9. using WCS.Core;
  10. using WCS.Entity;
  11. using WCS.Entity.Protocol.DataStructure;
  12. using WCS.Entity.Protocol.SRM;
  13. using WCS.WorkEngineering.Extensions;
  14. using WCS.WorkEngineering.Systems;
  15. using WCS.WorkEngineering.WebApi.Models.WCS.Request;
  16. using WCS.WorkEngineering.WebApi.Models.WCS.Response;
  17. namespace WCS.WorkEngineering.WebApi.Controllers
  18. {
  19. /// <summary>
  20. /// WCS相关接口控制器
  21. /// </summary>
  22. [ApiController]
  23. [Route("api/[controller]/[action]")]
  24. public class WcsController : ControllerBase, IDeviceWriter
  25. {
  26. /// <summary>
  27. /// 任务处理接口
  28. /// </summary>
  29. /// <param name="req"></param>
  30. /// <returns></returns>
  31. [HttpPost]
  32. public HandleTaskResponse HandleTask([FromBody] HandleTaskRequest req)
  33. {
  34. var response = new HandleTaskResponse() { IsSuccess = false, Message = "失败" };
  35. //取消任务
  36. if (req.Type == HandleTaskTypeEnum.取消任务)
  37. {
  38. foreach (var item in req.TaskIds)
  39. {
  40. SqlSugarHelper.Do(db =>
  41. {
  42. var task = db.Default.Queryable<WCS_TaskInfo>().Where(t => t.ID == item).First();
  43. if (task != null)
  44. {
  45. //验证wms是否能取消
  46. var res = WmsApi.HandleTaskVerify(new List<int>() { task.ID }, "106");
  47. if (res.ResCode == Models.WMS.Response.ResponseStatusCodeEnum.Sucess)
  48. {
  49. //取消任务
  50. var cancelRes = WmsApi.CarryTaskInfo(new List<int>() { task.ID }, "106");
  51. if (cancelRes.ResCode == Models.WMS.Response.ResponseStatusCodeEnum.Sucess)
  52. {
  53. //更新任务状态
  54. task.Status = Entity.TaskStatus.Cancel;
  55. task.EditTime = DateTime.Now;
  56. task.AddWCS_TASK_DTL(db, "未知", "任务取消");
  57. db.Default.Updateable(task).ExecuteCommand();
  58. response.IsSuccess = true;
  59. response.Message = "成功";
  60. }
  61. else
  62. {
  63. response.IsSuccess = false;
  64. response.Message = $"取消任务失败,{cancelRes.ResMsg}";
  65. }
  66. }
  67. else
  68. {
  69. response.IsSuccess = false;
  70. response.Message = $"取消任务失败,{res.ResMsg}";
  71. }
  72. }
  73. else
  74. {
  75. response.IsSuccess = false;
  76. response.Message = $"未找到对应任务{item}";
  77. }
  78. });
  79. }
  80. }
  81. // 完成任务
  82. else if (req.Type == HandleTaskTypeEnum.完成任务)
  83. {
  84. foreach (var item in req.TaskIds)
  85. {
  86. }
  87. }
  88. return response;
  89. }
  90. /// <summary>
  91. /// 设备信息写入接口
  92. /// </summary>
  93. /// <param name="deviceType">需要写入信息的设备类型</param>
  94. /// <param name="devCode">设备编号</param>
  95. /// <param name="protocol">设备协议类名</param>
  96. /// <param name="propName">写入字段名</param>
  97. /// <param name="value">值</param>
  98. [HttpPost]
  99. public void Write(DeviceTypeEnum deviceType, string devCode, string protocol, string propName, string value)
  100. {
  101. World.GetSystemInstance<DeviceWriteSystem>().Invoke(new DeviceWriteInfo
  102. {
  103. DeviceType = deviceType,
  104. Code = devCode,
  105. Protocol = protocol,
  106. Property = propName,
  107. Value = value
  108. });
  109. }
  110. /// <summary>
  111. /// 设备信息写入接口
  112. /// </summary>
  113. /// <param name="deviceType">需要写入信息的设备类型</param>
  114. /// <param name="devCode">设备编号</param>
  115. /// <param name="protocol">设备协议类名</param>
  116. /// <param name="propName">写入字段名</param>
  117. /// <param name="value">值</param>
  118. [HttpPost]
  119. public void GetDevList()
  120. {
  121. var a = RedisHub.Monitor.LRange("Packs", 0, 80000);
  122. List<DeviceDataPack> packs = new List<DeviceDataPack>();
  123. foreach (var item in a)
  124. {
  125. packs.Add(JsonConvert.DeserializeObject<DeviceDataPack>(item));
  126. }
  127. }
  128. /// <summary>
  129. /// 获取设备配置信息接口
  130. /// </summary>
  131. /// <returns></returns>
  132. [HttpGet]
  133. public List<Device> GetDeviceList()
  134. {
  135. return Device.All.ToList();
  136. }
  137. /// <summary>
  138. /// 获取设备信息
  139. /// </summary>
  140. /// <param name="name">设备名称</param>
  141. /// <returns></returns>
  142. [HttpGet]
  143. public object GetDeviceInfo(string name)
  144. {
  145. var obj = World.GetSystemInstance<GetDeviceSystem>().Invoke(name);
  146. return obj;
  147. }
  148. /// <summary>
  149. /// 堆垛机测试
  150. /// </summary>
  151. /// <param name="srmcod">堆垛机编号</param>
  152. /// <param name="typeEnum">任务类型</param>
  153. /// <param name="value1">起始行</param>
  154. /// <param name="value2">起始列</param>
  155. /// <param name="value3">起始层</param>
  156. /// <param name="value4">目标行</param>
  157. /// <param name="value5">目标列</param>
  158. /// <param name="value6">目标层</param>
  159. [HttpPost]
  160. public void SrmDeBug(string srmcod, SrmTaskTypeEnum typeEnum, short value1, short value2, short value3, short value4, short value5, short value6)
  161. {
  162. World.GetSystemInstance<SrmDebugSystem>().Invoke(new SrmDebugInfo
  163. {
  164. SrmCode = srmcod,
  165. srmTaskType = typeEnum,
  166. SLine = value1,
  167. SCol = value2,
  168. SLayer = value3,
  169. ELine = value4,
  170. ECol = value5,
  171. ELayer = value6
  172. });
  173. }
  174. #region 设备IP相关
  175. /// <summary>
  176. /// 获取设备Ip集合
  177. /// </summary>
  178. /// <returns>设备Ip集合</returns>
  179. [HttpGet]
  180. public List<string> GetDeviceIpList()
  181. {
  182. if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
  183. return ServiceHub.DeviceIPList;
  184. }
  185. /// <summary>
  186. /// 获取设备IP检测结果
  187. /// </summary>
  188. /// <returns>设备IP检测结果</returns>
  189. [HttpGet]
  190. public List<DeviceIpTestResults> DeviceIpTest()
  191. {
  192. if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
  193. List<DeviceIpTestResults> deviceIpTestResults = new List<DeviceIpTestResults>();
  194. ServiceHub.DeviceIPList.ForEach(ip =>
  195. {
  196. deviceIpTestResults.Add(new DeviceIpTestResults
  197. {
  198. Ip = ip,
  199. Result = PingIpOrDomainName(ip)
  200. });
  201. });
  202. return deviceIpTestResults;
  203. }
  204. /// <summary>
  205. /// 检查Ip是否正常联通
  206. /// </summary>
  207. /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
  208. /// <returns></returns>
  209. public static bool PingIpOrDomainName(string strIpOrDName)
  210. {
  211. try
  212. {
  213. Ping objPingSender = new Ping();
  214. PingOptions objPinOptions = new PingOptions();
  215. objPinOptions.DontFragment = true;
  216. string data = "";
  217. byte[] buffer = Encoding.UTF8.GetBytes(data);
  218. int intTimeout = 120;
  219. PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);
  220. string strInfo = objPinReply.Status.ToString();
  221. if (strInfo == "Success")
  222. {
  223. return true;
  224. }
  225. else
  226. {
  227. return false;
  228. }
  229. }
  230. catch (Exception)
  231. {
  232. return false;
  233. }
  234. }
  235. #endregion 设备IP相关
  236. }
  237. public interface IDeviceWriter
  238. {
  239. [HttpPost]
  240. void Write(DeviceTypeEnum deviceType, string devCode, string protocol, string propName, string value);
  241. }
  242. /// <summary>
  243. /// 设备Ip通讯检测结构
  244. /// </summary>
  245. public class DeviceIpTestResults
  246. {
  247. public string Ip { get; set; }
  248. public bool Result { get; set; }
  249. }
  250. }