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

Control_MORKIC.cs 19 KiB

2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Threading;
  6. using BPA.Message;
  7. using BPA.Utility;
  8. using HBLConsole.Communication;
  9. using HBLConsole.Factory;
  10. using HBLConsole.GVL;
  11. using HBLConsole.Interface;
  12. using HBLConsole.Model;
  13. using HBLConsole.Service;
  14. using HBLDevice.Coffee;
  15. using HBLDevice.IceCream;
  16. using Robotc;
  17. using System.Collections.Concurrent;
  18. using System.Diagnostics;
  19. using BPA.Message.IOT;
  20. using HBLDevice.ICChip;
  21. namespace HBLConsole.MORKIC
  22. {
  23. /*
  24. * 冰淇淋咖啡机组合套装
  25. * 物料位置:
  26. * 1:冰淇料
  27. * 2:冰淇淋杯
  28. * 5:咖啡
  29. * 6:咖啡杯
  30. */
  31. public class Control_MORKIC : IControl
  32. {
  33. GVL_MORIC mORKD = new GVL_MORIC();
  34. //咖啡机主控程序
  35. private CoffeeMachine coffeeMachine;
  36. //单片机主控程序
  37. private ICChipMachine icchipMachine;
  38. //物料存放位置
  39. private Dictionary<string, PolymerBatching> batchings = new Dictionary<string, PolymerBatching>();
  40. //容器位置
  41. private string holderLoc;
  42. //主料位置
  43. private string mainMaterialLoc;
  44. //子订单ID
  45. private string subOrderId;
  46. /// <summary>
  47. /// 获取乐百机器人的数据
  48. /// </summary>
  49. SignalResult lebai = new SignalResult();
  50. private bool enableFunny = false;
  51. private DateTime lastRecvdOrder = DateTime.Now;
  52. private bool working = false;
  53. public void ConnectOk()
  54. {
  55. }
  56. ConcurrentQueue<MorkOrderPush> morkOrderPushes = new ConcurrentQueue<MorkOrderPush>();
  57. public void Init()
  58. {
  59. ActionManage.GetInstance.Register(new Action<object>((s) =>
  60. {
  61. if (s is DrCoffeeDrinksCode cf)
  62. {
  63. mainMaterialLoc = ((int)cf).ToString();
  64. DoCoffee();
  65. }
  66. }), "SimCoffee");
  67. //构建所有商品物料信息
  68. batchings = PolymerBatching.BuildAll();
  69. EventBus.GetInstance().Subscribe<CoffeEndCook>(CoffeEndCookHandle);
  70. System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
  71. //一系列外围基础配置
  72. var com_Coffee = config.AppSettings.Settings["COM_Coffee"].Value;
  73. var baud_Coffee = config.AppSettings.Settings["BAUD_Coffee"].Value;
  74. var com_IceCream = config.AppSettings.Settings["COM_IceCream"].Value;
  75. var baud_IceCream = config.AppSettings.Settings["BAUD_IceCream"].Value;
  76. var iceCreamCXBThreshold = int.Parse(config.AppSettings.Settings["IceCream_CXB_Threshold"].Value);
  77. var com_ICChip = config.AppSettings.Settings["COM_ICChip"].Value;
  78. var baud_ICChip = config.AppSettings.Settings["BAUD_IChip"].Value;
  79. if (iceCreamCXBThreshold > 0)
  80. {
  81. //设置冰淇淋成型比
  82. MorkIStatus.GetInstance().CXB_Threshold = (byte)iceCreamCXBThreshold;
  83. }
  84. //咖啡机创建
  85. coffeeMachine = new CoffeeMachine(com_Coffee, (BaudRates)Enum.Parse(typeof(BaudRates), baud_Coffee));
  86. //单片机机创建
  87. icchipMachine = new ICChipMachine(com_ICChip, (BaudRates)Enum.Parse(typeof(BaudRates), baud_ICChip));
  88. Main();
  89. ReadData();
  90. ThreadManage.GetInstance.StartLong(new Action(() =>
  91. {
  92. while (morkOrderPushes.Count > 0)
  93. {
  94. while (enableFunny) { Thread.Sleep(10); }
  95. MessageLog.GetInstance.Show("当前非自嗨模式,允许开工");
  96. working = true;
  97. if (morkOrderPushes.TryDequeue(out MorkOrderPush order))
  98. {
  99. MessageLog.GetInstance.Show($"开始制作订单[{order.SortNum}]");
  100. //Thread.Sleep(5000);
  101. //SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK);
  102. //商品类型
  103. GOODS_TYPE currentGoodsType = GOODS_TYPE.NEITHER;
  104. //子订单ID
  105. subOrderId = order.SuborderId;
  106. //遍历物料
  107. foreach (var item in order.GoodBatchings)
  108. {
  109. var res = Json<BatchingInfoPar>.Data.orderMaterialDelivery.BatchingInfo.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  110. if (res != null)
  111. {
  112. //验证商品是咖啡还是冰淇淋
  113. if (ValidateGoodsByBatching(res.BatchingLoc) != GOODS_TYPE.NEITHER)
  114. {
  115. //获取当前物料所属商品类型
  116. currentGoodsType = ValidateGoodsByBatching(res.BatchingLoc);
  117. }
  118. //获取主料和容器位置
  119. switch (batchings[res.BatchingLoc].BatchingClass)
  120. {
  121. case BATCHING_CLASS.HOLDER:
  122. holderLoc = res.BatchingLoc;
  123. break;
  124. case BATCHING_CLASS.MAIN_MATERIAL:
  125. mainMaterialLoc = res.BatchingLoc;
  126. break;
  127. }
  128. }
  129. }
  130. //根据商品类型执行具体制作流程
  131. switch (currentGoodsType)
  132. {
  133. case GOODS_TYPE.COFFEE:
  134. DoCoffee();
  135. break;
  136. case GOODS_TYPE.ICECREAM:
  137. DoIceCream();
  138. break;
  139. }
  140. }
  141. working = false;
  142. lastRecvdOrder = DateTime.Now;
  143. }
  144. Thread.Sleep(1000);
  145. }), "订单制作");
  146. }
  147. public void Main()
  148. {
  149. //咖啡机开启主线程
  150. coffeeMachine.Start();
  151. //单片机开启主线程
  152. icchipMachine.Start();
  153. new ModeSetEvent() { Mode = MORKI_MODE.制冷模式 }.Publish();
  154. //开始心跳刷新,根据咖啡机及冰淇淋机来判断
  155. ThreadManage.GetInstance.StartLong(new Action(() =>
  156. {
  157. GeneralConfig.Healthy =
  158. LebaiHelper.GetInstance.IsConnected &&
  159. MorkCStatus.GetInstance().CanDo &&
  160. ChipStatus.GetInstance().CanDo;
  161. //GeneralConfig.Healthy = true;
  162. Thread.Sleep(100);
  163. }), "MORK-IC心跳刷新");
  164. ThreadManage.GetInstance.Start(new Action(() =>
  165. {
  166. while (!LebaiHelper.GetInstance.IsConnected)
  167. {
  168. Thread.Sleep(10);
  169. }
  170. LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_欢迎);
  171. }), "MORK-IC欢迎");
  172. //ThreadManage.GetInstance.StartLong(new Action(() =>
  173. //{
  174. // if (null != lebai)
  175. // {
  176. // if (!working)
  177. // {
  178. // if (DateTime.Now.Subtract(lastRecvdOrder).TotalSeconds >= 30)
  179. // {
  180. // MessageLog.GetInstance.Show("进入自嗨模式");
  181. // enableFunny = true;
  182. // //ThreadManage.GetInstance.Start(new Action(() =>
  183. // //{
  184. // //}), "调用乐百机器人做咖啡场景");
  185. // LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_自嗨);
  186. // Thread.Sleep(2000);
  187. // while (lebai.Ok && lebai.Value != 10)
  188. // {
  189. // Thread.Sleep(5);
  190. // }
  191. // enableFunny = false;
  192. // MessageLog.GetInstance.Show("退出自嗨模式");
  193. // Thread.Sleep(60*1000);
  194. // }
  195. // }
  196. // }
  197. // Thread.Sleep(100);
  198. //}), "MORK-IC自嗨");
  199. }
  200. public void DataParse<T>(T order)
  201. {
  202. if (order is MorkOrderPush morkOrderPush)
  203. {
  204. morkOrderPushes.Enqueue(morkOrderPush);
  205. }
  206. }
  207. /// <summary>
  208. /// 验证当前是做咖啡还是做冰淇淋
  209. /// </summary>
  210. /// <param name="batchingLoc">物料位置</param>
  211. private GOODS_TYPE ValidateGoodsByBatching(string batchingLoc)
  212. {
  213. if (batchings.ContainsKey(batchingLoc))
  214. return batchings[batchingLoc].GoodsType;
  215. return GOODS_TYPE.NEITHER;
  216. }
  217. private AutoResetEvent are = new AutoResetEvent(false);
  218. /// <summary>
  219. /// 做咖啡
  220. /// </summary>
  221. private void DoCoffee()
  222. {
  223. //are.Reset();
  224. //LebaiHelper.GetInstance.SetValue(0);
  225. ////订单状态改变:开始制作
  226. //SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  227. ////todo:先调用机器人
  228. //LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_咖啡);
  229. ////ThreadManage.GetInstance.Start(new Action(() => { LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_咖啡); }), "调用乐百机器人做咖啡场景");
  230. //Thread.Sleep(2000);
  231. //while (!(lebai.Ok && lebai.Value == 1))
  232. //{
  233. // Thread.Sleep(5);
  234. //}
  235. ////todo:咖啡杯下杯
  236. //new TakeCupEvent() { Cup = IC_CUP.CUP_COFFEE }.Publish();
  237. //Thread.Sleep(500);
  238. ////while (!ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE) { Thread.Sleep(5); }
  239. //MessageLog.GetInstance.Show("咖啡杯取杯完成");
  240. //LebaiHelper.GetInstance.SetValue(100);
  241. //while (!(lebai.Ok && lebai.Value == 2))
  242. //{
  243. // Thread.Sleep(5);
  244. //}
  245. //MessageLog.GetInstance.Show("机器人到达接咖啡口位置");
  246. //new MakeCoffeeEvent() { DrinkCode = (DrCoffeeDrinksCode)int.Parse(mainMaterialLoc) }.Publish();
  247. //are.WaitOne(1000 * 180);
  248. ////are.WaitOne(1 * 180);
  249. //MessageLog.GetInstance.Show("咖啡机制作咖啡完成");
  250. //LebaiHelper.GetInstance.SetValue(101);
  251. //while (!(lebai.Ok && lebai.Value == 3))
  252. //{
  253. // Thread.Sleep(5);
  254. //}
  255. //MessageLog.GetInstance.Show("机器人到达接咖啡口位置");
  256. ////订单状态改变:完成
  257. //SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK);
  258. //---------------------------------------------------------含咖啡杯检测-----------------------------------------//
  259. #region 含咖啡杯检测的多场景
  260. int checkeNum = 0;
  261. are.Reset();
  262. LebaiHelper.GetInstance.SetValue(0);
  263. //订单状态改变:开始制作
  264. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  265. //todo:先调用机器人
  266. LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_取咖啡杯);
  267. //ThreadManage.GetInstance.Start(new Action(() => { LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_咖啡); }), "调用乐百机器人做咖啡场景");
  268. Thread.Sleep(2000);
  269. while (!(lebai.Ok && lebai.Value == 1))
  270. {
  271. Thread.Sleep(5);
  272. }
  273. //todo:咖啡杯下杯
  274. new TakeCupEvent() { Cup = IC_CUP.CUP_COFFEE }.Publish();
  275. Thread.Sleep(500);
  276. //while (!ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE) { Thread.Sleep(5); }
  277. MessageLog.GetInstance.Show("咖啡杯取杯完成");
  278. LebaiHelper.GetInstance.SetValue(100);
  279. //咖啡杯检测场景
  280. while (!ChipStatus.GetInstance().ArticleExits)
  281. {
  282. checkeNum++;
  283. if (checkeNum > 1)
  284. {
  285. LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_再次取杯);
  286. while (!(lebai.Ok && lebai.Value ==3))
  287. {
  288. Thread.Sleep(5);
  289. }
  290. new TakeCupEvent() { Cup = IC_CUP.CUP_COFFEE }.Publish();
  291. Thread.Sleep(500);
  292. }
  293. LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_咖啡杯检测);
  294. while (!(lebai.Ok && lebai.Value == 2))
  295. {
  296. Thread.Sleep(5);
  297. }
  298. new ArticleExitsEvent() { }.Publish();
  299. Thread.Sleep(1000);
  300. }
  301. checkeNum = 0;
  302. LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_接咖啡);
  303. while (!(lebai.Ok && lebai.Value == 4))
  304. {
  305. Thread.Sleep(5);
  306. }
  307. MessageLog.GetInstance.Show("机器人到达接咖啡口位置");
  308. new MakeCoffeeEvent() { DrinkCode = (DrCoffeeDrinksCode)int.Parse(mainMaterialLoc) }.Publish();
  309. are.WaitOne(1000 * 180);
  310. //are.WaitOne(1 * 180);
  311. MessageLog.GetInstance.Show("咖啡机制作咖啡完成");
  312. LebaiHelper.GetInstance.SetValue(101);
  313. LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_送咖啡);
  314. while (!(lebai.Ok && lebai.Value == 5))
  315. {
  316. Thread.Sleep(5);
  317. }
  318. MessageLog.GetInstance.Show("机器人到达顾客取咖啡位置");
  319. //订单状态改变:完成
  320. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK);
  321. #endregion
  322. }
  323. /// <summary>
  324. /// 做冰淇淋
  325. /// </summary>
  326. private void DoIceCream()
  327. {
  328. LebaiHelper.GetInstance.SetValue(0);
  329. new RotorSwitchEvent() { TurnOn = true }.Publish();
  330. int scene = LebaiHelper.SENCE_接冰淇淋2;
  331. var se = IC_SE.SE_2;
  332. se = PolymerBatching.GetIceCreamSE(mainMaterialLoc, out scene);
  333. //订单状态改变:开始制作
  334. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  335. //todo:先调用机器人
  336. LebaiHelper.GetInstance.Scene(scene);
  337. MessageLog.GetInstance.Show("调用乐百机器人做冰淇淋场景");
  338. Thread.Sleep(2000);
  339. while (!(lebai.Ok && lebai.Value == 1))
  340. {
  341. Thread.Sleep(5);
  342. }
  343. new TakeCupEvent() { Cup = IC_CUP.CUP_ICECREAM }.Publish();
  344. Thread.Sleep(500);
  345. //while (!ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM) { Thread.Sleep(5); }
  346. LebaiHelper.GetInstance.SetValue(100);
  347. while (!(lebai.Ok && lebai.Value == 2))
  348. {
  349. Thread.Sleep(5);
  350. }
  351. new MakeIceCreamEvent() { SteeringEngine = se }.Publish();
  352. Thread.Sleep(1000);
  353. LebaiHelper.GetInstance.SetValue(101);
  354. new RotorSwitchEvent() { TurnOn = false }.Publish();
  355. while (!(lebai.Ok && lebai.Value == 3))
  356. {
  357. Thread.Sleep(5);
  358. }
  359. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK);
  360. }
  361. private void CoffeEndCookHandle(IEvent @event, EventBus.EventCallBackHandle callBack)
  362. {
  363. are.Set();
  364. }
  365. //public void Main()
  366. //{
  367. // //咖啡机开启主线程
  368. // coffeeMachine.Start();
  369. // //冰淇淋机开启主线程
  370. // iceCreamMachine.Start();
  371. // icchipMachine.Start();
  372. // new ModeSetEvent() { Mode = MORKI_MODE.制冷模式 }.Publish();
  373. // //开始心跳刷新,根据咖啡机及冰淇淋机来判断
  374. // ThreadManage.GetInstance.StartLong(new Action(() =>
  375. // {
  376. // //GeneralConfig.Healthy = true;
  377. // //GeneralConfig.Healthy =
  378. // // LebaiHelper.GetInstance.IsConnected &&
  379. // // MorkIStatus.GetInstance().CanDo &&
  380. // // MorkCStatus.GetInstance().CanDo;
  381. // GeneralConfig.Healthy =
  382. // LebaiHelper.GetInstance.IsConnected &&
  383. // MorkCStatus.GetInstance().CanDo;
  384. // GeneralConfig.Healthy = true;
  385. // Thread.Sleep(100);
  386. // }), "MORK-IC心跳刷新");
  387. //}
  388. public void ReadData()
  389. {
  390. ThreadManage.GetInstance.StartLong(new Action(() =>
  391. {
  392. lebai = LebaiHelper.GetInstance.GetValueAsync();
  393. LebaiHelper.GetInstance.GetRobotModeStatus();
  394. Thread.Sleep(100);
  395. }), "乐百机器人数据读取");
  396. }
  397. public void SimOrder<T>(T simOrder)
  398. {
  399. //ThreadManage.GetInstance.Start(new Action(() =>
  400. //{
  401. // DoIceCream();
  402. // //DoCoffee();
  403. //}), "aaa");
  404. }
  405. /// <summary>
  406. /// IOT 广播消息命令
  407. /// </summary>
  408. public void IotBroadcast<T>(T broadcast)
  409. {
  410. if (broadcast != null && broadcast is IOTCommandModel iOTCommand)
  411. {
  412. switch (iOTCommand.CommandName)
  413. {
  414. case 0://控制类
  415. if (iOTCommand.CommandValue != null)
  416. {
  417. if (iOTCommand.CommandValue.ContainsKey("SimOrder"))
  418. {
  419. SimOrder(new SimOrderData { NoodleLoc = 1, BowlLoc = 10 });
  420. }
  421. }
  422. break;
  423. case 1://设置属性
  424. break;
  425. case 2://通知消息
  426. break;
  427. default:
  428. break;
  429. }
  430. }
  431. }
  432. }
  433. }