WcsController.cs 27 KB

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