终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

132 lines
5.8 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. //PLC控制
  43. //动画
  44. if (deviceName != null)
  45. {
  46. var top= TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName);
  47. if (top != null)
  48. {
  49. ActionManage.GetInstance.Send("StartTopDevice", deviceName);
  50. }
  51. var bottom = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName);
  52. if (bottom != null)
  53. {
  54. ActionManage.GetInstance.Send("StartBottomDevice", deviceName);
  55. }
  56. }
  57. });
  58. StopCommand = new RelayCommand<string>((deviceName) => {
  59. //PLC控制
  60. //动画
  61. if (deviceName != null)
  62. {
  63. var top = TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName);
  64. if (top != null)
  65. {
  66. ActionManage.GetInstance.Send("StopTopDevice", deviceName);
  67. }
  68. var bottom = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName);
  69. if (bottom != null)
  70. {
  71. ActionManage.GetInstance.Send("StopBottomDevice", deviceName);
  72. }
  73. }
  74. });
  75. UpdateRawMaterName = new RelayCommand<string>((deviceName) =>
  76. {
  77. ActionManage.GetInstance.CancelRegister("UpdateDeviceName");
  78. ActionManage.GetInstance.Register(new Action<object>((res) =>
  79. {
  80. if (res != null && res is string newName)
  81. {
  82. int cnt = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == deviceName);
  83. if (cnt >= 0)
  84. {
  85. int index = Array.FindIndex(BottomDeviceCurrentStatuses.ToArray(), p => p.DeviceName == newName);
  86. int index1 = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == newName);
  87. if (index == -1 && index1 == -1)
  88. {
  89. var obj = TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName);
  90. obj.DeviceName = newName;
  91. }
  92. else
  93. {
  94. System.Windows.Forms.MessageBox.Show("设备名称与其他名称冲突", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  95. }
  96. }
  97. else
  98. {
  99. int index = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == newName);
  100. int index1 = Array.FindIndex(BottomDeviceCurrentStatuses.ToArray(), p => p.DeviceName == newName);
  101. if (index == -1 && index1 == -1)
  102. {
  103. var obj = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == deviceName);
  104. obj.DeviceName = newName;
  105. }
  106. else
  107. {
  108. System.Windows.Forms.MessageBox.Show("设备名称与其他名称冲突", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  109. }
  110. }
  111. }
  112. }), "UpdateDeviceName");
  113. ChangeDeviceNameView view = new ChangeDeviceNameView();
  114. view.ShowDialog();
  115. });
  116. }
  117. public ObservableCollection<DeviceCurrentStatus> AllDeviceCurrentStatuses;
  118. public ObservableCollection<DeviceCurrentStatus> TopDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
  119. public ObservableCollection<DeviceCurrentStatus> BottomDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
  120. public RelayCommand<string> UpdateRawMaterName { get; set; }
  121. public RelayCommand<string> StartCommand { get; set; }
  122. public RelayCommand<string> StopCommand { get; set; }
  123. }
  124. }