|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Toolkit.Mvvm.ComponentModel;
- using System.Collections.Concurrent;
- using System.Collections.ObjectModel;
- using System.Windows;
- using BPASmartClient.Helper;
- using Microsoft.Toolkit.Mvvm.Input;
- using BPASmartClient.DosingSystem.Model;
- using System.Threading;
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.CustomResource.UserControls.MessageShow;
- using BPASmartClient.CustomResource.UserControls;
- using BPASmartClient.Model.柔性味魔方;
- using BPASmartClient.Model;
-
- namespace BPASmartClient.DosingSystem.ViewModel
- {
- public class RecipeControlViewModel : ObservableObject
- {
- ConcurrentQueue<string> devices = new ConcurrentQueue<string>();
-
- public RecipeControlViewModel()
- {
- StartCommand = new RelayCommand<object>((o) =>
- {
- if (o != null && o is string deviceName)
- {
- int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == deviceName);
- if (index >= 0 && index < Recipes.Count)
- {
- Recipes.ElementAt(index).IsEnable = false;
- }
- MessageLog.GetInstance.ShowUserLog($"下发工单 { Recipes.ElementAt(index).RecipeName}");
- devices.Enqueue(deviceName);
- var res = Recipes.FirstOrDefault(p => p.RecipeName == deviceName);
- UserTreeWait.Add(new RecipeModel { RecipeName = deviceName, RawMaterials = res.RawMaterials });
- }
-
-
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方下发成功!");
- MessageLog.GetInstance.ShowUserLog($"下发工单 {Guid.NewGuid().ToString()}");
- });
-
- ChangeRecipeStateCommand = new RelayCommand<object>(ChangeRecipeState);
-
-
-
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- if (devices.Count > 0)
- {
- int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == devices.ElementAt(0));
- if (index >= 0 && index < Recipes.Count)
- {
- Recipes.ElementAt(index).Are.Reset();
- Recipes.ElementAt(index).IsEnable = false;
- App.Current.Dispatcher.Invoke(new Action(() =>
- {
- recipeProcesses.Clear();
- UserTreeWait.RemoveAt(0);
- }));
- CurrentRecipeName = Recipes.ElementAt(index).RecipeName;
- foreach (var item in Recipes.ElementAt(index).RawMaterials)
- {
- DeviceInquire.GetInstance.GetDevice(item.DeviceIp)?.Start(item.RawMaterialWeight);//启动并写入每个原料重量
-
- App.Current.Dispatcher.Invoke(new Action(() =>
- {
- recipeProcesses.Add(new RawMaterialModel()
- {
- RawMaterialName = item.RawMaterialName,
- RecipeStatus = item.RecipeStatus,
- RawMaterialSource = item.RawMaterialSource,
- RawMaterialId = item.RawMaterialId,
- });
- }));
-
- }
-
- Recipes.ElementAt(index).Are.WaitOne();//阻塞,直到当前配方完成
- devices.TryDequeue(out string deviceName);
- UserTreeCompelete.Add(Recipes.ElementAt(index));//当前配方完成后添加到已完成的配方列表
- App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));//完成后清空当前配方
- }
-
- }
- Thread.Sleep(100);
- }), "启动配方下发");
-
-
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- for (int i = 0; i < Recipes.Count; i++)
- {
- for (int m = 0; m < Recipes.ElementAt(i).RawMaterials.Count; m++)
- {
- var RunStatus = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).deviceStatus.RunStatus;
-
- //设备状态显示
- if (Recipes.ElementAt(i).RecipeName == CurrentRecipeName)
- {
- string deviceName = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).DeviceName;
- int index = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == deviceName);
-
- if (index >= 0 && index < recipeProcesses.Count)
- {
- App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.ElementAt(index).RecipeStatus = RunStatus; }));
- }
- }
-
- Recipes.ElementAt(i).RawMaterials.ElementAt(m).RecipeStatus = RunStatus;
- var res = Recipes.ElementAt(i).RawMaterials.Where(p => p.RecipeStatus == 3).ToList();
- if (res != null && res.Count == Recipes.ElementAt(i).RawMaterials.Count)//配方所有配料完成下料
- {
- for (int r = 0; r < Recipes.ElementAt(i).RawMaterials.Count; r++)
- {
- DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(r).DeviceIp).StatusReset();//完成配料的设备运行状态地址写0
- App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));
- }
- Recipes.ElementAt(i).IsEnable = true;
- Recipes.ElementAt(i).Are.Set();
- }
- }
- }
- Thread.Sleep(100);
- }), "RecipeControlViewModelStatusInquire");
- //测试数据
- RawMaterialModel rawMaterial_1 = new RawMaterialModel { RawMaterialName = "香料_1" };
- RawMaterialModel rawMaterial_2 = new RawMaterialModel { RawMaterialName = "香料_2" };
- RawMaterialModel rawMaterial_3 = new RawMaterialModel { RawMaterialName = "香料_3" };
- RawMaterialModel rawMaterial_4 = new RawMaterialModel { RawMaterialName = "香料_4" };
- ObservableCollection<RawMaterialModel> rawMaterials = new ObservableCollection<RawMaterialModel> { rawMaterial_1, rawMaterial_2, rawMaterial_3, rawMaterial_4 };
- UserTreeCompelete.Add(new RecipeModel { RecipeName = "完成的香料1", RawMaterials = rawMaterials });
- UserTreeCompelete.Add(new RecipeModel { RecipeName = "完成的香料2", RawMaterials = rawMaterials });
-
- }
-
- public RelayCommand<object> StartCommand { get; set; }
-
- public RelayCommand<object> ChangeRecipeStateCommand { get; set; }
-
- public static ObservableCollection<RecipeModel> Recipes { get; set; } = Json<LocaPar>.Data.Recipes;
-
- public string CurrentRecipeName { get { return _RecipeName; }set { _RecipeName = value; OnPropertyChanged(); } }
- private static string _RecipeName;
-
- /// <summary>
- /// 当前正在制作的配方
- /// </summary>
-
- public static ObservableCollection<RawMaterialModel> recipeProcesses { get; set; } = new ObservableCollection<RawMaterialModel>();
- /// <summary>
- /// 等待制作的配方
- /// </summary>
- public static ObservableCollection<RecipeModel> UserTreeWait { get; set; } = new ObservableCollection<RecipeModel>();
- /// <summary>
- /// 已完成的配方
- /// </summary>
- public static ObservableCollection<RecipeModel> UserTreeCompelete { get; set; } = new ObservableCollection<RecipeModel>();
-
-
- private void ChangeRecipeState(object o)
- {
- if (o == null) return;
- if(o is string id)
- {
- var res = recipeProcesses.FirstOrDefault(p => p.RawMaterialId == id);
- if (res != null)
- {
- if(res.RecipeStatus == 3)
- {
- res.RecipeStatus = 1;
- }else
- {
- res.RecipeStatus = 3;
- }
- }
- }
- }
- }
-
-
- }
|