终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

127 lines
5.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Toolkit.Mvvm.ComponentModel;
  7. using System.Collections.Concurrent;
  8. using System.Collections.ObjectModel;
  9. using System.Windows;
  10. using BPASmartClient.Helper;
  11. using Microsoft.Toolkit.Mvvm.Input;
  12. using BPASmartClient.JXJFoodBigStation.Model;
  13. using BPASmartClient.JXJFoodBigStation.View;
  14. using System.Windows.Forms;
  15. namespace BPASmartClient.JXJFoodBigStation.ViewModel
  16. {
  17. public class HardwareStatusViewModel : ObservableObject
  18. {
  19. public HardwareStatusViewModel()
  20. {
  21. for (int i = 6; i > 0; i--)
  22. {
  23. TopDeviceCurrentStatuses.Add(new DeviceCurrentStatus()
  24. {
  25. DeviceName = i.ToString(),
  26. DeviceNum = i,
  27. RunStatus = true,
  28. Weight = new Random().Next(0, 100)
  29. });
  30. }
  31. for (int i = 7; i < 13; i++)
  32. {
  33. BottomDeviceCurrentStatuses.Add(new DeviceCurrentStatus()
  34. {
  35. DeviceName = i.ToString(),
  36. DeviceNum = i,
  37. RunStatus = false,
  38. Weight = new Random().Next(0, 100)
  39. });
  40. }
  41. StartCommand = new RelayCommand<string>((deviceName) =>
  42. {
  43. if (deviceName != null)
  44. {
  45. var top = TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName);
  46. if (top != null)
  47. {
  48. ActionManage.GetInstance.Send("StartTopDevice", deviceName);
  49. }
  50. var bottom = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName);
  51. if (bottom != null)
  52. {
  53. ActionManage.GetInstance.Send("StartBottomDevice", deviceName);
  54. }
  55. }
  56. });
  57. StopCommand = new RelayCommand<string>((deviceName) =>
  58. {
  59. if (deviceName != null)
  60. {
  61. var top = TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName);
  62. if (top != null)
  63. {
  64. ActionManage.GetInstance.Send("StopTopDevice", deviceName);
  65. }
  66. var bottom = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName);
  67. if (bottom != null)
  68. {
  69. ActionManage.GetInstance.Send("StopBottomDevice", deviceName);
  70. }
  71. }
  72. });
  73. UpdateRawMaterName = new RelayCommand<string>((deviceName) =>
  74. {
  75. ActionManage.GetInstance.CancelRegister("UpdateDeviceName");
  76. ActionManage.GetInstance.Register(new Action<object>((res) =>
  77. {
  78. if (res != null && res is string newName)
  79. {
  80. int cnt = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == deviceName);
  81. if (cnt >= 0)
  82. {
  83. int index = Array.FindIndex(BottomDeviceCurrentStatuses.ToArray(), p => p.DeviceName == newName);
  84. int index1 = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == newName);
  85. if (index == -1 && index1 == -1)
  86. {
  87. var obj = TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName);
  88. obj.DeviceName = newName;
  89. }
  90. else
  91. {
  92. System.Windows.Forms.MessageBox.Show("设备名称与其他名称冲突", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  93. }
  94. }
  95. else
  96. {
  97. int index = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == newName);
  98. int index1 = Array.FindIndex(BottomDeviceCurrentStatuses.ToArray(), p => p.DeviceName == newName);
  99. if (index == -1 && index1 == -1)
  100. {
  101. var obj = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName);
  102. obj.DeviceName = newName;
  103. }
  104. else
  105. {
  106. System.Windows.Forms.MessageBox.Show("设备名称与其他名称冲突", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  107. }
  108. }
  109. }
  110. }), "UpdateDeviceName");
  111. ChangeDeviceNameView view = new ChangeDeviceNameView();
  112. view.ShowDialog();
  113. });
  114. }
  115. public ObservableCollection<DeviceCurrentStatus> TopDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
  116. public ObservableCollection<DeviceCurrentStatus> BottomDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
  117. public RelayCommand<string> UpdateRawMaterName { get; set; }
  118. public RelayCommand<string> StartCommand { get; set; }
  119. public RelayCommand<string> StopCommand { get; set; }
  120. }
  121. }