满轮主线预写入目标地址.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using PlcSiemens.Core.Extension;
  2. using ServiceCenter.Extensions;
  3. using ServiceCenter.Logs;
  4. using ServiceCenter.SqlSugars;
  5. using SqlSugar;
  6. using System.Collections.Concurrent;
  7. using System.ComponentModel;
  8. using WCS.Core;
  9. using WCS.Entity;
  10. using WCS.Entity.Protocol.BCR;
  11. using WCS.Entity.Protocol.Station;
  12. using WCS.WorkEngineering.Extensions;
  13. using WCS.WorkEngineering.Model.WMS;
  14. using WCS.WorkEngineering.Worlds;
  15. using TaskStatus = WCS.Entity.TaskStatus;
  16. namespace WCS.WorkEngineering.Systems
  17. {
  18. /// <summary>
  19. /// 满轮主线预写入目标地址
  20. /// </summary>
  21. [BelongTo(typeof(SortingMainLineWorld))]
  22. [Description("满轮主线预写入目标地址")]
  23. public class 满轮主线预写入目标地址 : DeviceSystem<Device<IStation523, IBCR83, IStation525>>
  24. {
  25. protected override bool ParallelDo => true;
  26. private static Dictionary<string, string> CodeMap { get; set; } = new Dictionary<string, string>();
  27. private static ConcurrentDictionary<string, fjSysConfig> FlowConfig { get; set; } = new ConcurrentDictionary<string, fjSysConfig>();
  28. public 满轮主线预写入目标地址()
  29. {
  30. // 映射 Code 到 devCode 的映射关系
  31. CodeMap = new Dictionary<string, string>
  32. {
  33. ["18"] = "22",
  34. ["38"] = "41",
  35. ["58"] = "61",
  36. ["118"] = "122",
  37. ["138"] = "141",
  38. ["158"] = "161"
  39. };
  40. }
  41. public override void Do(Device<IStation523, IBCR83, IStation525> obj)
  42. {
  43. try
  44. {
  45. if (!CodeMap.TryGetValue(obj.Entity.Code, out var devCode))
  46. {
  47. World.Log($"设备号[{obj.Entity.Code}]未初始化对应关系", LogLevelEnum.High);
  48. return;
  49. }
  50. var dev = new Device<IStation520>(Device.All.First(x => x.Code == devCode), World);
  51. dev.Data.Mode = StationMode.Automatic;
  52. //从DB83中获取待处理条码组
  53. var pendingBcrList = obj.Data2.GetBcrCodeList().Where(x => !x.IsNullOrEmpty()).ToList();
  54. //从DB525获取已处理条码组
  55. var cacheBcrList = obj.Data3.GetBcrCodeList().ToList();
  56. //筛选出未处理条码组
  57. var codes = pendingBcrList.Where(x => !cacheBcrList.Contains(x)).ToList();
  58. fjSysConfig configFlowError = null;
  59. //循环处理所有缓存条码组中没有的条码
  60. foreach (var bcrCode in codes)
  61. {
  62. WCS_TaskInfo taskInfo = null;
  63. fjSysConfig config = null;
  64. SqlSugarHelper.Do(_db =>
  65. {
  66. var db = _db.Default;
  67. taskInfo = db.Queryable<WCS_TaskInfo>().NoLock().First(x => x.BarCode == bcrCode && x.Status == TaskStatus.NewBuild);
  68. if (taskInfo == null)
  69. {
  70. LogAndRecordError(bcrCode, "找不到匹配的任务", obj.Entity.Code);
  71. return;
  72. }
  73. var key = taskInfo.WarehouseCode + "-Flow";
  74. if (!FlowConfig.TryGetValue(key, out config))
  75. {
  76. config = db.Queryable<fjSysConfig>().NoLock().First(x => x.Code == key) ?? new fjSysConfig { SContent = "4" };
  77. FlowConfig.AddOrUpdate(key, config, (k, v) => config);
  78. }
  79. if (!db.Queryable<fjSysConfig>().NoLock().Any(x => x.Code == key && x.SContent == config.SContent))
  80. {
  81. config = db.Queryable<fjSysConfig>().NoLock().First(x => x.Code == key) ?? new fjSysConfig { SContent = "4" };
  82. FlowConfig.AddOrUpdate(key, config, (k, v) => config);
  83. }
  84. //获取排异错误
  85. if (configFlowError == null)
  86. {
  87. configFlowError = db.Queryable<fjSysConfig>().NoLock().First(x => x.Code == "FlowErrorAllocation") ?? new fjSysConfig { SContent = "3" };
  88. }
  89. });
  90. if (taskInfo == null)
  91. {
  92. dev.Data.Mode = StationMode.Automatic;
  93. continue;
  94. }
  95. var srmCode = taskInfo.WarehouseCode.WarehouseToSrm();
  96. var flowError = false;
  97. //北侧主线
  98. if (obj.Entity.Code == "18" || obj.Entity.Code == "38" || obj.Entity.Code == "58")
  99. {
  100. if (taskInfo.WarehouseCode.Contains("S"))
  101. {
  102. flowError = true;
  103. }
  104. }
  105. //南侧主线
  106. else if (obj.Entity.Code == "118" || obj.Entity.Code == "138" || obj.Entity.Code == "158")
  107. {
  108. if (taskInfo.WarehouseCode.Contains("N"))
  109. {
  110. flowError = true;
  111. }
  112. }
  113. var path = DevicePath.GetPath(obj.Entity.Code, srmCode);
  114. if ((path == null || path is { Points.Count: < 2 }) && !flowError)
  115. {
  116. var msg = $"{bcrCode}:路径错误,当前位置{obj.Entity.Code},目标位置:{srmCode}";
  117. LogAndRecordError(bcrCode, msg, obj.Entity.Code);
  118. dev.Data.Mode = StationMode.Automatic;
  119. continue;
  120. }
  121. var next = path != null ? path.Points[1].Code : devCode;
  122. #region 计算应该去哪个分拣库
  123. //应该要去的分拣库
  124. if (taskInfo.WarehouseCode.Contains("N"))
  125. {
  126. next = config.SContent switch
  127. {
  128. "1" => "418",
  129. "2" => "818",
  130. "3" => "1218",
  131. _ => next
  132. };
  133. }
  134. else if (taskInfo.WarehouseCode.Contains("S"))
  135. {
  136. next = config.SContent switch
  137. {
  138. "1" => "618",
  139. "2" => "1018",
  140. "3" => "1418",
  141. _ => next
  142. };
  143. }
  144. if (flowError)
  145. {
  146. if (taskInfo.WarehouseCode.Contains("N"))
  147. {
  148. next = configFlowError.SContent switch
  149. {
  150. "1" => "618",
  151. "2" => "1018",
  152. "3" => "1418",
  153. _ => next
  154. };
  155. }
  156. else if (taskInfo.WarehouseCode.Contains("S"))
  157. {
  158. next = configFlowError.SContent switch
  159. {
  160. "1" => "418",
  161. "2" => "818",
  162. "3" => "1218",
  163. _ => next
  164. };
  165. }
  166. }
  167. #endregion 计算应该去哪个分拣库
  168. //开始赋值
  169. SetStation525Value(obj.Data3, "TaskNumber", taskInfo.ID);
  170. SetStation525Value(obj.Data3, "GoodsEnd", next.ToShort());
  171. SetStation525Value(obj.Data3, "BcrCode", bcrCode);
  172. obj.Data3.NextIndex = (short)(obj.Data3.NextIndex >= 49 ? 0 : obj.Data3.NextIndex + 1);
  173. dev.Data.Mode = StationMode.Automatic;
  174. }
  175. dev.Data.Mode = StationMode.Automatic;
  176. }
  177. catch (Exception e)
  178. {
  179. World.Log($"{e.Message}:---{e.StackTrace}", LogLevelEnum.High);
  180. }
  181. }
  182. public override bool Select(Device dev)
  183. {
  184. var codes = new List<string>();
  185. if (WorkStart.WareHouses.Contains("FJ1")) codes.AddRange(new List<string>() { "18", "118" });
  186. if (WorkStart.WareHouses.Contains("FJ2")) codes.AddRange(new List<string>() { "38", "58" });
  187. if (WorkStart.WareHouses.Contains("FJ3")) codes.AddRange(new List<string>() { "138", "158" });
  188. return codes.Contains(dev.Code);
  189. }
  190. private void LogAndRecordError(string barcode, string message, string memo)
  191. {
  192. World.Log($"{barcode}:{message}", LogLevelEnum.High);
  193. var errorInfo = new BaseErrorInfo()
  194. {
  195. BusName = "主线分流",
  196. BarCode = barcode,
  197. Message = message,
  198. Count = 1,
  199. AddTime = DateTime.Now,
  200. Memo = memo
  201. };
  202. errorInfo.AddBaseErrorInfo();
  203. }
  204. /// <summary>
  205. /// 设置站525的值
  206. /// </summary>
  207. /// <param name="data3"></param>
  208. /// <param name="propertyName"></param>
  209. /// <param name="value"></param>
  210. private void SetStation525Value(IStation525 data3, string propertyName, object value)
  211. {
  212. var property = data3.GetType().GetProperty($"{propertyName}{data3.NextIndex}");
  213. if (property != null && property.CanWrite)
  214. {
  215. property.SetValue(data3, value);
  216. property.SetValue(data3, value);
  217. }
  218. }
  219. }
  220. /// <summary>
  221. /// 主线分流
  222. /// </summary>
  223. public class MainlineDiversion
  224. {
  225. /// <summary>
  226. /// 任务号
  227. /// </summary>
  228. public int TaskId { get; set; }
  229. /// <summary>
  230. /// 仓库号
  231. /// </summary>
  232. public string WarehouseCode { get; set; }
  233. }
  234. }