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

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