终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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