using BPA.Helper; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading; namespace BPASmartClient.DosingSystem.ViewModel { public class HardwareStatusViewModel : NotifyBase { public HardwareStatusViewModel() { //TopDeviceCurrentStatuses = DeviceInquire.GetInstance.TopDeviceCurrentStatuses; //BottomDeviceCurrentStatuses = DeviceInquire.GetInstance.BottomDeviceCurrentStatuses; //2023/7/3调整 TopDeviceCurrentStatuses = ConvertToObservableCollention(DeviceInquire.GetInstance.TopDeviceCurrentStatuses.OrderByDescending(p => p.DeviceNum).Reverse()); BottomDeviceCurrentStatuses = ConvertToObservableCollention(DeviceInquire.GetInstance.BottomDeviceCurrentStatuses.OrderByDescending(p => p.DeviceNum).Reverse()); UnifordColumNum(); //for (int i = 0; i < Json.Data.BaseParModel.ConveyerBeltCount; i++) //{ // ConveyerBeltModels.Add(new ConveyerBeltModel() { Name = $"输送带{i}", Num = i++ }); //} //ConveyerBeltControlCommand = new BPARelayCommand(o => //{ // if (o != null && o is int tempCount) // { // string add = SiemensDevice.GetInstance.GetSiemensBitSingleAdd("DB5.DBX", tempCount, 6); // int index = ConveyerBeltModels.ToList().FindIndex(p => p.Num == tempCount); // if (index >= 0 && index < ConveyerBeltModels.Count) // { // SiemensDevice.GetInstance.MySiemens.Write(add, !ConveyerBeltModels.ElementAt(index).Control); // } // } //}); TaskManage.GetInstance.StartLong(new Action(() => { for (int i = 0; i < Json.Data.OutletInfoModels.Count; i++) { int count = Json.Data.OutletInfoModels.ElementAt(i).SiloInfos.Count; if (count >= 1) { for (int m = 0; m < count; m++) { var deviceName = Json.Data.OutletInfoModels.ElementAt(i).SiloInfos.ElementAt(m); int topIndex = TopDeviceCurrentStatuses.ToList().FindIndex(p => p.DeviceName == deviceName); int bottomIndex = BottomDeviceCurrentStatuses.ToList().FindIndex(p => p.DeviceName == deviceName); if (topIndex >= 0 && topIndex < TopDeviceCurrentStatuses.Count) TopDeviceCurrentStatuses.ElementAt(i).BucketFlagbit = GlobalDevice.PlcData.StationDetection[i]; if (bottomIndex >= 0 && bottomIndex < BottomDeviceCurrentStatuses.Count) BottomDeviceCurrentStatuses.ElementAt(i).BucketFlagbit = GlobalDevice.PlcData.StationDetection[i]; } } } StatusUpdate(TopDeviceCurrentStatuses); StatusUpdate(BottomDeviceCurrentStatuses); OnDetection = GlobalDevice.PlcData.OnDetection; UnderDetection = GlobalDevice.PlcData.UnderDetection; ConveyerBeltStatus = GlobalDevice.MotorControlFeedback > 0; Thread.Sleep(100); }), "输送带料仓状态监控", true); RecipeControlCommand = new BPARelayCommand((o) => { if (o != null && o is DeviceCurrentStatus currentStatus) { var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == currentStatus.DeviceName); if (res != null) { DeviceInquire.GetInstance.GetDevice(res.IpAddress).Start(currentStatus.SetWeight); } } }); } /// /// 数组集合转换 /// /// /// /// public ObservableCollection ConvertToObservableCollention(IEnumerable array) { ObservableCollection collec = new ObservableCollection(); foreach (var item in array) { collec.Add(item); } return collec; } private void StatusUpdate(ObservableCollection devices) { if (devices == null) return; for (int i = 0; i < devices.Count; i++) { if (devices.ElementAt(i).BaitingControl) { var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == devices.ElementAt(i).DeviceName); if (res != null) { var runStatus = DeviceInquire.GetInstance.GetDevice(res.IpAddress).deviceStatus.RunStatus; if (runStatus == 3) { if (TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == devices.ElementAt(i).DeviceName) != null) { TopDeviceCurrentStatuses.ElementAt(i).BaitingControl = false; } else if (BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == devices.ElementAt(i).DeviceName) != null) { BottomDeviceCurrentStatuses.ElementAt(i).BaitingControl = false; } DeviceInquire.GetInstance.GetDevice(res.IpAddress).StatusReset(); } } } } } /// /// 根据料仓数来进行uniform列划分 /// private void UnifordColumNum() { StockNum = TopDeviceCurrentStatuses.Count >= BottomDeviceCurrentStatuses.Count ? TopDeviceCurrentStatuses.Count : BottomDeviceCurrentStatuses.Count; //switch ((ConveryType)System.Enum.Parse(typeof(ConveryType), Json.Data.BaseParModel.ConveryType)) //{ // case ConveryType.直线型: StockNum = Json.Data.BaseParModel.StockCount; break; // //case ConveryType.直线型: // // if (Json.Data.BaseParModel.StockCount > 0 && Json.Data.BaseParModel.StockCount % 2 == 0) // // { // // StockNum = Json.Data.BaseParModel.StockCount / 2; // // } // // else // // { // // StockNum = 8;//默认值 // // } // // break; //} } public ObservableCollection TopDeviceCurrentStatuses { get; set; } public ObservableCollection BottomDeviceCurrentStatuses { get; set; } public ObservableCollection ConveyerBeltModels { get; set; } = new ObservableCollection(); public BPARelayCommand ConveyerBeltControlCommand { get; set; } public BPARelayCommand RecipeControlCommand { get; set; } /// /// Uniford根据料仓数量来划分列数量 /// private int _stockNum; public int StockNum { get { return _stockNum; } set { _stockNum = value; OnPropertyChanged(); } } /// /// 输送带状态 /// public bool ConveyerBeltStatus { get { return _mConveyerBeltStatus; } set { _mConveyerBeltStatus = value; OnPropertyChanged(); } } private bool _mConveyerBeltStatus; /// /// 上桶工位检测 /// public bool OnDetection { get { return _mOnDetection; } set { _mOnDetection = value; OnPropertyChanged(); } } private bool _mOnDetection; /// /// 下桶工位检测 /// public bool UnderDetection { get { return _mUnderDetection; } set { _mUnderDetection = value; OnPropertyChanged(); } } private bool _mUnderDetection; } }