using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using BPA.Helper; using System.Collections.Concurrent; using System.Collections.ObjectModel; using System.Windows; using System.Threading; namespace BPASmartClient.DosingSystemSingle.ViewModel { public class HardwareStatusViewModel : NotifyBase { public HardwareStatusViewModel() { TopDeviceCurrentStatuses = DeviceInquire.GetInstance.TopDeviceCurrentStatuses; BottomDeviceCurrentStatuses = DeviceInquire.GetInstance.BottomDeviceCurrentStatuses; //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); }), "输送带料仓状态监控"); 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); } } }); } 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(); } } } } } 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; } /// /// 输送带状态 /// 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; } }