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

1177 lines
46 KiB

  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. /*
  27. * 冰淇淋咖啡机组合套装
  28. * 物料位置:
  29. * 1:冰淇料
  30. * 2:冰淇淋杯
  31. * 5:咖啡
  32. * 6:咖啡杯
  33. * 9: 茶
  34. * 10: 茶杯
  35. */
  36. public class Control_MorkT_Container : BaseDevice
  37. {
  38. private Dictionary<string, PolymerBatching> batchings = new Dictionary<string, PolymerBatching>();
  39. //容器位置
  40. private string holderLoc;
  41. //主料位置
  42. private string mainMaterialLoc;
  43. public override global::BPA.Message.Enum.DeviceClientType DeviceType { get { return BPA.Message.Enum.DeviceClientType.MORKT; } }
  44. GVL_MorkT morkTLebaiJC =new GVL_MorkT();
  45. /// <summary>
  46. /// 果汁机做法,true:热饮,false:冷饮
  47. /// </summary>
  48. private bool GuMake = false;
  49. /// <summary>
  50. /// 订单是否加冰
  51. /// </summary>
  52. private bool IsAddIce = false;
  53. public override void DoMain()
  54. {
  55. if (Json<KeepDataBase>.Data.IsVerify)
  56. {
  57. IsHealth = true;
  58. }
  59. IsHealth = true;
  60. serverInit();
  61. DataParse();
  62. ActionManage.GetInstance.Register(new Action<object>((o) => { SimOrder(o); }), "SimOrder");//模拟订单委托注册
  63. }
  64. private void serverInit()
  65. {
  66. EventBus.EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  67. {
  68. if (@event == null) return;
  69. if (@event is MaterialDeliveryEvent material)
  70. {
  71. orderMaterialDelivery = material.orderMaterialDelivery;
  72. }
  73. });
  74. }
  75. private void DataParse()
  76. {
  77. EventBus.EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  78. {
  79. if (@event == null) return;
  80. if (@event is DoOrderEvent order)
  81. {
  82. if (order.MorkOrder.GoodBatchings == null) return;
  83. OrderCount++;
  84. DeviceProcessLogShow($"接收到{OrderCount}次订单");
  85. batchings = PolymerBatching.BuildAll();
  86. //商品类型
  87. GOODS_TYPE currentGoodsType = GOODS_TYPE.NEITHER;
  88. foreach (var item in order.MorkOrder.GoodBatchings)
  89. {
  90. var res = orderMaterialDelivery?.BatchingInfo?.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  91. if (res != null)
  92. {
  93. //验证商品是做的某种饮料
  94. if (ValidateGoodsByBatching(res.BatchingLoc) != GOODS_TYPE.NEITHER)
  95. {
  96. //获取当前物料所属商品类型
  97. currentGoodsType = ValidateGoodsByBatching(res.BatchingLoc);
  98. }
  99. switch (batchings[res.BatchingLoc].BatchingClass)
  100. {
  101. case BATCHING_CLASS.HOLDER:
  102. holderLoc = res.BatchingLoc;
  103. break;
  104. case BATCHING_CLASS.MAIN_MATERIAL:
  105. mainMaterialLoc = res.BatchingLoc;
  106. break;
  107. }
  108. //根据商品类型执行具体制作流程
  109. switch (currentGoodsType)
  110. {
  111. case GOODS_TYPE.COFFEE:
  112. if (morkTLebaiJC.morkOrderPushesCoffee.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  113. {
  114. morkTLebaiJC.morkOrderPushesCoffee.Enqueue(new OrderLocInfo()
  115. {
  116. SuborderId = order.MorkOrder.SuborderId,
  117. BatchingId = res.BatchingId,
  118. Loc = ushort.Parse( mainMaterialLoc),
  119. GoodName = order.MorkOrder.GoodsName,
  120. });
  121. }
  122. break;
  123. case GOODS_TYPE.JUICE:
  124. if (morkTLebaiJC.morkOrderPushesJuicer.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  125. {
  126. morkTLebaiJC.morkOrderPushesJuicer.Enqueue(new OrderLocInfo()
  127. {
  128. SuborderId = order.MorkOrder.SuborderId,
  129. BatchingId = res.BatchingId,
  130. Loc = ushort.Parse(mainMaterialLoc),
  131. GoodName = order.MorkOrder.GoodsName,
  132. makeID = order.MorkOrder.MakeID,
  133. });
  134. }
  135. break;
  136. case GOODS_TYPE.TEA:
  137. if (morkTLebaiJC.morkOrderPushesTea.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  138. {
  139. morkTLebaiJC.morkOrderPushesTea.Enqueue(new OrderLocInfo()
  140. {
  141. SuborderId = order.MorkOrder.SuborderId,
  142. BatchingId = res.BatchingId,
  143. Loc = ushort.Parse(mainMaterialLoc),
  144. GoodName = order.MorkOrder.GoodsName,
  145. });
  146. }
  147. break;
  148. case GOODS_TYPE.WATER:
  149. if (morkTLebaiJC.morkOrderPushesWater.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  150. {
  151. morkTLebaiJC.morkOrderPushesWater.Enqueue(new OrderLocInfo()
  152. {
  153. SuborderId = order.MorkOrder.SuborderId,
  154. BatchingId = res.BatchingId,
  155. Loc = ushort.Parse(mainMaterialLoc),
  156. GoodName = order.MorkOrder.GoodsName,
  157. });
  158. }
  159. break;
  160. case GOODS_TYPE.NEITHER:
  161. DeviceProcessLogShow("未知的商品类型");
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. });
  168. }
  169. /// <summary>
  170. /// 将空杯放好到接饮料的地方的标志位
  171. /// </summary>
  172. private bool bFirstTrig_TeaWater = false;
  173. private bool bFirstTrig_Coffee = false;
  174. private bool bFirstTrig_Juice = false;
  175. /// <summary>
  176. /// 延迟的超时时间
  177. /// </summary>
  178. DateTime delayTimeOut_Water;
  179. DateTime delayTimeOut_Coffee;
  180. DateTime delayTimeOut_Juice;
  181. public override void MainTask()
  182. {
  183. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CoffeEndCookEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  184. {
  185. if (morkTLebaiJC.IsHaveCoffeeCup && morkTLebaiJC.MakeCoffeeEnd != true)
  186. morkTLebaiJC.MakeCoffeeEnd = true;
  187. });
  188. if (morkTLebaiJC.IsHaveCoffeeCup && morkTLebaiJC.MakeCoffeeEnd != true)
  189. {
  190. if (!bFirstTrig_Coffee)
  191. {
  192. bFirstTrig_Coffee = true;
  193. delayTimeOut_Coffee = DateTime.Now;
  194. }
  195. else if (DateTime.Now.Subtract(delayTimeOut_Coffee).TotalSeconds > 180 && bFirstTrig_Coffee == true)
  196. {
  197. DeviceProcessLogShow("接咖啡超时,接咖啡结束,等待取咖啡");
  198. bFirstTrig_Coffee = false;
  199. if (morkTLebaiJC.IsHaveCoffeeCup)
  200. morkTLebaiJC.MakeCoffeeEnd = true;
  201. }
  202. }
  203. if (morkTLebaiJC.IsHaveJuiceCup && morkTLebaiJC.MakeJuiceEnd != true)
  204. {
  205. var Juicestate = GetStatus<int[]>("GetDeviceStatus");
  206. if (Juicestate != null)
  207. {
  208. if (Juicestate.Length > 0)
  209. {
  210. var Juicestate1 = Convert.ToString(Juicestate[0], 2);
  211. var Juicestate2 = Juicestate[1];
  212. if (Juicestate1.IndexOf("0") == 1 && Juicestate2 == 0)
  213. {
  214. morkTLebaiJC.MakeJuiceEnd = true;
  215. bFirstTrig_Juice = false;
  216. }
  217. }
  218. }
  219. if (!morkTLebaiJC.MakeJuiceEnd)
  220. {
  221. //若无状态返回 则加延迟
  222. if (!bFirstTrig_Juice)
  223. {
  224. bFirstTrig_Juice = true;
  225. delayTimeOut_Juice = DateTime.Now;
  226. }
  227. else if (DateTime.Now.Subtract(delayTimeOut_Juice).TotalSeconds > 15 && bFirstTrig_Juice == true)
  228. {
  229. DeviceProcessLogShow("接果汁超时,接果汁结束,等待取果汁");
  230. bFirstTrig_Juice = false;
  231. morkTLebaiJC.MakeJuiceEnd = true;
  232. }
  233. }
  234. }
  235. if (morkTLebaiJC.IsHaveTeaWaterCup && morkTLebaiJC.MakeTeaEnd != true)
  236. {
  237. if (!bFirstTrig_TeaWater)
  238. {
  239. bFirstTrig_TeaWater = true;
  240. delayTimeOut_Water = DateTime.Now;//开启接水信号后,记录当前时间
  241. }
  242. else if (DateTime.Now.Subtract(delayTimeOut_Water).TotalSeconds >= 50 && bFirstTrig_TeaWater == true)//接水超过50s后,启动接水完成标志,开启接水程序
  243. {
  244. DeviceProcessLogShow("接茶或水延迟时间结束");
  245. bFirstTrig_TeaWater = false;
  246. morkTLebaiJC.MakeTeaEnd = true;
  247. }
  248. }
  249. DoCoffee();
  250. DoJuice();
  251. DoBoiledTea();
  252. DoBoiledWater();
  253. }
  254. /// <summary>
  255. /// 订单状态改变
  256. /// </summary>
  257. /// <param name="subid"></param>
  258. /// <param name="oRDER_STATUS"></param>
  259. private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
  260. {
  261. EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent() { Status = oRDER_STATUS, SubOrderId = subid, deviceClientType = DeviceType });
  262. }
  263. /// <summary>
  264. /// 验证商品是做的某种饮料
  265. /// </summary>
  266. /// <param name="batchingLoc">物料位置</param>
  267. private GOODS_TYPE ValidateGoodsByBatching(string batchingLoc)
  268. {
  269. if (batchings.ContainsKey(batchingLoc))
  270. return batchings[batchingLoc].GoodsType;
  271. return GOODS_TYPE.NEITHER;
  272. }
  273. /// <summary>
  274. /// 乐白的场景结束等待
  275. /// </summary>
  276. /// <param name="value"></param>
  277. private void Wait(int value = 101)
  278. {
  279. while (!((bool)peripheralStatus["RobotOK"] && (int)peripheralStatus["RobotValue"] == value))
  280. {
  281. Thread.Sleep(5);
  282. }
  283. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  284. }
  285. /// <summary>
  286. /// 乐白的场景
  287. /// </summary>
  288. /// <param name="sen"></param>
  289. private void Sence(int sen)
  290. {
  291. new LebaiRobot_LebaiSenceEvent { DeviceId = DeviceId, LebaiSence = sen }.Publish();
  292. }
  293. /// <summary>
  294. /// 乐白的数字量输出
  295. /// </summary>
  296. /// <param name="value"></param>
  297. /// <param name="pin"></param>
  298. private void Output(bool value,int pin)
  299. {
  300. new LebaiRobot_SetOutPutEvent { DeviceId = DeviceId, Pin = pin,Value=value }.Publish();
  301. }
  302. private T GetStatus<T>(string key)
  303. {
  304. if (peripheralStatus.ContainsKey(key))
  305. {
  306. if (peripheralStatus[key] != null)
  307. {
  308. return (T)(peripheralStatus[key]);
  309. }
  310. }
  311. return default;
  312. }
  313. int[] devStatusBy = new int[2] { 0, 0 };
  314. bool outCupCheck = false;//放纸杯位置有无判断
  315. /// <summary>
  316. /// 传感器的输入信号 0:无意义 1:有信号 2:无信号 3:信号不正确
  317. /// </summary>
  318. int bSensorInput;
  319. /// <summary>
  320. /// 判断是否有咖啡订单
  321. /// </summary>
  322. /// <returns></returns>
  323. private bool IsMakeCoffee()
  324. {
  325. bool bMake = (IsHealth && morkTLebaiJC.morkOrderPushesCoffee.Count > 0 && !morkTLebaiJC.IsHaveCoffeeCup) ? true : false;
  326. return bMake;
  327. }
  328. /// <summary>
  329. /// 判断是否有果汁订单
  330. /// </summary>
  331. /// <returns></returns>
  332. private bool IsMakeJuice()
  333. {
  334. bool bMake = (IsHealth && morkTLebaiJC.morkOrderPushesJuicer.Count > 0 && !morkTLebaiJC.IsHaveJuiceCup) ? true : false;
  335. return bMake;
  336. }
  337. /// <summary>
  338. /// 判断是否有茶订单
  339. /// </summary>
  340. /// <returns></returns>
  341. private bool IsMakeTeaWater()
  342. {
  343. bool bMake = (IsHealth && morkTLebaiJC.morkOrderPushesTea.Count > 0 && !morkTLebaiJC.IsHaveTeaWaterCup) ? true : false;
  344. return bMake;
  345. }
  346. /// <summary>
  347. /// 判断是否有开水订单
  348. /// </summary>
  349. /// <returns></returns>
  350. private bool IsMakeWater()
  351. {
  352. bool bMake = (IsHealth && morkTLebaiJC.morkOrderPushesWater.Count > 0 && !morkTLebaiJC.IsHaveTeaWaterCup) ? true : false;
  353. return bMake;
  354. }
  355. /// <summary>
  356. /// 做咖啡流程
  357. /// </summary>
  358. private void DoCoffee()
  359. {
  360. if (IsMakeCoffee())
  361. {
  362. PickUpCoffee();//接咖啡
  363. morkTLebaiJC.IsHaveCoffeeCup = true;
  364. }
  365. else if(morkTLebaiJC.MakeCoffeeEnd)
  366. {
  367. try
  368. {
  369. PutCoffeeCup();
  370. morkTLebaiJC.MakeCoffeeEnd = false;
  371. morkTLebaiJC.IsHaveCoffeeCup = false;
  372. morkTLebaiJC.morkOrderPushesCoffee.TryDequeue(out OrderLocInfo orderLoc);
  373. }
  374. catch (Exception ex)
  375. {
  376. MessageLog.GetInstance.ShowEx(ex.ToString());
  377. }
  378. }
  379. }
  380. private void DoJuice()
  381. {
  382. if (IsMakeJuice())
  383. {
  384. switch (morkTLebaiJC.morkOrderPushesJuicer.ElementAt(0).makeID)
  385. {
  386. case "1":
  387. GuMake = true;
  388. IsAddIce = false;
  389. break;
  390. case "2":
  391. GuMake = false;
  392. IsAddIce = false;
  393. break;
  394. case "3":
  395. GuMake = false;
  396. IsAddIce = true;
  397. break;
  398. default:
  399. break;
  400. }
  401. PickUpJuicer();
  402. morkTLebaiJC.IsHaveJuiceCup = true;
  403. }
  404. else if (morkTLebaiJC.MakeJuiceEnd)
  405. {
  406. try
  407. {
  408. Thread.Sleep(5000);//延迟五秒,防止饮料口滴饮料
  409. putJuice();
  410. morkTLebaiJC.IsHaveJuiceCup = false;
  411. morkTLebaiJC.MakeJuiceEnd = false;
  412. morkTLebaiJC.morkOrderPushesJuicer.TryDequeue(out OrderLocInfo orderLoc);
  413. }
  414. catch (Exception ex)
  415. {
  416. MessageLog.GetInstance.ShowEx(ex.ToString());
  417. }
  418. }
  419. }
  420. public bool DoTeaOrWater;
  421. private void DoBoiledTea()
  422. {
  423. if (IsMakeTeaWater())
  424. {
  425. PickUpTea();
  426. morkTLebaiJC.IsHaveTeaWaterCup = true;
  427. DoTeaOrWater = true;
  428. }
  429. else if (morkTLebaiJC.MakeTeaEnd && morkTLebaiJC.morkOrderPushesTea.Count > 0 && DoTeaOrWater)
  430. {
  431. try
  432. {
  433. PutWaterCup();
  434. OrderChange(morkTLebaiJC.morkOrderPushesTea.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  435. DeviceProcessLogShow("-------------茶 制作完成------------");
  436. morkTLebaiJC.IsHaveTeaWaterCup = false;
  437. morkTLebaiJC.MakeTeaEnd = false;
  438. morkTLebaiJC.morkOrderPushesTea.TryDequeue(out OrderLocInfo orderLoc);
  439. }
  440. catch (Exception ex)
  441. {
  442. MessageLog.GetInstance.ShowEx(ex.ToString());
  443. }
  444. }
  445. }
  446. private void DoBoiledWater()
  447. {
  448. if (IsMakeWater())
  449. {
  450. PickUpWater();
  451. morkTLebaiJC.IsHaveTeaWaterCup = true;
  452. DoTeaOrWater = false;
  453. }
  454. else if (morkTLebaiJC.MakeTeaEnd && morkTLebaiJC.morkOrderPushesWater.Count > 0 && DoTeaOrWater == false)
  455. {
  456. try
  457. {
  458. PutWaterCup();
  459. OrderChange(morkTLebaiJC.morkOrderPushesWater.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  460. DeviceProcessLogShow("-------------开水 制作完成------------");
  461. morkTLebaiJC.IsHaveTeaWaterCup = false;
  462. morkTLebaiJC.MakeTeaEnd = false;
  463. morkTLebaiJC.morkOrderPushesWater.TryDequeue(out OrderLocInfo orderLoc);
  464. }
  465. catch (Exception ex)
  466. {
  467. MessageLog.GetInstance.ShowEx(ex.ToString());
  468. }
  469. }
  470. }
  471. #region 做咖啡流程
  472. /// <summary>
  473. /// 接咖啡
  474. /// </summary>
  475. private void PickUpCoffee()
  476. {
  477. if (!morkTLebaiJC.IsHaveCoffeeCup)
  478. {
  479. outCupCheck = false;
  480. OrderChange(morkTLebaiJC.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  481. int resultTakeCup = takeCup();
  482. if (resultTakeCup == 1)
  483. {
  484. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  485. Sence(GVL_MorkT.机器人安全位);
  486. //Wait();
  487. WritePLCData("",2);//将轴移动到接咖啡的位置
  488. DeviceProcessLogShow("伺服电机移动到接咖啡的位置");
  489. DeviceProcessLogShow("咖啡杯取杯完成");
  490. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  491. Sence(GVL_MorkT.接咖啡);
  492. //Wait();
  493. new DRCoffee_MakeCoffeeEvent() { DrinkCode = (DrCoffeeDrinksCode)(morkTLebaiJC.morkOrderPushesCoffee.ElementAt(0).Loc)}.Publish(); //接咖啡控制 //DrCoffeeDrinksCode.热水
  494. }
  495. else
  496. {
  497. DeviceProcessLogShow("取杯失败 回到初始位,请及时处理!!");
  498. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  499. Sence(GVL_MorkT.机器人初始位);
  500. //Wait();
  501. }
  502. }
  503. }
  504. /// <summary>
  505. /// 咖啡杯接好,放咖啡杯
  506. /// </summary>
  507. private void PutCoffeeCup()
  508. {
  509. while (GetStatus<bool>("RobotValue1"))//判断放杯位置是否有物品
  510. {
  511. if (!outCupCheck)
  512. DeviceProcessLogShow("成品处有纸杯存在,请取走!!");
  513. outCupCheck = true;
  514. }
  515. outCupCheck = false;
  516. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  517. Sence(GVL_MorkT.机器人安全位);
  518. //Wait();
  519. WritePLCData("", 2);//将轴移动到接咖啡的位置
  520. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  521. Sence(GVL_MorkT.放咖啡杯);
  522. //Wait();
  523. OrderChange(morkTLebaiJC.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  524. DeviceProcessLogShow($"-------------{Regex.Replace(morkTLebaiJC.morkOrderPushesCoffee.ElementAt(0).GoodName, @"[\r\n]", "")}制作完成------------");
  525. }
  526. #endregion
  527. #region 做开水流程
  528. /// <summary>
  529. /// 接开水
  530. /// </summary>
  531. private void PickUpWater()
  532. {
  533. #region 接水流程
  534. if (!morkTLebaiJC.IsHaveTeaWaterCup)
  535. {
  536. OrderChange(morkTLebaiJC.morkOrderPushesWater.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  537. int resultTakeCup = takeCup();
  538. if (resultTakeCup == 1)
  539. {
  540. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  541. Sence(GVL_MorkT.接开水);
  542. //Wait();
  543. Output(false, 1);
  544. Output(false, 0);
  545. Thread.Sleep(100);
  546. Output(true, 0);
  547. Thread.Sleep(3000);
  548. Output(false, 0);
  549. Thread.Sleep(100);
  550. Output(false, 1);
  551. Thread.Sleep(100);
  552. Output(true, 1);
  553. Thread.Sleep(500);
  554. Output(false, 1);
  555. }
  556. else
  557. {
  558. return;
  559. }
  560. }
  561. #endregion
  562. }
  563. #endregion
  564. #region 做茶流程
  565. /// <summary>
  566. /// 做茶
  567. /// </summary>
  568. private void PickUpTea()
  569. {
  570. #region 接茶流程
  571. if (!morkTLebaiJC.IsHaveTeaWaterCup)
  572. {
  573. OrderChange(morkTLebaiJC.morkOrderPushesTea.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  574. int resultTakeCup = takeCup();
  575. if (resultTakeCup == 1)
  576. {
  577. DeviceProcessLogShow("取茶杯完成");
  578. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  579. Sence(GVL_MorkT.接茶叶);
  580. //Wait();
  581. new WriteMcu() { TagName = "ServoControl", Address = "1", Value = 90 }.Publish();
  582. Thread.Sleep(1000);
  583. new WriteMcu() { TagName = "ServoControl", Address = "1", Value = 150 }.Publish();
  584. Thread.Sleep(1000);
  585. new WriteMcu() { TagName = "ServoControl", Address = "1", Value = 90 }.Publish();
  586. Thread.Sleep(3000);
  587. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  588. Sence(GVL_MorkT.接茶水);
  589. //Wait();
  590. Output(false, 1);
  591. Output(false, 0);
  592. Thread.Sleep(100);
  593. Output(true, 0);
  594. Thread.Sleep(3000);
  595. Output(false, 0);
  596. Thread.Sleep(100);
  597. Output(false, 1);
  598. Thread.Sleep(100);
  599. Output(true, 1);
  600. Thread.Sleep(500);
  601. Output(false, 1);
  602. morkTLebaiJC.IsHaveTeaWaterCup = true;
  603. }
  604. else
  605. {
  606. return;
  607. }
  608. }
  609. #endregion
  610. }
  611. /// <summary>
  612. /// 放水杯流程
  613. /// </summary>
  614. private void PutWaterCup()
  615. {
  616. while (GetStatus<bool>("RobotValue1"))//判断放杯位置是否有物品
  617. {
  618. if (!outCupCheck)
  619. DeviceProcessLogShow("成品处有纸杯存在,请取走!!");
  620. outCupCheck = true;
  621. }
  622. outCupCheck = false;
  623. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  624. Sence(GVL_MorkT.放水杯);
  625. }
  626. #endregion
  627. #region 做果汁流程
  628. /// <summary>
  629. /// 果汁机控制信号
  630. /// </summary>
  631. private byte JuicerNum;
  632. private void getIce()
  633. {
  634. IsAddIceNow = IsAddIce;
  635. if (IsAddIce)
  636. {
  637. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  638. Sence(GVL_MorkT.取杯位_机器人安全位);
  639. //Wait();
  640. WritePLCData("",3);//将模组移动到接冰的位置
  641. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  642. Sence(GVL_MorkT.接冰块);
  643. //Wait();
  644. if (Math.Pow(1, GetStatus<byte>("GetMakeIceDeviceStatus")) == 0)//制冰机在待机状态才可以制作冰
  645. {
  646. new StartMakeIce() { }.Publish();
  647. }
  648. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  649. Sence(GVL_MorkT.接冰_机器人安全位);
  650. //Wait();
  651. }
  652. }
  653. /// <summary>
  654. /// 当前订单是否加冰
  655. /// </summary>
  656. private bool IsAddIceNow = false;
  657. private int JuiceCH;
  658. /// <summary>
  659. /// 接果汁
  660. /// </summary>
  661. private void PickUpJuicer()
  662. {
  663. #region 接果汁流程
  664. if (!morkTLebaiJC.IsHaveJuiceCup)
  665. {
  666. OrderChange(morkTLebaiJC.morkOrderPushesJuicer.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  667. int resultTakeCup = takeCup();
  668. JuiceCH = morkTLebaiJC.morkOrderPushesJuicer.ElementAt(0).Loc;
  669. if (resultTakeCup == 1)
  670. {
  671. getIce();
  672. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  673. Sence(GVL_MorkT.机器人安全位);
  674. //Wait();
  675. WritePLCData("", 3);//将模组移动到接冰的位置
  676. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  677. Sence(GVL_MorkT.接果汁过渡位);
  678. //Wait();
  679. switch (JuiceCH)
  680. {
  681. case 52:
  682. if (GuMake)
  683. JuicerNum = 0x00;
  684. else
  685. JuicerNum = 0x01;
  686. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  687. Sence(GVL_MorkT.接果汁1);
  688. //Wait();
  689. break;
  690. case 53:
  691. if (GuMake)
  692. JuicerNum = 0x02;
  693. else
  694. JuicerNum = 0x03;
  695. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  696. Sence(GVL_MorkT.接果汁2);
  697. //Wait();
  698. break;
  699. case 54:
  700. if (GuMake)
  701. JuicerNum = 0x04;
  702. else
  703. JuicerNum = 0x05;
  704. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  705. Sence(GVL_MorkT.接果汁3);
  706. //Wait();
  707. break;
  708. case 55:
  709. if (GuMake)
  710. JuicerNum = 0x06;
  711. else
  712. JuicerNum = 0x07;
  713. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  714. Sence(GVL_MorkT.接果汁4);
  715. //Wait();
  716. break;
  717. default:
  718. JuicerNum = 0x00;
  719. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  720. Sence(GVL_MorkT.接果汁1);
  721. //Wait();
  722. break;
  723. }
  724. new WriteJuicer() { Value = JuicerNum }.Publish();
  725. morkTLebaiJC.IsHaveJuiceCup = true;
  726. }
  727. else
  728. {
  729. return;
  730. }
  731. }
  732. #endregion
  733. }
  734. /// <summary>
  735. /// 取接好果汁杯
  736. /// </summary>
  737. private void putJuice()
  738. {
  739. while (GetStatus<bool>("RobotValue1"))//判断放杯位置是否有物品
  740. {
  741. if (!outCupCheck)
  742. DeviceProcessLogShow("成品处有纸杯存在,请取走!!");
  743. outCupCheck = true;
  744. }
  745. outCupCheck = false;
  746. switch (JuiceCH)
  747. {
  748. case 52:
  749. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  750. Sence(GVL_MorkT.放果汁杯1);
  751. //Wait();
  752. break;
  753. case 53:
  754. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  755. Sence(GVL_MorkT.放果汁杯2);
  756. //Wait();
  757. break;
  758. case 54:
  759. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  760. Sence(GVL_MorkT.放果汁杯3);
  761. //Wait();
  762. break;
  763. case 55:
  764. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  765. Sence(GVL_MorkT.放果汁杯4);
  766. //Wait();
  767. break;
  768. default:
  769. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  770. Sence(GVL_MorkT.放果汁杯1);
  771. //Wait();
  772. break;
  773. }
  774. OrderChange(morkTLebaiJC.morkOrderPushesJuicer.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  775. DeviceProcessLogShow($"-------------{morkTLebaiJC.morkOrderPushesJuicer.ElementAt(0).GoodName}果汁 制作完成------------");
  776. }
  777. #endregion
  778. /// <summary>
  779. /// 取杯的次数
  780. /// </summary>
  781. private int nCnt;
  782. /// <summary>
  783. /// 取杯流程
  784. /// </summary>
  785. /// <returns>0:无意义,1:取杯成功 2:取杯失败</returns>
  786. private int takeCup()
  787. {
  788. try
  789. {
  790. nCnt = 0;
  791. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  792. Sence(GVL_MorkT.机器人初始位);
  793. //Wait();
  794. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  795. Sence(GVL_MorkT.机器人安全位);
  796. //Wait();
  797. WritePLCData("", 1);
  798. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  799. Sence(GVL_MorkT.取纸杯);
  800. //Wait();
  801. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  802. Sence(GVL_MorkT.取纸杯检测);
  803. //Wait();
  804. nCnt++;
  805. Thread.Sleep(2000);
  806. while (!GetStatus<bool>("GetInput")) //读取传感器的值
  807. {
  808. if (nCnt > 3)
  809. {
  810. nCnt = 0;
  811. DeviceProcessLogShow("三次取杯失败,回原点");
  812. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  813. Sence(GVL_MorkT.机器人安全位);
  814. //Wait();
  815. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  816. Sence(GVL_MorkT.机器人初始位);
  817. //Wait();
  818. return 2;
  819. }
  820. else
  821. {
  822. nCnt++;
  823. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  824. Sence(GVL_MorkT.二次取杯);
  825. //Wait();
  826. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  827. Sence(GVL_MorkT.取纸杯检测);
  828. //Wait();
  829. }
  830. Thread.Sleep(100);
  831. }
  832. new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
  833. Sence(GVL_MorkT.取杯位_机器人安全位);
  834. //Wait();
  835. return 1;
  836. }
  837. catch (Exception ex)
  838. {
  839. DeviceProcessLogShow(ex.ToString());
  840. }
  841. return 2;
  842. }
  843. /// <summary>
  844. /// 放杯
  845. /// </summary>
  846. /// <returns>0:无意义 1:放杯成功 2:执行失败(传感器还有信号) 3:放杯异常</returns>
  847. private int putCup()
  848. {
  849. try
  850. {
  851. if (GetStatus<bool>("GetInput2")) return 2;
  852. Sence(GVL_MorkT.JUICE_放杯);
  853. //Wait();
  854. new LebaiRobot_SetValueEvent() { RobotSetValue = 1 }.Publish();
  855. Sence(GVL_MorkT.JUICE_放杯检测);
  856. //Wait();
  857. new LebaiRobot_SetValueEvent() { RobotSetValue = 1 }.Publish();
  858. if (GetStatus<bool>("GetInput2"))
  859. {
  860. return 1;
  861. }
  862. else
  863. {
  864. return 3;
  865. }
  866. }
  867. catch (Exception ex)
  868. {
  869. DeviceProcessLogShow(ex.ToString());
  870. return 0;
  871. }
  872. }
  873. public void SimOrder<T>(T simOrder)
  874. {
  875. }
  876. #region PLC 控制函数
  877. private void WritePLCData(string address, object value)
  878. {
  879. EventBus.EventBus.GetInstance().Publish(new WriteModel() { DeviceId = DeviceId, Address = address, Value = value });
  880. }
  881. private void GetStatus(string key, Action<object> action)
  882. {
  883. if (peripheralStatus.ContainsKey(key))
  884. {
  885. if (peripheralStatus[key] != null)
  886. {
  887. action?.Invoke(peripheralStatus[key]);
  888. }
  889. }
  890. }
  891. public override void ReadData()
  892. {
  893. #region 乐白机器人状态
  894. GetStatus("RobotIsConnected", new Action<object>((o) =>
  895. {
  896. if (o is bool b)
  897. {
  898. morkTLebaiJC.RobotIsConnected = b;
  899. }
  900. }));
  901. GetStatus("RobotMode", new Action<object>((o) =>
  902. {
  903. if (o is ELebaiRModel eLebaiRModel)
  904. {
  905. morkTLebaiJC.RobotMode = eLebaiRModel;
  906. }
  907. }));
  908. GetStatus("LeibaiGetTcpInput", new Action<object>((o) =>
  909. {
  910. if (o is bool b)
  911. {
  912. morkTLebaiJC.RobotGetTcpInput = b;
  913. }
  914. }));
  915. GetStatus("LeibaiGetInput1", new Action<object>((o) =>
  916. {
  917. if (o is bool b)
  918. {
  919. morkTLebaiJC.RobotGetInput0 = b;
  920. }
  921. }));
  922. GetStatus("LeibaiGetInput2", new Action<object>((o) =>
  923. {
  924. if (o is bool b)
  925. {
  926. morkTLebaiJC.RobotGetInput1 = b;
  927. }
  928. }));
  929. GetStatus("LeibaiGetInput3", new Action<object>((o) =>
  930. {
  931. if (o is bool b)
  932. {
  933. morkTLebaiJC.RobotGetInput2 = b;
  934. }
  935. }));
  936. GetStatus("LeibaiGetInput4", new Action<object>((o) =>
  937. {
  938. if (o is bool b)
  939. {
  940. morkTLebaiJC.RobotGetInput3 = b;
  941. }
  942. }));
  943. #endregion
  944. #region 制冰机状态
  945. GetStatus("MakeIceConnected", new Action<object>((o) =>
  946. {
  947. if (o is bool bt)
  948. {
  949. morkTLebaiJC.IceMakerConnect = bt;
  950. }
  951. }));
  952. GetStatus("MakeIceDeviceStatus", new Action<object>((o) =>
  953. {
  954. if (o is byte bt)
  955. {
  956. morkTLebaiJC.IceMakerState = bt;
  957. }
  958. }));
  959. #endregion
  960. #region 咖啡机状态
  961. GetStatus("CoffeeIsConnected", new Action<object>((o) =>
  962. {
  963. if (o is bool b)
  964. {
  965. morkTLebaiJC.CoffeeIsConnected = b;
  966. }
  967. }));
  968. GetStatus("CoffeeStatus", new Action<object>((o) =>
  969. {
  970. if (o is DrCoffeeStatus coffeeStatus)
  971. {
  972. morkTLebaiJC.DrCoffeeStatus = coffeeStatus;
  973. }
  974. }));
  975. GetStatus("CoffeeAppStatus", new Action<object>((o) =>
  976. {
  977. if (o is DrCoffeeAppStatus appStatus)
  978. {
  979. morkTLebaiJC.CoffeeAppStatus = appStatus;
  980. }
  981. }));
  982. GetStatus("CoffeeWarning", new Action<object>((o) =>
  983. {
  984. if (o is DrCoffeeWarning coffeeWarning)
  985. {
  986. morkTLebaiJC.CoffeeWarning = coffeeWarning;
  987. }
  988. }));
  989. GetStatus("CoffeeFault", new Action<object>((o) =>
  990. {
  991. if (o is DrCoffeeFault coffeeFault)
  992. {
  993. morkTLebaiJC.CaffeeFault = coffeeFault;
  994. }
  995. }));
  996. #endregion
  997. #region 单片机状态
  998. EventBus.EventBus.GetInstance().Publish(new ReadMcu() { DeviceId = DeviceId, ReadPar = 0, TagName = "SCChipGetInput0" });
  999. EventBus.EventBus.GetInstance().Publish(new ReadMcu() { DeviceId = DeviceId, ReadPar = 0, TagName = "SCChipGetInput1" });
  1000. EventBus.EventBus.GetInstance().Publish(new ReadMcu() { DeviceId = DeviceId, ReadPar = 0, TagName = "SCChipGetInput2" });
  1001. EventBus.EventBus.GetInstance().Publish(new ReadMcu() { DeviceId = DeviceId, ReadPar = 0, TagName = "SCChipGetInput3" });
  1002. EventBus.EventBus.GetInstance().Publish(new ReadMcu() { DeviceId = DeviceId, ReadPar = 0, TagName = "SCChipGetInput4" });
  1003. EventBus.EventBus.GetInstance().Publish(new ReadMcu() { DeviceId = DeviceId, ReadPar = 0, TagName = "SCChipGetInput5" });
  1004. EventBus.EventBus.GetInstance().Publish(new ReadMcu() { DeviceId = DeviceId, ReadPar = 0, TagName = "SCChipGetInput6" });
  1005. EventBus.EventBus.GetInstance().Publish(new ReadMcu() { DeviceId = DeviceId, ReadPar = 0, TagName = "SCChipGetInput7" });
  1006. GetStatus("SCChipIsConnect", new Action<object>((o) =>
  1007. {
  1008. if (o is bool b)
  1009. {
  1010. morkTLebaiJC.SCChipIsConnect = b;
  1011. }
  1012. }));
  1013. GetStatus("SCChipGetInput0", new Action<object>((o) =>
  1014. {
  1015. if (o is bool b)
  1016. {
  1017. morkTLebaiJC.SCChipInput0 = b;
  1018. }
  1019. }));
  1020. GetStatus("SCChipGetInput1", new Action<object>((o) =>
  1021. {
  1022. if (o is bool b)
  1023. {
  1024. morkTLebaiJC.SCChipInput1 = b;
  1025. }
  1026. }));
  1027. GetStatus("SCChipGetInput2", new Action<object>((o) =>
  1028. {
  1029. if (o is bool b)
  1030. {
  1031. morkTLebaiJC.SCChipInput2 = b;
  1032. }
  1033. }));
  1034. GetStatus("SCChipGetInput3", new Action<object>((o) =>
  1035. {
  1036. if (o is bool b)
  1037. {
  1038. morkTLebaiJC.SCChipInput3 = b;
  1039. }
  1040. }));
  1041. GetStatus("SCChipGetInput4", new Action<object>((o) =>
  1042. {
  1043. if (o is bool b)
  1044. {
  1045. morkTLebaiJC.SCChipInput4 = b;
  1046. }
  1047. }));
  1048. GetStatus("SCChipGetInput5", new Action<object>((o) =>
  1049. {
  1050. if (o is bool b)
  1051. {
  1052. morkTLebaiJC.SCChipInput5 = b;
  1053. }
  1054. }));
  1055. GetStatus("SCChipGetInput6", new Action<object>((o) =>
  1056. {
  1057. if (o is bool b)
  1058. {
  1059. morkTLebaiJC.SCChipInput6 = b;
  1060. }
  1061. }));
  1062. GetStatus("SCChipGetInput7", new Action<object>((o) =>
  1063. {
  1064. if (o is bool b)
  1065. {
  1066. morkTLebaiJC.SCChipInput7 = b;
  1067. }
  1068. }));
  1069. #endregion
  1070. #region 果汁机
  1071. GetStatus("GetJuicerConnected", new Action<object>((o) =>
  1072. {
  1073. if (o is bool b)
  1074. {
  1075. morkTLebaiJC.JuicerConnected = b;
  1076. }
  1077. }));
  1078. GetStatus("GetJuicerDeviceStatus", new Action<object>(async (o) =>
  1079. {
  1080. if (o is int[] b)
  1081. {
  1082. morkTLebaiJC.JuicerState = b;
  1083. }
  1084. if (morkTLebaiJC.JuicerState != null && morkTLebaiJC.JuicerState.Length>0)
  1085. {
  1086. if ((morkTLebaiJC.JuicerState[0] >> 7 & 1) == 1)
  1087. {
  1088. morkTLebaiJC.JuiceState_Using = true;
  1089. }
  1090. if ((morkTLebaiJC.JuicerState[0] >> 6 & 1) == 1)
  1091. {
  1092. morkTLebaiJC.JuiceState_LackOfWater = true;
  1093. }
  1094. if ((morkTLebaiJC.JuicerState[0] >> 5 & 1) == 1)
  1095. {
  1096. morkTLebaiJC.JuiceState_Heating = true;
  1097. }
  1098. }
  1099. }));
  1100. #endregion
  1101. }
  1102. #endregion
  1103. public override void Stop()
  1104. {
  1105. }
  1106. public override void ResetProgram()
  1107. {
  1108. }
  1109. public override void SimOrder()
  1110. {
  1111. EventBus.EventBus.GetInstance().Subscribe<MorkTSimOrder>(0, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  1112. {
  1113. string guid = Guid.NewGuid().ToString();
  1114. if (@event != null && @event is MorkTSimOrder msm)
  1115. {
  1116. DeviceProcessLogShow("----开始模拟订单----");
  1117. if (msm.OrderNum == 1)
  1118. {
  1119. morkTLebaiJC.morkOrderPushesCoffee.Enqueue(new OrderLocInfo() { Loc =(ushort)msm.DrinkCode, SuborderId = guid, GoodName="模拟咖啡订单" });
  1120. }
  1121. else if (msm.OrderNum == 2)
  1122. {
  1123. morkTLebaiJC.morkOrderPushesJuicer.Enqueue(new OrderLocInfo() { Loc = (ushort) (msm.JuiceList + 51), makeID = msm.JuiceMakeID, SuborderId = guid, GoodName = "模拟果汁订单" });
  1124. }
  1125. else if (msm.OrderNum == 3)
  1126. {
  1127. morkTLebaiJC.morkOrderPushesTea.Enqueue(new OrderLocInfo() { SuborderId = guid, GoodName = "模拟茶水订单" });
  1128. }
  1129. else if (msm.OrderNum == 4)
  1130. {
  1131. morkTLebaiJC.morkOrderPushesWater.Enqueue(new OrderLocInfo() { SuborderId = guid, GoodName = "模拟开水订单" });
  1132. }
  1133. }
  1134. });
  1135. }
  1136. }
  1137. }