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

312 行
11 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. using BPA.Message.IOT;
  20. namespace HBLConsole.MORKIC
  21. {
  22. /*
  23. * 冰淇淋咖啡机组合套装
  24. * 物料位置:
  25. * 1:冰淇料
  26. * 2:冰淇淋杯
  27. * 5:咖啡
  28. * 6:咖啡杯
  29. */
  30. public class Control_MORKIC : IControl
  31. {
  32. GVL_MORIC mORKD = new GVL_MORIC();
  33. //咖啡机主控程序
  34. private CoffeeMachine coffeeMachine;
  35. //冰淇淋主控程序
  36. private IceCreamMachine iceCreamMachine;
  37. //物料存放位置
  38. private Dictionary<string, PolymerBatching> batchings = new Dictionary<string, PolymerBatching>();
  39. //容器位置
  40. private string holderLoc;
  41. //主料位置
  42. private string mainMaterialLoc;
  43. //子订单ID
  44. private string subOrderId;
  45. /// <summary>
  46. /// 获取乐百机器人的数据
  47. /// </summary>
  48. SignalResult lebai = new SignalResult();
  49. public void ConnectOk()
  50. {
  51. }
  52. ConcurrentQueue<MorkOrderPush> morkOrderPushes = new ConcurrentQueue<MorkOrderPush>();
  53. public void Init()
  54. {
  55. //构建所有商品物料信息
  56. batchings = PolymerBatching.BuildAll();
  57. EventBus.GetInstance().Subscribe<IceCreamEndCook>(IceCreamEndCookHandle);
  58. EventBus.GetInstance().Subscribe<CoffeEndCook>(CoffeEndCookHandle);
  59. System.Configuration.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. ThreadOperate.GetInstance.StartLong(new Action(() =>
  78. {
  79. while (morkOrderPushes.Count > 0)
  80. {
  81. if (morkOrderPushes.TryDequeue(out MorkOrderPush order))
  82. {
  83. //Thread.Sleep(5000);
  84. //SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK);
  85. //商品类型
  86. GOODS_TYPE currentGoodsType = GOODS_TYPE.NEITHER;
  87. //子订单ID
  88. subOrderId = order.SuborderId;
  89. //遍历物料
  90. foreach (var item in order.GoodBatchings)
  91. {
  92. var res = Json<BatchingInfoPar>.Data.orderMaterialDelivery.BatchingInfo.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  93. if (res != null)
  94. {
  95. //验证商品是咖啡还是冰淇淋
  96. if (ValidateGoodsByBatching(res.BatchingLoc) != GOODS_TYPE.NEITHER)
  97. {
  98. //获取当前物料所属商品类型
  99. currentGoodsType = ValidateGoodsByBatching(res.BatchingLoc);
  100. }
  101. //获取主料和容器位置
  102. switch (batchings[res.BatchingLoc].BatchingClass)
  103. {
  104. case BATCHING_CLASS.HOLDER:
  105. holderLoc = res.BatchingLoc;
  106. break;
  107. case BATCHING_CLASS.MAIN_MATERIAL:
  108. mainMaterialLoc = res.BatchingLoc;
  109. break;
  110. }
  111. }
  112. }
  113. //根据商品类型执行具体制作流程
  114. switch (currentGoodsType)
  115. {
  116. case GOODS_TYPE.COFFEE:
  117. DoCoffee();
  118. break;
  119. case GOODS_TYPE.ICECREAM:
  120. DoIceCream();
  121. break;
  122. }
  123. }
  124. }
  125. Thread.Sleep(1000);
  126. }), "订单制作");
  127. }
  128. public void DataParse<T>(T order)
  129. {
  130. if (order is MorkOrderPush morkOrderPush)
  131. {
  132. morkOrderPushes.Enqueue(morkOrderPush);
  133. }
  134. }
  135. /// <summary>
  136. /// 验证当前是做咖啡还是做冰淇淋
  137. /// </summary>
  138. /// <param name="batchingLoc">物料位置</param>
  139. private GOODS_TYPE ValidateGoodsByBatching(string batchingLoc)
  140. {
  141. if (batchings.ContainsKey(batchingLoc))
  142. return batchings[batchingLoc].GoodsType;
  143. return GOODS_TYPE.NEITHER;
  144. }
  145. private AutoResetEvent are = new AutoResetEvent(false);
  146. /// <summary>
  147. /// 做咖啡
  148. /// </summary>
  149. private void DoCoffee()
  150. {
  151. //订单状态改变:开始制作
  152. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  153. //todo:先调用机器人
  154. ThreadOperate.GetInstance.Start(new Action(() => { LebaiHelper.GetInstance.Scene(10002); }), "调用乐百机器人做咖啡场景");
  155. while (!(lebai.Ok && lebai.Value == 1))
  156. {
  157. Thread.Sleep(5);
  158. }
  159. //todo:咖啡杯下杯
  160. MessageLog.GetInstance.Show("咖啡杯取杯完成");
  161. LebaiHelper.GetInstance.SetValue(100);
  162. while (!(lebai.Ok && lebai.Value == 2))
  163. {
  164. Thread.Sleep(5);
  165. }
  166. MessageLog.GetInstance.Show("机器人到达接咖啡口位置");
  167. new MakeCoffeeEvent() { DrinkCode = (DrCoffeeDrinksCode)int.Parse(mainMaterialLoc) }.Publish();
  168. are.WaitOne(1000 * 180);
  169. MessageLog.GetInstance.Show("咖啡机制作咖啡完成");
  170. LebaiHelper.GetInstance.SetValue(101);
  171. while (!(lebai.Ok && lebai.Value == 3))
  172. {
  173. Thread.Sleep(5);
  174. }
  175. MessageLog.GetInstance.Show("机器人到达接咖啡口位置");
  176. //订单状态改变:完成
  177. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK);
  178. }
  179. /// <summary>
  180. /// 做冰淇淋
  181. /// </summary>
  182. private void DoIceCream()
  183. {
  184. //订单状态改变:开始制作
  185. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  186. //todo:先调用机器人
  187. ThreadOperate.GetInstance.Start(new Action(() => { LebaiHelper.GetInstance.Scene(10001); }), "调用乐百机器人做冰淇淋场景");
  188. while (!(lebai.Ok && lebai.Value == 1))
  189. {
  190. Thread.Sleep(5);
  191. }
  192. new DischargeEvent().Publish();
  193. //冰淇淋没有模式切换,强制等待10s
  194. Thread.Sleep(10000);
  195. LebaiHelper.GetInstance.SetValue(100);
  196. //are.WaitOne(100 * 90);
  197. //订单状态改变:完成
  198. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK);
  199. }
  200. private void CoffeEndCookHandle(IEvent @event, EventBus.EventCallBackHandle callBack)
  201. {
  202. are.Set();
  203. }
  204. private void IceCreamEndCookHandle(IEvent @event, EventBus.EventCallBackHandle callBack)
  205. {
  206. //are.Set();
  207. }
  208. public void Main()
  209. {
  210. //咖啡机开启主线程
  211. coffeeMachine.Start();
  212. //冰淇淋机开启主线程
  213. iceCreamMachine.Start();
  214. new ModeSetEvent() { Mode = MORKI_MODE.制冷模式 }.Publish();
  215. //开始心跳刷新,根据咖啡机及冰淇淋机来判断
  216. ThreadOperate.GetInstance.StartLong(new Action(() =>
  217. {
  218. //GeneralConfig.Healthy = true;
  219. //GeneralConfig.Healthy =
  220. // LebaiHelper.GetInstance.IsConnected &&
  221. // MorkIStatus.GetInstance().CanDo &&
  222. // MorkCStatus.GetInstance().CanDo;
  223. GeneralConfig.Healthy =
  224. LebaiHelper.GetInstance.IsConnected &&
  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. LebaiHelper.GetInstance.GetRobotModeStatus();
  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. /// <summary>
  247. /// IOT 广播消息命令
  248. /// </summary>
  249. public void IotBroadcast<T>(T broadcast)
  250. {
  251. if (broadcast != null && broadcast is IOTCommandModel iOTCommand)
  252. {
  253. switch (iOTCommand.CommandName)
  254. {
  255. case 0://控制类
  256. if (iOTCommand.CommandValue != null)
  257. {
  258. if (iOTCommand.CommandValue.ContainsKey("SimOrder"))
  259. {
  260. SimOrder(new SimOrderData { NoodleLoc = 1, BowlLoc = 10 });
  261. }
  262. }
  263. break;
  264. case 1://设置属性
  265. break;
  266. case 2://通知消息
  267. break;
  268. default:
  269. break;
  270. }
  271. }
  272. }
  273. }
  274. }