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.
 
 

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