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.
 
 

279 lines
9.9 KiB

  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. namespace HBLConsole.MORKIC
  20. {
  21. /*
  22. * 冰淇淋咖啡机组合套装
  23. * 物料位置:
  24. * 1:冰淇料
  25. * 2:冰淇淋杯
  26. * 5:咖啡
  27. * 6:咖啡杯
  28. */
  29. public class Control_MORKIC : IControl
  30. {
  31. #region 单例模式
  32. private static Control_MORKIC _instance;
  33. public static Control_MORKIC Instance
  34. {
  35. get
  36. {
  37. if (_instance == null)
  38. _instance = new Control_MORKIC();
  39. return _instance;
  40. }
  41. }
  42. public Control_MORKIC()
  43. {
  44. }
  45. #endregion
  46. GVL_MORIC mORKD = new GVL_MORIC();
  47. //咖啡机主控程序
  48. private CoffeeMachine coffeeMachine;
  49. //冰淇淋主控程序
  50. private IceCreamMachine iceCreamMachine;
  51. //物料存放位置
  52. private Dictionary<string, PolymerBatching> batchings = new Dictionary<string, PolymerBatching>();
  53. //容器位置
  54. private string holderLoc;
  55. //主料位置
  56. private string mainMaterialLoc;
  57. //子订单ID
  58. private string subOrderId;
  59. /// <summary>
  60. /// 获取乐百机器人的数据
  61. /// </summary>
  62. SignalResult lebai=new SignalResult();
  63. public void ConnectOk()
  64. {
  65. }
  66. public object GetT()
  67. {
  68. return mORKD;
  69. }
  70. ConcurrentQueue<MorkOrderPush> morkOrderPushes = new ConcurrentQueue<MorkOrderPush>();
  71. public void Init()
  72. {
  73. //构建所有商品物料信息
  74. batchings = PolymerBatching.BuildAll();
  75. EventBus.GetInstance().Subscribe<IceCreamEndCook>(IceCreamEndCookHandle);
  76. EventBus.GetInstance().Subscribe<CoffeEndCook>(CoffeEndCookHandle);
  77. System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
  78. //一系列外围基础配置
  79. var com_Coffee = config.AppSettings.Settings["COM_Coffee"].Value;
  80. var baud_Coffee = config.AppSettings.Settings["BAUD_Coffee"].Value;
  81. var com_IceCream = config.AppSettings.Settings["COM_IceCream"].Value;
  82. var baud_IceCream = config.AppSettings.Settings["BAUD_IceCream"].Value;
  83. var iceCreamCXBThreshold = int.Parse(config.AppSettings.Settings["IceCream_CXB_Threshold"].Value);
  84. if (iceCreamCXBThreshold > 0)
  85. {
  86. //设置冰淇淋成型比
  87. MorkIStatus.GetInstance().CXB_Threshold = (byte)iceCreamCXBThreshold;
  88. }
  89. //咖啡机创建
  90. coffeeMachine = new CoffeeMachine(com_Coffee, (BaudRates)Enum.Parse(typeof(BaudRates), baud_Coffee));
  91. //冰淇淋机创建
  92. iceCreamMachine = new IceCreamMachine(com_IceCream, (BaudRates)Enum.Parse(typeof(BaudRates), baud_IceCream));
  93. Main();
  94. ReadData();
  95. ThreadOperate.GetInstance.StartLong(new Action(() =>
  96. {
  97. while (morkOrderPushes.Count > 0)
  98. {
  99. if (morkOrderPushes.TryDequeue(out MorkOrderPush order))
  100. {
  101. //商品类型
  102. GOODS_TYPE currentGoodsType = GOODS_TYPE.NEITHER;
  103. //子订单ID
  104. subOrderId = order.SuborderId;
  105. //遍历物料
  106. foreach (var item in order.GoodBatchings)
  107. {
  108. var res = Json<BatchingInfoPar>.Data.orderMaterialDelivery.BatchingInfo.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  109. if (res != null)
  110. {
  111. //验证商品是咖啡还是冰淇淋
  112. if (ValidateGoodsByBatching(res.BatchingLoc) != GOODS_TYPE.NEITHER)
  113. {
  114. //获取当前物料所属商品类型
  115. currentGoodsType = ValidateGoodsByBatching(res.BatchingLoc);
  116. }
  117. //获取主料和容器位置
  118. switch (batchings[res.BatchingLoc].BatchingClass)
  119. {
  120. case BATCHING_CLASS.HOLDER:
  121. holderLoc = res.BatchingLoc;
  122. break;
  123. case BATCHING_CLASS.MAIN_MATERIAL:
  124. mainMaterialLoc = res.BatchingLoc;
  125. break;
  126. }
  127. }
  128. }
  129. //根据商品类型执行具体制作流程
  130. switch (currentGoodsType)
  131. {
  132. case GOODS_TYPE.COFFEE:
  133. DoCoffee();
  134. break;
  135. case GOODS_TYPE.ICECREAM:
  136. DoIceCream();
  137. break;
  138. }
  139. }
  140. }
  141. Thread.Sleep(1000);
  142. }), "订单制作");
  143. }
  144. public void DataParse<T>(T order)
  145. {
  146. if (order is MorkOrderPush morkOrderPush)
  147. {
  148. morkOrderPushes.Enqueue(morkOrderPush);
  149. }
  150. }
  151. /// <summary>
  152. /// 验证当前是做咖啡还是做冰淇淋
  153. /// </summary>
  154. /// <param name="batchingLoc">物料位置</param>
  155. private GOODS_TYPE ValidateGoodsByBatching(string batchingLoc)
  156. {
  157. if (batchings.ContainsKey(batchingLoc))
  158. return batchings[batchingLoc].GoodsType;
  159. return GOODS_TYPE.NEITHER;
  160. }
  161. private AutoResetEvent are = new AutoResetEvent(false);
  162. /// <summary>
  163. /// 做咖啡
  164. /// </summary>
  165. private void DoCoffee()
  166. {
  167. //订单状态改变:开始制作
  168. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  169. //todo:先调用机器人
  170. ThreadOperate.GetInstance.Start(new Action(() => { LebaiHelper.GetInstance.Scene(10002); }), "调用乐百机器人做咖啡场景");
  171. while (!(lebai.Ok && lebai.Value == 2))
  172. {
  173. Thread.Sleep(5);
  174. }
  175. MessageLog.GetInstance.Show("机器人到达接咖啡口位置");
  176. new MakeCoffeeEvent() { DrinkCode = (DrCoffeeDrinksCode)int.Parse(mainMaterialLoc) }.Publish();
  177. are.WaitOne(1000 * 90);
  178. MessageLog.GetInstance.Show("咖啡机制作咖啡完成");
  179. LebaiHelper.GetInstance.SetValue(101);
  180. //订单状态改变:完成
  181. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK);
  182. }
  183. /// <summary>
  184. /// 做冰淇淋
  185. /// </summary>
  186. private void DoIceCream()
  187. {
  188. //订单状态改变:开始制作
  189. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  190. //todo:先调用机器人
  191. ThreadOperate.GetInstance.Start(new Action(() => { LebaiHelper.GetInstance.Scene(10001); }), "调用乐百机器人做冰淇淋场景");
  192. while (!(lebai.Ok && lebai.Value == 1))
  193. {
  194. Thread.Sleep(5);
  195. }
  196. new DischargeEvent().Publish();
  197. //冰淇淋没有模式切换,强制等待10s
  198. Thread.Sleep(10000);
  199. LebaiHelper.GetInstance.SetValue(100);
  200. //are.WaitOne(100 * 90);
  201. //订单状态改变:完成
  202. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK);
  203. }
  204. private void CoffeEndCookHandle(IEvent @event, EventBus.EventCallBackHandle callBack)
  205. {
  206. are.Set();
  207. }
  208. private void IceCreamEndCookHandle(IEvent @event, EventBus.EventCallBackHandle callBack)
  209. {
  210. //are.Set();
  211. }
  212. public void Main()
  213. {
  214. //咖啡机开启主线程
  215. coffeeMachine.Start();
  216. //冰淇淋机开启主线程
  217. iceCreamMachine.Start();
  218. new ModeSetEvent() { Mode = MORKI_MODE.制冷模式 }.Publish();
  219. //开始心跳刷新,根据咖啡机及冰淇淋机来判断
  220. ThreadOperate.GetInstance.StartLong(new Action(() =>
  221. {
  222. GeneralConfig.Healthy = true;
  223. //GeneralConfig.Healthy =
  224. // MorkIStatus.GetInstance().CanDo &&
  225. // MorkCStatus.GetInstance().CanDo;
  226. Thread.Sleep(100);
  227. }), "MORK-IC心跳刷新");
  228. }
  229. public void ReadData()
  230. {
  231. ThreadOperate.GetInstance.StartLong(new Action(() =>
  232. {
  233. lebai = LebaiHelper.GetInstance.GetValueAsync();
  234. Debug.WriteLine(LebaiHelper.GetInstance.GetClawWdight());
  235. Thread.Sleep(100);
  236. }), "乐百机器人数据读取");
  237. }
  238. public void SimOrder<T>(T simOrder)
  239. {
  240. //ThreadOperate.GetInstance.Start(new Action(() =>
  241. //{
  242. // DoIceCream();
  243. // //DoCoffee();
  244. //}), "aaa");
  245. }
  246. }
  247. }