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.
 
 

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