终端一体化运控平台
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

CommunicationSetViewModel.cs 5.0 KiB

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