终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

128 righe
5.0 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.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. }