终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

786 строки
33 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Collections.Concurrent;
  7. using System.Diagnostics;
  8. using System.Threading.Tasks;
  9. using BPASmartClient.Device;
  10. using BPA.Message.Enum;
  11. using BPA.Message;
  12. using BPASmartClient.Helper;
  13. using BPASmartClient.Model.咖啡机.Enum;
  14. using BPASmartClient.Model;
  15. using BPASmartClient.EventBus;
  16. using static BPASmartClient.EventBus.EventBus;
  17. using BPASmartClient.Model.PLC;
  18. using BPASmartClient.Model.单片机;
  19. using BPASmartClient.Message;
  20. namespace BPASmartClient.MorktJAKAJC
  21. {
  22. public class Control_MORKJC : BaseDevice
  23. {
  24. GVL_MORKJC mORKD = new GVL_MORKJC();
  25. //物料存放位置
  26. private Dictionary<string, PolymerBatching> batchings = new Dictionary<string, PolymerBatching>();
  27. //容器位置
  28. private string holderLoc;
  29. //主料位置
  30. private string mainMaterialLoc;
  31. //子订单ID
  32. private string subOrderId;
  33. private bool enableFunny = false;
  34. private DateTime lastRecvdOrder = DateTime.Now;
  35. private bool working = false;
  36. /// <summary>
  37. /// 果汁机做法,true:热饮,false:冷饮
  38. /// </summary>
  39. private bool GuMake = false;
  40. private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
  41. {
  42. EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent() { Status = oRDER_STATUS, SubOrderId = subid, deviceClientType = DeviceType });
  43. }
  44. //private SerialPortClient commProxy;
  45. public void ConnectOk()
  46. {
  47. }
  48. ConcurrentQueue<MorkOrderPush> morkOrderPushes = new ConcurrentQueue<MorkOrderPush>();
  49. public void Init()
  50. {
  51. ActionManage.GetInstance.Register(new Action<object>((s) =>
  52. {
  53. if (s is DrCoffeeDrinksCode cf)
  54. {
  55. mainMaterialLoc = ((int)cf).ToString();
  56. DoCoffee();
  57. }
  58. }), "SimCoffee");
  59. //构建所有商品物料信息
  60. batchings = PolymerBatching.BuildAll();
  61. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CoffeEndCookEvent>(DeviceId, DRCoffee_CoffeEndCookEventHandle);
  62. Main();
  63. ReadData();
  64. ThreadManage.GetInstance().StartLong(new Action(() =>
  65. {
  66. while (morkOrderPushes.Count > 0)
  67. {
  68. while (enableFunny) { Thread.Sleep(10); }
  69. DeviceProcessLogShow("当前非自嗨模式,允许开工");
  70. working = true;
  71. if (morkOrderPushes.TryDequeue(out MorkOrderPush order))
  72. {
  73. DeviceProcessLogShow($"开始制作订单[{order.SortNum}]");
  74. //商品类型
  75. GOODS_TYPE currentGoodsType = GOODS_TYPE.NEITHER;
  76. //子订单ID
  77. subOrderId = order.SuborderId;
  78. //遍历物料
  79. foreach (var item in order.GoodBatchings)
  80. {
  81. var res = Json<BatchingInfoPar>.Data.orderMaterialDelivery.BatchingInfo.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  82. if (res != null)
  83. {
  84. //获取主料和容器位置
  85. switch (batchings[res.BatchingLoc].BatchingClass)
  86. {
  87. case BATCHING_CLASS.HOLDER:
  88. holderLoc = res.BatchingLoc;
  89. break;
  90. case BATCHING_CLASS.MAIN_MATERIAL:
  91. // mainMaterialLoc ="1";
  92. mainMaterialLoc = res.BatchingLoc;
  93. //验证商品是咖啡还是冰淇淋
  94. if (ValidateGoodsByBatching(res.BatchingLoc) != GOODS_TYPE.NEITHER)
  95. {
  96. //获取当前物料所属商品类型
  97. currentGoodsType = ValidateGoodsByBatching(res.BatchingLoc);
  98. }
  99. break;
  100. }
  101. }
  102. }
  103. //根据商品类型执行具体制作流程
  104. switch (currentGoodsType)
  105. {
  106. case GOODS_TYPE.COFFEE:
  107. DoCoffee();
  108. break;
  109. case GOODS_TYPE.JUICE:
  110. GuMake = order.MakeID == "2";
  111. DoJuicer();
  112. break;
  113. case GOODS_TYPE.TEA:
  114. DoTea();
  115. break;
  116. case GOODS_TYPE.WATER:
  117. DoWater();
  118. break;
  119. case GOODS_TYPE.NEITHER:
  120. DeviceProcessLogShow("未知的商品类型");
  121. break;
  122. }
  123. }
  124. working = false;
  125. lastRecvdOrder = DateTime.Now;
  126. }
  127. Thread.Sleep(1000);
  128. }), "订单制作");
  129. }
  130. public void Main()
  131. {
  132. //开始心跳刷新,根据咖啡机及冰淇淋机来判断
  133. ThreadManage.GetInstance().StartLong(new Action(() =>
  134. {
  135. //IsHealth =
  136. // Write.IsConnected &&
  137. // MorkCStatus.GetInstance().CanDo &&
  138. // JuicerHelper.GetInstance.IsOpen &&
  139. // MCUSerialHelper.GetInstance.IsOpen;
  140. //GeneralConfig.Healthy = true;
  141. Thread.Sleep(100);
  142. }), "MORK-IC心跳刷新");
  143. }
  144. public void DataParse<T>(T order)
  145. {
  146. if (order is MorkOrderPush morkOrderPush)
  147. {
  148. morkOrderPushes.Enqueue(morkOrderPush);
  149. }
  150. }
  151. /// <summary>
  152. /// 验证当前是做咖啡还是做冰淇淋
  153. /// </summary>
  154. /// <param name="batchingLoc">物料位置</param>
  155. private GOODS_TYPE ValidateGoodsByBatching(string batchingLoc)
  156. {
  157. if (batchings.ContainsKey(batchingLoc))
  158. return batchings[batchingLoc].GoodsType;
  159. return GOODS_TYPE.NEITHER;
  160. }
  161. private AutoResetEvent are = new AutoResetEvent(false);
  162. private T GetStatus<T>(string key)
  163. {
  164. if (peripheralStatus.ContainsKey(key))
  165. {
  166. if (peripheralStatus[key] != null)
  167. {
  168. return (T)(peripheralStatus[key]);
  169. }
  170. }
  171. return default;
  172. }
  173. private void Wait(int value)
  174. {
  175. while (!((GetStatus<int>("Get_RobotAO1") == value) && GetStatus<int>("GetProgramStatus") == 0))//判断文件是否已经执行结束 且 文件末端变量值==文件名
  176. {
  177. Thread.Sleep(5);
  178. }
  179. }
  180. int[] devStatusBy = new int[2] { 0, 0 };
  181. /// <summary>
  182. /// 传感器的输入信号 0:无意义 1:有信号 2:无信号 3:信号不正确
  183. /// </summary>
  184. int bSensorInput;
  185. /// <summary>
  186. /// 延迟的超时时间
  187. /// </summary>
  188. DateTime delayTimeOut;
  189. /// <summary>
  190. /// 做咖啡
  191. /// </summary>
  192. private void DoCoffee()
  193. {
  194. #region 接咖啡流程
  195. are.Reset();
  196. OrderChange(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  197. int resultTakeCup = takeCup();
  198. if (resultTakeCup == 1)
  199. {
  200. DeviceProcessLogShow("咖啡杯取杯完成");
  201. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接咖啡 }.Publish();
  202. Wait(int.Parse(JakaModel.SENCE_接咖啡));
  203. new DRCoffee_MakeCoffeeEvent() { DeviceId = DeviceId, DrinkCode = (DrCoffeeDrinksCode)int.Parse(mainMaterialLoc) }.Publish(); //接咖啡控制 //DrCoffeeDrinksCode.热水
  204. are.WaitOne(1000 * 180);
  205. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放咖啡杯 }.Publish();
  206. Wait(int.Parse(JakaModel.SENCE_放咖啡杯));
  207. int resultputCup = putCup();
  208. if (resultputCup == 1)
  209. {
  210. //订单状态改变:完成
  211. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  212. Wait(int.Parse(JakaModel.SENCE_初始位));
  213. OrderChange(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  214. }
  215. else
  216. {
  217. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  218. Wait(int.Parse(JakaModel.SENCE_初始位));
  219. }
  220. }
  221. else if (resultTakeCup == 2 || resultTakeCup == 3)
  222. {
  223. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  224. Wait(int.Parse(JakaModel.SENCE_初始位));
  225. }
  226. #endregion
  227. }
  228. /// <summary>
  229. /// 做茶
  230. /// </summary>
  231. private void DoTea()
  232. {
  233. #region 接茶流程
  234. OrderChange(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  235. int resultTakeCup = takeCup();
  236. if (resultTakeCup == 1)
  237. {
  238. DeviceProcessLogShow("取茶杯完成");
  239. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接茶 }.Publish();
  240. Wait(int.Parse(JakaModel.SENCE_接茶));
  241. new WriteMcu() { TagName = "ServoControl", Address = "1", Value = 105 }.Publish();
  242. Thread.Sleep(1000);
  243. new WriteMcu() { TagName = "ServoControl", Address = "1", Value = 130 }.Publish();
  244. Thread.Sleep(1000);
  245. new WriteMcu() { TagName = "ServoControl", Address = "1", Value = 105 }.Publish();
  246. Thread.Sleep(3000);
  247. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接茶_接水 }.Publish();
  248. Wait(int.Parse(JakaModel.SENCE_接茶_接水));
  249. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "4" }.Publish();
  250. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "3" }.Publish();
  251. Thread.Sleep(100);
  252. new WriteMcu() { TagName = "OutputControl", Value = true, Address = "3" }.Publish();
  253. Thread.Sleep(3000);
  254. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "3" }.Publish();
  255. Thread.Sleep(100);
  256. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "4" }.Publish();
  257. Thread.Sleep(100);
  258. new WriteMcu() { TagName = "OutputControl", Value = true, Address = "4" }.Publish();
  259. Thread.Sleep(500);
  260. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "4" }.Publish();
  261. Thread.Sleep(50000);
  262. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放茶水杯 }.Publish();
  263. Wait(int.Parse(JakaModel.SENCE_放茶水杯));
  264. int resultputCup = putCup();
  265. if (resultputCup == 1)
  266. {
  267. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  268. Wait(int.Parse(JakaModel.SENCE_初始位));
  269. OrderChange(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  270. }
  271. else
  272. {
  273. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  274. Wait(int.Parse(JakaModel.SENCE_初始位));
  275. }
  276. }
  277. else if (resultTakeCup == 2 || resultTakeCup == 3)
  278. {
  279. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  280. Wait(int.Parse(JakaModel.SENCE_初始位));
  281. }
  282. #endregion
  283. }
  284. /// <summary>
  285. /// 接开水
  286. /// </summary>
  287. private void DoWater()
  288. {
  289. #region 接水流程
  290. OrderChange(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  291. int resultTakeCup = takeCup();
  292. if (resultTakeCup == 1)
  293. {
  294. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接水 }.Publish();
  295. Wait(int.Parse(JakaModel.SENCE_接水));
  296. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "4" }.Publish();
  297. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "3" }.Publish();
  298. Thread.Sleep(100);
  299. new WriteMcu() { TagName = "OutputControl", Value = true, Address = "3" }.Publish();
  300. Thread.Sleep(3000);
  301. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "3" }.Publish();
  302. Thread.Sleep(100);
  303. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "4" }.Publish();
  304. Thread.Sleep(100);
  305. new WriteMcu() { TagName = "OutputControl", Value = true, Address = "4" }.Publish();
  306. Thread.Sleep(500);
  307. new WriteMcu() { TagName = "OutputControl", Value = false, Address = "4" }.Publish();
  308. Thread.Sleep(50000);
  309. //添加控制接水机构程序
  310. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放茶水杯 }.Publish();
  311. Wait(int.Parse(JakaModel.SENCE_放茶水杯));
  312. int resultputCup = putCup();
  313. if (resultputCup == 1)
  314. {
  315. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  316. Wait(int.Parse(JakaModel.SENCE_初始位));
  317. OrderChange(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  318. }
  319. else
  320. {
  321. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  322. Wait(int.Parse(JakaModel.SENCE_初始位));
  323. }
  324. }
  325. else if (resultTakeCup == 2 || resultTakeCup == 3)
  326. {
  327. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  328. Wait(int.Parse(JakaModel.SENCE_初始位));
  329. }
  330. #endregion
  331. }
  332. /// <summary>
  333. /// 果汁机控制信号
  334. /// </summary>
  335. private byte JuicerNum;
  336. /// <summary>
  337. /// 做果汁
  338. /// </summary>
  339. private void DoJuicer()
  340. {
  341. #region 接果汁流程
  342. are.Reset();
  343. OrderChange(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  344. int resultTakeCup = takeCup();
  345. if (resultTakeCup == 1)
  346. {
  347. int JuicerNum1 = int.Parse(mainMaterialLoc);
  348. switch (JuicerNum1)
  349. {
  350. case 52:
  351. if (GuMake)
  352. {
  353. JuicerNum = 0x00;
  354. }
  355. else
  356. {
  357. JuicerNum = 0x01;
  358. }
  359. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接果汁1 }.Publish();
  360. Wait(int.Parse(JakaModel.SENCE_接果汁1));
  361. break;
  362. case 53:
  363. if (GuMake)
  364. {
  365. JuicerNum = 0x02;
  366. }
  367. else
  368. {
  369. JuicerNum = 0x03;
  370. }
  371. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接果汁2 }.Publish();
  372. Wait(int.Parse(JakaModel.SENCE_接果汁2));
  373. break;
  374. case 54:
  375. if (GuMake)
  376. {
  377. JuicerNum = 0x04;
  378. }
  379. else
  380. {
  381. JuicerNum = 0x05;
  382. }
  383. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接果汁3 }.Publish();
  384. Wait(int.Parse(JakaModel.SENCE_接果汁3));
  385. break;
  386. case 55:
  387. if (GuMake)
  388. {
  389. JuicerNum = 0x06;
  390. }
  391. else
  392. {
  393. JuicerNum = 0x07;
  394. }
  395. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接果汁4 }.Publish();
  396. Wait(int.Parse(JakaModel.SENCE_接果汁4));
  397. break;
  398. default:
  399. JuicerNum = 0x00;
  400. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_接果汁1 }.Publish();
  401. Wait(int.Parse(JakaModel.SENCE_接果汁1));
  402. break;
  403. }
  404. var devStatus = GetStatus<int[]>("GetDeviceStatus");
  405. var devStatus1 = Convert.ToString(devStatus[0], 2);
  406. var devStatus2 = devStatus[1];
  407. if (devStatus1.IndexOf("0") == 1 && devStatus2 == 0)
  408. {
  409. if (sensor_Sign(1) == 1)
  410. {
  411. new WriteJuicer() { Value = JuicerNum }.Publish();
  412. }
  413. Thread.Sleep(100);
  414. devStatusBy = GetStatus<int[]>("GetDeviceStatus");
  415. while (!(devStatusBy[1] == 0))
  416. {
  417. Thread.Sleep(100);
  418. devStatusBy = GetStatus<int[]>("GetDeviceStatus");
  419. while (devStatusBy.Length != 2)
  420. {
  421. Thread.Sleep(100);
  422. devStatusBy = GetStatus<int[]>("GetDeviceStatus");
  423. }
  424. }
  425. devStatusBy = GetStatus<int[]>("GetDeviceStatus");
  426. Thread.Sleep(5000);
  427. switch (JuicerNum1)
  428. {
  429. case 52:
  430. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯1 }.Publish();
  431. Wait(int.Parse(JakaModel.SENCE_放果汁杯1));
  432. break;
  433. case 53:
  434. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯2 }.Publish();
  435. Wait(int.Parse(JakaModel.SENCE_放果汁杯2));
  436. break;
  437. case 54:
  438. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯3 }.Publish();
  439. Wait(int.Parse(JakaModel.SENCE_放果汁杯3));
  440. break;
  441. case 55:
  442. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯4 }.Publish();
  443. Wait(int.Parse(JakaModel.SENCE_放果汁杯4));
  444. break;
  445. default:
  446. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯1 }.Publish();
  447. Wait(int.Parse(JakaModel.SENCE_放果汁杯1));
  448. break;
  449. }
  450. int resultputCup = putCup();
  451. if (resultputCup == 1)
  452. {
  453. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  454. Wait(int.Parse(JakaModel.SENCE_初始位));
  455. OrderChange(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  456. }
  457. else
  458. {
  459. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  460. Wait(int.Parse(JakaModel.SENCE_初始位));
  461. }
  462. }
  463. else
  464. {
  465. switch (JuicerNum1)
  466. {
  467. case 52:
  468. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯1 }.Publish();
  469. Wait(int.Parse(JakaModel.SENCE_放果汁杯1));
  470. break;
  471. case 53:
  472. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯2 }.Publish();
  473. Wait(int.Parse(JakaModel.SENCE_放果汁杯2));
  474. break;
  475. case 54:
  476. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯3 }.Publish();
  477. Wait(int.Parse(JakaModel.SENCE_放果汁杯3));
  478. break;
  479. case 55:
  480. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯4 }.Publish();
  481. Wait(int.Parse(JakaModel.SENCE_放果汁杯4));
  482. break;
  483. default:
  484. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放果汁杯1 }.Publish();
  485. Wait(int.Parse(JakaModel.SENCE_放果汁杯1));
  486. break;
  487. }
  488. int resultputCup = putCup();
  489. if (resultputCup == 1)
  490. {
  491. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  492. Wait(int.Parse(JakaModel.SENCE_初始位));
  493. OrderChange(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  494. }
  495. else
  496. {
  497. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  498. Wait(int.Parse(JakaModel.SENCE_初始位));
  499. }
  500. }
  501. }
  502. else if (resultTakeCup == 2 || resultTakeCup == 3)
  503. {
  504. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  505. Wait(int.Parse(JakaModel.SENCE_初始位));
  506. }
  507. #endregion
  508. }
  509. private int getCup_cnt;
  510. /// <summary>
  511. /// 取杯流程
  512. /// </summary>
  513. /// <returns>0:无意义,1:取杯成功 2:机构有杯子,取杯失败 3:机构没有杯子</returns>
  514. private int takeCup()
  515. {
  516. try
  517. {
  518. getCup_cnt = 0;//取杯次数复位
  519. new WriteJaka() { TagName = "Set_RobotAO1", Value = 0 }.Publish();
  520. Wait(0);
  521. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  522. Wait(int.Parse(JakaModel.SENCE_初始位));
  523. new WriteMcu() { TagName = "OutputControl", Address = "1", Value = true }.Publish();
  524. Thread.Sleep(10);
  525. new WriteMcu() { TagName = "OutputControl", Address = "1", Value = true }.Publish();
  526. new WriteJaka() { TagName = "Set_RobotAO1", Value = 0 }.Publish();
  527. Wait(0);
  528. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯 }.Publish();
  529. Wait(int.Parse(JakaModel.SENCE_取杯));
  530. bSensorInput = sensor_Sign(1);
  531. if (bSensorInput == 2)
  532. {
  533. Thread.Sleep(100);
  534. new WriteMcu() { TagName = "OutputControl", Address = "1", Value = false }.Publish();
  535. new WriteMcu() { TagName = "OutputControl", Address = "1", Value = false }.Publish();
  536. Thread.Sleep(100);
  537. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯检测 }.Publish();
  538. Wait(int.Parse(JakaModel.SENCE_取杯检测));
  539. DeviceProcessLogShow("落杯器没有纸杯了");
  540. return 3;
  541. }
  542. if (bSensorInput == 1)
  543. {
  544. Thread.Sleep(100);
  545. new WriteMcu() { TagName = "OutputControl", Address = "1", Value = false }.Publish();
  546. new WriteMcu() { TagName = "OutputControl", Address = "1", Value = false }.Publish();
  547. Thread.Sleep(100);
  548. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯检测 }.Publish();
  549. Wait(int.Parse(JakaModel.SENCE_取杯检测));
  550. bSensorInput = sensor_Sign(1);
  551. while (getCup_cnt < 4 && (bSensorInput == 2 || bSensorInput == 3))
  552. {
  553. DeviceProcessLogShow($"第{getCup_cnt}次取杯失败");
  554. Thread.Sleep(100);
  555. new WriteMcu() { TagName = "OutputControl", Address = "1", Value = true }.Publish();
  556. new WriteMcu() { TagName = "OutputControl", Address = "1", Value = true }.Publish();
  557. Thread.Sleep(100);
  558. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯 }.Publish();
  559. Wait(int.Parse(JakaModel.SENCE_取杯));
  560. getCup_cnt = getCup_cnt + 1;
  561. Thread.Sleep(100);
  562. new WriteMcu() { TagName = "OutputControl", Address = "1", Value = false }.Publish();
  563. new WriteMcu() { TagName = "OutputControl", Address = "1", Value = false }.Publish();
  564. Thread.Sleep(100);
  565. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_取杯检测 }.Publish();
  566. Wait(int.Parse(JakaModel.SENCE_取杯检测));
  567. bSensorInput = sensor_Sign(1);
  568. }
  569. if (bSensorInput == 1)
  570. {
  571. return 1;
  572. }
  573. else
  574. {
  575. return 2;
  576. }
  577. }
  578. return 1;
  579. }
  580. catch (Exception ex)
  581. {
  582. MessageLog.GetInstance.ShowEx(ex.ToString());
  583. return 0;
  584. }
  585. }
  586. /// <summary>
  587. /// 放杯
  588. /// </summary>
  589. /// <returns>0:无意义 1:执行成功 2:执行失败(传感器还有信号)</returns>
  590. private int putCup()
  591. {
  592. try
  593. {
  594. while (checkCup() == 2)
  595. {
  596. Thread.Sleep(100);
  597. }
  598. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放杯 }.Publish();
  599. Wait(int.Parse(JakaModel.SENCE_放杯));
  600. new WriteMcu() { TagName = "OutputControl", Address = "1", Value = true }.Publish();
  601. Thread.Sleep(10);
  602. new WriteMcu() { TagName = "OutputControl", Address = "1", Value = true }.Publish();
  603. Thread.Sleep(10);
  604. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_放杯检测 }.Publish();
  605. Wait(int.Parse(JakaModel.SENCE_放杯检测));
  606. bSensorInput = sensor_Sign(2);
  607. delayTimeOut = DateTime.Now;
  608. while (bSensorInput == 2)
  609. {
  610. Thread.Sleep(100);
  611. bSensorInput = sensor_Sign(2);
  612. if (DateTime.Now.Subtract(delayTimeOut).TotalSeconds >= 2) return 2;
  613. }
  614. if (bSensorInput == 2)
  615. {
  616. DeviceProcessLogShow("放杯失败传感器没有信号");
  617. new WriteJaka() { TagName = "JaKaProgramName", Value = JakaModel.SENCE_初始位 }.Publish();
  618. Wait(int.Parse(JakaModel.SENCE_初始位));
  619. return 1;
  620. }
  621. return 1;
  622. }
  623. catch (Exception ex)
  624. {
  625. MessageLog.GetInstance.ShowEx(ex.ToString());
  626. return 0;
  627. }
  628. }
  629. private int cnt_Check;
  630. public override DeviceClientType DeviceType => throw new NotImplementedException();
  631. /// <summary>
  632. /// 检测放杯位,是否有杯子
  633. /// </summary>
  634. /// <returns>0:无意义 1:没有杯子 2:有杯子 </returns>
  635. private int checkCup()
  636. {
  637. try
  638. {
  639. bSensorInput = sensor_Sign(2);
  640. if (bSensorInput == 2)
  641. {
  642. DeviceProcessLogShow($"放杯位传感器没有信号:{cnt_Check}");
  643. return 1;
  644. }
  645. else if (bSensorInput == 1)
  646. {
  647. DeviceProcessLogShow($"放杯位传感器有信号:{cnt_Check}");
  648. return 2;
  649. }
  650. return 2;
  651. }
  652. catch (Exception ex)
  653. {
  654. MessageLog.GetInstance.ShowEx(ex.ToString());
  655. return 0;
  656. }
  657. }
  658. private T McuRead<T>(string tagName, object par)
  659. {
  660. new ReadMcu() { DeviceId = DeviceId, TagName = tagName, ReadPar = par };
  661. if (peripheralStatus.ContainsKey(tagName))
  662. {
  663. if (peripheralStatus[tagName] != null)
  664. {
  665. return (T)peripheralStatus[tagName];
  666. }
  667. }
  668. return default;
  669. }
  670. /// <summary>
  671. /// 传感器防抖0.2s内检测20次,
  672. /// </summary>
  673. /// <param name="num"></param>
  674. /// <returns></returns>
  675. private int sensor_Sign(byte num)
  676. {
  677. DeviceProcessLogShow($"开始检测{num}号传感器信号");
  678. int cnt = 0;
  679. cnt_Check = 0;
  680. while (true)
  681. {
  682. Thread.Sleep(10);
  683. bSensorInput = McuRead<int>("GetInputStatus", num);
  684. if (bSensorInput == 1)
  685. {
  686. cnt_Check = cnt_Check + 1;
  687. cnt = cnt + 1;
  688. }
  689. else if (bSensorInput == 2)
  690. {
  691. cnt_Check = cnt_Check - 1;
  692. cnt = cnt + 1;
  693. }
  694. if (cnt >= 20)
  695. {
  696. break;
  697. }
  698. }
  699. if (cnt_Check >= 0)
  700. {
  701. DeviceProcessLogShow($"{num}传感器有信号:{cnt_Check}");
  702. return 1;
  703. }
  704. else
  705. {
  706. DeviceProcessLogShow($"{num}传感器没有信号:{cnt_Check}");
  707. return 2;
  708. }
  709. }
  710. private void DRCoffee_CoffeEndCookEventHandle(IEvent @event, EventCallBackHandle callBack)
  711. {
  712. are.Set();
  713. }
  714. public void SimOrder<T>(T simOrder)
  715. {
  716. }
  717. public override void DoMain()
  718. {
  719. }
  720. public override void Stop()
  721. {
  722. }
  723. public override void ReadData()
  724. {
  725. }
  726. public override void MainTask()
  727. {
  728. }
  729. public override void ResetProgram()
  730. {
  731. }
  732. public override void SimOrder()
  733. {
  734. }
  735. }
  736. }