终端一体化运控平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

400 satır
16 KiB

  1. using BPA.Helper;
  2. using BPA.Message.Enum;
  3. using BPA.Models;
  4. using BPASmartClient.Device;
  5. using BPASmartClient.EventBus;
  6. using BPASmartClient.Modbus;
  7. using BPASmartClient.Model;
  8. using BPASmartClient.Model.PLC;
  9. using BPASmartClient.Model.订单;
  10. using BPASmartClient.Model.调酒机;
  11. using BPASmartClient.MorkMW.Model;
  12. using Newtonsoft.Json;
  13. using Org.BouncyCastle.Bcpg.OpenPgp;
  14. using System;
  15. using System.Collections.Concurrent;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Net.Http.Json;
  19. using System.Threading;
  20. using System.Windows.Documents;
  21. using static BPASmartClient.EventBus.EventBus;
  22. namespace BPASmartClient.MorkMW
  23. {
  24. public class Control_MorkMW : BaseDevice
  25. {
  26. ModbusTcp modbus;
  27. public override DeviceClientType DeviceType => DeviceClientType.MORKMW;
  28. GVL_MorkMW morkMW = new GVL_MorkMW();
  29. public override void DoMain()
  30. {
  31. ConnectKlpRobot("192.168.0.100", 8001);
  32. ServerInit();
  33. DataParse();
  34. }
  35. private void ServerInit()
  36. {
  37. //配方数据信息
  38. EventBus.EventBus.GetInstance().Subscribe<MorkMWGoodsEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  39. {
  40. if (@event == null) return;
  41. if (@event is MorkMWGoodsEvent recipe)
  42. {
  43. listMorkMWGoods= recipe.morkMWPushMessage.MorkMWGoods;
  44. DeviceProcessLogShow($"接收到调酒机【{listMorkMWGoods.Count}】个商品配方数据");
  45. }
  46. });
  47. }
  48. public override void MainTask()
  49. {
  50. ToGetWink();
  51. ToMixWink();
  52. ToPourWink();
  53. SignalDetect();
  54. }
  55. /// <summary>
  56. /// 接酒过程
  57. /// </summary>
  58. public void ToGetWink()
  59. {
  60. if (!GVL_MorkMW.AllowLocalSimOrder)
  61. {
  62. if (morkMW.RobotIdle && orderLocInfos.Count > 0 && !morkMW.TaskLock && !morkMW.PourWinkComplete && morkMW.CupSignal)
  63. {
  64. DeviceProcessLogShow("订单开始制作");
  65. morkMW.TaskLock = true;
  66. if (orderLocInfos.TryDequeue(out OrderLocInfo res))
  67. {
  68. foreach (var item in res.mixWink)
  69. {
  70. switch (item.Loc)
  71. {
  72. // case 7: ToMixWink(); break;
  73. case 1: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
  74. case 2: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
  75. case 3: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
  76. case 4: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
  77. case 5: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
  78. case 6: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
  79. }
  80. switch (item.Loc)
  81. {
  82. //case 7: while (!morkMW.MixWinkComplte) { Thread.Sleep(5); } break;
  83. case 1: while (!morkMW.TakeWinkOneComplete) { Thread.Sleep(5); } break;
  84. case 2: while (!morkMW.TakeWinkTwoComplete) { Thread.Sleep(5); } break;
  85. case 3: while (!morkMW.TakeWinkThreeComplete) { Thread.Sleep(5); } break;
  86. case 4: while (!morkMW.TakeWinkFourComplete) { Thread.Sleep(5); } break;
  87. case 5: while (!morkMW.TakeWinkFiveComplete) { Thread.Sleep(5); } break;
  88. case 6: while (!morkMW.TakeWinkSixComplete) { Thread.Sleep(5); } break;
  89. }
  90. }
  91. morkMW.AllowMixWink = true;//接酒完成,允许调酒
  92. // morkMW.AllowPourWink = true;//调酒完成,允许倒酒
  93. }
  94. }
  95. }
  96. else
  97. {
  98. if (morkMW.RobotIdle && orderLocInfos.Count > 0 && !morkMW.TaskLock && !morkMW.PourWinkComplete && morkMW.CupSignal)
  99. {
  100. DeviceProcessLogShow("订单开始制作");
  101. morkMW.TaskLock = true;
  102. if (simOrderLocInfos.TryDequeue(out SimOrderLocInfo res))
  103. {
  104. foreach (var item in res.mixWink)
  105. {
  106. switch (item.Loc)
  107. {
  108. // case 7: ToMixWink(); break;
  109. case 1: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
  110. case 2: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
  111. case 3: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
  112. case 4: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
  113. case 5: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
  114. case 6: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
  115. }
  116. switch (item.Loc)
  117. {
  118. //case 7: while (!morkMW.MixWinkComplte) { Thread.Sleep(5); } break;
  119. case 1: while (!morkMW.TakeWinkOneComplete) { Thread.Sleep(5); } break;
  120. case 2: while (!morkMW.TakeWinkTwoComplete) { Thread.Sleep(5); } break;
  121. case 3: while (!morkMW.TakeWinkThreeComplete) { Thread.Sleep(5); } break;
  122. case 4: while (!morkMW.TakeWinkFourComplete) { Thread.Sleep(5); } break;
  123. case 5: while (!morkMW.TakeWinkFiveComplete) { Thread.Sleep(5); } break;
  124. case 6: while (!morkMW.TakeWinkSixComplete) { Thread.Sleep(5); } break;
  125. }
  126. }
  127. DeviceProcessLogShow($"所有位置接酒完成");
  128. morkMW.AllowMixWink = true;//接酒完成,允许调酒
  129. // morkMW.AllowPourWink = true;//调酒完成,允许倒酒
  130. }
  131. }
  132. }
  133. }
  134. /// <summary>
  135. /// 调酒过程
  136. /// </summary>
  137. public void ToMixWink()
  138. {
  139. // modbus.WriteSingleRegister(0000, 7);//机器人调酒
  140. if (morkMW.AllowMixWink)
  141. {
  142. DeviceProcessLogShow("开始调酒");
  143. modbus.WriteSingleRegister(0000, 7);//机器人调酒
  144. morkMW.AllowMixWink = false;
  145. }
  146. }
  147. /// <summary>
  148. /// 去对应位置接酒
  149. /// </summary>
  150. /// <param name="loc"></param>
  151. /// <param name="time"></param>
  152. public void ToSpecifiedLocTakeWink(int loc, int time)
  153. {
  154. //发送接酒信号
  155. switch (loc)
  156. {
  157. case 1: modbus.WriteSingleRegister(0000, 1);DeviceProcessLogShow($"开始接【{loc}】号位置原酒"); break;
  158. case 2: modbus.WriteSingleRegister(0000, 2);DeviceProcessLogShow($"开始接【{loc}】号位置原酒"); break;
  159. case 3: modbus.WriteSingleRegister(0000, 3);DeviceProcessLogShow($"开始接【{loc}】号位置原酒"); break;
  160. case 4: modbus.WriteSingleRegister(0000, 4);DeviceProcessLogShow($"开始接【{loc}】号位置原酒"); break;
  161. case 5: modbus.WriteSingleRegister(0000, 5);DeviceProcessLogShow($"开始接【{loc}】号位置原酒"); break;
  162. case 6: modbus.WriteSingleRegister(0000, 6); DeviceProcessLogShow($"开始接【{loc}】号位置原酒"); break;
  163. }
  164. //等待机器人到达接酒位置
  165. switch (loc)
  166. {
  167. case 1: while (!morkMW.ArriveWinkOneLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"机械臂到达【{loc}】号接酒位置"); break;
  168. case 2: while (!morkMW.ArriveWinkTwoLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"机械臂到达【{loc}】号接酒位置"); break;
  169. case 3: while (!morkMW.ArriveWinkThreeLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"机械臂到达【{loc}】号接酒位置"); break;
  170. case 4: while (!morkMW.ArriveWinkFourLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"机械臂到达【{loc}】号接酒位置"); break;
  171. case 5: while (!morkMW.ArriveWinkFiveLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"机械臂到达【{loc}】号接酒位置"); break;
  172. case 6: while (!morkMW.ArriveWinkSixLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"机械臂到达【{loc}】号接酒位置"); break;
  173. }
  174. Thread.Sleep(time * 1000);//接酒等待时间
  175. //发送接酒完成信号
  176. switch (loc)
  177. {
  178. case 1: modbus.WriteSingleCoil(4596, true);DeviceProcessLogShow($"【{loc}】号位置接酒完成"); break;
  179. case 2: modbus.WriteSingleCoil(4597, true);DeviceProcessLogShow($"【{loc}】号位置接酒完成"); break;
  180. case 3: modbus.WriteSingleCoil(4598, true);DeviceProcessLogShow($"【{loc}】号位置接酒完成"); break;
  181. case 4: modbus.WriteSingleCoil(4599, true);DeviceProcessLogShow($"【{loc}】号位置接酒完成"); break;
  182. case 5: modbus.WriteSingleCoil(4600, true);DeviceProcessLogShow($"【{loc}】号位置接酒完成"); break;
  183. case 6: modbus.WriteSingleCoil(4601, true); DeviceProcessLogShow($"【{loc}】号位置接酒完成"); break;
  184. }
  185. }
  186. /// <summary>
  187. /// 倒酒过程
  188. /// </summary>
  189. public void ToPourWink()
  190. {
  191. //if (morkMW.AllowPourWink && morkMW.CupSignal)
  192. //{
  193. // morkMW.AllowPourWink = false;
  194. // modbus.WriteSingleRegister(0000, 8);//倒酒
  195. //}
  196. if (morkMW.CupSignal && RTrig.GetInstance("AllowOut").Start(morkMW.MixWinkComplte))
  197. {
  198. DeviceProcessLogShow("开始倒酒");
  199. modbus.WriteSingleRegister(0000, 8);//倒酒
  200. }
  201. }
  202. /// <summary>
  203. /// 信号检测
  204. /// </summary>
  205. public void SignalDetect()
  206. {
  207. if (RTrig.GetInstance("AllComplete").Start(morkMW.ProcessComplete))
  208. {
  209. morkMW.PourWinkComplete = true;
  210. DeviceProcessLogShow("订单制作完成,请取走调好酒杯");
  211. }
  212. if (morkMW.PourWinkComplete)
  213. {
  214. if (TTrig.GetInstance("PoutWinkCom").Start(morkMW.CupSignal))
  215. {
  216. DeviceProcessLogShow("客户取走调好酒杯,请放置空酒杯,以待下次订单制作");
  217. }
  218. if (RTrig.GetInstance("PoutWinkCom").Start(morkMW.CupSignal))
  219. {
  220. DeviceProcessLogShow("空酒杯就位,允许执行下一订单");
  221. morkMW.PourWinkComplete = false;
  222. morkMW.TaskLock = false;
  223. Thread.Sleep(2000);
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// 读卡乐普机器人状态
  229. /// </summary>
  230. public override void ReadData()
  231. {
  232. //GetStatus("M230.0", new Action<object>((obj) =>
  233. //{
  234. //}));
  235. if (modbus.Connected)
  236. {
  237. morkMW.ArriveWinkOneLoc = modbus.ReadCoils(4616);
  238. morkMW.ArriveWinkTwoLoc = modbus.ReadCoils(4617);
  239. morkMW.ArriveWinkThreeLoc = modbus.ReadCoils(4618);
  240. morkMW.ArriveWinkFourLoc = modbus.ReadCoils(4619);
  241. morkMW.ArriveWinkFiveLoc = modbus.ReadCoils(4620);
  242. morkMW.ArriveWinkSixLoc = modbus.ReadCoils(4621);
  243. morkMW.RobotIdle = modbus.ReadCoils(4201);
  244. morkMW.CupSignal = modbus.ReadInputs(0);
  245. morkMW.TakeWinkOneComplete = modbus.ReadCoils(4606);
  246. morkMW.TakeWinkTwoComplete = modbus.ReadCoils(4607);
  247. morkMW.TakeWinkThreeComplete = modbus.ReadCoils(4608);
  248. morkMW.TakeWinkFourComplete = modbus.ReadCoils(4609);
  249. morkMW.TakeWinkFiveComplete = modbus.ReadCoils(4610);
  250. morkMW.TakeWinkSixComplete = modbus.ReadCoils(4611);
  251. morkMW.MixWinkComplte = modbus.ReadCoils(4612);
  252. morkMW.ProcessComplete = modbus.ReadCoils(4613);
  253. }
  254. else
  255. {
  256. modbus.ModbusTcpConnect("192.168.0.100", 8001);
  257. }
  258. }
  259. public override void ResetProgram()
  260. {
  261. morkMW = null;
  262. morkMW = new GVL_MorkMW();
  263. }
  264. public override void SimOrder()
  265. {
  266. EventBus.EventBus.GetInstance().Subscribe<MorkMWSimOrder>(0, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  267. {
  268. if (@event != null && @event is MorkMWSimOrder order)
  269. {
  270. if (order.mixWink.Count > 0)
  271. {
  272. string guid = Guid.NewGuid().ToString();
  273. simOrderLocInfos.Enqueue(new SimOrderLocInfo { mixWink = order.mixWink, SuborderId = guid });
  274. DeviceProcessLogShow("收到模拟订单");
  275. }
  276. }
  277. });
  278. }
  279. public override void Stop()
  280. {
  281. }
  282. private void GetStatus(string key, Action<object> action)
  283. {
  284. if (peripheralStatus.ContainsKey(key))
  285. {
  286. if (peripheralStatus[key] != null)
  287. {
  288. action?.Invoke(peripheralStatus[key]);
  289. }
  290. }
  291. }
  292. /// <summary>
  293. /// 卡乐普写数据
  294. /// </summary>
  295. /// <param name="address"></param>
  296. /// <param name="value"></param>
  297. private void WriteData(string address, object value)
  298. {
  299. EventBus.EventBus.GetInstance().Publish(new WriteModel() { DeviceId = DeviceId, Address = address, Value = value });
  300. }
  301. /// <summary>
  302. /// 数据解析
  303. /// </summary>
  304. public void DataParse()
  305. {
  306. EventBus.EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  307. {
  308. if (@event != null && @event is DoOrderEvent order)
  309. {
  310. var res= listMorkMWGoods.FirstOrDefault(p => p.GoodsKey == order.MorkOrder.GoodsKey);
  311. if (res != null)
  312. {
  313. string guid = Guid.NewGuid().ToString();
  314. orderLocInfos.Enqueue(new OrderLocInfo { mixWink = res.MorkMWBoms, SuborderId = order.MorkOrder.SuborderId });
  315. DeviceProcessLogShow($"收到小程序订单,订单号【{order.MorkOrder.SuborderId}】");
  316. }
  317. }
  318. });
  319. }
  320. /// <summary>
  321. /// 连接卡乐普机器人
  322. /// </summary>
  323. /// <param name="ip"></param>
  324. /// <param name="port"></param>
  325. public void ConnectKlpRobot(string ip, int port)
  326. {
  327. modbus = new ModbusTcp();
  328. modbus.Show += new Action<string>((s) =>
  329. {
  330. if (s != null) DeviceProcessLogShow(s);
  331. });
  332. modbus.ShowEx += new Action<string>((s) =>
  333. {
  334. if ((s != null)) DeviceProcessLogShow(s);
  335. });
  336. modbus.ModbusTcpConnect(ip, port);
  337. }
  338. /// <summary>
  339. /// 调酒小程序订单队列
  340. /// </summary>
  341. public ConcurrentQueue<OrderLocInfo> orderLocInfos = new ConcurrentQueue<OrderLocInfo>();
  342. /// <summary>
  343. /// 调酒模拟订单队列
  344. /// </summary>
  345. public ConcurrentQueue<SimOrderLocInfo> simOrderLocInfos = new ConcurrentQueue<SimOrderLocInfo>();
  346. /// <summary>
  347. /// 调酒机配方集合
  348. /// </summary>
  349. public List<MORKMWGoods> listMorkMWGoods = new List<MORKMWGoods>();
  350. }
  351. }