WcsController.cs 25 KB

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