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

Control_MORKJC.cs 41 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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.Model.PLC;
  18. using BPASmartClient.Model.单片机;
  19. using BPASmartClient.Message;
  20. namespace BPASmartClient.MorktJAKAJC
  21. {
  22. public class Control_MORKJC : BaseDevice
  23. {
  24. GVL_MORKJC morkTJakaJC = new GVL_MORKJC();
  25. //物料存放位置
  26. private Dictionary<string, PolymerBatching> batchings = new Dictionary<string, PolymerBatching>();
  27. //容器位置
  28. private string holderLoc;
  29. //主料位置
  30. private string mainMaterialLoc;
  31. public override global::BPA.Message.Enum.DeviceClientType DeviceType { get { return BPA.Message.Enum.DeviceClientType.MORKT; } }
  32. /// <summary>
  33. /// 果汁机做法,true:热饮,false:冷饮
  34. /// </summary>
  35. private bool GuMake = false;
  36. private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
  37. {
  38. EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent() { Status = oRDER_STATUS, SubOrderId = subid, deviceClientType = DeviceType });
  39. }
  40. public override void DoMain()
  41. {
  42. if (Json<KeepDataBase>.Data.IsVerify)
  43. {
  44. IsHealth = true;
  45. }
  46. IsHealth = true;
  47. serverInit();
  48. DataParse();
  49. }
  50. private void serverInit()
  51. {
  52. EventBus.EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  53. {
  54. if (@event == null) return;
  55. if (@event is MaterialDeliveryEvent material)
  56. {
  57. orderMaterialDelivery = material.orderMaterialDelivery;
  58. }
  59. });
  60. }
  61. private void DataParse()
  62. {
  63. EventBus.EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  64. {
  65. if (@event == null) return;
  66. if (@event is DoOrderEvent order)
  67. {
  68. if (order.MorkOrder.GoodBatchings == null) return;
  69. OrderCount++;
  70. DeviceProcessLogShow($"接收到{OrderCount}次订单");
  71. batchings = PolymerBatching.BuildAll();
  72. //商品类型
  73. GOODS_TYPE currentGoodsType = GOODS_TYPE.NEITHER;
  74. foreach (var item in order.MorkOrder.GoodBatchings)
  75. {
  76. var res = orderMaterialDelivery?.BatchingInfo?.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  77. if (res != null)
  78. {
  79. //验证商品是做的某种饮料
  80. if (ValidateGoodsByBatching(res.BatchingLoc) != GOODS_TYPE.NEITHER)
  81. {
  82. //获取当前物料所属商品类型
  83. currentGoodsType = ValidateGoodsByBatching(res.BatchingLoc);
  84. }
  85. //
  86. switch (batchings[res.BatchingLoc].BatchingClass)
  87. {
  88. case BATCHING_CLASS.HOLDER:
  89. holderLoc = res.BatchingLoc;
  90. break;
  91. case BATCHING_CLASS.MAIN_MATERIAL:
  92. mainMaterialLoc = res.BatchingLoc;
  93. break;
  94. }
  95. //根据商品类型执行具体制作流程
  96. switch (currentGoodsType)
  97. {
  98. case GOODS_TYPE.COFFEE:
  99. if (morkTJakaJC.morkOrderPushesCoffee.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  100. {
  101. morkTJakaJC.morkOrderPushesCoffee.Enqueue(new OrderLocInfo()
  102. {
  103. SuborderId = order.MorkOrder.SuborderId,
  104. BatchingId = res.BatchingId,
  105. Loc = ushort.Parse(mainMaterialLoc),
  106. GoodName = order.MorkOrder.GoodsName,
  107. });
  108. }
  109. break;
  110. case GOODS_TYPE.JUICE:
  111. GuMake = order.MorkOrder.MakeID == "2";//判断果汁的冷热
  112. if (morkTJakaJC.morkOrderPushesJuicer.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  113. {
  114. morkTJakaJC.morkOrderPushesJuicer.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.TEA:
  124. if (morkTJakaJC.morkOrderPushesTea.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  125. {
  126. morkTJakaJC.morkOrderPushesTea.Enqueue(new OrderLocInfo()
  127. {
  128. SuborderId = order.MorkOrder.SuborderId,
  129. BatchingId = res.BatchingId,
  130. Loc = ushort.Parse(mainMaterialLoc),
  131. GoodName = order.MorkOrder.GoodsName,
  132. });
  133. }
  134. break;
  135. case GOODS_TYPE.WATER:
  136. if (morkTJakaJC.morkOrderPushesWater.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  137. {
  138. morkTJakaJC.morkOrderPushesWater.Enqueue(new OrderLocInfo()
  139. {
  140. SuborderId = order.MorkOrder.SuborderId,
  141. BatchingId = res.BatchingId,
  142. Loc = ushort.Parse(mainMaterialLoc),
  143. GoodName = order.MorkOrder.GoodsName,
  144. });
  145. }
  146. break;
  147. case GOODS_TYPE.NEITHER:
  148. DeviceProcessLogShow("未知的商品类型");
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. });
  155. }
  156. private bool bFirstTrig_TeaWater = false;
  157. private bool bFirstTrig_Coffee = false;
  158. DateTime delayTimeOut_Coffee;
  159. private bool bFirstTrig_Juice = false;
  160. DateTime delayTimeOut_Juice;
  161. /// <summary>
  162. /// 判断接咖啡的位置是否有杯子
  163. /// </summary>
  164. bool pickUpCoffeeHaveCup = false;
  165. /// <summary>
  166. /// 判断接果汁的位置是否有杯子
  167. /// </summary>
  168. bool pickUpJuiceHaveCup = false;
  169. /// <summary>
  170. /// 判断接开水的位置是否有杯子
  171. /// </summary>
  172. bool pickUpHotWaterHaveCup = false;
  173. public override void MainTask()
  174. {
  175. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CoffeEndCookEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  176. {
  177. if (morkTJakaJC.IsHaveCoffeeCup)
  178. morkTJakaJC.MakeCoffeeEnd = true;
  179. });
  180. if (pickUpCoffeeHaveCup)
  181. {
  182. if (!bFirstTrig_Coffee)
  183. {
  184. bFirstTrig_Coffee = true;
  185. delayTimeOut_Coffee = DateTime.Now;
  186. }
  187. else if (DateTime.Now.Subtract(delayTimeOut_Coffee).TotalSeconds > 180 && bFirstTrig_Coffee == true)
  188. {
  189. bFirstTrig_Coffee = false;
  190. if (morkTJakaJC.IsHaveCoffeeCup)
  191. morkTJakaJC.MakeCoffeeEnd = true;
  192. }
  193. }
  194. if (morkTJakaJC.IsHaveJuiceCup)
  195. {
  196. var Juicestate = GetStatus<int[]>("GetDeviceStatus");
  197. if (Juicestate != null)
  198. {
  199. if (Juicestate.Length > 0)
  200. {
  201. var Juicestate1 = Convert.ToString(Juicestate[0], 2);
  202. var Juicestate2 = Juicestate[1];
  203. if (Juicestate1.IndexOf("0") == 1 && Juicestate2 == 0)
  204. {
  205. morkTJakaJC.MakeJuiceEnd = true;
  206. }
  207. }
  208. }
  209. //若无状态返回 则加延迟
  210. if (!bFirstTrig_Juice)
  211. {
  212. bFirstTrig_Juice = true;
  213. delayTimeOut_Juice = DateTime.Now;
  214. }
  215. else if (DateTime.Now.Subtract(delayTimeOut_Juice).TotalSeconds > 30 && bFirstTrig_Juice == true)
  216. {
  217. bFirstTrig_Juice = false;
  218. morkTJakaJC.MakeJuiceEnd = true;
  219. }
  220. }
  221. if (morkTJakaJC.IsHaveTeaWaterCup)
  222. {
  223. if (!bFirstTrig_TeaWater)
  224. {
  225. bFirstTrig_TeaWater = true;
  226. delayTimeOut = DateTime.Now;
  227. }
  228. else if (DateTime.Now.Subtract(delayTimeOut).TotalSeconds >= 50 && bFirstTrig_TeaWater == true)
  229. {
  230. bFirstTrig_TeaWater = false;
  231. morkTJakaJC.MakeTeaEnd = true;
  232. }
  233. }
  234. DoCoffee();
  235. DoJuice();
  236. DoBoiledTea();
  237. DoBoiledWater();
  238. }
  239. /// <summary>
  240. /// 验证当前是做咖啡还是做冰淇淋
  241. /// </summary>
  242. /// <param name="batchingLoc">物料位置</param>
  243. private GOODS_TYPE ValidateGoodsByBatching(string batchingLoc)
  244. {
  245. if (batchings.ContainsKey(batchingLoc))
  246. return batchings[batchingLoc].GoodsType;
  247. return GOODS_TYPE.NEITHER;
  248. }
  249. private AutoResetEvent are = new AutoResetEvent(false);
  250. private T GetStatus<T>(string key)
  251. {
  252. if (peripheralStatus.ContainsKey(key))
  253. {
  254. if (peripheralStatus[key] != null)
  255. {
  256. return (T)(peripheralStatus[key]);
  257. }
  258. }
  259. return default;
  260. }
  261. private void Wait(int value)
  262. {
  263. while (!((GetStatus<int>("Get_RobotAO1") == value) && GetStatus<int>("GetProgramStatus") == 0))//判断文件是否已经执行结束 且 文件末端变量值==文件名
  264. {
  265. Thread.Sleep(5);
  266. }
  267. }
  268. int[] devStatusBy = new int[2] { 0, 0 };
  269. private bool IsMakeCoffee()
  270. {
  271. bool bMake = (IsHealth && morkTJakaJC.morkOrderPushesCoffee.Count > 0 && !morkTJakaJC.IsHaveCoffeeCup) ? true : false;
  272. return bMake;
  273. }
  274. private bool IsMakeJuice()
  275. {
  276. bool bMake = (IsHealth && morkTJakaJC.morkOrderPushesJuicer.Count > 0 && !morkTJakaJC.IsHaveJuiceCup) ? true : false;
  277. return bMake;
  278. }
  279. private bool IsMakeTeaWater()
  280. {
  281. bool bMake = (IsHealth && morkTJakaJC.morkOrderPushesTea.Count > 0 && !morkTJakaJC.IsHaveTeaWaterCup) ? true : false;
  282. return bMake;
  283. }
  284. private bool IsMakeWater()
  285. {
  286. bool bMake = (IsHealth && morkTJakaJC.morkOrderPushesWater.Count > 0 && !morkTJakaJC.IsHaveTeaWaterCup) ? true : false;
  287. return bMake;
  288. }
  289. /// <summary>
  290. /// 传感器的输入信号 0:无意义 1:有信号 2:无信号 3:信号不正确
  291. /// </summary>
  292. int bSensorInput;
  293. /// <summary>
  294. /// 延迟的超时时间
  295. /// </summary>
  296. DateTime delayTimeOut;
  297. /// <summary>
  298. /// 做咖啡
  299. /// </summary>
  300. private void DoCoffee()
  301. {
  302. if (IsMakeCoffee())
  303. {
  304. if (morkTJakaJC.morkOrderPushesCoffee.TryDequeue(out OrderLocInfo orderLoc))
  305. {
  306. PickUpCoffee();//接咖啡
  307. morkTJakaJC.IsHaveCoffeeCup = true;
  308. }
  309. }
  310. else if (morkTJakaJC.MakeCoffeeEnd)
  311. {
  312. PutCoffeeCup();
  313. pickUpCoffeeHaveCup = false;
  314. morkTJakaJC.IsHaveCoffeeCup = false;
  315. }
  316. }
  317. private void DoJuice()
  318. {
  319. if (IsMakeJuice())
  320. {
  321. if (morkTJakaJC.morkOrderPushesJuicer.TryDequeue(out OrderLocInfo orderLoc))
  322. {
  323. PickUpJuicer();
  324. morkTJakaJC.IsHaveJuiceCup = true;
  325. }
  326. }
  327. else if (morkTJakaJC.MakeJuiceEnd)
  328. {
  329. Thread.Sleep(5000);//延迟五秒,防止接饮料口滴饮料
  330. putJuice();
  331. pickUpJuiceHaveCup = false;
  332. morkTJakaJC.IsHaveJuiceCup = false;
  333. morkTJakaJC.MakeJuiceEnd = false;
  334. }
  335. }
  336. private void DoBoiledTea()
  337. {
  338. if (IsMakeTeaWater())
  339. {
  340. if (morkTJakaJC.morkOrderPushesTea.TryDequeue(out OrderLocInfo orderLoc))
  341. {
  342. PickUpTea();
  343. morkTJakaJC.IsHaveTeaWaterCup = true;
  344. }
  345. }
  346. else if (morkTJakaJC.MakeTeaEnd)
  347. {
  348. PutWaterCup();
  349. pickUpHotWaterHaveCup = false;
  350. morkTJakaJC.IsHaveTeaWaterCup = false;
  351. morkTJakaJC.MakeTeaEnd = false;
  352. }
  353. }
  354. private void DoBoiledWater()
  355. {
  356. if (IsMakeWater())
  357. {
  358. if (morkTJakaJC.morkOrderPushesWater.TryDequeue(out OrderLocInfo orderLoc))
  359. {
  360. PickUpWater();
  361. }
  362. }
  363. else if (morkTJakaJC.MakeTeaEnd)
  364. {
  365. PutWaterCup();
  366. pickUpHotWaterHaveCup = false;
  367. morkTJakaJC.IsHaveTeaWaterCup = false;
  368. morkTJakaJC.MakeTeaEnd = false;
  369. }
  370. }
  371. private void PickUpCoffee()
  372. {
  373. if (!pickUpCoffeeHaveCup)
  374. {
  375. OrderChange(morkTJakaJC.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  376. int resultTakeCup = takeCup();
  377. if (resultTakeCup == 1)
  378. {
  379. DeviceProcessLogShow("咖啡杯取杯完成");
  380. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接咖啡 }.Publish();
  381. Wait(int.Parse(JakaModel.SENCE_接咖啡));
  382. new DRCoffee_MakeCoffeeEvent() { DeviceId = DeviceId, DrinkCode = (DrCoffeeDrinksCode)int.Parse(mainMaterialLoc) }.Publish(); //接咖啡控制 //DrCoffeeDrinksCode.热水
  383. are.WaitOne(1000 * 180);
  384. }
  385. else
  386. {
  387. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  388. Wait(int.Parse(JakaModel.SENCE_初始位));
  389. }
  390. }
  391. }
  392. private void PutCoffeeCup()
  393. {
  394. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放咖啡杯 }.Publish();
  395. Wait(int.Parse(JakaModel.SENCE_放咖啡杯));
  396. int resultputCup = putCup();
  397. if (resultputCup == 1)
  398. {
  399. //订单状态改变:完成
  400. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  401. Wait(int.Parse(JakaModel.SENCE_初始位));
  402. OrderChange(morkTJakaJC.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  403. }
  404. else
  405. {
  406. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  407. Wait(int.Parse(JakaModel.SENCE_初始位));
  408. }
  409. }
  410. /// <summary>
  411. /// 接开水
  412. /// </summary>
  413. private void PickUpWater()
  414. {
  415. #region 接水流程
  416. if (!pickUpHotWaterHaveCup)
  417. {
  418. OrderChange(morkTJakaJC.morkOrderPushesWater.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  419. int resultTakeCup = takeCup();
  420. if (resultTakeCup == 1)
  421. {
  422. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接水 }.Publish();
  423. Wait(int.Parse(JakaModel.SENCE_接水));
  424. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "4" }.Publish();
  425. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "3" }.Publish();
  426. Thread.Sleep(100);
  427. new WriteMcu() { TagName = "OutputControl", Value = true, Address = "3" }.Publish();
  428. Thread.Sleep(3000);
  429. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "3" }.Publish();
  430. Thread.Sleep(100);
  431. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "4" }.Publish();
  432. Thread.Sleep(100);
  433. new WriteMcu() { TagName = "OutputControl", Value = true, Address = "4" }.Publish();
  434. Thread.Sleep(500);
  435. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "4" }.Publish();
  436. }
  437. else if (resultTakeCup == 2 || resultTakeCup == 3)
  438. {
  439. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  440. Wait(int.Parse(JakaModel.SENCE_初始位));
  441. }
  442. }
  443. #endregion
  444. }
  445. /// <summary>
  446. /// 放水杯
  447. /// </summary>
  448. private void PutWaterCup()
  449. {
  450. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放茶水杯 }.Publish();
  451. Wait(int.Parse(JakaModel.SENCE_放茶水杯));
  452. int resultputCup = putCup();
  453. if (resultputCup == 1)
  454. {
  455. //订单状态改变:完成
  456. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  457. Wait(int.Parse(JakaModel.SENCE_初始位));
  458. OrderChange(morkTJakaJC.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  459. }
  460. else
  461. {
  462. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  463. Wait(int.Parse(JakaModel.SENCE_初始位));
  464. }
  465. }
  466. /// <summary>
  467. /// 做茶
  468. /// </summary>
  469. private void PickUpTea()
  470. {
  471. #region 接茶流程
  472. if (!pickUpHotWaterHaveCup)
  473. {
  474. OrderChange(morkTJakaJC.morkOrderPushesTea.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  475. int resultTakeCup = takeCup();
  476. if (resultTakeCup == 1)
  477. {
  478. DeviceProcessLogShow("取茶杯完成");
  479. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接茶 }.Publish();
  480. Wait(int.Parse(JakaModel.SENCE_接茶));
  481. new WriteMcu() { TagName = "ServoControl", Address = "1", Value = 105 }.Publish();
  482. Thread.Sleep(1000);
  483. new WriteMcu() { TagName = "ServoControl", Address = "1", Value = 130 }.Publish();
  484. Thread.Sleep(1000);
  485. new WriteMcu() { TagName = "ServoControl", Address = "1", Value = 105 }.Publish();
  486. Thread.Sleep(3000);
  487. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接茶_接水 }.Publish();
  488. Wait(int.Parse(JakaModel.SENCE_接茶_接水));
  489. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "4" }.Publish();
  490. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "3" }.Publish();
  491. Thread.Sleep(100);
  492. new WriteMcu() { TagName = "OutputControl", Value = true, Address = "3" }.Publish();
  493. Thread.Sleep(3000);
  494. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "3" }.Publish();
  495. Thread.Sleep(100);
  496. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "4" }.Publish();
  497. Thread.Sleep(100);
  498. new WriteMcu() { TagName = "OutputControl", Value = true, Address = "4" }.Publish();
  499. Thread.Sleep(500);
  500. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "4" }.Publish();
  501. }
  502. else if (resultTakeCup == 2 || resultTakeCup == 3)
  503. {
  504. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  505. Wait(int.Parse(JakaModel.SENCE_初始位));
  506. }
  507. }
  508. #endregion
  509. }
  510. /// <summary>
  511. /// 果汁机控制信号
  512. /// </summary>
  513. private byte JuicerNum;
  514. private int JuiceCH;
  515. private void PickUpJuicer()
  516. {
  517. #region 接果汁流程
  518. if (!pickUpJuiceHaveCup)
  519. {
  520. OrderChange(morkTJakaJC.morkOrderPushesWater.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  521. int resultTakeCup = takeCup();
  522. JuiceCH = int.Parse(mainMaterialLoc);
  523. if (resultTakeCup == 1)
  524. {
  525. switch (JuiceCH)
  526. {
  527. case 52:
  528. if (GuMake)
  529. {
  530. JuicerNum = 0x00;
  531. }
  532. else
  533. {
  534. JuicerNum = 0x01;
  535. }
  536. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接果汁1 }.Publish();
  537. Wait(int.Parse(JakaModel.SENCE_接果汁1));
  538. break;
  539. case 53:
  540. if (GuMake)
  541. {
  542. JuicerNum = 0x02;
  543. }
  544. else
  545. {
  546. JuicerNum = 0x03;
  547. }
  548. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接果汁2 }.Publish();
  549. Wait(int.Parse(JakaModel.SENCE_接果汁2));
  550. break;
  551. case 54:
  552. if (GuMake)
  553. {
  554. JuicerNum = 0x04;
  555. }
  556. else
  557. {
  558. JuicerNum = 0x05;
  559. }
  560. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接果汁3 }.Publish();
  561. Wait(int.Parse(JakaModel.SENCE_接果汁3));
  562. break;
  563. case 55:
  564. if (GuMake)
  565. {
  566. JuicerNum = 0x06;
  567. }
  568. else
  569. {
  570. JuicerNum = 0x07;
  571. }
  572. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接果汁4 }.Publish();
  573. Wait(int.Parse(JakaModel.SENCE_接果汁4));
  574. break;
  575. default:
  576. JuicerNum = 0x00;
  577. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接果汁1 }.Publish();
  578. Wait(int.Parse(JakaModel.SENCE_接果汁1));
  579. break;
  580. }
  581. new WriteJuicer() { Value = JuicerNum }.Publish();
  582. pickUpJuiceHaveCup = true;
  583. }
  584. else
  585. {
  586. return;
  587. }
  588. }
  589. #endregion
  590. }
  591. private void putJuice()
  592. {
  593. switch (JuiceCH)
  594. {
  595. case 52:
  596. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯1 }.Publish();
  597. Wait(int.Parse(JakaModel.SENCE_放果汁杯1));
  598. break;
  599. case 53:
  600. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯2 }.Publish();
  601. Wait(int.Parse(JakaModel.SENCE_放果汁杯2));
  602. break;
  603. case 54:
  604. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯3 }.Publish();
  605. Wait(int.Parse(JakaModel.SENCE_放果汁杯3));
  606. break;
  607. case 55:
  608. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯4 }.Publish();
  609. Wait(int.Parse(JakaModel.SENCE_放果汁杯4));
  610. break;
  611. default:
  612. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯1 }.Publish();
  613. Wait(int.Parse(JakaModel.SENCE_放果汁杯1));
  614. break;
  615. }
  616. int resultputCup = putCup();
  617. if (resultputCup == 1)
  618. {
  619. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  620. Wait(int.Parse(JakaModel.SENCE_初始位));
  621. OrderChange(morkTJakaJC.morkOrderPushesWater.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  622. }
  623. else
  624. {
  625. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  626. Wait(int.Parse(JakaModel.SENCE_初始位));
  627. }
  628. }
  629. private int getCup_cnt;
  630. /// <summary>
  631. /// 取杯流程
  632. /// </summary>
  633. /// <returns>0:无意义,1:取杯成功 2:机构有杯子,取杯失败 3:机构没有杯子</returns>
  634. private int takeCup()
  635. {
  636. #region 单片机控制的IO
  637. //try
  638. //{
  639. // getCup_cnt = 0;//取杯次数复位
  640. // new WriteJaka() { TagName = "Set_RobotAO1", Value = 0 }.Publish();
  641. // Wait(0);
  642. // new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  643. // Wait(int.Parse(JakaModel.SENCE_初始位));
  644. // new WriteMcu() { TagName = "OutputControl", Address = "1", Value = true }.Publish();
  645. // Thread.Sleep(10);
  646. // new WriteMcu() { TagName = "OutputControl", Address = "1", Value = true }.Publish();
  647. // new WriteJaka() { TagName = "Set_RobotAO1", Value = 0 }.Publish();
  648. // Wait(0);
  649. // new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯 }.Publish();
  650. // Wait(int.Parse(JakaModel.SENCE_取杯));
  651. // bSensorInput = sensor_Sign(1);
  652. // if (bSensorInput == 2)
  653. // {
  654. // Thread.Sleep(100);
  655. // new WriteMcu() { TagName = "OutputControl", Address = "1", Value = false }.Publish();
  656. // new WriteMcu() { TagName = "OutputControl", Address = "1", Value = false }.Publish();
  657. // Thread.Sleep(100);
  658. // new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯检测 }.Publish();
  659. // Wait(int.Parse(JakaModel.SENCE_取杯检测));
  660. // DeviceProcessLogShow("落杯器没有纸杯了");
  661. // return 3;
  662. // }
  663. // if (bSensorInput == 1)
  664. // {
  665. // Thread.Sleep(100);
  666. // new WriteMcu() { TagName = "OutputControl", Address = "1", Value = false }.Publish();
  667. // new WriteMcu() { TagName = "OutputControl", Address = "1", Value = false }.Publish();
  668. // Thread.Sleep(100);
  669. // new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯检测 }.Publish();
  670. // Wait(int.Parse(JakaModel.SENCE_取杯检测));
  671. // bSensorInput = sensor_Sign(1);
  672. // while (getCup_cnt < 4 && (bSensorInput == 2 || bSensorInput == 3))
  673. // {
  674. // DeviceProcessLogShow($"第{getCup_cnt}次取杯失败");
  675. // Thread.Sleep(100);
  676. // new WriteMcu() { TagName = "OutputControl", Address = "1", Value = true }.Publish();
  677. // new WriteMcu() { TagName = "OutputControl", Address = "1", Value = true }.Publish();
  678. // Thread.Sleep(100);
  679. // new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯 }.Publish();
  680. // Wait(int.Parse(JakaModel.SENCE_取杯));
  681. // getCup_cnt = getCup_cnt + 1;
  682. // Thread.Sleep(100);
  683. // new WriteMcu() { TagName = "OutputControl", Address = "1", Value = false }.Publish();
  684. // new WriteMcu() { TagName = "OutputControl", Address = "1", Value = false }.Publish();
  685. // Thread.Sleep(100);
  686. // new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯检测 }.Publish();
  687. // Wait(int.Parse(JakaModel.SENCE_取杯检测));
  688. // bSensorInput = sensor_Sign(1);
  689. // }
  690. // if (bSensorInput == 1)
  691. // {
  692. // return 1;
  693. // }
  694. // else
  695. // {
  696. // return 2;
  697. // }
  698. // }
  699. // return 1;
  700. //}
  701. //catch (Exception ex)
  702. //{
  703. // MessageLog.GetInstance.ShowEx(ex.ToString());
  704. // return 0;
  705. //}
  706. #endregion
  707. try
  708. {
  709. getCup_cnt = 0;//取杯次数复位
  710. new WriteJaka() { TagName = "Set_RobotAO1", Value = 0 }.Publish();
  711. Wait(0);
  712. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  713. Wait(int.Parse(JakaModel.SENCE_初始位));
  714. new WriteJaka() { TagName = "JakaDOutput", DO_Index=0, Value = true }.Publish();
  715. Thread.Sleep(100);
  716. new WriteJaka() { TagName = "Set_RobotAO1", Value = 0 }.Publish();
  717. Wait(0);
  718. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯 }.Publish();
  719. Wait(int.Parse(JakaModel.SENCE_取杯));
  720. //取杯到位,使用落杯器
  721. new WriteJaka() { TagName = "JakaDOutput", DO_Index = 1, Value = true }.Publish();
  722. if (sensor_Sign(1)==1 )
  723. {
  724. new WriteJaka() { TagName = "JakaDOutput", DO_Index = 1, Value = false }.Publish();
  725. Thread.Sleep(2000);
  726. bSensorInput = sensor_Sign(0);
  727. if (bSensorInput == 2)
  728. {
  729. Thread.Sleep(100);
  730. new WriteJaka() { TagName = "JakaDOutput", DO_Index = 0, Value = false }.Publish();
  731. Thread.Sleep(100);
  732. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯检测 }.Publish();
  733. Wait(int.Parse(JakaModel.SENCE_取杯检测));
  734. DeviceProcessLogShow("落杯器没有纸杯了");
  735. return 3;
  736. }
  737. if (bSensorInput == 1)
  738. {
  739. Thread.Sleep(100);
  740. new WriteJaka() { TagName = "JakaDOutput", DO_Index = 0, Value = false }.Publish();
  741. Thread.Sleep(100);
  742. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯检测 }.Publish();
  743. Wait(int.Parse(JakaModel.SENCE_取杯检测));
  744. bSensorInput = sensor_Sign(1);
  745. while (getCup_cnt < 4 && (bSensorInput == 2 || bSensorInput == 3))
  746. {
  747. DeviceProcessLogShow($"第{getCup_cnt}次取杯失败");
  748. Thread.Sleep(100);
  749. new WriteJaka() { TagName = "JakaDOutput", DO_Index = 0, Value = true }.Publish();
  750. Thread.Sleep(100);
  751. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯 }.Publish();
  752. Wait(int.Parse(JakaModel.SENCE_取杯));
  753. getCup_cnt = getCup_cnt + 1;
  754. Thread.Sleep(100);
  755. new WriteJaka() { TagName = "JakaDOutput", DO_Index = 0, Value = false }.Publish();
  756. Thread.Sleep(100);
  757. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯检测 }.Publish();
  758. Wait(int.Parse(JakaModel.SENCE_取杯检测));
  759. bSensorInput = sensor_Sign(1);
  760. }
  761. if (bSensorInput == 1)
  762. {
  763. return 1;
  764. }
  765. else
  766. {
  767. return 2;
  768. }
  769. }
  770. }
  771. return 1;
  772. }
  773. catch (Exception ex)
  774. {
  775. MessageLog.GetInstance.ShowEx(ex.ToString());
  776. return 0;
  777. }
  778. }
  779. /// <summary>
  780. /// 放杯
  781. /// </summary>
  782. /// <returns>0:无意义 1:执行成功 2:执行失败(传感器还有信号)</returns>
  783. private int putCup()
  784. {
  785. try
  786. {
  787. while (checkCup() == 2)
  788. {
  789. Thread.Sleep(100);
  790. }
  791. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放杯 }.Publish();
  792. Wait(int.Parse(JakaModel.SENCE_放杯));
  793. Thread.Sleep(10);
  794. new WriteJaka() { TagName = "JakaDOutput", DO_Index = 0, Value = true }.Publish();
  795. Thread.Sleep(10);
  796. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放杯检测 }.Publish();
  797. Wait(int.Parse(JakaModel.SENCE_放杯检测));
  798. bSensorInput = sensor_Sign(3);
  799. delayTimeOut = DateTime.Now;
  800. while (bSensorInput == 2)
  801. {
  802. Thread.Sleep(100);
  803. bSensorInput = sensor_Sign(3);
  804. if (DateTime.Now.Subtract(delayTimeOut).TotalSeconds >= 2) return 2;
  805. }
  806. if (bSensorInput == 2)
  807. {
  808. DeviceProcessLogShow("放杯失败传感器没有信号");
  809. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  810. Wait(int.Parse(JakaModel.SENCE_初始位));
  811. return 1;
  812. }
  813. return 1;
  814. }
  815. catch (Exception ex)
  816. {
  817. MessageLog.GetInstance.ShowEx(ex.ToString());
  818. return 0;
  819. }
  820. }
  821. private int cnt_Check;
  822. /// <summary>
  823. /// 检测放杯位,是否有杯子
  824. /// </summary>
  825. /// <returns>0:无意义 1:没有杯子 2:有杯子 </returns>
  826. private int checkCup()
  827. {
  828. try
  829. {
  830. bSensorInput = sensor_Sign(2);
  831. if (bSensorInput == 2)
  832. {
  833. DeviceProcessLogShow($"放杯位传感器没有信号:{cnt_Check}");
  834. return 1;
  835. }
  836. else if (bSensorInput == 1)
  837. {
  838. DeviceProcessLogShow($"放杯位传感器有信号:{cnt_Check}");
  839. return 2;
  840. }
  841. return 2;
  842. }
  843. catch (Exception ex)
  844. {
  845. MessageLog.GetInstance.ShowEx(ex.ToString());
  846. return 0;
  847. }
  848. }
  849. private T McuRead<T>(string tagName, object par)
  850. {
  851. new ReadMcu() { DeviceId = DeviceId, TagName = tagName, ReadPar = par };
  852. if (peripheralStatus.ContainsKey(tagName))
  853. {
  854. if (peripheralStatus[tagName] != null)
  855. {
  856. return (T)peripheralStatus[tagName];
  857. }
  858. }
  859. return default;
  860. }
  861. /// <summary>
  862. /// 传感器防抖0.2s内检测20次,
  863. /// </summary>
  864. /// <param name="num"></param>
  865. /// <returns></returns>
  866. private int sensor_Sign(byte num)
  867. {
  868. DeviceProcessLogShow($"开始检测{num}号传感器信号");
  869. int cnt = 0;
  870. cnt_Check = 0;
  871. while (true)
  872. {
  873. Thread.Sleep(10);
  874. if (num == 0)
  875. {
  876. bSensorInput = GetStatus<bool>("Get_RobotDI0") ? 1 : 2;
  877. }
  878. else if (num == 1)
  879. {
  880. bSensorInput = GetStatus<bool>("Get_RobotDI1") ? 1 : 2;
  881. }
  882. else if (num == 2)
  883. {
  884. bSensorInput = GetStatus<bool>("Get_RobotDI2") ? 1 : 2;
  885. }
  886. else if (num == 3)
  887. {
  888. bSensorInput = GetStatus<bool>("Get_RobotDI3") ? 1 : 2;
  889. }
  890. if (bSensorInput == 1)
  891. {
  892. cnt_Check = cnt_Check + 1;
  893. cnt = cnt + 1;
  894. }
  895. else if (bSensorInput == 2)
  896. {
  897. cnt_Check = cnt_Check - 1;
  898. cnt = cnt + 1;
  899. }
  900. if (cnt >= 20)
  901. {
  902. break;
  903. }
  904. }
  905. if (cnt_Check >= 0)
  906. {
  907. DeviceProcessLogShow($"{num}传感器有信号:{cnt_Check}");
  908. return 1;
  909. }
  910. else
  911. {
  912. DeviceProcessLogShow($"{num}传感器没有信号:{cnt_Check}");
  913. return 2;
  914. }
  915. }
  916. private void DRCoffee_CoffeEndCookEventHandle(IEvent @event, EventCallBackHandle callBack)
  917. {
  918. are.Set();
  919. }
  920. public void SimOrder<T>(T simOrder)
  921. {
  922. }
  923. public override void Stop()
  924. {
  925. }
  926. public override void ReadData()
  927. {
  928. }
  929. public override void ResetProgram()
  930. {
  931. }
  932. public override void SimOrder()
  933. {
  934. }
  935. }
  936. }