终端一体化运控平台
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

1005 linhas
39 KiB

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