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.
 
 

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