WcsController.cs 29 KB

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