终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using BPA.Message.Enum;
  2. using BPASmartClient.Device;
  3. using BPASmartClient.EventBus;
  4. using BPASmartClient.Helper;
  5. using BPASmartClient.Model;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using static BPASmartClient.EventBus.EventBus;
  12. namespace BPASmartClient.MorkMOC
  13. {
  14. public class Control_MorkMOC : BaseDevice
  15. {
  16. public override global::BPA.Message.Enum.DeviceClientType DeviceType { get { return BPA.Message.Enum.DeviceClientType.TMC_MT; } }
  17. GVL_MorkMOC morkMoc = new GVL_MorkMOC();
  18. PolymerBatching polymerBatching = new PolymerBatching();
  19. //放大倍数
  20. const int expand = 10;
  21. public override void DoMain()
  22. {
  23. ServerInit();
  24. DataParse();
  25. }
  26. private void ServerInit()
  27. {
  28. //物料信息
  29. EventBus.EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  30. {
  31. if (@event == null) return;
  32. if (@event is MaterialDeliveryEvent material)
  33. {
  34. orderMaterialDelivery = material.orderMaterialDelivery;
  35. }
  36. });
  37. //配方数据信息
  38. EventBus.EventBus.GetInstance().Subscribe<RecipeBomEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  39. {
  40. if (@event == null) return;
  41. if (@event is RecipeBomEvent recipe)
  42. {
  43. recipeBoms = recipe.recipeBoms;
  44. }
  45. });
  46. }
  47. private void DataParse()
  48. {
  49. EventBus.EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  50. {
  51. if (@event == null) return;
  52. if (@event is DoOrderEvent order)
  53. {
  54. if (order.MorkOrder.GoodBatchings == null) return;
  55. OrderCount++;
  56. morkMoc.doOrderEvents.Add(order);
  57. OrderChange(order.MorkOrder.SuborderId, ORDER_STATUS.WAIT);
  58. DeviceProcessLogShow($"接收到{OrderCount}次订单");
  59. Dictionary<int, int> OrderPushes = new Dictionary<int, int>();
  60. foreach (var item in order.MorkOrder.GoodBatchings)
  61. {
  62. var res = orderMaterialDelivery?.BatchingInfo?.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  63. if (res != null)
  64. {
  65. OrderPushes.TryAdd(int.Parse(res.BatchingLoc), item.BatchingCount);
  66. }
  67. }
  68. morkMoc.morkOrderPushes.Enqueue(new OrderLocInfo()
  69. {
  70. GoodName = order.MorkOrder.GoodsName,
  71. SuborderId = order.MorkOrder.SuborderId,
  72. GoodPushes = OrderPushes
  73. });
  74. }
  75. });
  76. }
  77. /// <summary>
  78. /// 订单改变通知
  79. /// </summary>
  80. /// <param name="subid"></param>
  81. /// <param name="oRDER_STATUS"></param>
  82. private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
  83. {
  84. var res = morkMoc.doOrderEvents.FirstOrDefault(p => p.MorkOrder.SuborderId == subid);
  85. string goodName = string.Empty;
  86. string SortNum = string.Empty;
  87. EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent()
  88. {
  89. SortNum = res.MorkOrder.SortNum.ToString(),
  90. GoodName = res.MorkOrder.GoodsName,
  91. Status = oRDER_STATUS,
  92. SubOrderId = res.MorkOrder.SuborderId,
  93. deviceClientType = DeviceType
  94. });
  95. if (oRDER_STATUS == ORDER_STATUS.COMPLETED_COOK) morkMoc.doOrderEvents.Remove(res);
  96. }
  97. public override void MainTask()
  98. {
  99. MakeProcess();
  100. }
  101. /// <summary>
  102. /// 制作流程
  103. /// </summary>
  104. private void MakeProcess()
  105. {
  106. if (morkMoc.morkOrderPushes.Count > 0)
  107. {
  108. if (morkMoc.morkOrderPushes.TryDequeue(out OrderLocInfo orderLoc))
  109. {
  110. // 第一步:设置每个仓的出料量
  111. SetMaterialsWeight(orderLoc.GoodPushes);
  112. DeviceProcessLogShow("设置出料量完成");
  113. //第二步:循环出料
  114. OutMaterials(orderLoc.GoodPushes);
  115. DeviceProcessLogShow($"{orderLoc.GoodName}制作完成");
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// 设定出料重量
  121. /// </summary>
  122. /// <param name="goodPushes"></param>
  123. private void SetMaterialsWeight(Dictionary<int,int> goodPushes)
  124. {
  125. foreach(var good in goodPushes)
  126. {
  127. WriteControl(polymerBatching.plcData[good.Key - 1].WeightVar, good.Value * expand);
  128. DeviceProcessLogShow($"{good.Key}号通道出料量:{good.Value}g");
  129. Thread.Sleep(300);
  130. }
  131. }
  132. /// <summary>
  133. /// 循环出料
  134. /// </summary>
  135. private void OutMaterials(Dictionary<int, int> goodPushes)
  136. {
  137. foreach (var good in goodPushes)
  138. {
  139. WriteControl(polymerBatching.plcData[good.Key - 1].StartUpVar, true);
  140. Thread.Sleep(3000);//写入PLC开启通道后 PLC会先把完成信号置0,等待出料完成后置1
  141. while(!morkMoc.OutMaterailCompelete)//接收到完成信号后退出循环
  142. {
  143. Thread.Sleep(1000);
  144. //需要加入超时提示或处理
  145. }
  146. DeviceProcessLogShow($"{good.Key}号通道出料完成");
  147. }
  148. }
  149. public override void ReadData()
  150. {
  151. GetStatus("M10.0", new Action<object>((obj) =>
  152. {
  153. if (obj is bool[] bools && bools.Length > 0 )
  154. {
  155. morkMoc.OutMaterailCompelete = bools[0];
  156. }
  157. }));
  158. }
  159. public override void ResetProgram()
  160. {
  161. throw new NotImplementedException();
  162. }
  163. public override void SimOrder()
  164. {
  165. ActionManage.GetInstance.Register(new Action<object>((o) =>
  166. {
  167. if (o == null) return;
  168. if(o is LocalRecipe recipe)
  169. {
  170. Dictionary<int, int> OrderPushes = new Dictionary<int, int>();
  171. foreach(var item in recipe.localMaterails)
  172. {
  173. OrderPushes.Add(Convert.ToInt32(item.MaterialPosition), Convert.ToInt32(item.MaterialWeight));
  174. }
  175. morkMoc.morkOrderPushes.Enqueue(new OrderLocInfo()
  176. {
  177. GoodName = recipe.RecipeName,
  178. SuborderId = Guid.NewGuid().ToString(),
  179. GoodPushes = OrderPushes
  180. });
  181. }
  182. }),"MakeGoods");
  183. }
  184. public override void Stop()
  185. {
  186. throw new NotImplementedException();
  187. }
  188. private void GetStatus(string key, Action<object> action)
  189. {
  190. if (peripheralStatus.ContainsKey(key))
  191. {
  192. if (peripheralStatus[key] != null)
  193. {
  194. action?.Invoke(peripheralStatus[key]);
  195. }
  196. }
  197. }
  198. }
  199. }