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

457 lines
16 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;
  10. using BPASmartClient.MorkTM.Model;
  11. using System.Collections.Concurrent;
  12. using static BPASmartClient.EventBus.EventBus;
  13. namespace BPASmartClient.MorkTM
  14. {
  15. public class Control_MorkTM : BaseDevice
  16. {
  17. public override global::BPA.Message.Enum.DeviceClientType DeviceType { get { return BPA.Message.Enum.DeviceClientType.TMC_MT; } }
  18. GVL_MorkTM morkTM = new GVL_MorkTM();
  19. PolymerBatching polymer = new PolymerBatching();
  20. //浮点数放大倍数
  21. const int expand = 100;
  22. public override void DoMain()
  23. {
  24. ServerInit();
  25. DataParse();
  26. polymer.GetMaterialInfo();
  27. ActionManage.GetInstance.Register(new Action<object[]>((o) =>
  28. {
  29. if(o.Length > 0)
  30. {
  31. Dictionary<int, float> res = new Dictionary<int, float>();
  32. res.Add(Convert.ToInt32(o[0]), Convert.ToSingle(o[1]));
  33. SetMatertialWeight(res);
  34. Thread.Sleep(1000);
  35. OpenUsePassageWay(Convert.ToInt32(o[0]));
  36. }
  37. }), "通道口出料");
  38. ActionManage.GetInstance.Register(new Action<object[]>((o) =>
  39. {
  40. if(o.Length > 0)
  41. {
  42. PosionTurnOnTest(Convert.ToInt32(o[0]));
  43. }
  44. }), "转盘转动");
  45. ActionManage.GetInstance.Register(new Action<object[]>((o) =>
  46. {
  47. if (o.Length > 0)
  48. {
  49. OpenPassway(Convert.ToInt32(o[0]));
  50. }
  51. }), "开启通道");
  52. ActionManage.GetInstance.Register(new Action<object[]>((o) =>
  53. {
  54. if (o.Length > 0)
  55. {
  56. CheckPassway(Convert.ToInt32(o[0]), Convert.ToInt32(o[1]));
  57. }
  58. }), "开始矫正");
  59. ActionManage.GetInstance.Register(new Action<object[]>((o) =>
  60. {
  61. if (o.Length > 0)
  62. {
  63. CheckMaterailWeight(Convert.ToInt32(o[0]), Convert.ToInt32(o[1]));
  64. }
  65. }), "矫正重量");
  66. ActionManage.GetInstance.Register(new Action<object>((o) =>
  67. {
  68. if (o != null && o is WritePar writePar) WriteData(writePar.Address, writePar.Value);
  69. }), "WriteVW");
  70. ActionManage.GetInstance.Register(new Action<object>((o) =>
  71. {
  72. if (o != null && o is WritePar writePar) WriteData(writePar.Address, writePar.Value);
  73. }), "WriteBools");
  74. 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 };
  75. DeviceProcessLogShow("设备初始化完成");
  76. }
  77. private void ServerInit()
  78. {
  79. //物料信息
  80. EventBus.EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  81. {
  82. if (@event == null) return;
  83. if (@event is MaterialDeliveryEvent material)
  84. {
  85. orderMaterialDelivery = material.orderMaterialDelivery;
  86. }
  87. });
  88. //配方数据信息
  89. EventBus.EventBus.GetInstance().Subscribe<RecipeBomEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  90. {
  91. if (@event == null) return;
  92. if (@event is RecipeBomEvent recipe)
  93. {
  94. recipeBoms = recipe.recipeBoms;
  95. }
  96. });
  97. }
  98. private void DataParse()
  99. {
  100. EventBus.EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  101. {
  102. if(@event == null) return;
  103. if(@event is DoOrderEvent order)
  104. {
  105. if (order.MorkOrder.GoodBatchings == null) return;
  106. OrderCount++;
  107. morkTM.doOrderEvents.Add(order);
  108. OrderChange(order.MorkOrder.SuborderId, ORDER_STATUS.WAIT);
  109. DeviceProcessLogShow($"接收到{OrderCount}次订单");
  110. Dictionary<int, float> OrderPushes = new Dictionary<int, float>();
  111. foreach (var item in order.MorkOrder.GoodBatchings)
  112. {
  113. var res = orderMaterialDelivery?.BatchingInfo?.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  114. if (res != null)
  115. {
  116. OrderPushes.TryAdd(int.Parse(res.BatchingLoc), item.BatchingCount);
  117. }
  118. }
  119. morkTM.morkOrderPushesTeaWithMilk.Enqueue(new OrderLocInfo()
  120. {
  121. GoodName = order.MorkOrder.GoodsName,
  122. SuborderId = order.MorkOrder.SuborderId,
  123. GoodPushes = OrderPushes
  124. });
  125. }
  126. });
  127. }
  128. private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
  129. {
  130. var res = morkTM.doOrderEvents.FirstOrDefault(p => p.MorkOrder.SuborderId == subid);
  131. string goodName = string.Empty;
  132. string SortNum = string.Empty;
  133. EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent()
  134. {
  135. SortNum = res.MorkOrder. SortNum.ToString(),
  136. GoodName = res.MorkOrder.GoodsName,
  137. Status = oRDER_STATUS,
  138. SubOrderId = res.MorkOrder.SuborderId,
  139. deviceClientType = DeviceType
  140. });
  141. if(oRDER_STATUS == ORDER_STATUS.COMPLETED_COOK) morkTM.doOrderEvents.Remove(res);
  142. }
  143. public override void MainTask()
  144. {
  145. IsHealth = true;
  146. MakeTeaWithMilkProcess();
  147. }
  148. private void MakeTeaWithMilkProcess()
  149. {
  150. if (morkTM.morkOrderPushesTeaWithMilk.Count > 0)
  151. {
  152. if (morkTM.morkOrderPushesTeaWithMilk.TryDequeue(out OrderLocInfo orderLoc)) //&&原点位置是否有杯子)
  153. {
  154. morkTM.MakeCount = 0;
  155. SetMatertialWeight(orderLoc.GoodPushes);//设置物料出料量
  156. morkTM.RecipesPushes.Clear();
  157. morkTM.RecipesPushes = orderLoc.GoodPushes;
  158. MakeProcess(orderLoc.GoodName, morkTM.MakeCount, morkTM.RecipesPushes.Count);
  159. OrderChange(orderLoc.SuborderId, ORDER_STATUS.COOKING);
  160. foreach (var item in morkTM.RecipesPushes)
  161. {
  162. morkTM.MakeCount++;
  163. PosionTurnOn(item.Key);//设定转盘位置并等待到位信号
  164. Thread.Sleep(1000);
  165. OpenUsePassageWay(item.Key);//打开通道并等待出料完成
  166. DeviceProcessLogShow($"奶茶{orderLoc.GoodName}:配料{item.Key}:添加量{item.Value}");
  167. MakeProcess(orderLoc.GoodName,morkTM.MakeCount,morkTM.RecipesPushes.Count);
  168. }
  169. TurnOutPosion();
  170. OrderChange(orderLoc.SuborderId, ORDER_STATUS.COMPLETED_COOK);
  171. MakeProcess(orderLoc.GoodName, 1, 1);
  172. DeviceProcessLogShow($"奶茶{orderLoc.GoodName}制作完成");
  173. }
  174. }
  175. }
  176. /// <summary>
  177. /// 打开出料通道
  178. /// </summary>
  179. private void OpenUsePassageWay(int pos)
  180. {
  181. string address = "";
  182. foreach (var item in polymer.OutPosionPLCs)//根据位置筛选物料plc点位
  183. {
  184. if(item.posion is MaterialPosion posion)
  185. {
  186. if(posion == (MaterialPosion)pos)
  187. {
  188. WriteData(item.SetPLCPosion,true);
  189. address = item.GetPLCPosion;
  190. return;
  191. }
  192. }
  193. }
  194. while(!morkTM.outMaterialCompletes[pos]) //等待出料完成
  195. {
  196. Thread.Sleep(1000);
  197. }
  198. WriteData(address, false);
  199. }
  200. /// <summary>
  201. /// 转盘旋转位置设定
  202. /// </summary>
  203. /// <param name="posion"></param>
  204. private void PosionTurnOn(int posion)
  205. {
  206. int i = 0;
  207. string address = "";
  208. foreach (var item in polymer.GoodsMaterialPosion)
  209. {
  210. if (item.Key.Contains((MaterialPosion)posion))
  211. {
  212. WriteData(item.Value.SetPLCPosion,true);
  213. i = Convert.ToInt32(item.Value.posion);
  214. address = item.Value.GetPLCPosion;
  215. return;
  216. }
  217. }
  218. while(!morkTM.ReachPosions[i])//等待转盘到达信号
  219. {
  220. Thread.Sleep(1000);
  221. }
  222. if(address != null) WriteData(address, false);//把信号置为0
  223. }
  224. #region 调试功能
  225. /// <summary>
  226. /// 转盘回原点
  227. /// </summary>
  228. private void TurnOutPosion()
  229. {
  230. WriteData("M4.6", true);
  231. while (!morkTM.ReachPosions[6])
  232. {
  233. Thread.Sleep(1000);
  234. }
  235. WriteData("M14.6", false);
  236. }
  237. /// <summary>
  238. /// 调试转盘
  239. /// </summary>
  240. /// <param name="posion"></param>
  241. private void PosionTurnOnTest(int posion)
  242. {
  243. if(posion==0) TurnOutPosion();
  244. else
  245. {
  246. string address = string.Empty;
  247. int i = 0;
  248. foreach (var item in polymer.TurnPosionPLCs)
  249. {
  250. if ((OutMaterialPosion)item.posion == (OutMaterialPosion)posion)
  251. {
  252. WriteData(item.SetPLCPosion, true);
  253. address = item.GetPLCPosion;
  254. i = Convert.ToInt32(item.posion);
  255. return ;
  256. }
  257. }
  258. while(morkTM.ReachPosions[i-1])
  259. {
  260. Thread.Sleep(1000);
  261. }
  262. WriteData(address,false);
  263. }
  264. }
  265. /// <summary>
  266. /// 开启通道
  267. /// </summary>
  268. private void OpenPassway(int posion)
  269. {
  270. foreach(var item in polymer.OutPosionPLCs)
  271. {
  272. if((MaterialPosion)item.posion == (MaterialPosion)posion)
  273. {
  274. WriteData(item.SetPLCPosion,true);
  275. return ;
  276. }
  277. }
  278. }
  279. /// <summary>
  280. /// 开始校正
  281. /// </summary>
  282. /// <param name="passway"></param>
  283. /// <param name="time"></param>
  284. private void CheckPassway(int passway,int time)
  285. {
  286. WriteData(polymer.PasswayPosionList[(MaterialPosion)passway], Convert.ToInt32(time * expand));//写入出料时间
  287. WriteData("M5.0", true);//开始校正
  288. }
  289. /// <summary>
  290. /// 确认校正重量
  291. /// </summary>
  292. private void CheckMaterailWeight(int passway,float weight)
  293. {
  294. WriteData(polymer.PasswayPosionList[(MaterialPosion)passway], weight*expand);
  295. }
  296. /// <summary>
  297. /// 奶茶制作进度
  298. /// </summary>
  299. private void MakeProcess(string goodName,int percent,int count)
  300. {
  301. int res = Convert.ToInt32(Math.Floor((double)percent / count)) * 100;
  302. ActionManage.GetInstance.Send("奶茶制作进度",new object[] {goodName, res });
  303. }
  304. #endregion
  305. /// <summary>
  306. /// 把每一个物料的用量写入plc
  307. /// </summary>
  308. /// <param name="materials"></param>
  309. private void SetMatertialWeight(Dictionary<int, float> materials)
  310. {
  311. foreach (var material in materials)
  312. {
  313. int value = Convert.ToInt32(material.Value*expand);
  314. WriteData(polymer.MaterialPosionList[(MaterialPosion)material.Key], value);
  315. Thread.Sleep(200);
  316. }
  317. }
  318. public override void ReadData()
  319. {
  320. //启用通道的地址1
  321. GetStatus("M0.0", new Action<object>((obj) =>
  322. {
  323. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 28)
  324. {
  325. }
  326. }));
  327. GetStatus("M10.0",new Action<object>((obj) =>
  328. {
  329. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 28)
  330. {
  331. for (int i = 0; i < 28; i++)
  332. {
  333. if (morkTM.outMaterialCompletes.ContainsKey(i+1))
  334. {
  335. morkTM.outMaterialCompletes[i+1] = bools[i];
  336. }
  337. else
  338. {
  339. morkTM.outMaterialCompletes.Add(i+1, bools[i]);
  340. }
  341. }
  342. }
  343. }));
  344. //转盘移动的地址
  345. GetStatus("M4.0", new Action<object>((obj) =>
  346. {
  347. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 7)
  348. {
  349. }
  350. }));
  351. //装盘移动到位的地址
  352. GetStatus("M14.0", new Action<object>((obj) =>
  353. {
  354. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 7)
  355. {
  356. morkTM.ReachPosion_1 = bools[0];
  357. morkTM.ReachPosion_2 = bools[1];
  358. morkTM.ReachPosion_3 = bools[2];
  359. morkTM.ReachPosion_4 = bools[3];
  360. morkTM.ReachPosion_5 = bools[4];
  361. morkTM.ReachPosion_6 = bools[5];
  362. morkTM.ReachOutPosion_0 = bools[6];
  363. for (int i = 0; i < 7; i++)
  364. {
  365. morkTM.ReachPosions[i] = bools[i];
  366. }
  367. }
  368. }));
  369. }
  370. public override void ResetProgram()
  371. {
  372. morkTM = null;
  373. morkTM = new GVL_MorkTM();
  374. }
  375. private void WriteData(string address, object value)
  376. {
  377. EventBus.EventBus.GetInstance().Publish(new WriteModel() { DeviceId = DeviceId, Address = address, Value = value });
  378. }
  379. private void GetStatus(string key, Action<object> action)
  380. {
  381. if (peripheralStatus.ContainsKey(key))
  382. {
  383. if (peripheralStatus[key] != null)
  384. {
  385. action?.Invoke(peripheralStatus[key]);
  386. }
  387. }
  388. }
  389. public override void SimOrder()
  390. {
  391. ActionManage.GetInstance.Register(new Action<object>((o) =>
  392. {
  393. if (o is string goodName)
  394. {
  395. }
  396. }), "");
  397. }
  398. public override void Stop()
  399. {
  400. throw new NotImplementedException();
  401. }
  402. }
  403. }