DigitalKeyForm.xaml.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.Wpf;
  11. namespace PRSDesktop
  12. {
  13. /// <summary>
  14. /// Interaction logic for DigitalKeyForm.xaml
  15. /// </summary>
  16. public partial class DigitalKeyForm : ThemableWindow
  17. {
  18. private readonly DigitalKey _key;
  19. private string _macaddress = "";
  20. private string _service = "";
  21. private string _characteristic = "";
  22. private readonly string[] otherkeys = { };
  23. private SerialPort port;
  24. public DigitalKeyForm(DigitalKey key)
  25. {
  26. _key = key;
  27. otherkeys = new Client<DigitalKey>().Query(
  28. new Filter<DigitalKey>(x => x.ID).IsNotEqualTo(_key.ID),
  29. new Columns<DigitalKey>(x => x.MacAddress)
  30. ).Rows.Select(r => r.Get<DigitalKey, string>(c => c.MacAddress)).ToArray();
  31. InitializeComponent();
  32. var names = new List<string> { "" };
  33. names.AddRange(SerialPort.GetPortNames());
  34. Ports.ItemsSource = names;
  35. if (names.Count == 2)
  36. Ports.SelectedIndex = 1;
  37. }
  38. private void DisposeSerialPort()
  39. {
  40. if (port != null)
  41. {
  42. port.Close();
  43. port.Dispose();
  44. port = null;
  45. }
  46. }
  47. private void CreateSerialPort(string name)
  48. {
  49. if (string.IsNullOrWhiteSpace(name))
  50. return;
  51. DisposeSerialPort();
  52. port = new SerialPort
  53. {
  54. PortName = name,
  55. BaudRate = 9600,
  56. Parity = Parity.None,
  57. DataBits = 8,
  58. StopBits = StopBits.One,
  59. ReadTimeout = 1000
  60. };
  61. try
  62. {
  63. port.Open();
  64. }
  65. catch (Exception e)
  66. {
  67. MessageBox.Show(string.Format("Unable to Open {0}\n\n{1}", name, e.Message));
  68. DisposeSerialPort();
  69. }
  70. }
  71. private void Ports_SelectionChanged(object sender, SelectionChangedEventArgs e)
  72. {
  73. CreateSerialPort(Ports.SelectedValue.ToString());
  74. if (port == null)
  75. {
  76. Duplicate.Visibility = Visibility.Collapsed;
  77. OK.Visibility = Visibility.Collapsed;
  78. Connect.Content = "Select a valid Serial Port!";
  79. return;
  80. }
  81. port.WriteLine("");
  82. var junk = port.ReadLine();
  83. port.WriteLine("MODEL");
  84. var model = port.ReadLine().Replace("\r", "").Replace("\n", "");
  85. Model.Text = model;
  86. port.WriteLine("ADDRESS");
  87. _macaddress = port.ReadLine().Replace("\r", "").Replace("\n", "").ToUpper();
  88. MacAddress.Text = _macaddress;
  89. port.WriteLine("SERVICE");
  90. _service = port.ReadLine().Replace("\r", "").Replace("\n", "").ToUpper();
  91. port.WriteLine("CHARACTERISTIC");
  92. _characteristic = port.ReadLine().Replace("\r", "").Replace("\n", "").ToUpper();
  93. Duplicate.Visibility = otherkeys.Contains(_macaddress) ? Visibility.Visible : Visibility.Collapsed;
  94. OK.Visibility = otherkeys.Contains(_macaddress) ? Visibility.Collapsed : Visibility.Visible;
  95. Connect.IsEnabled = !otherkeys.Contains(_macaddress);
  96. Connect.Content = otherkeys.Contains(_macaddress) ? "Key Already Exists!" : "Connect";
  97. }
  98. private void Connect_Click(object sender, RoutedEventArgs e)
  99. {
  100. var key = Guid.NewGuid().ToString();
  101. port.WriteLine(string.Format("PRIVATE {0}", key));
  102. var confirm = port.ReadLine().Replace("\r", "").Replace("\n", "");
  103. if (string.Equals(confirm, "OK"))
  104. {
  105. _key.Model = Model.Text;
  106. _key.MacAddress = _macaddress;
  107. _key.Service = _service;
  108. _key.Characteristic = _characteristic;
  109. _key.Key = key;
  110. DisposeSerialPort();
  111. DialogResult = true;
  112. }
  113. }
  114. }
  115. }