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

956 строки
39 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using BPA.Message.Enum;
  4. using BPASmartClient.Device;
  5. using BPASmartClient.Model;
  6. using BPASmartClient.Peripheral;
  7. using BPA.Helper;
  8. using System.Threading;
  9. using BPA.Message;
  10. using System.Linq;
  11. using BPASmartClient.Model.PLC;
  12. using System.Threading.Tasks;
  13. using System.Reflection;
  14. using BPASmartClient.MorkS.Model;
  15. using System.Collections.ObjectModel;
  16. using BPASmartClient.MorkS.ViewModel;
  17. using BPASmartClient.Business;
  18. using BPASmartClient.Model.小炒机;
  19. using BPA.Models;
  20. using System.Speech.Synthesis;
  21. using System.Windows.Forms;
  22. using System.Media;
  23. using BPASmartClient.CustomResource;
  24. using static BPA.Helper.EventBus;
  25. namespace BPASmartClient.MorkS
  26. {
  27. public class Control_Morks : BaseDevice
  28. {
  29. public override DeviceClientType DeviceType => DeviceClientType.MORKS;
  30. GVL_MORKS mORKS = new GVL_MORKS();
  31. Alarm alarm = new Alarm();
  32. public override void DoMain()
  33. {
  34. MonitorViewModel.DeviceId = DeviceId;
  35. ServerInit();
  36. DataParse();
  37. Json<MorksPar>.Read();
  38. Json<OrderStatistics>.Read();
  39. if (Json<MorksPar>.Data.parSets == null) Json<MorksPar>.Data.parSets = new ObservableCollection<ParSet>();
  40. if (Json<MorksPar>.Data.parSets.Count < 6)
  41. {
  42. Json<MorksPar>.Data.parSets.Clear();
  43. for (int i = 0; i < 6; i++)
  44. {
  45. Json<MorksPar>.Data.parSets.Add(new ParSet()
  46. {
  47. CheckBoxContext = $"煮面口{i + 1}屏蔽",
  48. Minute = 1,
  49. Second = 0,
  50. IsShield = false,
  51. TextBlockContext = $"煮面口{i + 1}时间设定"
  52. });
  53. }
  54. }
  55. ActionManage.GetInstance.Register(new Action<object[]>((o) =>
  56. {
  57. if (o.Length > 0)
  58. {
  59. Random rd = new Random();
  60. TaskManage.GetInstance.StartLong(new Action(() =>
  61. {
  62. int NoodleLoc = (int)o[0] == 0 ? rd.Next(1, 6) : (int)o[0];
  63. int BowlLoc = (int)o[1] == 0 ? rd.Next(10, 12) : (int)o[1];
  64. string guid = new Guid().ToString();
  65. mORKS.RBTakeNoodleTask.Enqueue(new OrderLocInfo() { Loc = (ushort)NoodleLoc, SuborderId = guid });
  66. MessageLog.GetInstance.Show($"添加订单:面条位置【{NoodleLoc}】");
  67. mORKS.TakeBowlTask.Enqueue(new OrderLocInfo() { Loc = (ushort)BowlLoc, SuborderId = guid });
  68. MessageLog.GetInstance.Show($"添加订单:碗位置【{BowlLoc}】");
  69. Thread.Sleep(60000);
  70. }), "ForOrder");
  71. }
  72. }), "EnableForOrder");
  73. ActionManage.GetInstance.Register(new Action<object>((o) =>
  74. {
  75. if (o != null && o is WritePar writePar) WriteData(writePar.Address, writePar.Value);
  76. }), "WriteVW");
  77. ActionManage.GetInstance.Register(new Action<object>((o) =>
  78. {
  79. if (o != null && o is WritePar writePar) WriteData(writePar.Address, writePar.Value);
  80. }), "WriteBools");
  81. ActionManage.GetInstance.Register(new Action(() => { DeviceInit(); }), "InitDevice");
  82. }
  83. public override void ResetProgram()
  84. {
  85. mORKS = null;
  86. mORKS = new GVL_MORKS();
  87. }
  88. public override void Stop()
  89. {
  90. }
  91. private void ServerInit()
  92. {
  93. //物料信息
  94. EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  95. {
  96. if (@event == null) return;
  97. if (@event is MaterialDeliveryEvent material)
  98. {
  99. orderMaterialDelivery = material.orderMaterialDelivery;
  100. }
  101. });
  102. //配方数据信息
  103. EventBus.GetInstance().Subscribe<RecipeBomEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  104. {
  105. if (@event == null) return;
  106. if (@event is RecipeBomEvent recipe)
  107. {
  108. recipeBoms = recipe.recipeBoms;
  109. WriteRecipeBoms();
  110. }
  111. });
  112. }
  113. private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
  114. {
  115. var res = mORKS.doOrderEvents.FirstOrDefault(p => p.MorkOrder.SuborderId == subid);
  116. string goodName = string.Empty;
  117. string SortNum = string.Empty;
  118. if (res != null)
  119. {
  120. goodName = res.MorkOrder.GoodsName;
  121. SortNum = res.MorkOrder.SortNum.ToString();
  122. }
  123. //if (mORKS.doe.ContainsKey(subid))
  124. //{
  125. //goodName = mORKS.doe[subid].MorkOrder.GoodsName;
  126. //SortNum = mORKS.doe[subid].MorkOrder.SortNum.ToString();
  127. if (!string.IsNullOrEmpty(goodName) && !string.IsNullOrEmpty(SortNum))
  128. {
  129. EventBus.GetInstance().Publish(new OrderStatusChangedEvent() { SortNum = SortNum, GoodName = goodName, Status = oRDER_STATUS, SubOrderId = subid, deviceClientType = DeviceType });
  130. var index = DataServer.GetInstance.morkS.MakeOrder.FindIndex(p => p.SortNum == SortNum);
  131. if (index >= 0 && index < DataServer.GetInstance.morkS.MakeOrder.Count)
  132. {
  133. if (oRDER_STATUS == ORDER_STATUS.COMPLETED_COOK)
  134. {
  135. DataServer.GetInstance.morkS.MakeOrder.RemoveAt(index);
  136. DataServer.GetInstance.morkS.MakeOrderOver.Add(new OrderMakeModel()
  137. {
  138. Status = oRDER_STATUS,
  139. GoodName = goodName,
  140. SortNum = SortNum,
  141. StopTime = DateTime.Now.ToString("HH:mm:ss")
  142. });
  143. }
  144. else if (oRDER_STATUS == ORDER_STATUS.COMPLETED_TAKE)
  145. {
  146. var temp = DataServer.GetInstance.morkS.MakeOrderOver.FirstOrDefault(p => p.SortNum == SortNum);
  147. if (temp != null) DataServer.GetInstance.morkS.MakeOrderOver.Remove(temp);
  148. }
  149. else
  150. {
  151. DataServer.GetInstance.morkS.MakeOrder.ElementAt(index).Status = oRDER_STATUS;
  152. }
  153. }
  154. else
  155. {
  156. DataServer.GetInstance.morkS.MakeOrder.Add(new OrderMakeModel()
  157. {
  158. Status = oRDER_STATUS,
  159. GoodName = goodName,
  160. SortNum = SortNum,
  161. StartTime = DateTime.Now.ToString("HH:mm:ss")
  162. });
  163. }
  164. //mORKS.doe.Remove(subid, out _);
  165. }
  166. //}
  167. }
  168. private void GetStatus(string key, Action<object> action)
  169. {
  170. if (peripheralStatus.ContainsKey(key))
  171. {
  172. if (peripheralStatus[key] != null)
  173. {
  174. action?.Invoke(peripheralStatus[key]);
  175. }
  176. }
  177. }
  178. public override void ReadData()
  179. {
  180. DataServer.GetInstance.morkS.Alarm.Clear();
  181. alarms.ForEach(item =>
  182. {
  183. DataServer.GetInstance.morkS.Alarm.Add(new AlarmModel()
  184. {
  185. AlarmTime = $"{item.Date} {item.Time}",
  186. AlarmMs = item.Info
  187. });
  188. });
  189. GetStatus("M230.0", new Action<object>((obj) =>
  190. {
  191. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 24)
  192. {
  193. alarm.MachineLeftLowTemperature = bools[0];
  194. alarm.MachineRightLowTemperature = bools[1];
  195. alarm.Supply1_LossBowl = bools[2];
  196. alarm.Supply2_LossBowl = bools[3];
  197. alarm.Supply1_ErrorOutBowl = bools[4];
  198. alarm.Supply2_ErrorOutBowl = bools[5];
  199. alarm.PushBowlCylinderError = bools[6];
  200. alarm.NoodleMacCommunicateError = bools[7];
  201. alarm.DosingMacCommunicateError = bools[8];
  202. alarm.RobotMacCommunicateError = bools[9];
  203. alarm.DeviceEstop = bools[10];
  204. alarm.RobotInitError = bools[11];
  205. alarm.RobotUrgentStop = bools[12];
  206. alarm.RobotNotInRemoteMode = bools[13];
  207. alarm.RobotNotInReady = bools[14];
  208. alarm.RobotSelfInException = bools[15];
  209. alarm.LeftLackWater = bools[16];
  210. alarm.RightLackWater = bools[17];
  211. alarm.SvrewInitFail = bools[18];
  212. alarm.TurntableInitFail = bools[19];
  213. alarm.RobotInitFail = bools[20];
  214. alarm.NoodleCookerInitFail = bools[21];
  215. alarm.PushBowlInitFail1 = bools[22];
  216. alarm.PushBowlInitFail2 = bools[23];
  217. }
  218. }));
  219. GetStatus("M0.3", new Action<object>((obj) =>
  220. {
  221. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 3)
  222. {
  223. mORKS.RobotTakeNoodle = bools[0];
  224. mORKS.RobotOutMeal = bools[1];
  225. mORKS.MoveTurntable = bools[2];
  226. }
  227. }));
  228. GetStatus("M100.0", new Action<object>((obj) =>
  229. {
  230. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 16)
  231. {
  232. mORKS.InitComplete = bools[0];
  233. mORKS.TakeBowlIdle = bools[1];
  234. mORKS.TemperatureReached = bools[2];
  235. mORKS.AllowFallNoodle = bools[3];
  236. mORKS.RbTakeNoodleComplete = bools[4];
  237. mORKS.RbFallNoodleComplete = bools[5];
  238. mORKS.RbOutMealComplete = bools[6];
  239. mORKS.RobotIdle = bools[7];
  240. mORKS.TakeMealDetect = bools[8];
  241. mORKS.MissingBowl = bools[9];
  242. Initing = bools[10];
  243. mORKS.TurntableLowerLimit = bools[11];
  244. mORKS.MissingBowlSignal2 = bools[12];
  245. mORKS.TurntableUpLimit = bools[13];
  246. mORKS.FeedComplete = bools[14];
  247. mORKS.TurntableMoveInPlace = bools[15];
  248. DataServer.GetInstance.morkS.MorkS_Temp = mORKS.TemperatureReached;
  249. DataServer.GetInstance.morkS.Morks_SiloMeasUp = mORKS.TurntableUpLimit;
  250. DataServer.GetInstance.morkS.Morks_SiloMeasDown = mORKS.TurntableLowerLimit;
  251. DataServer.GetInstance.morkS.MorkS_NoBowMeas1 = mORKS.MissingBowl;
  252. DataServer.GetInstance.morkS.MorkS_NoBowMeas2 = mORKS.MissingBowlSignal2;
  253. }
  254. }));
  255. GetStatus("M235.0", new Action<object>((obj) =>
  256. {
  257. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 1)
  258. {
  259. mORKS.Error = bools[0];
  260. }
  261. }));
  262. GetStatus("M102.0", new Action<object>((obj) =>
  263. {
  264. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 7)
  265. {
  266. for (int i = 0; i < 6; i++)
  267. {
  268. mORKS.NoodleCookerStatus[i] = bools[i];
  269. }
  270. mORKS.Feeding = bools[6];
  271. }
  272. }));
  273. GetStatus("M103.0", new Action<object>((obj) =>
  274. {
  275. if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 6)
  276. {
  277. for (int i = 0; i < 6; i++)
  278. {
  279. mORKS.CookNoodlesComplete[i] = bools[i];
  280. DataServer.GetInstance.morkS.Morks_NoodleUpOrDown[i] = bools[i];
  281. }
  282. }
  283. }));
  284. GetStatus("VW372", new Action<object>((obj) =>
  285. {
  286. if (obj is ushort[] UshortValue && UshortValue.Length > 0 && UshortValue.Length <= 1)
  287. mORKS.TurntableFeedbackloc = UshortValue[0];
  288. DataServer.GetInstance.morkS.MorkS_BinLocation = mORKS.TurntableFeedbackloc;
  289. }));
  290. }
  291. /// <summary>
  292. /// 数据解析
  293. /// </summary>
  294. private void DataParse()
  295. {
  296. if (!Global.LocalOrderModel)//小程序下单
  297. {
  298. EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  299. {
  300. if (@event == null) return;
  301. if (@event is DoOrderEvent order)
  302. {
  303. mORKS.doOrderEvents.Add(order);
  304. //mORKS.doe.TryAdd(order.MorkOrder.SuborderId, order);
  305. DeviceProcessLogShow($"接收到{order.MorkOrder.SortNum}号订单");
  306. if (order.MorkOrder.GoodBatchings == null) return;
  307. if (mORKS.HistorySuborderId.Contains(order.MorkOrder.SuborderId)) return;
  308. OrderCount++;
  309. if (DateTime.Now.Subtract(Json<OrderStatistics>.Data.StatisticsTime).Days != 0)
  310. Json<OrderStatistics>.Data.Count = 0;
  311. Json<OrderStatistics>.Data.StatisticsTime = DateTime.Now;
  312. Json<OrderStatistics>.Data.Count++;
  313. Json<OrderStatistics>.Save();
  314. OrderChange(order.MorkOrder.SuborderId, ORDER_STATUS.WAIT);
  315. if (order.MorkOrder.GoodBatchings.Count <= 1)
  316. {
  317. DeviceProcessLogShow($"数据解析失败,商品物料信息为空,请检查后台配置!");
  318. return;
  319. }
  320. DeviceProcessLogShow($"接收到{OrderCount}次订单,订单ID:{order.MorkOrder.SuborderId}");
  321. mORKS.HistorySuborderId.Add(order.MorkOrder.SuborderId);
  322. foreach (var item in order.MorkOrder.GoodBatchings)
  323. {
  324. var res = orderMaterialDelivery?.BatchingInfo?.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  325. if (res != null)
  326. {
  327. if (ushort.TryParse(res.BatchingLoc, out ushort loc))
  328. {
  329. if (loc >= 1 && loc <= 5)
  330. {
  331. if (mORKS.RBTakeNoodleTask.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  332. mORKS.RBTakeNoodleTask.Enqueue(new OrderLocInfo()
  333. {
  334. GoodName = order.MorkOrder.GoodsName,
  335. Loc = ushort.Parse(res.BatchingLoc),
  336. SuborderId = order.MorkOrder.SuborderId,
  337. SortNum = order.MorkOrder.SortNum,
  338. BatchingId = res.BatchingId
  339. });
  340. }
  341. else if (loc >= 10 && loc <= 11)
  342. {
  343. int index = 0;
  344. if (recipeBoms != null)
  345. {
  346. index = Array.FindIndex(recipeBoms.RecipeIds?.ToArray(), p => p.RecipeId == order.MorkOrder.RecipeId);
  347. index++;
  348. }
  349. if (mORKS.TakeBowlTask.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  350. mORKS.TakeBowlTask.Enqueue(new OrderLocInfo()
  351. {
  352. BatchingId = res.BatchingId,
  353. GoodName = order.MorkOrder.GoodsName,
  354. Loc = ushort.Parse(res.BatchingLoc),
  355. SuborderId = order.MorkOrder.SuborderId,
  356. SortNum = order.MorkOrder.SortNum,
  357. RecipeNumber = (index >= 1 && index <= 10) ? (ushort)index : (ushort)0
  358. });
  359. }
  360. }
  361. }
  362. else
  363. {
  364. DeviceProcessLogShow($"数据解析失败,未找到商品信息,请检查后台配置!");
  365. }
  366. }
  367. }
  368. });
  369. }
  370. }
  371. public override void MainTask()
  372. {
  373. mORKS.AllowRun = mORKS.InitComplete;
  374. if (Json<KeepDataBase>.Data.IsVerify)
  375. IsHealth = mORKS.Error && mORKS.InitComplete;
  376. else
  377. IsHealth = true;
  378. TakeBowlTask();
  379. TakeNoodleTask();
  380. OutNoodleTask();
  381. SingleDetect();
  382. TurntableControl();
  383. var data = new List<bool>();
  384. for (int i = 0; i < Json<MorksPar>.Data.parSets.Count; i++)
  385. {
  386. data.Add(Json<MorksPar>.Data.parSets.ElementAt(i).IsShield);
  387. }
  388. WriteControl("M260.0", data.ToArray());
  389. }
  390. private void BowlControl(OrderLocInfo orderLocInfo)
  391. {
  392. if (orderLocInfo.Loc >= 10 && orderLocInfo.Loc <= 11)
  393. {
  394. mORKS.TakeBowlId = orderLocInfo.SuborderId;
  395. mORKS.TakeBowName = orderLocInfo.GoodName;
  396. mORKS.TakeBowSortNum = orderLocInfo.SortNum;
  397. TakeBowlControl(orderLocInfo.Loc);
  398. SetRecipeNumber(orderLocInfo.RecipeNumber);
  399. OrderChange(mORKS.TakeBowlId, ORDER_STATUS.COOKING);
  400. DeviceProcessLogShow($"订单【{mORKS.TakeBowlId}】执行取碗控制,位置:[{orderLocInfo.Loc}]");
  401. mORKS.TakeBowlInterlock = true;
  402. }
  403. }
  404. /// <summary>
  405. /// 取碗控制
  406. /// </summary>
  407. private void TakeBowlTask()
  408. {
  409. if (mORKS.AllowRun && mORKS.TakeBowlTask.Count > 0 && !mORKS.TakeBowlIdle && !mORKS.TakeBowlInterlock)
  410. {
  411. DeviceProcessLogShow("开始取碗流程");
  412. ushort BowLoc = 0;
  413. var res = orderMaterialDelivery?.BatchingInfo?.Where(p => p.BatchingId == mORKS.TakeBowlTask.ElementAt(0).BatchingId).ToList();
  414. if (res == null || res?.Count == 0)//本地或者模拟订单
  415. {
  416. var bowlLoc= mORKS.TakeBowlTask.ElementAt(0).Loc;
  417. if (bowlLoc == 10 && !alarm.Supply1_LossBowl)
  418. {
  419. BowLoc = bowlLoc;
  420. }
  421. else if (bowlLoc == 11 && !alarm.Supply2_LossBowl)
  422. {
  423. BowLoc = bowlLoc;
  424. }
  425. if (BowLoc >= 10 && BowLoc <= 11&&mORKS.TakeBowlTask.TryDequeue(out OrderLocInfo orderLocInfo))
  426. {
  427. BowlControl(orderLocInfo);
  428. }
  429. }
  430. else
  431. {
  432. foreach (var item in res)
  433. {
  434. if (ushort.TryParse(item.BatchingLoc, out ushort loc))
  435. {
  436. //DeviceProcessLogShow($"位置:={loc},检测开关1:{alarm.Supply1_LossBowl},检测开关1:{alarm.Supply2_LossBowl}");
  437. if (loc == 10 && !alarm.Supply1_LossBowl)
  438. {
  439. BowLoc = loc;
  440. break;
  441. }
  442. else if (loc == 11 && !alarm.Supply2_LossBowl)
  443. {
  444. BowLoc = loc;
  445. break;
  446. }
  447. }
  448. }
  449. if (BowLoc >= 10 && BowLoc <= 11)
  450. {
  451. if (mORKS.TakeBowlTask.TryDequeue(out OrderLocInfo orderLocInfo))
  452. {
  453. orderLocInfo.Loc = BowLoc;
  454. BowlControl(orderLocInfo);
  455. }
  456. }
  457. }
  458. }
  459. }
  460. /// <summary>
  461. /// 转台控制
  462. /// </summary>
  463. private void TurntableControl()
  464. {
  465. if (Global.EnableLocalSimOrder|| Global.LocalOrderModel)
  466. {
  467. //不做轮询,直接取面,模拟订单使用
  468. if (mORKS.TurntableMoveInPlace && !mORKS.Feeding && mORKS.InitComplete && !mORKS.AllowTakeNoodle && mORKS.RBTakeNoodleTask.Count > 0)
  469. {
  470. if (mORKS.TurntableLowerLimit)
  471. {
  472. TurntableStart(mORKS.RBTakeNoodleTask.ElementAt(0).Loc);
  473. mORKS.TurntableLocLists.Clear();
  474. mORKS.AllowTakeNoodle = true;
  475. DeviceProcessLogShow($"控制机器人去转台【{mORKS.RBTakeNoodleTask.ElementAt(0).Loc}】号位置取面");
  476. }
  477. }
  478. }
  479. else
  480. {
  481. //正常轮询
  482. if (mORKS.TurntableMoveInPlace && !mORKS.Feeding && mORKS.InitComplete && !mORKS.AllowTakeNoodle && mORKS.RBTakeNoodleTask.Count > 0)
  483. {
  484. var result = orderMaterialDelivery.BatchingInfo.Where(p => p.BatchingId == mORKS.RBTakeNoodleTask.ElementAt(0).BatchingId).ToList();
  485. if (result != null)
  486. {
  487. var res = result.FirstOrDefault(P => P.BatchingLoc == mORKS.TurntableFeedbackloc.ToString());
  488. if (mORKS.TurntableLowerLimit && res != null)
  489. {
  490. TurntableStart(mORKS.TurntableFeedbackloc);
  491. mORKS.TurntableLocLists.Clear();
  492. mORKS.AllowTakeNoodle = true;
  493. DeviceProcessLogShow($"控制机器人去转台【{mORKS.TurntableFeedbackloc}】号位置取面");
  494. }
  495. else
  496. {
  497. if (!mORKS.TurntableInterlock)
  498. {
  499. foreach (var item in result)
  500. {
  501. if (ushort.TryParse(item.BatchingLoc, out ushort loc))
  502. {
  503. if (mORKS.TurntableFeedbackloc != loc && !mORKS.TurntableLocLists.Contains(loc))
  504. {
  505. if (!mORKS.TurntableLowerLimit)
  506. {
  507. WriteData("M32.7", false);
  508. DeviceProcessLogShow($"执行了转台启动互锁信号复位");
  509. }
  510. TurntableStart(loc);
  511. DeviceProcessLogShow($"没有物料检测的启动转台控制,转台位置:[{loc}]");
  512. break;
  513. }
  514. else if (mORKS.TurntableFeedbackloc == loc && !mORKS.TurntableLocLists.Contains(loc)) mORKS.TurntableLocLists.Add(loc);
  515. }
  516. }
  517. }
  518. }
  519. }
  520. else DeviceProcessLogShow("未找到可用的物料信息");
  521. }
  522. }
  523. //if (DelayRTrig.GetInstance("互锁信号复位").Start(!mORKS.TurntableLowerLimit && mORKS.TurntableMoveInPlace, 2))
  524. //{
  525. // if (!mORKS.TurntableLowerLimit)
  526. // {
  527. // WriteData("M32.7", false);
  528. // DeviceProcessLogShow($"执行了转台启动互锁信号复位");
  529. // }
  530. //}
  531. //补料中检测
  532. if (RTrig.GetInstance("mORKS.Feeding").Start(mORKS.Feeding))
  533. {
  534. mORKS.AllowTakeNoodle = false;
  535. mORKS.TakeNoodleInterlock = false;
  536. }
  537. //转台到位检测
  538. if (RTrig.GetInstance("TurntableInPlace").Start(mORKS.TurntableMoveInPlace && mORKS.CurrentLoc == mORKS.TurntableFeedbackloc))
  539. {
  540. mORKS.CurrentLoc = 0;
  541. mORKS.TurntableInterlock = false;
  542. DeviceProcessLogShow("转台到位检测");
  543. }
  544. //补料完成检测
  545. if (RTrig.GetInstance("FeedComplete").Start(mORKS.FeedComplete))
  546. {
  547. if (!mORKS.AllowTakeNoodle && mORKS.TurntableLocLists.Count > 0)
  548. {
  549. mORKS.TurntableLocLists.Clear();
  550. mORKS.TurntableInterlock = false;
  551. DeviceProcessLogShow("补料完成检测");
  552. }
  553. }
  554. }
  555. /// <summary>
  556. /// 取面任务
  557. /// </summary>
  558. private void TakeNoodleTask()
  559. {
  560. //取面控制
  561. if (mORKS.AllowRun && mORKS.RobotIdle && !mORKS.Feeding && !mORKS.RobotTaskInterlock && mORKS.AllowTakeNoodle && mORKS.TurntableMoveInPlace && !mORKS.TakeNoodleInterlock && !mORKS.OutNoodleing && mORKS.RBTakeNoodleTask.Count > 0)
  562. {
  563. int loc = Array.FindIndex(mORKS.NoodleCookerStatus, p => p == false);//查找煮面炉空闲位置
  564. if (loc >= 0 && loc <= 5)
  565. {
  566. if (mORKS.RBTakeNoodleTask.TryDequeue(out OrderLocInfo orderLocInfo))
  567. {
  568. mORKS.CookNodelId[loc] = orderLocInfo.SuborderId;
  569. SetFallNoodleLoc((ushort)(loc + 1));
  570. //机器人开始取面
  571. RobotTakeNoodle();
  572. OrderChange(orderLocInfo.SuborderId, ORDER_STATUS.COOKING);
  573. DeviceProcessLogShow($"订单【{orderLocInfo.SuborderId}】,机器人倒面至【{loc + 1}】号煮面栏");
  574. //写入煮面时间
  575. List<ushort> values = new List<ushort>();
  576. values.Add(Json<MorksPar>.Data.parSets.ElementAt(loc).Minute);
  577. values.Add(Json<MorksPar>.Data.parSets.ElementAt(loc).Second);
  578. WriteData($"VW{116 + (loc * 6)}", values.ToArray());
  579. mORKS.TakeNoodleInterlock = true;
  580. }
  581. }
  582. }
  583. }
  584. /// <summary>
  585. /// 出餐控制
  586. /// </summary>
  587. private void OutNoodleTask()
  588. {
  589. if (mORKS.AllowFallNoodle && mORKS.RobotTaskInterlock && !mORKS.TakeNoodleInterlock && mORKS.RobotIdle && !mORKS.TakeMealDetect)
  590. {
  591. int loc = Array.FindIndex(mORKS.CookNodelId, p => p == mORKS.IngredientsCompleteId && p.Length > 0);
  592. if (loc >= 0 && loc <= 5)
  593. {
  594. if (mORKS.CookNoodlesComplete[loc])
  595. {
  596. SetTakeNoodleLoc((ushort)(loc + 1));
  597. RobotOutMeal();
  598. CookNoodleStatusReset((ushort)(loc + 1));
  599. ResetAllowFallNoodle();
  600. //新增,待测试
  601. if (mORKS.RbOutMealComplete)
  602. {
  603. ResetCookComplete();
  604. mORKS.CookCompleteFlatBit = false;
  605. DeviceProcessLogShow("取餐过程中复位出餐完成信号");
  606. }
  607. if (!string.IsNullOrEmpty(mORKS.OutMealId)) OrderChange(mORKS.OutMealId, ORDER_STATUS.COMPLETED_TAKE);
  608. mORKS.OutMealId = mORKS.IngredientsCompleteId;
  609. mORKS.OutMealName = mORKS.IngredientsCompleteName;
  610. mORKS.OutMealSortNum = mORKS.IngredientsCompleteSortNum;
  611. mORKS.IngredientsCompleteId = string.Empty;
  612. mORKS.CookNodelId[loc] = string.Empty;
  613. DeviceProcessLogShow($"{loc + 1} 号位置出餐控制,订单ID:{mORKS.OutMealId}");
  614. mORKS.OutNoodleing = true;
  615. }
  616. }
  617. }
  618. }
  619. /// <summary>
  620. /// 信号检测
  621. /// </summary>
  622. private void SingleDetect()
  623. {
  624. //允许倒面信号检测
  625. if (RTrig.GetInstance("AllowFallNoodle").Start(mORKS.AllowFallNoodle))
  626. {
  627. mORKS.IngredientsCompleteId = mORKS.TakeBowlId;
  628. mORKS.IngredientsCompleteName = mORKS.TakeBowName;
  629. mORKS.IngredientsCompleteSortNum = mORKS.TakeBowSortNum;
  630. mORKS.TakeBowlId = string.Empty;
  631. mORKS.TakeBowName = string.Empty;
  632. mORKS.TakeBowSortNum = 0;
  633. DeviceProcessLogShow($"碗到位,允许到面,{mORKS.IngredientsCompleteId}");
  634. mORKS.TakeBowlInterlock = false;
  635. }
  636. //出餐完成信号检测
  637. if (RTrig.GetInstance("CompleteChange").Start(mORKS.RbOutMealComplete))
  638. {
  639. OrderChange(mORKS.OutMealId, ORDER_STATUS.COMPLETED_COOK);
  640. DeviceProcessLogShow($"订单【{mORKS.OutMealId}】制作完成");
  641. mORKS.CookCompleteFlatBit = true;
  642. mORKS.OutNoodleing = false;
  643. }
  644. //加汤
  645. /*if (Delay.GetInstance("加汤延时判断").Start(mORKS.TakeMealDetect && mORKS.CookCompleteFlatBit, 1))
  646. {
  647. AddSoup();
  648. }*/
  649. //取餐完成逻辑处理
  650. if (Delay.GetInstance("CompleteChange1").Start(!mORKS.TakeMealDetect, 1) && mORKS.CookCompleteFlatBit == true)
  651. {
  652. OrderChange(mORKS.OutMealId, ORDER_STATUS.COMPLETED_TAKE);
  653. DeviceProcessLogShow($"订单【{mORKS.OutMealId}】取餐完成");
  654. ResetCookComplete();
  655. DeviceProcessLogShow($"出餐订单【{mORKS.OutMealSortNum.ToString()}】");
  656. DeviceProcessLogShow($"出餐订单序号【{mORKS.OutMealSortNum}】");
  657. VoiceAPI.Speak(mORKS.OutMealSortNum.ToString());
  658. //DeviceProcessLogShow($"叫号系统通知主题【MORKS/VoiceCall/{DeviceId}】");
  659. //Plugin.GetInstance().GetPlugin<MQTTMgr>().Publish($"MORKS/VoiceCall/{DeviceId}", mORKS.OutMealSortNum.ToString());
  660. mORKS.CookCompleteFlatBit = false;
  661. mORKS.OutMealId = string.Empty;
  662. mORKS.OutMealName = string.Empty;
  663. mORKS.OutMealSortNum = 0;
  664. }
  665. //机器人取面完成信号检测
  666. if (RTrig.GetInstance("TakeNoodleComplete").Start(mORKS.RbTakeNoodleComplete))
  667. {
  668. mORKS.TakeNoodleInterlock = false;
  669. mORKS.AllowTakeNoodle = false;
  670. mORKS.TurntableInterlock = false;
  671. DeviceProcessLogShow("机器人取面完成信号检测");
  672. TakeNoodleCompleteReset();
  673. }
  674. int OutMealRequstCount = mORKS.CookNoodlesComplete.Where(p => p == true).ToList().Count;
  675. int mlCount = mORKS.NoodleCookerStatus.Where(p => p == true).ToList().Count;
  676. int index = Array.FindIndex(mORKS.CookNodelId, p => p == mORKS.IngredientsCompleteId);
  677. bool isok = index >= 0 && index < mORKS.CookNoodlesComplete.Length && mORKS.CookNoodlesComplete[index];
  678. mORKS.PriorityJudgment = Delay.GetInstance("取餐优先级判断").Start(mORKS.TurntableLocLists.Count > 0 && !mORKS.TurntableLowerLimit, 4);
  679. //mORKS.RobotTaskInterlock = OutMealRequstCount > 0 && mORKS.AllowFallNoodle && (mlCount >= 2 || mORKS.RBTakeNoodleTask.Count == 0 || mORKS.PriorityJudgment);
  680. mORKS.InvertedNoodlesOpenBusy = Json<MorksPar>.Data.InvertedNoodles_OpenBusyNum;
  681. mORKS.RobotTaskInterlock = isok && mORKS.AllowFallNoodle && (mlCount >= mORKS.InvertedNoodlesOpenBusy || mORKS.RBTakeNoodleTask.Count == 0 || mORKS.PriorityJudgment) && !mORKS.TakeMealDetect;//加个取餐口检测判断是否去面
  682. }
  683. ///// <summary>
  684. ///// 语音提醒取餐
  685. ///// </summary>
  686. ///// <param name="meal"></param>
  687. //private void WaitMeaLSpeak(string meal)
  688. //{
  689. // VoiceAPI.m_SystemPlayWav(@"Vioce\电子提示音.wav");
  690. // Thread.Sleep(1000);
  691. // if (meal != null) mORKS.speech.Speak(meal);
  692. // VoiceAPI.m_SystemPlayWav(@"Vioce\取餐通知.wav");
  693. //}
  694. #region PLC 控制函数
  695. private void WriteData(string address, object value)
  696. {
  697. EventBus.GetInstance().Publish(new WriteModel() { DeviceId = DeviceId, Address = address, Value = value });
  698. }
  699. /// <summary>
  700. /// 写入配方数据到 PLC
  701. /// </summary>
  702. private void WriteRecipeBoms()
  703. {
  704. List<ushort> recipeBoms = new List<ushort>();
  705. if (this.recipeBoms == null) return;
  706. foreach (var item in this.recipeBoms.RecipeIds)
  707. {
  708. foreach (var rec in item.Recipes)
  709. {
  710. recipeBoms.Add((ushort)rec);
  711. }
  712. }
  713. if (recipeBoms.Count > 0)
  714. {
  715. //配方数据地址范围:VW2000 - VW2278
  716. WriteData("VW2000", recipeBoms.ToArray());
  717. DeviceProcessLogShow("写配方成功");
  718. }
  719. else { DeviceProcessLogShow("配方数据为空"); }
  720. }
  721. /// <summary>
  722. /// 取面完成复位
  723. /// </summary>
  724. private void TakeNoodleCompleteReset()
  725. {
  726. WriteData("M100.4", false);
  727. }
  728. /// <summary>
  729. /// 指定煮面口状态复位
  730. /// </summary>
  731. /// <param name="num"></param>
  732. private void CookNoodleStatusReset(int num)
  733. {
  734. if (num >= 1 && num <= 6)
  735. {
  736. WriteData($"102.{num - 1}", false);
  737. DeviceProcessLogShow($"{num}号煮面口占用复位");
  738. }
  739. }
  740. /// <summary>
  741. /// 写配方编号
  742. /// </summary>
  743. /// <param name="num"></param>
  744. private void SetRecipeNumber(ushort num)
  745. {
  746. WriteData("VW0", num);
  747. }
  748. /// <summary>
  749. /// 启动转台
  750. /// </summary>
  751. /// <param name="loc"></param>
  752. private void TurntableStart(ushort loc)
  753. {
  754. mORKS.CurrentLoc = loc;
  755. mORKS.TurntableInterlock = true;
  756. mORKS.TurntableLocLists.Add(loc);
  757. WriteData("VW2", loc);
  758. WriteData("M0.5", true);
  759. }
  760. /// <summary>
  761. /// 设置倒面位置
  762. /// </summary>
  763. /// <param name="loc"></param>
  764. private void SetFallNoodleLoc(ushort loc)
  765. {
  766. WriteData("VW4", loc);
  767. }
  768. /// <summary>
  769. /// 设置取面位置
  770. /// </summary>
  771. /// <param name="loc"></param>
  772. private void SetTakeNoodleLoc(ushort loc)
  773. {
  774. WriteData("VW6", loc);
  775. }
  776. /// <summary>
  777. /// 加汤控制
  778. /// </summary>
  779. private async void AddSoup()
  780. {
  781. //M0.6
  782. WriteData("M0.6", true);
  783. await Task.Delay(Convert.ToInt32(Json<MorksPar>.Data.AddSoupTime * 1000));
  784. WriteData("M0.6", false);
  785. }
  786. /// <summary>
  787. /// 取碗控制
  788. /// </summary>
  789. /// <param name="loc"></param>
  790. private void TakeBowlControl(ushort loc)
  791. {
  792. if (loc == 10)//小碗
  793. {
  794. WriteData("M0.1", true);
  795. }
  796. else if (loc == 11)//大碗
  797. {
  798. WriteData("M0.2", true);
  799. }
  800. }
  801. /// <summary>
  802. /// 机器人取面
  803. /// </summary>
  804. private void RobotTakeNoodle()
  805. {
  806. WriteData("M0.3", true);
  807. }
  808. /// <summary>
  809. /// 机器人取餐
  810. /// </summary>
  811. private void RobotOutMeal()
  812. {
  813. WriteData("M0.4", true);
  814. }
  815. /// <summary>
  816. /// 制作完成信号复位
  817. /// </summary>
  818. private void ResetCookComplete()
  819. {
  820. WriteData("M100.6", false);
  821. }
  822. /// <summary>
  823. /// 复位允许取面信号
  824. /// </summary>
  825. private void ResetAllowFallNoodle()
  826. {
  827. WriteData("M100.3", false);
  828. }
  829. /// <summary>
  830. /// 设备初始化
  831. /// </summary>
  832. public async void DeviceInit()
  833. {
  834. WriteData("M0.0", true);
  835. await Task.Delay(1000);
  836. WriteData("M0.0", false);
  837. }
  838. public override void SimOrder()
  839. {
  840. EventBus.GetInstance().Subscribe<MorksSimorderModel>(0, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  841. {
  842. if (@event != null && @event is MorksSimorderModel msm)
  843. {
  844. string guid = Guid.NewGuid().ToString();
  845. if (msm.NoodleLoc >= 1 && msm.NoodleLoc <= 5)
  846. {
  847. mORKS.RBTakeNoodleTask.Enqueue(new OrderLocInfo() { Loc = (ushort)msm.NoodleLoc, SuborderId = guid });
  848. MessageLog.GetInstance.Show($"添加订单:面条位置【{(ushort)msm.NoodleLoc}】");
  849. }
  850. if (msm.Bowloc >= 10 && msm.Bowloc <= 11)
  851. {
  852. mORKS.TakeBowlTask.Enqueue(new OrderLocInfo() { Loc = (ushort)msm.Bowloc, SuborderId = guid });
  853. MessageLog.GetInstance.Show($"添加订单:碗位置【{(ushort)msm.Bowloc}】");
  854. }
  855. }
  856. });
  857. }
  858. #endregion
  859. }
  860. }