终端一体化运控平台
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.
 
 
 

384 line
13 KiB

  1. using BPA.Message;
  2. using BPA.Message.Enum;
  3. using BPASmartClient.Device;
  4. using BPASmartClient.EventBus;
  5. using BPASmartClient.Helper;
  6. using BPASmartClient.Message;
  7. using BPASmartClient.Model;
  8. using BPASmartClient.Model.PLC;
  9. using BPASmartClient.MorkTM.Model;
  10. using System.Collections.Concurrent;
  11. using static BPASmartClient.EventBus.EventBus;
  12. namespace BPASmartClient.MorkTM
  13. {
  14. public class Control_MorkTM : BaseDevice
  15. {
  16. public override global::BPA.Message.Enum.DeviceClientType DeviceType { get { return BPA.Message.Enum.DeviceClientType.TMC_MT; } }
  17. GVL_MorkTM morkTM = new GVL_MorkTM();
  18. PolymerBatching polymer = new PolymerBatching();
  19. //浮点数放大倍数
  20. const int expand = 100;
  21. public override void DoMain()
  22. {
  23. ServerInit();
  24. DataParse();
  25. polymer.GetMaterialInfo();
  26. ActionManage.GetInstance.Register(new Action<object>((o) =>
  27. {
  28. if (o != null && o is WritePar writePar) WriteData(writePar.Address, writePar.Value);
  29. }), "WriteVW");
  30. ActionManage.GetInstance.Register(new Action<object>((o) =>
  31. {
  32. if (o != null && o is WritePar writePar) WriteData(writePar.Address, writePar.Value);
  33. }), "WriteBools");
  34. morkTM.ReachPosions = new List<bool>() { morkTM.ReachOutPosion_0, morkTM.ReachPosion_1, morkTM.ReachPosion_2, morkTM.ReachPosion_3, morkTM.ReachPosion_4, morkTM.ReachPosion_5, morkTM.ReachPosion_6 };
  35. DeviceProcessLogShow("设备初始化完成");
  36. }
  37. private void ServerInit()
  38. {
  39. //物料信息
  40. EventBus.EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  41. {
  42. if (@event == null) return;
  43. if (@event is MaterialDeliveryEvent material)
  44. {
  45. orderMaterialDelivery = material.orderMaterialDelivery;
  46. }
  47. });
  48. //配方数据信息
  49. EventBus.EventBus.GetInstance().Subscribe<RecipeBomEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  50. {
  51. if (@event == null) return;
  52. if (@event is RecipeBomEvent recipe)
  53. {
  54. recipeBoms = recipe.recipeBoms;
  55. }
  56. });
  57. }
  58. private void DataParse()
  59. {
  60. EventBus.EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  61. {
  62. if(@event == null) return;
  63. if(@event is DoOrderEvent order)
  64. {
  65. if (order.MorkOrder.GoodBatchings == null) return;
  66. OrderCount++;
  67. OrderChange(order.MorkOrder, ORDER_STATUS.WAIT);
  68. DeviceProcessLogShow($"接收到{OrderCount}次订单");
  69. Dictionary<int, float> OrderPushes = new Dictionary<int, float>();
  70. foreach (var item in order.MorkOrder.GoodBatchings)
  71. {
  72. var res = orderMaterialDelivery?.BatchingInfo?.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  73. if (res != null)
  74. {
  75. OrderPushes.TryAdd(int.Parse(res.BatchingLoc), item.BatchingCount);
  76. }
  77. }
  78. morkTM.morkOrderPushesTeaWithMilk.Enqueue(new OrderLocInfo()
  79. {
  80. GoodName = order.MorkOrder.GoodsName,
  81. SuborderId = order.MorkOrder.SuborderId,
  82. GoodPushes = OrderPushes
  83. });
  84. }
  85. });
  86. }
  87. private void OrderChange(MorkOrderPush orderPush, ORDER_STATUS oRDER_STATUS)
  88. {
  89. EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent()
  90. {
  91. SortNum = orderPush.SortNum.ToString(),
  92. GoodName = orderPush.GoodsName,
  93. Status = oRDER_STATUS,
  94. SubOrderId = orderPush.SuborderId,
  95. deviceClientType = DeviceType
  96. });
  97. }
  98. public override void MainTask()
  99. {
  100. IsHealth = true;
  101. MakeTeaWithMilkProcess();
  102. }
  103. private void MakeTeaWithMilkProcess()
  104. {
  105. if (morkTM.morkOrderPushesTeaWithMilk.Count > 0)
  106. {
  107. if (morkTM.morkOrderPushesTeaWithMilk.TryDequeue(out OrderLocInfo orderLoc)) //&&原点位置是否有杯子)
  108. {
  109. SetMatertialWeight(orderLoc.GoodPushes);//设置物料出料量
  110. morkTM.RecipesPushes.Clear();
  111. morkTM.RecipesPushes = orderLoc.GoodPushes;
  112. foreach (var item in morkTM.RecipesPushes)
  113. {
  114. PosionTurnOn(item.Key);//设定转盘位置并等待到位信号
  115. Thread.Sleep(1000);
  116. OpenUsePassageWay(item.Key);//打开通道并等待出料完成
  117. DeviceProcessLogShow($"奶茶{orderLoc.GoodName}:配料{item.Key}:添加量{item.Value}");
  118. }
  119. }
  120. }
  121. }
  122. /// <summary>
  123. /// 打开出料通道
  124. /// </summary>
  125. private void OpenUsePassageWay(int pos)
  126. {
  127. string address = "";
  128. foreach (var item in polymer.OutPosionPLCs)
  129. {
  130. if(item.posion is MaterialPosion posion)
  131. {
  132. if(posion == (MaterialPosion)pos)
  133. {
  134. WriteData(item.SetPLCPosion,true);
  135. address = item.GetPLCPosion;
  136. return;
  137. }
  138. }
  139. }
  140. while(!morkTM.outMaterialCompletes[pos]) //等待出料完成
  141. {
  142. Thread.Sleep(1000);
  143. }
  144. WriteData(address, false);
  145. }
  146. /// <summary>
  147. /// 转盘旋转位置设定
  148. /// </summary>
  149. /// <param name="posion"></param>
  150. private void PosionTurnOn(int posion)
  151. {
  152. int i = 0;
  153. string address = "";
  154. foreach (var item in polymer.GoodsMaterialPosion)
  155. {
  156. if (item.Key.Contains((MaterialPosion)posion))
  157. {
  158. WriteData(item.Value.SetPLCPosion,true);
  159. i = Convert.ToInt32(item.Value.posion);
  160. address = item.Value.GetPLCPosion;
  161. return;
  162. }
  163. }
  164. while(!morkTM.ReachPosions[i])//等待转盘到达信号
  165. {
  166. Thread.Sleep(1000);
  167. }
  168. if(address != null) WriteData(address, false);//把信号置为0
  169. }
  170. /// <summary>
  171. /// 把每一个物料的用量写入plc
  172. /// </summary>
  173. /// <param name="materials"></param>
  174. private void SetMatertialWeight(Dictionary<int, float> materials)
  175. {
  176. foreach (var material in materials)
  177. {
  178. int value = Convert.ToInt32(material.Value*expand);
  179. WriteData(polymer.MaterialPosionList[(MaterialPosion)material.Key], value);
  180. Thread.Sleep(100);
  181. }
  182. }
  183. public override void ReadData()
  184. {
  185. //启用通道的地址1
  186. GetStatus("M0.0", new Action<object>((obj) =>
  187. {
  188. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 8)
  189. {
  190. }
  191. }));
  192. //启用通道的地址2
  193. GetStatus("M1.0", new Action<object>((obj) =>
  194. {
  195. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 8)
  196. {
  197. }
  198. }));
  199. //启用通道的地址3
  200. GetStatus("M2.0", new Action<object>((obj) =>
  201. {
  202. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 8)
  203. {
  204. }
  205. }));
  206. //打开通道的地址1
  207. GetStatus("M10.0", new Action<object>((obj) =>
  208. {
  209. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 8)
  210. {
  211. morkTM.UsePassageWay_1 = bools[0];
  212. morkTM.UsePassageWay_2 = bools[1];
  213. morkTM.UsePassageWay_3 = bools[2];
  214. morkTM.UsePassageWay_4 = bools[3];
  215. morkTM.UsePassageWay_5 = bools[4];
  216. morkTM.UsePassageWay_6 = bools[5];
  217. morkTM.UsePassageWay_7 = bools[6];
  218. morkTM.UsePassageWay_8 = bools[7];
  219. }
  220. }));
  221. //打开通道的地址2
  222. GetStatus("M11.0", new Action<object>((obj) =>
  223. {
  224. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 8)
  225. {
  226. morkTM.UsePassageWay_9 = bools[0];
  227. morkTM.UsePassageWay_10 = bools[1];
  228. morkTM.UsePassageWay_11 = bools[2];
  229. morkTM.UsePassageWay_12 = bools[3];
  230. morkTM.UsePassageWay_13 = bools[4];
  231. morkTM.UsePassageWay_14 = bools[5];
  232. morkTM.UsePassageWay_15 = bools[6];
  233. morkTM.UsePassageWay_16 = bools[7];
  234. }
  235. }));
  236. //打开通道的地址3
  237. GetStatus("M12.0", new Action<object>((obj) =>
  238. {
  239. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 8)
  240. {
  241. morkTM.UsePassageWay_17 = bools[0];
  242. morkTM.UsePassageWay_18 = bools[1];
  243. morkTM.UsePassageWay_19 = bools[2];
  244. morkTM.UsePassageWay_20 = bools[3];
  245. morkTM.UsePassageWay_21 = bools[4];
  246. morkTM.UsePassageWay_22 = bools[5];
  247. morkTM.UsePassageWay_23 = bools[6];
  248. morkTM.UsePassageWay_24 = bools[7];
  249. }
  250. }));
  251. //打开通道的地址4
  252. GetStatus("M13.0", new Action<object>((obj) =>
  253. {
  254. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 4)
  255. {
  256. morkTM.UsePassageWay_25 = bools[0];
  257. morkTM.UsePassageWay_26 = bools[1];
  258. morkTM.UsePassageWay_27 = bools[2];
  259. morkTM.UsePassageWay_28 = bools[3];
  260. }
  261. }));
  262. GetStatus("M50.0",new Action<object>((obj) =>
  263. {
  264. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 28)
  265. {
  266. for (int i = 0; i < 28; i++)
  267. {
  268. if (morkTM.outMaterialCompletes.ContainsKey(i+1))
  269. {
  270. morkTM.outMaterialCompletes[i+1] = bools[i];
  271. }
  272. else
  273. {
  274. morkTM.outMaterialCompletes.Add(i+1, bools[i]);
  275. }
  276. }
  277. }
  278. }));
  279. //转盘移动的地址
  280. GetStatus("M4.0", new Action<object>((obj) =>
  281. {
  282. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 7)
  283. {
  284. }
  285. }));
  286. //装盘移动到位的地址
  287. GetStatus("M14.0", new Action<object>((obj) =>
  288. {
  289. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 7)
  290. {
  291. morkTM.ReachPosion_1 = bools[0];
  292. morkTM.ReachPosion_2 = bools[1];
  293. morkTM.ReachPosion_3 = bools[2];
  294. morkTM.ReachPosion_4 = bools[3];
  295. morkTM.ReachPosion_5 = bools[4];
  296. morkTM.ReachPosion_6 = bools[5];
  297. morkTM.ReachOutPosion_0 = bools[6];
  298. for (int i = 0; i < 7; i++)
  299. {
  300. morkTM.ReachPosions[i] = bools[i];
  301. }
  302. }
  303. }));
  304. }
  305. public override void ResetProgram()
  306. {
  307. morkTM = null;
  308. morkTM = new GVL_MorkTM();
  309. }
  310. private void WriteData(string address, object value)
  311. {
  312. EventBus.EventBus.GetInstance().Publish(new WriteModel() { DeviceId = DeviceId, Address = address, Value = value });
  313. }
  314. private void GetStatus(string key, Action<object> action)
  315. {
  316. if (peripheralStatus.ContainsKey(key))
  317. {
  318. if (peripheralStatus[key] != null)
  319. {
  320. action?.Invoke(peripheralStatus[key]);
  321. }
  322. }
  323. }
  324. public override void SimOrder()
  325. {
  326. ActionManage.GetInstance.Register(new Action<object>((o) =>
  327. {
  328. if (o is string goodName)
  329. {
  330. }
  331. }), "");
  332. }
  333. public override void Stop()
  334. {
  335. throw new NotImplementedException();
  336. }
  337. }
  338. }