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.JXJFoodBigStation.Model; using BPASmartClient.JXJFoodBigStation.View; using System.Windows.Forms; namespace BPASmartClient.JXJFoodBigStation.ViewModel { public class HardwareStatusViewModel : ObservableObject { public HardwareStatusViewModel() { for (int i = 6; i > 0; i--) { TopDeviceCurrentStatuses.Add(new DeviceCurrentStatus() { DeviceName = i.ToString(), DeviceNum = i, RunStatus = true, Weight = new Random().Next(0, 100) }); } for (int i = 7; i < 13; i++) { BottomDeviceCurrentStatuses.Add(new DeviceCurrentStatus() { DeviceName = i.ToString(), DeviceNum = i, RunStatus = false, Weight = new Random().Next(0, 100) }); } StartCommand = new RelayCommand((deviceName) => { if (deviceName != null) { var top = TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName); if (top != null) { ActionManage.GetInstance.Send("StartTopDevice", deviceName); } var bottom = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName); if (bottom != null) { ActionManage.GetInstance.Send("StartBottomDevice", deviceName); } } }); StopCommand = new RelayCommand((deviceName) => { if (deviceName != null) { var top = TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName); if (top != null) { ActionManage.GetInstance.Send("StopTopDevice", deviceName); } var bottom = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName); if (bottom != null) { ActionManage.GetInstance.Send("StopBottomDevice", deviceName); } } }); UpdateRawMaterName = new RelayCommand((deviceName) => { ActionManage.GetInstance.CancelRegister("UpdateDeviceName"); ActionManage.GetInstance.Register(new Action((res) => { if (res != null && res is string newName) { int cnt = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == deviceName); if (cnt >= 0) { int index = Array.FindIndex(BottomDeviceCurrentStatuses.ToArray(), p => p.DeviceName == newName); int index1 = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == newName); if (index == -1 && index1 == -1) { var obj = TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName); obj.DeviceName = newName; } else { System.Windows.Forms.MessageBox.Show("设备名称与其他名称冲突", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { int index = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == newName); int index1 = Array.FindIndex(BottomDeviceCurrentStatuses.ToArray(), p => p.DeviceName == newName); if (index == -1 && index1 == -1) { var obj = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName); obj.DeviceName = newName; } else { System.Windows.Forms.MessageBox.Show("设备名称与其他名称冲突", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }), "UpdateDeviceName"); ChangeDeviceNameView view = new ChangeDeviceNameView(); view.ShowDialog(); }); } public ObservableCollection TopDeviceCurrentStatuses { get; set; } = new ObservableCollection(); public ObservableCollection BottomDeviceCurrentStatuses { get; set; } = new ObservableCollection(); public RelayCommand UpdateRawMaterName { get; set; } public RelayCommand StartCommand { get; set; } public RelayCommand StopCommand { get; set; } } }