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

Control_MorkT_Container.cs 18 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Collections.Concurrent;
  7. using System.Diagnostics;
  8. using System.Threading.Tasks;
  9. using BPASmartClient.Device;
  10. using BPA.Message.Enum;
  11. using BPA.Message;
  12. using BPASmartClient.Helper;
  13. using BPASmartClient.Model.咖啡机.Enum;
  14. using BPASmartClient.Model;
  15. using BPASmartClient.EventBus;
  16. using static BPASmartClient.EventBus.EventBus;
  17. using BPASmartClient.Message;
  18. using BPASmartClient.Model.乐白机器人;
  19. using BPASmartClient.Model.单片机;
  20. using BPASmartClient.Model.PLC;
  21. using BPASmartClient.DRCoffee;
  22. using BPASmartClient.LebaiRobot;
  23. using System.Text.RegularExpressions;
  24. namespace BPASmartClient.MorkT_Container
  25. {
  26. public class Control_MorkT_Container : BaseDevice
  27. {
  28. private Dictionary<string, PolymerBatching> batchings = new Dictionary<string, PolymerBatching>();
  29. //容器位置
  30. private string holderLoc;
  31. //主料位置
  32. private string mainMaterialLoc;
  33. public override global::BPA.Message.Enum.DeviceClientType DeviceType { get { return BPA.Message.Enum.DeviceClientType.MORKT; } }
  34. GVL_MorkT morkTLebaiJC =new GVL_MorkT();
  35. public override void DoMain()
  36. {
  37. if (Json<KeepDataBase>.Data.IsVerify)
  38. {
  39. IsHealth = true;
  40. }
  41. IsHealth = true;
  42. serverInit();
  43. DataParse();
  44. ActionManage.GetInstance.Register(new Action<object>((o) => { SimOrder(o); }), "SimOrder");//模拟订单委托注册
  45. }
  46. private void serverInit()
  47. {
  48. EventBus.EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  49. {
  50. if (@event == null) return;
  51. if (@event is MaterialDeliveryEvent material)
  52. {
  53. orderMaterialDelivery = material.orderMaterialDelivery;
  54. }
  55. });
  56. }
  57. private void DataParse()
  58. {
  59. EventBus.EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  60. {
  61. if (@event == null) return;
  62. if (@event is DoOrderEvent order)
  63. {
  64. if (order.MorkOrder.GoodBatchings == null) return;
  65. OrderCount++;
  66. DeviceProcessLogShow($"接收到{OrderCount}次订单");
  67. batchings = PolymerBatching.BuildAll();
  68. //商品类型
  69. GOODS_TYPE currentGoodsType = GOODS_TYPE.NEITHER;
  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. //验证商品是做的某种饮料
  76. if (ValidateGoodsByBatching(res.BatchingLoc) != GOODS_TYPE.NEITHER)
  77. {
  78. //获取当前物料所属商品类型
  79. currentGoodsType = ValidateGoodsByBatching(res.BatchingLoc);
  80. }
  81. switch (batchings[res.BatchingLoc].BatchingClass)
  82. {
  83. case BATCHING_CLASS.HOLDER:
  84. holderLoc = res.BatchingLoc;
  85. break;
  86. case BATCHING_CLASS.MAIN_MATERIAL:
  87. mainMaterialLoc = res.BatchingLoc;
  88. break;
  89. }
  90. //根据商品类型执行具体制作流程
  91. switch (currentGoodsType)
  92. {
  93. case GOODS_TYPE.COFFEE:
  94. if (morkTLebaiJC.morkOrderPushesCoffee.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  95. {
  96. morkTLebaiJC.morkOrderPushesCoffee.Enqueue(new OrderLocInfo()
  97. {
  98. SuborderId = order.MorkOrder.SuborderId,
  99. BatchingId = res.BatchingId,
  100. Loc = ushort.Parse( mainMaterialLoc),
  101. GoodName = order.MorkOrder.GoodsName,
  102. });
  103. }
  104. break;
  105. case GOODS_TYPE.NEITHER:
  106. DeviceProcessLogShow("未知的商品类型");
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. });
  113. }
  114. /// <summary>
  115. /// 将空杯放好到接饮料的地方的标志位
  116. /// </summary>
  117. private bool bFirstTrig_Coffee = false;
  118. /// <summary>
  119. /// 延迟的超时时间
  120. /// </summary>
  121. DateTime delayTimeOut_Coffee;
  122. public override void MainTask()
  123. {
  124. if (morkTLebaiJC.morkOrderPushesCoffee.Count > 0 && morkTLebaiJC.IsHaveCoffeeCup)
  125. {
  126. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CoffeEndCookEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  127. {
  128. if (morkTLebaiJC.IsHaveCoffeeCup && morkTLebaiJC.MakeCoffeeEnd != true)
  129. {
  130. morkTLebaiJC.MakeCoffeeEnd = true;
  131. bFirstTrig_Coffee = false;
  132. }
  133. });
  134. if (morkTLebaiJC.IsHaveCoffeeCup && morkTLebaiJC.MakeCoffeeEnd != true)
  135. {
  136. if (!bFirstTrig_Coffee)
  137. {
  138. bFirstTrig_Coffee = true;
  139. delayTimeOut_Coffee = DateTime.Now;
  140. }
  141. else if (DateTime.Now.Subtract(delayTimeOut_Coffee).TotalSeconds > 180 && bFirstTrig_Coffee == true)
  142. {
  143. DeviceProcessLogShow("接咖啡超时,接咖啡结束,等待取咖啡");
  144. bFirstTrig_Coffee = false;
  145. morkTLebaiJC.MakeCoffeeEnd = true;
  146. }
  147. }
  148. }
  149. DoCoffee();
  150. }
  151. /// <summary>
  152. /// 订单状态改变
  153. /// </summary>
  154. /// <param name="subid"></param>
  155. /// <param name="oRDER_STATUS"></param>
  156. private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
  157. {
  158. EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent() { Status = oRDER_STATUS, SubOrderId = subid, deviceClientType = DeviceType });
  159. }
  160. /// <summary>
  161. /// 验证商品是做的某种饮料
  162. /// </summary>
  163. /// <param name="batchingLoc">物料位置</param>
  164. private GOODS_TYPE ValidateGoodsByBatching(string batchingLoc)
  165. {
  166. if (batchings.ContainsKey(batchingLoc))
  167. return batchings[batchingLoc].GoodsType;
  168. return GOODS_TYPE.NEITHER;
  169. }
  170. /// <summary>
  171. /// 乐白的场景结束等待
  172. /// </summary>
  173. /// <param name="value"></param>
  174. private void Wait(int value = 101)
  175. {
  176. while (!((bool)peripheralStatus["RobotOK"] && (int)peripheralStatus["RobotValue"] == value))
  177. {
  178. Thread.Sleep(5);
  179. }
  180. EventBus.EventBus.GetInstance().Publish(new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }, (o) => { });
  181. }
  182. /// <summary>
  183. /// 乐白的场景
  184. /// </summary>
  185. /// <param name="sen"></param>
  186. private void Sence(int sen)
  187. {
  188. new LebaiRobot_LebaiSenceEvent { DeviceId = DeviceId, LebaiSence = sen }.Publish();
  189. }
  190. /// <summary>
  191. /// 乐白的数字量输出
  192. /// </summary>
  193. /// <param name="value"></param>
  194. /// <param name="pin"></param>
  195. private void Output(bool value,int pin)
  196. {
  197. new LebaiRobot_SetOutPutEvent { DeviceId = DeviceId, Pin = pin,Value=value }.Publish();
  198. }
  199. bool outCupCheck = false;//放纸杯位置有无判断
  200. /// <summary>
  201. /// 判断是否有咖啡订单
  202. /// </summary>
  203. /// <returns></returns>
  204. private bool IsMakeCoffee()
  205. {
  206. bool bMake = (IsHealth && morkTLebaiJC.morkOrderPushesCoffee.Count > 0 && !morkTLebaiJC.IsHaveCoffeeCup) ? true : false;
  207. return bMake;
  208. }
  209. /// <summary>
  210. /// 做咖啡流程
  211. /// </summary>
  212. private void DoCoffee()
  213. {
  214. if (IsMakeCoffee())
  215. {
  216. PickUpCoffee();//接咖啡
  217. morkTLebaiJC.IsHaveCoffeeCup = true;
  218. }
  219. else if(morkTLebaiJC.MakeCoffeeEnd)
  220. {
  221. try
  222. {
  223. PutCoffeeCup();
  224. morkTLebaiJC.MakeCoffeeEnd = false;
  225. morkTLebaiJC.IsHaveCoffeeCup = false;
  226. morkTLebaiJC.morkOrderPushesCoffee.TryDequeue(out OrderLocInfo orderLoc);
  227. }
  228. catch (Exception ex)
  229. {
  230. MessageLog.GetInstance.ShowEx(ex.ToString());
  231. }
  232. }
  233. }
  234. #region 做咖啡流程
  235. /// <summary>
  236. /// 接咖啡
  237. /// </summary>
  238. private void PickUpCoffee()
  239. {
  240. if (!morkTLebaiJC.IsHaveCoffeeCup)
  241. {
  242. outCupCheck = false;
  243. OrderChange(morkTLebaiJC.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  244. EventBus.EventBus.GetInstance().Publish(new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }, (o) => { });
  245. int resultTakeCup = takeCup();
  246. if (resultTakeCup == 1)
  247. {
  248. Sence(GVL_MorkT.攀华_接咖啡);
  249. Wait();
  250. new DRCoffee_MakeCoffeeEvent() { DeviceId=DeviceId, DrinkCode = (DrCoffeeDrinksCode)(morkTLebaiJC.morkOrderPushesCoffee.ElementAt(0).Loc)}.Publish();
  251. }
  252. else
  253. {
  254. DeviceProcessLogShow("取杯失败 回到初始位,请及时处理!!");
  255. /*new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  256. Sence(GVL_MorkT.攀华_初始位);
  257. Wait();*/
  258. }
  259. }
  260. }
  261. /// <summary>
  262. /// 咖啡杯接好,放咖啡杯
  263. /// </summary>
  264. private void PutCoffeeCup()
  265. {
  266. while (morkTLebaiJC.RobotGetInput0)//判断放杯位置是否有物品
  267. {
  268. if (!outCupCheck)
  269. DeviceProcessLogShow("成品处有纸杯存在,请取走!!");
  270. outCupCheck = true;
  271. }
  272. outCupCheck = false;
  273. Sence(GVL_MorkT.攀华_放杯);
  274. Wait();
  275. if (!morkTLebaiJC.RobotGetInput0)
  276. {
  277. DeviceProcessLogShow("咖啡杯未安全放置在放杯位");
  278. }
  279. Sence(GVL_MorkT.攀华_初始位);
  280. Wait();
  281. while (morkTLebaiJC.RobotGetTcpInput)
  282. {
  283. DeviceProcessLogShow("咖啡杯仍在夹爪上");
  284. }
  285. OrderChange(morkTLebaiJC.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  286. DeviceProcessLogShow($"-------------{Regex.Replace(morkTLebaiJC.morkOrderPushesCoffee.ElementAt(0).GoodName, @"[\r\n]", "")}制作完成------------");
  287. }
  288. #endregion
  289. /// <summary>
  290. /// 取杯的次数
  291. /// </summary>
  292. private int nCnt;
  293. /// <summary>
  294. /// 取杯流程
  295. /// </summary>
  296. /// <returns>0:无意义,1:取杯成功 2:取杯失败</returns>
  297. private int takeCup()
  298. {
  299. try
  300. {
  301. nCnt = 0;
  302. Sence(GVL_MorkT.攀华_初始位);
  303. Wait();
  304. Sence(GVL_MorkT.攀华_取杯);
  305. Wait();
  306. Sence(GVL_MorkT.攀华_落杯);
  307. Wait();//落杯使用机器人控制
  308. /*new LebaiRobot_SetOutPutEvent { DeviceId = DeviceId, Value = true, Pin = 1 }.Publish();//落杯器电机转动
  309. while (true)
  310. {
  311. if (GetStatus<bool>("LeibaiGetInput2"))
  312. {
  313. break;
  314. }
  315. }
  316. while (true)
  317. {
  318. if (!GetStatus<bool>("LeibaiGetInput2"))
  319. {
  320. break;
  321. }
  322. }
  323. new LebaiRobot_SetOutPutEvent { DeviceId = DeviceId, Value = true, Pin = 0 }.Publish();//落杯器电机转动*/
  324. nCnt++;
  325. Thread.Sleep(2000);
  326. while (!morkTLebaiJC.RobotGetTcpInput) //读取传感器的值
  327. {
  328. if (nCnt > 3)
  329. {
  330. nCnt = 0;
  331. DeviceProcessLogShow("三次取杯失败,回原点");
  332. Sence(GVL_MorkT.攀华_安全位);
  333. Wait();
  334. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  335. Sence(GVL_MorkT.攀华_初始位);
  336. Wait();
  337. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  338. return 2;
  339. }
  340. else
  341. {
  342. nCnt++;
  343. Sence(GVL_MorkT.攀华_二次取杯);
  344. Wait();
  345. EventBus.EventBus.GetInstance().Publish(new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }, (o) => { });
  346. Sence(GVL_MorkT.攀华_落杯);
  347. Wait();
  348. EventBus.EventBus.GetInstance().Publish(new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }, (o) => { });
  349. }
  350. Thread.Sleep(100);
  351. }
  352. return 1;
  353. }
  354. catch (Exception ex)
  355. {
  356. DeviceProcessLogShow(ex.ToString());
  357. }
  358. return 2;
  359. }
  360. public void SimOrder<T>(T simOrder)
  361. {
  362. }
  363. #region 流程控制
  364. private void GetStatus(string key, Action<object> action)
  365. {
  366. if (peripheralStatus.ContainsKey(key))
  367. {
  368. if (peripheralStatus[key] != null)
  369. {
  370. action?.Invoke(peripheralStatus[key]);
  371. }
  372. }
  373. }
  374. public override void ReadData()
  375. {
  376. #region 乐白机器人状态
  377. GetStatus("RobotIsConnected", new Action<object>((o) =>
  378. {
  379. if (o is bool b)
  380. {
  381. morkTLebaiJC.RobotIsConnected = b;
  382. }
  383. }));
  384. GetStatus("RobotMode", new Action<object>((o) =>
  385. {
  386. if (o is ELebaiRModel eLebaiRModel)
  387. {
  388. morkTLebaiJC.RobotMode = eLebaiRModel;
  389. }
  390. }));
  391. GetStatus("LeibaiGetTcpInput", new Action<object>((o) =>
  392. {
  393. if (o is bool b)
  394. {
  395. morkTLebaiJC.RobotGetTcpInput = b;
  396. }
  397. }));
  398. GetStatus("LeibaiGetInput1", new Action<object>((o) =>
  399. {
  400. if (o is bool b)
  401. {
  402. morkTLebaiJC.RobotGetInput0 = b;
  403. }
  404. }));
  405. GetStatus("LeibaiGetInput2", new Action<object>((o) =>
  406. {
  407. if (o is bool b)
  408. {
  409. morkTLebaiJC.RobotGetInput1 = b;
  410. }
  411. }));
  412. GetStatus("LeibaiGetInput3", new Action<object>((o) =>
  413. {
  414. if (o is bool b)
  415. {
  416. morkTLebaiJC.RobotGetInput2 = b;
  417. }
  418. }));
  419. GetStatus("LeibaiGetInput4", new Action<object>((o) =>
  420. {
  421. if (o is bool b)
  422. {
  423. morkTLebaiJC.RobotGetInput3 = b;
  424. }
  425. }));
  426. #endregion
  427. }
  428. #endregion
  429. public override void Stop()
  430. {
  431. }
  432. public override void ResetProgram()
  433. {
  434. }
  435. public override void SimOrder()
  436. {
  437. EventBus.EventBus.GetInstance().Subscribe<MorkTSimOrder>(0, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  438. {
  439. string guid = Guid.NewGuid().ToString();
  440. if (@event != null && @event is MorkTSimOrder msm)
  441. {
  442. DeviceProcessLogShow("----开始模拟订单----");
  443. if (msm.OrderNum == 1)
  444. {
  445. morkTLebaiJC.morkOrderPushesCoffee.Enqueue(new OrderLocInfo() { Loc =(ushort)msm.DrinkCode, SuborderId = guid, GoodName="模拟咖啡订单" });
  446. }
  447. }
  448. });
  449. }
  450. }
  451. }