KFDtool/sw/control/KFDtool.Gui/Control/P25MrEmulator.xaml.cs

139 lines
3.5 KiB
C#
Raw Normal View History

2019-12-28 11:28:57 -07:00
using KFDtool.Adapter.Protocol.Adapter;
using KFDtool.P25.ThreeWire;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace KFDtool.Gui.Control
{
/// <summary>
/// Interaction logic for P25MrEmulator.xaml
/// </summary>
public partial class P25MrEmulator : UserControl
{
private static NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();
2019-12-28 11:28:57 -07:00
AdapterProtocol ap;
ThreeWireProtocol twp;
public P25MrEmulator()
{
InitializeComponent();
StopEmulation(); // set up button states
}
private void StartEmulation()
{
Settings.InProgressScreen = "NavigateP25MrEmulator";
2019-12-28 11:28:57 -07:00
StartButton.IsEnabled = false;
StopButton.IsEnabled = true;
TextArea.Text = string.Empty;
}
private void StopEmulation()
{
Settings.InProgressScreen = string.Empty;
2019-12-28 11:28:57 -07:00
StartButton.IsEnabled = true;
StopButton.IsEnabled = false;
}
private void ErrorEmulation(string message)
{
this.Dispatcher.Invoke(() =>
{
MessageBox.Show(string.Format("Error -- {0}", message), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
StopEmulation();
});
}
private void Start_Button_Click(object sender, RoutedEventArgs e)
{
if (Settings.Port == string.Empty)
{
ErrorEmulation("port empty");
return;
}
2019-12-28 11:28:57 -07:00
StartEmulation();
Task.Run(() =>
{
ap = null;
try
{
ap = new AdapterProtocol(Settings.Port);
ap.Open();
ap.Clear();
twp = new ThreeWireProtocol(ap);
twp.StatusChanged += OnProgressUpdated;
twp.MrRunProducer();
}
catch (Exception ex)
{
ErrorEmulation(ex.Message);
}
finally
{
try
{
if (ap != null)
{
ap.Close();
}
}
catch (System.IO.IOException ex)
{
Log.Warn("could not close serial port: {0}", ex.Message);
2019-12-28 11:28:57 -07:00
}
}
});
}
private void OnProgressUpdated(object sender, EventArgs e)
{
this.Dispatcher.Invoke(() =>
{
TextArea.Text = twp.Status;
TextArea.ScrollToEnd();
});
}
private void Stop_Button_Click(object sender, RoutedEventArgs e)
{
if (ap != null)
{
ap.Cancel();
}
2019-12-28 11:28:57 -07:00
if (ap != null)
{
ap.Close();
}
2019-12-28 11:28:57 -07:00
StopEmulation();
}
}
}