终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

486 行
21 KiB

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