123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using System;
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.Wpf;
- namespace PRSDesktop
- {
- /// <summary>
- /// Interaction logic for DigitalKeyForm.xaml
- /// </summary>
- public partial class DigitalKeyForm : ThemableWindow
- {
- private readonly DigitalKey _key;
- private string _macaddress = "";
- private string _service = "";
- private string _characteristic = "";
- private readonly string[] otherkeys = { };
- private SerialPort port;
- public DigitalKeyForm(DigitalKey key)
- {
- _key = key;
- otherkeys = new Client<DigitalKey>().Query(
- new Filter<DigitalKey>(x => x.ID).IsNotEqualTo(_key.ID),
- new Columns<DigitalKey>(x => x.MacAddress)
- ).Rows.Select(r => r.Get<DigitalKey, string>(c => c.MacAddress)).ToArray();
- InitializeComponent();
- var names = new List<string> { "" };
- names.AddRange(SerialPort.GetPortNames());
- Ports.ItemsSource = names;
- if (names.Count == 2)
- Ports.SelectedIndex = 1;
- }
- private void DisposeSerialPort()
- {
- if (port != null)
- {
- port.Close();
- port.Dispose();
- port = null;
- }
- }
- private void CreateSerialPort(string name)
- {
- if (string.IsNullOrWhiteSpace(name))
- return;
- DisposeSerialPort();
- port = new SerialPort
- {
- PortName = name,
- BaudRate = 9600,
- Parity = Parity.None,
- DataBits = 8,
- StopBits = StopBits.One,
- ReadTimeout = 1000
- };
- try
- {
- port.Open();
- }
- catch (Exception e)
- {
- MessageBox.Show(string.Format("Unable to Open {0}\n\n{1}", name, e.Message));
- DisposeSerialPort();
- }
- }
- private void Ports_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- CreateSerialPort(Ports.SelectedValue.ToString());
- if (port == null)
- {
- Duplicate.Visibility = Visibility.Collapsed;
- OK.Visibility = Visibility.Collapsed;
- Connect.Content = "Select a valid Serial Port!";
- return;
- }
- port.WriteLine("");
- var junk = port.ReadLine();
- port.WriteLine("MODEL");
- var model = port.ReadLine().Replace("\r", "").Replace("\n", "");
- Model.Text = model;
- port.WriteLine("ADDRESS");
- _macaddress = port.ReadLine().Replace("\r", "").Replace("\n", "").ToUpper();
- MacAddress.Text = _macaddress;
- port.WriteLine("SERVICE");
- _service = port.ReadLine().Replace("\r", "").Replace("\n", "").ToUpper();
- port.WriteLine("CHARACTERISTIC");
- _characteristic = port.ReadLine().Replace("\r", "").Replace("\n", "").ToUpper();
- Duplicate.Visibility = otherkeys.Contains(_macaddress) ? Visibility.Visible : Visibility.Collapsed;
- OK.Visibility = otherkeys.Contains(_macaddress) ? Visibility.Collapsed : Visibility.Visible;
- Connect.IsEnabled = !otherkeys.Contains(_macaddress);
- Connect.Content = otherkeys.Contains(_macaddress) ? "Key Already Exists!" : "Connect";
- }
- private void Connect_Click(object sender, RoutedEventArgs e)
- {
- var key = Guid.NewGuid().ToString();
- port.WriteLine(string.Format("PRIVATE {0}", key));
- var confirm = port.ReadLine().Replace("\r", "").Replace("\n", "");
- if (string.Equals(confirm, "OK"))
- {
- _key.Model = Model.Text;
- _key.MacAddress = _macaddress;
- _key.Service = _service;
- _key.Characteristic = _characteristic;
- _key.Key = key;
- DisposeSerialPort();
- DialogResult = true;
- }
- }
- }
- }
|