|
- 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.DosingSystem.ViewModel
- {
- public class HardwareStatusViewModel : NotifyBase
- {
- public HardwareStatusViewModel()
- {
- TopDeviceCurrentStatuses = DeviceInquire.GetInstance.TopDeviceCurrentStatuses;
- BottomDeviceCurrentStatuses = DeviceInquire.GetInstance.BottomDeviceCurrentStatuses;
-
- //for (int i = 0; i < Json<DevicePar>.Data.BaseParModel.ConveyerBeltCount; i++)
- //{
- // ConveyerBeltModels.Add(new ConveyerBeltModel() { Name = $"输送带{i}", Num = i++ });
- //}
-
- //ConveyerBeltControlCommand = new BPARelayCommand<object>(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);
- // }
- // }
- //});
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- for (int i = 0; i < Json<DevicePar>.Data.OutletInfoModels.Count; i++)
- {
- int count = Json<DevicePar>.Data.OutletInfoModels.ElementAt(i).SiloInfos.Count;
- if (count >= 1)
- {
- for (int m = 0; m < count; m++)
- {
- var deviceName = Json<DevicePar>.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<object>((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<DeviceCurrentStatus> 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<DeviceCurrentStatus> TopDeviceCurrentStatuses { get; set; }
-
- public ObservableCollection<DeviceCurrentStatus> BottomDeviceCurrentStatuses { get; set; }
-
- public ObservableCollection<ConveyerBeltModel> ConveyerBeltModels { get; set; } = new ObservableCollection<ConveyerBeltModel>();
-
- public BPARelayCommand<object> ConveyerBeltControlCommand { get; set; }
-
- public BPARelayCommand<object> RecipeControlCommand { get; set; }
-
- /// <summary>
- /// 输送带状态
- /// </summary>
- public bool ConveyerBeltStatus { get { return _mConveyerBeltStatus; } set { _mConveyerBeltStatus = value; OnPropertyChanged(); } }
- private bool _mConveyerBeltStatus;
-
- /// <summary>
- /// 上桶工位检测
- /// </summary>
- public bool OnDetection { get { return _mOnDetection; } set { _mOnDetection = value; OnPropertyChanged(); } }
- private bool _mOnDetection;
-
- /// <summary>
- /// 下桶工位检测
- /// </summary>
- public bool UnderDetection { get { return _mUnderDetection; } set { _mUnderDetection = value; OnPropertyChanged(); } }
- private bool _mUnderDetection;
-
- }
-
-
- }
|