25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

475 satır
19 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 HBLDevice.ICChip;
  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.Juicer;
  16. using Robotc;
  17. using System.Collections.Concurrent;
  18. using System.Diagnostics;
  19. using BPA.Message.IOT;
  20. using System.Threading.Tasks;
  21. namespace HBLConsole.MORKJC
  22. {
  23. /*
  24. * 冰淇淋咖啡机组合套装
  25. * 物料位置:
  26. * 1:冰淇料
  27. * 2:冰淇淋杯
  28. * 5:咖啡
  29. * 6:咖啡杯
  30. * 9: 茶
  31. * 10: 茶杯
  32. */
  33. public class Control_MORKJC : IControl
  34. {
  35. GVL_MORKJC mORKD = new GVL_MORKJC();
  36. //咖啡机主控程序
  37. private CoffeeMachine coffeeMachine;
  38. ////果汁机主控程序
  39. //private JuicerMachine juicerMachine;
  40. ////单片机主控程序
  41. //private ICChipMachine icchipMachine;
  42. //物料存放位置
  43. private Dictionary<string, PolymerBatching> batchings = new Dictionary<string, PolymerBatching>();
  44. //容器位置
  45. private string holderLoc;
  46. //主料位置
  47. private string mainMaterialLoc;
  48. //子订单ID
  49. private string subOrderId;
  50. private bool enableFunny = false;
  51. private DateTime lastRecvdOrder = DateTime.Now;
  52. private bool working = false;
  53. private SerialPortClient commProxy;
  54. public void ConnectOk()
  55. {
  56. }
  57. ConcurrentQueue<MorkOrderPush> morkOrderPushes = new ConcurrentQueue<MorkOrderPush>();
  58. public void Init()
  59. {
  60. ActionManage.GetInstance.Register(new Action<object>((s) =>
  61. {
  62. if (s is DrCoffeeDrinksCode cf)
  63. {
  64. mainMaterialLoc = ((int)cf).ToString();
  65. DoCoffee();
  66. }
  67. }), "SimCoffee");
  68. //构建所有商品物料信息
  69. batchings = PolymerBatching.BuildAll();
  70. EventBus.GetInstance().Subscribe<CoffeEndCook>(CoffeEndCookHandle);
  71. System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
  72. //一系列外围基础配置
  73. var com_Coffee = config.AppSettings.Settings["COM_Coffee"].Value;
  74. var baud_Coffee = config.AppSettings.Settings["BAUD_Coffee"].Value;
  75. //var com_Juicer = config.AppSettings.Settings["COM_Juicer"].Value;
  76. //var baud_Juicer = config.AppSettings.Settings["BAUD_Juicer"].Value;
  77. //var com_ICChip = config.AppSettings.Settings["COM_ICChip"].Value;
  78. //var baud_ICChip = config.AppSettings.Settings["BAUD_IChip"].Value;
  79. //咖啡机创建
  80. coffeeMachine = new CoffeeMachine(com_Coffee, (BaudRates)Enum.Parse(typeof(BaudRates), baud_Coffee));
  81. ////单片机机创建
  82. //icchipMachine = new ICChipMachine(com_ICChip, (BaudRates)Enum.Parse(typeof(BaudRates), baud_ICChip));
  83. ////果汁机创建
  84. //juicerMachine = new JuicerMachine(com_Juicer, (BaudRates)Enum.Parse(typeof(BaudRates), baud_Juicer));
  85. Main();
  86. ReadData();
  87. ThreadManage.GetInstance.StartLong(new Action(() =>
  88. {
  89. while (morkOrderPushes.Count > 0)
  90. {
  91. if (!LebaiHelper.GetInstance.GetInput())
  92. {
  93. while (enableFunny) { Thread.Sleep(10); }
  94. MessageLog.GetInstance.Show("当前非自嗨模式,允许开工");
  95. working = true;
  96. if (morkOrderPushes.TryDequeue(out MorkOrderPush order))
  97. {
  98. MessageLog.GetInstance.Show($"开始制作订单[{order.SortNum}]");
  99. //Thread.Sleep(5000);
  100. //SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK);
  101. //商品类型
  102. GOODS_TYPE currentGoodsType = GOODS_TYPE.NEITHER;
  103. //子订单ID
  104. subOrderId = order.SuborderId;
  105. //遍历物料
  106. foreach (var item in order.GoodBatchings)
  107. {
  108. var res = Json<BatchingInfoPar>.Data.orderMaterialDelivery.BatchingInfo.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  109. if (res != null)
  110. {
  111. //验证商品是咖啡还是冰淇淋
  112. if (ValidateGoodsByBatching(res.BatchingLoc) != GOODS_TYPE.NEITHER)
  113. {
  114. //获取当前物料所属商品类型
  115. currentGoodsType = ValidateGoodsByBatching(res.BatchingLoc);
  116. }
  117. //获取主料和容器位置
  118. switch (batchings[res.BatchingLoc].BatchingClass)
  119. {
  120. case BATCHING_CLASS.HOLDER:
  121. holderLoc = res.BatchingLoc;
  122. break;
  123. case BATCHING_CLASS.MAIN_MATERIAL:
  124. mainMaterialLoc = res.BatchingLoc;
  125. break;
  126. }
  127. }
  128. }
  129. //根据商品类型执行具体制作流程
  130. switch (currentGoodsType)
  131. {
  132. case GOODS_TYPE.COFFEE:
  133. DoCoffee();
  134. break;
  135. case GOODS_TYPE.JUICE:
  136. DoJuicer();
  137. break;
  138. case GOODS_TYPE.TEA:
  139. DoTea();
  140. break;
  141. case GOODS_TYPE.WATER:
  142. DoWater();
  143. break;
  144. case GOODS_TYPE.NEITHER:
  145. MessageLog.GetInstance.Show("未知的商品类型");
  146. break;
  147. }
  148. }
  149. working = false;
  150. lastRecvdOrder = DateTime.Now;
  151. }
  152. }
  153. Thread.Sleep(1000);
  154. }), "订单制作");
  155. }
  156. public void Main()
  157. {
  158. //咖啡机开启主线程
  159. coffeeMachine.Start();
  160. //开始心跳刷新,根据咖啡机及冰淇淋机来判断
  161. ThreadManage.GetInstance.StartLong(new Action(() =>
  162. {
  163. GeneralConfig.Healthy =
  164. JaKaHelper.GetInstance.IsConnected &&
  165. MorkCStatus.GetInstance().CanDo;
  166. //GeneralConfig.Healthy = true;
  167. Thread.Sleep(100);
  168. }), "MORK-IC心跳刷新");
  169. ThreadManage.GetInstance.Start(new Action(() =>
  170. {
  171. while (!JaKaHelper.GetInstance.IsConnected)
  172. {
  173. Thread.Sleep(10);
  174. }
  175. //LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_欢迎);
  176. }), "MORK-JC欢迎");
  177. }
  178. public void DataParse<T>(T order)
  179. {
  180. if (order is MorkOrderPush morkOrderPush)
  181. {
  182. morkOrderPushes.Enqueue(morkOrderPush);
  183. }
  184. }
  185. /// <summary>
  186. /// 验证当前是做咖啡还是做冰淇淋
  187. /// </summary>
  188. /// <param name="batchingLoc">物料位置</param>
  189. private GOODS_TYPE ValidateGoodsByBatching(string batchingLoc)
  190. {
  191. if (batchings.ContainsKey(batchingLoc))
  192. return batchings[batchingLoc].GoodsType;
  193. return GOODS_TYPE.NEITHER;
  194. }
  195. private AutoResetEvent are = new AutoResetEvent(false);
  196. private void Wait(int value)
  197. {
  198. while (!((JaKaHelper.GetInstance.Get_RobotAO1() == value) && JaKaHelper.GetInstance.GetProgramStatus() == 0))//判断文件是否已经执行结束 且 文件末端变量值==文件名
  199. {
  200. Thread.Sleep(5);
  201. }
  202. }
  203. /// <summary>
  204. /// 做咖啡
  205. /// </summary>
  206. private void DoCoffee()
  207. {
  208. #region 且时且多入场设备程序
  209. int checkeNum = 0;
  210. are.Reset();
  211. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  212. JaKaHelper.GetInstance.Set_RobotAO1(0);
  213. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯);
  214. Wait(int.Parse(JaKaHelper.SENCE_取杯));
  215. Thread.Sleep(500);
  216. MessageLog.GetInstance.Show("咖啡杯取杯完成");
  217. p:
  218. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯检测);
  219. Wait(int.Parse(JaKaHelper.SENCE_取杯检测));
  220. if (!LebaiHelper.GetInstance.GetTcpInput())
  221. {
  222. MessageLog.GetInstance.Show("执行二次取咖啡杯");
  223. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯);
  224. Wait(int.Parse(JaKaHelper.SENCE_取杯));
  225. goto p;
  226. }
  227. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_接咖啡);
  228. Wait(int.Parse(JaKaHelper.SENCE_接咖啡));
  229. new MakeCoffeeEvent() { DrinkCode = (DrCoffeeDrinksCode)int.Parse(mainMaterialLoc) }.Publish(); //接咖啡控制
  230. are.WaitOne(1000 * 180);
  231. Thread.Sleep(3000);
  232. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放咖啡杯);
  233. Wait(int.Parse(JaKaHelper.SENCE_放咖啡杯));
  234. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放杯检测);
  235. Wait(int.Parse(JaKaHelper.SENCE_放杯检测));
  236. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_初始位);
  237. Wait(int.Parse(JaKaHelper.SENCE_初始位));
  238. //订单状态改变:完成
  239. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  240. #endregion
  241. }
  242. /// <summary>
  243. /// 做茶
  244. /// </summary>
  245. private void DoTea()
  246. {
  247. if (JaKaHelper.GetInstance.Get_RobotDI(1))
  248. return;
  249. #region 流程
  250. are.Reset();
  251. JaKaHelper.GetInstance.Set_RobotAO1(0);
  252. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  253. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯);
  254. Wait(int.Parse(JaKaHelper.SENCE_取杯));
  255. Thread.Sleep(500);
  256. MessageLog.GetInstance.Show("茶水杯取杯完成");
  257. p:
  258. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯检测);
  259. Wait(int.Parse(JaKaHelper.SENCE_取杯检测));
  260. if (!LebaiHelper.GetInstance.GetTcpInput())
  261. {
  262. MessageLog.GetInstance.Show("执行二次取茶水杯");
  263. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯);
  264. Wait(int.Parse(JaKaHelper.SENCE_取杯));
  265. goto p;
  266. }
  267. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_接茶);
  268. Wait(int.Parse(JaKaHelper.SENCE_接茶));
  269. MCUSerialHelper.GetInstance.ServoControl(1, 105);
  270. Thread.Sleep(1000);
  271. MCUSerialHelper.GetInstance.ServoControl(1, 130);
  272. Thread.Sleep(1000);
  273. MCUSerialHelper.GetInstance.ServoControl(1, 105);
  274. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_接茶_接水);
  275. Wait(int.Parse(JaKaHelper.SENCE_接茶_接水));
  276. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_接水);
  277. Wait(int.Parse(JaKaHelper.SENCE_接水));
  278. MCUSerialHelper.GetInstance.OutputControl(2, true);
  279. Thread.Sleep(1000);
  280. MCUSerialHelper.GetInstance.OutputControl(2, false);
  281. Thread.Sleep(1000);
  282. MCUSerialHelper.GetInstance.OutputControl(3, true);
  283. Thread.Sleep(1000);
  284. MCUSerialHelper.GetInstance.OutputControl(3, false);
  285. Thread.Sleep(3000);
  286. are.WaitOne(1000 * 180);
  287. //添加控制接水机构程序
  288. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放茶水杯);
  289. Wait(int.Parse(JaKaHelper.SENCE_放茶水杯));
  290. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放杯检测);
  291. Wait(int.Parse(JaKaHelper.SENCE_放杯检测));
  292. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_初始位);
  293. Wait(int.Parse(JaKaHelper.SENCE_初始位));
  294. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  295. #endregion
  296. }
  297. /// <summary>
  298. /// 接开水
  299. /// </summary>
  300. private void DoWater()
  301. {
  302. if (JaKaHelper.GetInstance.Get_RobotDI(1))
  303. return;
  304. #region 流程
  305. are.Reset();
  306. JaKaHelper.GetInstance.Set_RobotAO1(0);
  307. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  308. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯);
  309. Wait(int.Parse(JaKaHelper.SENCE_取杯));
  310. Thread.Sleep(500);
  311. MessageLog.GetInstance.Show("水杯取杯完成");
  312. p:
  313. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯检测);
  314. Wait(int.Parse(JaKaHelper.SENCE_取杯检测));
  315. if (!LebaiHelper.GetInstance.GetTcpInput())
  316. {
  317. MessageLog.GetInstance.Show("执行二次取水杯");
  318. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯);
  319. Wait(int.Parse(JaKaHelper.SENCE_取杯));
  320. goto p;
  321. }
  322. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_接水);
  323. Wait(int.Parse(JaKaHelper.SENCE_接水));
  324. are.WaitOne(1000 * 180);
  325. MCUSerialHelper.GetInstance.OutputControl(2, true);
  326. Thread.Sleep(1000);
  327. MCUSerialHelper.GetInstance.OutputControl(2, false);
  328. Thread.Sleep(1000);
  329. MCUSerialHelper.GetInstance.OutputControl(3, true);
  330. Thread.Sleep(1000);
  331. MCUSerialHelper.GetInstance.OutputControl(3, false);
  332. Thread.Sleep(3000);
  333. //添加控制接水机构程序
  334. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放水杯);
  335. Wait(int.Parse(JaKaHelper.SENCE_放水杯));
  336. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放杯检测);
  337. Wait(int.Parse(JaKaHelper.SENCE_放杯检测));
  338. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_初始位);
  339. Wait(int.Parse(JaKaHelper.SENCE_初始位));
  340. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  341. #endregion
  342. }
  343. /// <summary>
  344. /// 做果汁
  345. /// </summary>
  346. private void DoJuicer()
  347. {
  348. if (JaKaHelper.GetInstance.Get_RobotDI(1))
  349. return;
  350. #region 入场设备程序
  351. are.Reset();
  352. JaKaHelper.GetInstance.Set_RobotAO1(0);
  353. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  354. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯);
  355. Wait(int.Parse(JaKaHelper.SENCE_取杯));
  356. MessageLog.GetInstance.Show("果汁杯取杯完成");
  357. //new TakeCupEvent() { Cup = IC_CUP.CUP_ICECREAM }.Publish();//落碗控制
  358. Thread.Sleep(10);
  359. p:
  360. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯检测);
  361. Wait(int.Parse(JaKaHelper.SENCE_取杯检测));
  362. if (!JaKaHelper.GetInstance.Get_RobotDI(2))
  363. {
  364. MessageLog.GetInstance.Show("执行二次取果汁杯");
  365. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯);
  366. Wait(int.Parse(JaKaHelper.SENCE_取杯));
  367. MessageLog.GetInstance.Show("二次果汁杯取杯完成");
  368. goto p;
  369. }
  370. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_接果汁1);
  371. Wait(int.Parse(JaKaHelper.SENCE_接果汁1));
  372. are.WaitOne(1000 * 180);
  373. Thread.Sleep(3000);//等待几秒,饮料口延迟 防止有饮料滴在杯口
  374. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放果汁杯1);
  375. Wait(int.Parse(JaKaHelper.SENCE_放果汁杯1));
  376. //订单状态改变:完成
  377. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放杯检测);
  378. Wait(int.Parse(JaKaHelper.SENCE_放杯检测));
  379. JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_初始位);
  380. Wait(int.Parse(JaKaHelper.SENCE_初始位));
  381. SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  382. #endregion
  383. }
  384. private void CoffeEndCookHandle(IEvent @event, EventBus.EventCallBackHandle callBack)
  385. {
  386. are.Set();
  387. }
  388. public void ReadData()
  389. {
  390. ThreadManage.GetInstance.StartLong(new Action(() =>
  391. {
  392. Thread.Sleep(100);
  393. }), "机器人数据读取", true);
  394. }
  395. public void SimOrder<T>(T simOrder)
  396. {
  397. }
  398. /// <summary>
  399. /// IOT 广播消息命令
  400. /// </summary>
  401. public void IotBroadcast<T>(T broadcast)
  402. {
  403. if (broadcast != null && broadcast is IOTCommandModel iOTCommand)
  404. {
  405. switch (iOTCommand.CommandName)
  406. {
  407. case 0://控制类
  408. if (iOTCommand.CommandValue != null)
  409. {
  410. if (iOTCommand.CommandValue.ContainsKey("SimOrder"))
  411. {
  412. SimOrder(new SimOrderData { NoodleLoc = 1, BowlLoc = 10 });
  413. }
  414. }
  415. break;
  416. case 1://设置属性
  417. break;
  418. case 2://通知消息
  419. break;
  420. default:
  421. break;
  422. }
  423. }
  424. }
  425. }
  426. }