WcsController.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. using Microsoft.AspNetCore.Mvc;
  2. using Newtonsoft.Json;
  3. using PlcSiemens.Core.Extension;
  4. using ServiceCenter;
  5. using ServiceCenter.Attributes;
  6. using ServiceCenter.Extensions;
  7. using ServiceCenter.Logs;
  8. using ServiceCenter.Redis;
  9. using ServiceCenter.SqlSugars;
  10. using System.Net.NetworkInformation;
  11. using System.Text;
  12. using WCS.Core;
  13. using WCS.Entity;
  14. using WCS.Entity.Protocol.DataStructure;
  15. using WCS.WorkEngineering.Extensions;
  16. using WCS.WorkEngineering.Protocol.SRM;
  17. using WCS.WorkEngineering.Systems;
  18. using WCS.WorkEngineering.WebApi.Models.WCS.Request;
  19. using WCS.WorkEngineering.WebApi.Models.WCS.Response;
  20. using WCS.WorkEngineering.WebApi.Models.WMS.Response;
  21. namespace WCS.WorkEngineering.WebApi.Controllers
  22. {
  23. /// <summary>
  24. /// WCS相关接口控制器
  25. /// </summary>
  26. [ApiController]
  27. [Route("api/[controller]/[action]")]
  28. public class WcsController : ControllerBase, IDeviceWriter
  29. {
  30. /// <summary>
  31. /// 任务处理接口
  32. /// </summary>
  33. /// <param name="req"></param>
  34. /// <returns></returns>
  35. [HttpPost, Log("任务处理接口")]
  36. public SRes<HandleTaskResponse> HandleTask([FromBody] HandleTaskRequest req)
  37. {
  38. SRes<HandleTaskResponse> response = new SRes<HandleTaskResponse>() { ResCode = ResponseStatusCodeEnum.Sucess, ResDataList = new List<HandleTaskResponse>() };
  39. //取消任务
  40. if (req.Type == HandleTaskTypeEnum.取消任务)
  41. {
  42. SqlSugarHelper.Do(db =>
  43. {
  44. foreach (var item in req.TaskIds)
  45. {
  46. var task = db.Default.Queryable<WCS_TaskInfo>().Where(t => t.ID == item).First();
  47. if (task != null)
  48. {
  49. //验证wms是否能取消
  50. SRes res = WmsApi.HandleTaskVerify(response, item, 106);
  51. if (res == null) continue;
  52. switch (task.Type)
  53. {
  54. case TaskType.SetPlate:
  55. if (task.Status != Entity.TaskStatus.WaitingToExecute)
  56. {
  57. response.ResDataList.Add(new HandleTaskResponse()
  58. {
  59. IsSuccess = false,
  60. TaskNo = item,
  61. Message = $"只能取消待执行状态组盘任务",
  62. });
  63. continue;
  64. }
  65. break;
  66. case TaskType.EnterDepot:
  67. if (task.Status > Entity.TaskStatus.WaitingToExecute && task.Status > Entity.TaskStatus.AGVExecution)
  68. {
  69. response.ResDataList.Add(new HandleTaskResponse()
  70. {
  71. IsSuccess = false,
  72. TaskNo = item,
  73. Message = $"只能取消待执行状态入库任务",
  74. });
  75. continue;
  76. }
  77. break;
  78. case TaskType.OutDepot:
  79. if (task.Status > Entity.TaskStatus.WaitingToExecute)
  80. {
  81. response.ResDataList.Add(new HandleTaskResponse()
  82. {
  83. IsSuccess = false,
  84. TaskNo = item,
  85. Message = $"只能取消待执行状态出库任务",
  86. });
  87. continue;
  88. }
  89. break;
  90. case TaskType.TransferDepot:
  91. if (task.Status > Entity.TaskStatus.WaitingToExecute)
  92. {
  93. response.ResDataList.Add(new HandleTaskResponse()
  94. {
  95. IsSuccess = false,
  96. TaskNo = item,
  97. Message = $"无法取消{task.Status.GetDescription()}的移库任务,只能取消新建/待执行的移库任务",
  98. });
  99. continue;
  100. }
  101. break;
  102. case TaskType.Delivery:
  103. break;
  104. case TaskType.EmptyInit:
  105. if (task.Status != Entity.TaskStatus.WaitingToExecute)
  106. {
  107. response.ResDataList.Add(new HandleTaskResponse()
  108. {
  109. IsSuccess = false,
  110. TaskNo = item,
  111. Message = $"只能取消待执行状态空轮初始化任务",
  112. });
  113. continue;
  114. }
  115. break;
  116. }
  117. SRes cancelRes = WmsApi.CarryTaskInfo(response, item, 106);
  118. if (cancelRes == null) continue;
  119. //找到对应的AGV任务
  120. var agv = db.Default.Queryable<WCS_AgvTaskInfo>().Where(v => v.ID == task.AgvTaskID && v.AgvStatus < AGVTaskStatus.MissionCompleted).SplitTable(v => v.Take(2)).First();
  121. if (agv != null)
  122. {
  123. if (!agv.AgvID.IsNullOrEmpty())
  124. {
  125. var cancelTaskUpdateRes = AgvApi.CancelAgvTask(response, item, agv.AgvID);
  126. //if (cancelTaskUpdateRes == null) continue;
  127. }
  128. agv.Status = AGVTaskStatus.Cancel;
  129. agv.AgvStatus = AGVTaskStatus.Cancel;
  130. db.Default.Updateable(agv).SplitTable().ExecuteCommand();
  131. }
  132. //更新任务状态
  133. task.Status = Entity.TaskStatus.Cancel;
  134. task.EedTime = DateTime.Now;
  135. task.EditWho = req.User;
  136. task.ManualRemarks = req.ManualRemarks;
  137. task.AddWCS_TASK_DTL(db, "未知", "任务取消");
  138. db.Default.Updateable(task).ExecuteCommand();
  139. task.CompleteOrCancelTasks(db);
  140. }
  141. else
  142. {
  143. response.ResDataList.Add(new HandleTaskResponse()
  144. {
  145. IsSuccess = false,
  146. TaskNo = item,
  147. Message = $"未找到对应任务{item}"
  148. });
  149. }
  150. }
  151. });
  152. }
  153. // 完成任务
  154. else if (req.Type == HandleTaskTypeEnum.完成任务)
  155. {
  156. SqlSugarHelper.Do(db =>
  157. {
  158. foreach (var item in req.TaskIds)
  159. {
  160. var task = db.Default.Queryable<WCS_TaskInfo>().Where(t => t.ID == item).First();
  161. if (task != null)
  162. {
  163. if (task.Type == TaskType.OutDepot)
  164. {
  165. SRes res = WmsApi.HandleTaskVerify(response, item, 99);
  166. if (res == null) continue;
  167. switch (task.Type)
  168. {
  169. case TaskType.OutDepot:
  170. if (task.Status >= Entity.TaskStatus.Finish)
  171. {
  172. response.ResDataList.Add(new HandleTaskResponse()
  173. {
  174. IsSuccess = false,
  175. TaskNo = item,
  176. Message = $"只能完成未完成状态的任务",
  177. });
  178. continue;
  179. }
  180. break;
  181. }
  182. SRes cancelRes = WmsApi.CarryTaskInfo(response, item, 99);
  183. if (cancelRes == null) continue;
  184. //找到对应的AGV任务
  185. var agv = db.Default.Queryable<WCS_AgvTaskInfo>().Where(v => v.ID == task.AgvTaskID && v.AgvStatus < AGVTaskStatus.MissionCompleted).SplitTable(v => v.Take(2)).First();
  186. if (agv != null)
  187. {
  188. //if (!agv.AgvID.IsNullOrEmpty())
  189. //{
  190. // var cancelTaskUpdateRes = CancelAgvTask(response, item, agv.AgvID);
  191. // if (cancelTaskUpdateRes == null) continue;
  192. //}
  193. agv.Status = AGVTaskStatus.MissionCompleted;
  194. agv.AgvStatus = AGVTaskStatus.MissionCompleted;
  195. db.Default.Updateable(agv).SplitTable().ExecuteCommand();
  196. }
  197. //更新任务状态
  198. task.Status = Entity.TaskStatus.Finish;
  199. task.EedTime = DateTime.Now;
  200. task.EditWho = req.User;
  201. task.ManualRemarks = req.ManualRemarks;
  202. task.AddWCS_TASK_DTL(db, "未知", "任务完成");
  203. db.Default.Updateable(task).ExecuteCommand();
  204. task.CompleteOrCancelTasks(db);
  205. }
  206. else if (task.Type == TaskType.EnterDepot)
  207. {
  208. SRes res = WmsApi.HandleTaskVerify(response, item, 99);
  209. if (res == null) continue;
  210. switch (task.Type)
  211. {
  212. case TaskType.EnterDepot:
  213. if (task.Status >= Entity.TaskStatus.Finish)
  214. {
  215. response.ResDataList.Add(new HandleTaskResponse()
  216. {
  217. IsSuccess = false,
  218. TaskNo = item,
  219. Message = $"只能完成未完成状态的任务",
  220. });
  221. continue;
  222. }
  223. if (task.AddrTo.Length < 6)
  224. {
  225. response.ResDataList.Add(new HandleTaskResponse()
  226. {
  227. IsSuccess = false,
  228. TaskNo = item,
  229. Message = $"只能完成已分配货位的任务",
  230. });
  231. continue;
  232. }
  233. break;
  234. }
  235. SRes cancelRes = WmsApi.CarryTaskInfo(response, item, 99);
  236. if (cancelRes == null) continue;
  237. //找到对应的AGV任务
  238. var agv = db.Default.Queryable<WCS_AgvTaskInfo>().Where(v => v.ID == task.AgvTaskID && v.AgvStatus < AGVTaskStatus.MissionCompleted).SplitTable(v => v.Take(2)).First();
  239. if (agv != null)
  240. {
  241. //if (!agv.AgvID.IsNullOrEmpty())
  242. //{
  243. // var cancelTaskUpdateRes = CancelAgvTask(response, item, agv.AgvID);
  244. // if (cancelTaskUpdateRes == null) continue;
  245. //}
  246. agv.Status = AGVTaskStatus.MissionCompleted;
  247. agv.AgvStatus = AGVTaskStatus.MissionCompleted;
  248. db.Default.Updateable(agv).SplitTable().ExecuteCommand();
  249. }
  250. //更新任务状态
  251. task.Status = Entity.TaskStatus.Finish;
  252. task.EedTime = DateTime.Now;
  253. task.EditWho = req.User;
  254. task.ManualRemarks = req.ManualRemarks;
  255. task.AddWCS_TASK_DTL(db, "未知", "任务完成");
  256. db.Default.Updateable(task).ExecuteCommand();
  257. task.CompleteOrCancelTasks(db);
  258. }
  259. else
  260. {
  261. response.ResDataList.Add(new HandleTaskResponse()
  262. {
  263. IsSuccess = false,
  264. TaskNo = item,
  265. Message = $"未找到对应任务{item}"
  266. });
  267. }
  268. }
  269. }
  270. });
  271. }
  272. else if (req.Type == HandleTaskTypeEnum.重新下发AGV任务)
  273. {
  274. SqlSugarHelper.Do(db =>
  275. {
  276. foreach (var item in req.TaskIds)
  277. {
  278. var task = db.Default.Queryable<WCS_TaskInfo>().Where(t => t.ID == item).First();
  279. if (task != null)
  280. {
  281. if (task.Type == TaskType.SetPlate) //组盘任务
  282. {
  283. response.ResDataList.Add(new HandleTaskResponse()
  284. {
  285. IsSuccess = false,
  286. TaskNo = item,
  287. Message = $"组盘任务无AGV执行流程",
  288. });
  289. continue;
  290. }
  291. else if (task.Type == TaskType.EnterDepot) //入库任务
  292. {
  293. if (task.Floor == 1) //一楼入库
  294. {
  295. }
  296. else if (task.Floor == 2) //二楼入库
  297. {
  298. response.ResDataList.Add(new HandleTaskResponse()
  299. {
  300. IsSuccess = false,
  301. TaskNo = item,
  302. Message = $"二楼入库任务重新下发AGV未实现",
  303. });
  304. continue;
  305. }
  306. }
  307. else if (task.Type == TaskType.OutDepot) //出库
  308. {
  309. }
  310. else if (task.Type == TaskType.TransferDepot) //移库
  311. {
  312. response.ResDataList.Add(new HandleTaskResponse()
  313. {
  314. IsSuccess = false,
  315. TaskNo = item,
  316. Message = $"组盘任务无AGV执行流程",
  317. });
  318. continue;
  319. }
  320. else if (task.Type == TaskType.Delivery) //搬运
  321. {
  322. if (task.Floor == 1)
  323. {
  324. }
  325. else if (task.Floor == 2)
  326. {
  327. response.ResDataList.Add(new HandleTaskResponse()
  328. {
  329. IsSuccess = false,
  330. TaskNo = item,
  331. Message = $"二楼搬运任务重新下发AGV未实现",
  332. });
  333. continue;
  334. }
  335. }
  336. else if (task.Type == TaskType.EmptyInit) //空轮初始化
  337. {
  338. response.ResDataList.Add(new HandleTaskResponse()
  339. {
  340. IsSuccess = false,
  341. TaskNo = item,
  342. Message = $"空轮初始化无AGV执行流程",
  343. });
  344. continue;
  345. }
  346. //找到对应的AGV任务
  347. var agv = db.Default.Queryable<WCS_AgvTaskInfo>().Where(v => v.ID == task.AgvTaskID).SplitTable(v => v.Take(2)).First();
  348. if (agv != null)
  349. {
  350. agv.Status = AGVTaskStatus.NewBuild;
  351. agv.AgvStatus = AGVTaskStatus.NewBuild;
  352. db.Default.Updateable(agv).SplitTable().ExecuteCommand();
  353. }
  354. if (task.Floor == 1)
  355. {
  356. task.Status = Entity.TaskStatus.WaitingToExecute;
  357. }
  358. else if (task.Floor == 2)
  359. {
  360. task.Status = Entity.TaskStatus.ConveyorExecution;
  361. }
  362. task.AddWCS_TASK_DTL(db, "AGV", "重新下发AGV任务");
  363. db.Default.Updateable(task).ExecuteCommand();
  364. }
  365. else
  366. {
  367. response.ResDataList.Add(new HandleTaskResponse()
  368. {
  369. IsSuccess = false,
  370. TaskNo = item,
  371. Message = $"未找到对应任务{item}"
  372. });
  373. }
  374. }
  375. });
  376. }
  377. return response;
  378. }
  379. /// <summary>
  380. /// 设备信息写入接口
  381. /// </summary>
  382. /// <param name="deviceType">需要写入信息的设备类型</param>
  383. /// <param name="devCode">设备编号</param>
  384. /// <param name="protocol">设备协议类名</param>
  385. /// <param name="propName">写入字段名</param>
  386. /// <param name="value">值</param>
  387. [HttpPost]
  388. public void Write(DeviceTypeEnum deviceType, string devCode, string protocol, string propName, string value)
  389. {
  390. World.GetSystemInstance<DeviceWriteSystem>().Invoke(new DeviceWriteInfo
  391. {
  392. DeviceType = deviceType,
  393. Code = devCode,
  394. Protocol = protocol,
  395. Property = propName,
  396. Value = value
  397. });
  398. }
  399. /// <summary>
  400. /// 设备信息写入接口
  401. /// </summary>
  402. /// <param name="deviceType">需要写入信息的设备类型</param>
  403. /// <param name="devCode">设备编号</param>
  404. /// <param name="protocol">设备协议类名</param>
  405. /// <param name="propName">写入字段名</param>
  406. /// <param name="value">值</param>
  407. [HttpPost]
  408. public void GetDevList()
  409. {
  410. var a = RedisHub.Monitor.LRange("Packs", 0, 80000);
  411. List<DeviceDataPack> packs = new List<DeviceDataPack>();
  412. foreach (var item in a)
  413. {
  414. packs.Add(JsonConvert.DeserializeObject<DeviceDataPack>(item));
  415. }
  416. }
  417. /// <summary>
  418. /// 获取设备配置信息接口
  419. /// </summary>
  420. /// <returns></returns>
  421. [HttpGet]
  422. public List<Device> GetDeviceList()
  423. {
  424. return Device.All.ToList();
  425. }
  426. /// <summary>
  427. /// 获取设备信息
  428. /// </summary>
  429. /// <param name="name">设备名称</param>
  430. /// <returns></returns>
  431. [HttpGet]
  432. public object GetDeviceInfo(string name)
  433. {
  434. var remoteIpAddress = HttpContext.Connection.RemoteIpAddress.ToString();
  435. LogHub.InterfacePublish(nameof(GetDeviceInfo), $"IP:{remoteIpAddress}--传入参数--{JsonConvert.SerializeObject(name)}");
  436. var obj = World.GetSystemInstance<GetDeviceSystem>().Invoke(name);
  437. LogHub.InterfacePublish(nameof(GetDeviceInfo), $"IP:{remoteIpAddress}--返回结果--{JsonConvert.SerializeObject(obj)}");
  438. return obj;
  439. }
  440. /// <summary>
  441. /// 堆垛机测试
  442. /// </summary>
  443. /// <param name="srmcod">堆垛机编号</param>
  444. /// <param name="typeEnum">任务类型</param>
  445. /// <param name="value1">起始行</param>
  446. /// <param name="value2">起始列</param>
  447. /// <param name="value3">起始层</param>
  448. /// <param name="value4">目标行</param>
  449. /// <param name="value5">目标列</param>
  450. /// <param name="value6">目标层</param>
  451. [HttpPost]
  452. public void SrmDeBug(string srmcod, SrmTaskType typeEnum, short value1, short value2, short value3, short value4, short value5, short value6)
  453. {
  454. World.GetSystemInstance<SrmDebugSystem>().Invoke(new SrmDebugInfo
  455. {
  456. SrmCode = srmcod,
  457. srmTaskType = typeEnum,
  458. SLine = value1,
  459. SCol = value2,
  460. SLayer = value3,
  461. ELine = value4,
  462. ECol = value5,
  463. ELayer = value6
  464. });
  465. }
  466. #region 设备IP相关
  467. /// <summary>
  468. /// 获取设备Ip集合
  469. /// </summary>
  470. /// <returns>设备Ip集合</returns>
  471. [HttpGet]
  472. public List<string> GetDeviceIpList()
  473. {
  474. if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
  475. return ServiceHub.DeviceIPList;
  476. }
  477. /// <summary>
  478. /// 获取设备IP检测结果
  479. /// </summary>
  480. /// <returns>设备IP检测结果</returns>
  481. [HttpGet]
  482. public List<DeviceIpTestResults> DeviceIpTest()
  483. {
  484. if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
  485. List<DeviceIpTestResults> deviceIpTestResults = new List<DeviceIpTestResults>();
  486. ServiceHub.DeviceIPList.ForEach(ip =>
  487. {
  488. deviceIpTestResults.Add(new DeviceIpTestResults
  489. {
  490. Ip = ip,
  491. Result = PingIpOrDomainName(ip)
  492. });
  493. });
  494. return deviceIpTestResults;
  495. }
  496. /// <summary>
  497. /// 检查Ip是否正常联通
  498. /// </summary>
  499. /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
  500. /// <returns></returns>
  501. public static bool PingIpOrDomainName(string strIpOrDName)
  502. {
  503. try
  504. {
  505. Ping objPingSender = new Ping();
  506. PingOptions objPinOptions = new PingOptions();
  507. objPinOptions.DontFragment = true;
  508. string data = "";
  509. byte[] buffer = Encoding.UTF8.GetBytes(data);
  510. int intTimeout = 120;
  511. PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);
  512. string strInfo = objPinReply.Status.ToString();
  513. if (strInfo == "Success")
  514. {
  515. return true;
  516. }
  517. else
  518. {
  519. return false;
  520. }
  521. }
  522. catch (Exception)
  523. {
  524. return false;
  525. }
  526. }
  527. #endregion 设备IP相关
  528. }
  529. public interface IDeviceWriter
  530. {
  531. [HttpPost]
  532. void Write(DeviceTypeEnum deviceType, string devCode, string protocol, string propName, string value);
  533. }
  534. /// <summary>
  535. /// 设备Ip通讯检测结构
  536. /// </summary>
  537. public class DeviceIpTestResults
  538. {
  539. public string Ip { get; set; }
  540. public bool Result { get; set; }
  541. }
  542. }