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
{
///
/// Interaction logic for DigitalKeyForm.xaml
///
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().Query(
new Filter(x => x.ID).IsNotEqualTo(_key.ID),
new Columns(x => x.MacAddress)
).Rows.Select(r => r.Get(c => c.MacAddress)).ToArray();
InitializeComponent();
var names = new List { "" };
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;
}
}
}
}