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.
 
 

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