终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CommunicationSetViewModel.cs 5.0 KiB

2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.ObjectModel;
  8. using Microsoft.Toolkit.Mvvm.Input;
  9. using BPASmart.VariableManager.Views;
  10. using System.Windows;
  11. using BPASmartClient.Helper;
  12. using BPASmart.Model;
  13. //using BPASmartClient.Model;
  14. using System.IO.Ports;
  15. using System.IO;
  16. using System.Reflection;
  17. namespace BPASmart.VariableManager.ViewModels
  18. {
  19. public class CommunicationSetViewModel : ObservableObject
  20. {
  21. public CommunicationSetViewModel()
  22. {
  23. Json<CommunicationPar>.Read();
  24. communicationDevices = Json<CommunicationPar>.Data.CommunicationDevices;
  25. DataListInit();
  26. NewConnectCommand = new RelayCommand(() => { NewConnect(); });
  27. RemoveDeviceCommand = new RelayCommand<object>(RemoveDevice);
  28. SaveConnectSetCommand = new RelayCommand(() => { Json<CommunicationPar>.Save(); });
  29. DelegationNotifi.GetInstance.AddDeviceVerify = new Func<DeviceManagermentResult, bool>((p) =>
  30. {
  31. return Json<CommunicationPar>.Data.CommunicationDevices.FirstOrDefault(s => s.DeviceName == p.DeviceName) == null;
  32. });
  33. }
  34. public ObservableCollection<CommunicationModel> communicationDevices { get; set; } //= new ObservableCollection<CommunicationModel>();
  35. public RelayCommand NewConnectCommand { get; set; }
  36. public RelayCommand SaveConnectSetCommand { get; set; }
  37. public RelayCommand<object> RemoveDeviceCommand { get; set; }
  38. /// <summary>
  39. /// 创建新连接
  40. /// </summary>
  41. private void NewConnect()
  42. {
  43. NewDeviceView deviceManagermentSetView = new NewDeviceView();
  44. var result = deviceManagermentSetView.ShowDialog();
  45. var ResultTag = (DeviceManagermentResult)deviceManagermentSetView.Tag;
  46. if (ResultTag != null)
  47. {
  48. if ((bool)result)
  49. {
  50. var obj = communicationDevices.FirstOrDefault(p => p.DeviceName == ResultTag.DeviceName);
  51. if (obj == null)
  52. {
  53. CommunicationModel communicationObj = new CommunicationModel();
  54. Type type = Assembly.Load("BPASmart.Model").GetType($"BPASmart.Model.{ResultTag.DeviceType}");
  55. //Type type = Assembly.Load("BPASmart.VariableManager").GetType($"BPASmart.VariableManager.Models.{ResultTag.DeviceType}");
  56. var res = Activator.CreateInstance(type) as ICommunicationDevice;
  57. communicationObj.DeviceName = ResultTag.DeviceName;
  58. communicationObj.CommDevice = res;
  59. communicationObj.ModelName = type.Name;
  60. Json<CommunicationPar>.Data.CommunicationDevices.Add(communicationObj);
  61. ActionManage.GetInstance.Send("AddCommunicationDevice", ResultTag.DeviceName);
  62. }
  63. }
  64. }
  65. deviceManagermentSetView = null;
  66. }
  67. private void RemoveDevice(object o)
  68. {
  69. var result = Json<CommunicationPar>.Data.CommunicationDevices.FirstOrDefault(p => p.DeviceName == o.ToString());
  70. if (result != null)
  71. {
  72. Json<CommunicationPar>.Data.CommunicationDevices.Remove(result);
  73. ActionManage.GetInstance.Send("RemoveCommunicationDevice", result);
  74. }
  75. }
  76. #region 列表集合
  77. /// <summary>
  78. /// 端口号列表
  79. /// </summary>
  80. public static ObservableCollection<string> Ports { get; set; } = new ObservableCollection<string>();
  81. /// <summary>
  82. /// 波特率列表
  83. /// </summary>
  84. public static ObservableCollection<int> BaudRates { get; set; } = new ObservableCollection<int>();
  85. /// <summary>
  86. /// 奇偶校验列表
  87. /// </summary>
  88. public static ObservableCollection<string> Paritys { get; set; } = new ObservableCollection<string>();
  89. /// <summary>
  90. /// 西门子 PLC 设备类型
  91. /// </summary>
  92. public ObservableCollection<string> PlcTypes { get; set; } = new ObservableCollection<string>();
  93. private void DataListInit()
  94. {
  95. Ports.Clear();
  96. System.IO.Ports.SerialPort.GetPortNames().ToList().ForEach((item) => { Ports.Add(item); });
  97. BaudRates.Clear();
  98. int[] rb = new int[] { 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 43000, 57600, 76800, 115200 };
  99. rb.ToList().ForEach((item) => { BaudRates.Add(item); });
  100. Paritys.Clear();
  101. Enum.GetNames(typeof(Parity)).ToList().ForEach((item) => { Paritys.Add(item); });
  102. PlcTypes.Clear();
  103. foreach (var item in Enum.GetNames(typeof(ESiemensPlcType)))
  104. {
  105. PlcTypes.Add(item.Substring(1));
  106. }
  107. }
  108. #endregion
  109. }
  110. }