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.
 
 

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