终端一体化运控平台
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

220 wiersze
7.5 KiB

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