2019-07-29 15:24:10 -06:00
using KFDtool.Adapter.Device ;
using KFDtool.P25.TransferConstructs ;
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
2019-08-25 19:44:07 -06:00
using System.Diagnostics ;
2019-07-29 15:24:10 -06:00
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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static NLog . Logger Logger = NLog . LogManager . GetCurrentClassLogger ( ) ;
private AutoDetection AppDet ;
public MainWindow ( )
{
InitializeComponent ( ) ;
Logger . Info ( "starting" ) ;
AppDet = new AutoDetection ( ) ;
AppDet . DevicesChanged + = CheckConnectedDevices ;
AppDet . Start ( ) ;
// on load select the P25 Keyload function
2019-12-28 11:28:57 -07:00
SwitchScreen ( NavigateP25Keyload ) ;
2019-07-29 15:24:10 -06:00
}
void MainWindow_Closing ( object sender , CancelEventArgs e )
{
2019-12-28 11:56:11 -07:00
if ( Settings . InProgressScreen ! = string . Empty )
{
UpdateSelectionOnly ( Settings . InProgressScreen ) ;
MessageBox . Show ( "Unable to exit - please stop the current operation" , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
e . Cancel = true ;
}
2019-07-29 15:24:10 -06:00
Logger . Info ( "stopping" ) ;
// have to stop the WMI watcher or a RCW exception will be thrown
AppDet . Stop ( ) ;
}
private void UpdateTitle ( string s )
{
2019-08-11 12:17:37 -06:00
#if DEBUG
this . Title = string . Format ( "KFDtool {0} DEBUG [{1}]" , Settings . AssemblyInformationalVersion , s ) ;
#else
2019-07-29 15:24:10 -06:00
this . Title = string . Format ( "KFDtool {0} [{1}]" , Settings . AssemblyInformationalVersion , s ) ;
2019-08-11 12:17:37 -06:00
#endif
2019-07-29 15:24:10 -06:00
}
private void Navigate_MenuItem_Click ( object sender , RoutedEventArgs e )
{
MenuItem mi = sender as MenuItem ;
if ( mi ! = null )
{
2019-12-28 11:28:57 -07:00
if ( Settings . InProgressScreen ! = string . Empty )
2019-07-29 15:24:10 -06:00
{
2019-12-28 11:28:57 -07:00
UpdateSelectionOnly ( Settings . InProgressScreen ) ;
MessageBox . Show ( "Unable to change screens - please stop the current operation" , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
2019-07-29 15:24:10 -06:00
}
2019-12-28 11:28:57 -07:00
else
2019-07-29 15:24:10 -06:00
{
2019-12-28 11:28:57 -07:00
SwitchScreen ( mi ) ;
2019-07-29 15:24:10 -06:00
}
2019-12-28 11:28:57 -07:00
}
}
2019-07-29 15:24:10 -06:00
2019-12-28 11:28:57 -07:00
private void ClearAllSelections ( )
{
foreach ( MenuItem item in P25KfdMenu . Items )
{
item . IsChecked = false ;
}
2019-07-29 15:24:10 -06:00
2019-12-28 11:28:57 -07:00
foreach ( MenuItem item in P25MrMenu . Items )
{
item . IsChecked = false ;
}
foreach ( MenuItem item in UtilityMenu . Items )
{
item . IsChecked = false ;
}
}
private void UpdateSelectionOnly ( string item )
{
ClearAllSelections ( ) ;
if ( item = = "NavigateP25MrEmulator" )
{
NavigateP25MrEmulator . IsChecked = true ;
}
else
{
Logger . Fatal ( "unknown item passed to UpdateSelectionOnly - {0}" , item ) ;
Application . Current . Shutdown ( 1 ) ;
}
}
private void SwitchScreen ( MenuItem mi )
{
ClearAllSelections ( ) ;
mi . IsChecked = true ;
if ( mi . Name = = "NavigateP25Keyload" )
{
AppView . Content = new Control . P25Keyload ( ) ;
UpdateTitle ( "P25 KFD - Keyload" ) ;
}
else if ( mi . Name = = "NavigateP25KeyErase" )
{
AppView . Content = new Control . P25KeyErase ( ) ;
UpdateTitle ( "P25 KFD - Key Erase" ) ;
}
else if ( mi . Name = = "NavigateP25EraseAllKeys" )
{
AppView . Content = new Control . P25EraseAllKeys ( ) ;
UpdateTitle ( "P25 KFD - Erase All Keys" ) ;
}
else if ( mi . Name = = "NavigateP25ViewKeyInfo" )
{
AppView . Content = new Control . P25ViewKeyInfo ( ) ;
UpdateTitle ( "P25 KFD - View Key Info" ) ;
}
else if ( mi . Name = = "NavigateP25MrEmulator" )
{
AppView . Content = new Control . P25MrEmulator ( ) ;
UpdateTitle ( "P25 MR - Emulator" ) ;
}
else if ( mi . Name = = "NavigateUtilityFixDesKeyParity" )
{
AppView . Content = new Control . UtilFixDesKeyParity ( ) ;
UpdateTitle ( "Utility - Fix DES Key Parity" ) ;
}
else if ( mi . Name = = "NavigateUtilityUpdateAdapterFirmware" )
{
AppView . Content = new Control . UtilUpdateAdapterFw ( ) ;
UpdateTitle ( "Utility - Update Adapter Firmware" ) ;
}
else if ( mi . Name = = "NavigateUtilityInitializeAdapter" )
{
AppView . Content = new Control . UtilInitAdapter ( ) ;
UpdateTitle ( "Utility - Initialize Adapter" ) ;
}
else if ( mi . Name = = "NavigateUtilityAdapterSelfTest" )
{
AppView . Content = new Control . UtilAdapterSelfTest ( ) ;
UpdateTitle ( "Utility - Adapter Self Test" ) ;
}
else
{
Logger . Fatal ( "unknown item passed to SwitchScreen - {0}" , mi . Name ) ;
Application . Current . Shutdown ( 1 ) ;
2019-07-29 15:24:10 -06:00
}
}
private void CheckConnectedDevices ( object sender , EventArgs e )
{
Logger . Debug ( "device list updated" ) ;
// needed to access UI elements from different thread
this . Dispatcher . Invoke ( ( ) = >
{
bool first = true ;
List < string > ports = AppDet . Devices ;
// sort ports lowest to highest (COM6,COM12,COM26)
ports . Sort ( ) ;
DeviceMenu . Items . Clear ( ) ;
// no devices detected
if ( ports . Count = = 0 )
{
Settings . Port = string . Empty ;
lblSelectedDevice . Text = "Selected Device: None" ;
MenuItem item = new MenuItem ( ) ;
item . Header = "No devices found" ;
item . IsCheckable = false ;
item . IsEnabled = false ;
DeviceMenu . Items . Add ( item ) ;
}
// one or more devices detected
foreach ( string port in ports )
{
MenuItem item = new MenuItem ( ) ;
item . Name = port ;
item . Header = port ;
item . IsCheckable = true ;
item . Click + = Device_MenuItem_Click ;
DeviceMenu . Items . Add ( item ) ;
// there was a change in the device list, but the device that was previously selected is still connected
if ( port = = Settings . Port )
{
SelectDevice ( item ) ;
first = false ;
}
// select the lowest numbered device if there is no device currently selected
// or if the device that was previously selected was disconnected
if ( first )
{
SelectDevice ( item ) ;
first = false ;
}
}
} ) ;
}
private void Device_MenuItem_Click ( object sender , RoutedEventArgs e )
{
MenuItem mi = sender as MenuItem ;
SelectDevice ( mi ) ;
}
private void SelectDevice ( MenuItem mi )
{
if ( mi ! = null )
{
foreach ( MenuItem item in DeviceMenu . Items )
{
item . IsChecked = false ;
}
2019-12-28 11:18:24 -07:00
string apVerStr = string . Empty ;
2019-07-29 15:24:10 -06:00
try
{
2019-12-28 11:18:24 -07:00
apVerStr = Interact . ReadAdapterProtocolVersion ( mi . Name ) ;
2019-07-29 15:24:10 -06:00
}
catch ( Exception ex )
{
MessageBox . Show ( string . Format ( "Error -- {0}" , ex . Message ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
return ;
}
2019-12-28 11:18:24 -07:00
Version apVersion = new Version ( apVerStr ) ;
if ( apVersion . Major ! = 1 )
2019-07-29 15:24:10 -06:00
{
2019-12-28 11:18:24 -07:00
MessageBox . Show ( string . Format ( "Adapter protocol version not compatible ({0})" , apVerStr ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
2019-08-14 07:53:42 -06:00
return ;
}
2019-07-29 15:24:10 -06:00
2019-08-14 07:53:42 -06:00
string fwVersion = string . Empty ;
string uniqueId = string . Empty ;
string model = string . Empty ;
string hwRev = string . Empty ;
string serialNum = string . Empty ;
2019-07-29 15:24:10 -06:00
2019-08-14 07:53:42 -06:00
try
{
fwVersion = Interact . ReadFirmwareVersion ( mi . Name ) ;
uniqueId = Interact . ReadUniqueId ( mi . Name ) ;
model = Interact . ReadModel ( mi . Name ) ;
hwRev = Interact . ReadHardwareRevision ( mi . Name ) ;
serialNum = Interact . ReadSerialNumber ( mi . Name ) ;
2019-07-29 15:24:10 -06:00
}
2019-08-14 07:53:42 -06:00
catch ( Exception ex )
2019-07-29 15:24:10 -06:00
{
2019-08-14 07:53:42 -06:00
MessageBox . Show ( string . Format ( "Error -- {0}" , ex . Message ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
return ;
2019-07-29 15:24:10 -06:00
}
2019-08-14 07:53:42 -06:00
mi . IsChecked = true ;
Settings . Port = mi . Name ;
lblSelectedDevice . Text = string . Format ( "Selected Device: {0}, Model: {1}, HW: {2}, Serial: {3}, UID: {4}, FW: {5}" , Settings . Port , model , hwRev , serialNum , uniqueId , fwVersion ) ;
2019-07-29 15:24:10 -06:00
}
}
private void Exit_MenuItem_Click ( object sender , RoutedEventArgs e )
{
Application . Current . Shutdown ( ) ;
}
2019-08-25 19:44:07 -06:00
private void Manual_MenuItem_Click ( object sender , RoutedEventArgs e )
{
try
{
Process . Start ( "KFDtool_Manual.pdf" ) ;
}
catch ( Exception ex )
{
MessageBox . Show ( string . Format ( "Error -- {0}" , ex . Message ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
}
}
private void Website_MenuItem_Click ( object sender , RoutedEventArgs e )
{
Process . Start ( "https://www.kfdtool.com" ) ;
}
2019-07-29 15:24:10 -06:00
private void About_MenuItem_Click ( object sender , RoutedEventArgs e )
{
2019-08-11 12:17:37 -06:00
#if DEBUG
2020-01-01 07:49:39 -07:00
MessageBox . Show ( string . Format ( "KFDtool Control Application{0}{0}Copyright 2019-2020 Daniel Dugger{0}{0}Version: {1} DEBUG" , Environment . NewLine , Settings . AssemblyInformationalVersion ) , "About" , MessageBoxButton . OK ) ;
2019-08-11 12:17:37 -06:00
#else
2020-01-01 07:49:39 -07:00
MessageBox . Show ( string . Format ( "KFDtool Control Application{0}{0}Copyright 2019-2020 Daniel Dugger{0}{0}Version: {1}" , Environment . NewLine , Settings . AssemblyInformationalVersion ) , "About" , MessageBoxButton . OK ) ;
2019-08-11 12:17:37 -06:00
#endif
2019-07-29 15:24:10 -06:00
}
}
}