终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

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