DlgAddDevice.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using MV485.db;
  2. using MV485.helper;
  3. using MV485.model;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. namespace MV485.Dlg
  18. {
  19. /// <summary>
  20. /// DlgAddDevice.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class DlgAddDevice : Window
  23. {
  24. private string PortName { get; set; }
  25. private int BaudRate { get; set; }
  26. private int[] SlaveAddreses { get; set; }
  27. public List<TSlave> NewSlaveList { get; private set; }
  28. public DlgAddDevice()
  29. {
  30. InitializeComponent();
  31. Loaded += DlgAddDevice_Loaded;
  32. }
  33. private void DlgAddDevice_Loaded(object sender, RoutedEventArgs e)
  34. {
  35. LoadSerialPorts();
  36. cmbBaudrate.ItemsSource = SerialHelper.BaudRates;
  37. mcmbAddress.SetItems(Enumerable.Range(1, 247).ToArray().Select(i => i.ToString()).ToArray(),false);
  38. var readInterval = ConfigManager.Instance.GetConfigValue(ConfigKey.ReadInterval, "60");
  39. txtReadInterval.Text = readInterval;
  40. }
  41. private void LoadSerialPorts()
  42. {
  43. Dispatcher.BeginInvoke(new Action(() =>
  44. {
  45. cmbPortNames.ItemsSource = SerialHelper.GetSerialPortsWithDetails();
  46. if (cmbPortNames.Items.Count > 0)
  47. cmbPortNames.SelectedIndex = 0;
  48. //是否使用-基本配置选择的端口(应该用)
  49. var portName = ConfigManager.Instance.GetConfigValue(ConfigKey.ConfigPortName, "");
  50. cmbPortNames.SelectedValue = portName;
  51. }));
  52. }
  53. private async void BtnOK_Click(object sender, RoutedEventArgs e)
  54. {
  55. if(cmbPortNames.SelectedItem == null)
  56. {
  57. MessageBox.Show(this, "请选择端口号", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  58. return;
  59. }
  60. if(cmbBaudrate.SelectedItem == null)
  61. {
  62. MessageBox.Show(this, "请选择波特率", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  63. return;
  64. }
  65. string addresses = mcmbAddress.GetSelectedItems();
  66. if (string.IsNullOrWhiteSpace(addresses))
  67. {
  68. MessageBox.Show(this, "请选择485地址", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  69. return;
  70. }
  71. if(!int.TryParse(txtReadInterval.Text,out int readInterval) || readInterval < 5 || readInterval > 1440)
  72. {
  73. MessageBox.Show(this, "请输入正确的间隔时间(5-1440)分钟", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  74. return;
  75. }
  76. PortName = cmbPortNames.SelectedValue.ToString();
  77. BaudRate = int.Parse(cmbBaudrate.SelectedItem.ToString());
  78. SlaveAddreses = addresses.Split(',').Select(int.Parse).ToArray();
  79. string titleInfo = "正在插入数据库,请稍候...";
  80. WaitWindow waitWindow = new WaitWindow(titleInfo)
  81. {
  82. Owner = this,
  83. WindowStartupLocation = WindowStartupLocation.CenterOwner
  84. };
  85. waitWindow.Show();
  86. NewSlaveList = new List<TSlave>();
  87. foreach(var address in SlaveAddreses)
  88. {
  89. TSlave slave = new TSlave()
  90. {
  91. SlaveId = Guid.NewGuid().ToString().Replace("-",""),
  92. PortName = PortName,
  93. BaudRate = BaudRate,
  94. Address = address,
  95. RunFlag = chkRun.IsChecked == true ? 1 : 0,
  96. ReadImageFlag = chkReadImage.IsChecked == true ? 1 : 0,
  97. ReadInterval = readInterval,
  98. LastReadTime = ""
  99. };
  100. try
  101. {
  102. bool blInsert = await Task.Run(() =>
  103. {
  104. return DBSlave.InsertTSlave(slave);
  105. });
  106. if (blInsert)
  107. {
  108. NewSlaveList.Add(slave);
  109. }
  110. }
  111. catch(Exception ex)
  112. {
  113. MessageBox.Show($"插入数据失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  114. }
  115. }
  116. ConfigManager.Instance.UpdateConfig(ConfigKey.ReadInterval, readInterval.ToString());
  117. waitWindow.Close();
  118. DialogResult = true;
  119. this.Close();
  120. }
  121. private void BtnClose_Click(object sender, RoutedEventArgs e)
  122. {
  123. DialogResult = false;
  124. this.Close();
  125. }
  126. }
  127. }