/*-------------------------------------------------------------------------
// UNCLASSIFIED
/*-------------------------------------------------------------------------
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
COMPANY.
THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
GOVERNMENT.
UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Xml.XPath;
using MeasurementManagerLib;
using NLog;
using ProgramGui.GUI.ViewModel;
using ProgramLib.GUI.Model;
using Raytheon.Common.Coe;
using Raytheon.Instruments;
using Raytheon.Instruments.PowerSupply;
namespace ProgramLib.GUI.View
{
///
/// Interaction logic for ManualControlWindow.xaml
///
internal partial class ManualControlWindow : Window
{
public ManualWindowViewModel _manualWindowViewModel;
public LiveDataWindow LiveDataWindow { get; set; }
private ILogger _logger;
private Message _currentMsg;
private Dictionary _xmlDocs = new Dictionary();
private Dictionary _fieldNameToCoeMessageDataDict = new Dictionary();
private ManualGuiPowerSupplyReadThread _powerSupplyReadThread;
private CancellationTokenSource _coeSendMessageTaskCancellationSource = new CancellationTokenSource();
public ManualControlWindow()
{
InitializeComponent();
_logger = LogManager.GetCurrentClassLogger();
Uri iconUri = new Uri($"pack://application:,,,/{GetType().Assembly.GetName().Name};component/Resources/Icons/app.ico");
this.Icon = BitmapFrame.Create(iconUri);
WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
_manualWindowViewModel = new ManualWindowViewModel();
DataContext = _manualWindowViewModel;
_manualWindowViewModel._poweredModuleDataItems = new ObservableCollection();
_manualWindowViewModel._powerModuleComboBoxDataItems = new ObservableCollection();
}
~ManualControlWindow()
{
_logger?.Debug($"Entering {this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() ...");
_coeSendMessageTaskCancellationSource.Cancel();
if (_powerSupplyReadThread != null)
{
_powerSupplyReadThread.Quit();
_powerSupplyReadThread.WaitForExit();
}
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
if (_powerSupplyReadThread != null)
{
_powerSupplyReadThread.Quit();
_powerSupplyReadThread.WaitForExit();
_powerSupplyReadThread = null;
}
this.Hide();
if (LiveDataWindow != null)
{
LiveDataWindow.Show();
LiveDataWindow = null;
}
}
private void btnMax_Click(object sender, RoutedEventArgs e)
{
if (this.WindowState == WindowState.Maximized)
{
this.WindowState = WindowState.Normal;
imgMax.Source = new BitmapImage(new System.Uri($"pack://application:,,,/{GetType().Assembly.GetName().Name};component/Resources/Images/Title_Bar_Buttons/maximize.png"));
}
else
{
this.WindowState = WindowState.Maximized;
imgMax.Source = new BitmapImage(new System.Uri($"pack://application:,,,/{GetType().Assembly.GetName().Name};component/Resources/Images/Title_Bar_Buttons/restore.png"));
}
}
private void btnMin_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
TextBox textBox = Keyboard.FocusedElement as TextBox;
// unfocus text box if click outside of textbox
if (textBox != null)
{
// Kill logical focus
FocusManager.SetFocusedElement(FocusManager.GetFocusScope(textBox), null);
// Kill keyboard focus
Keyboard.ClearFocus();
}
DragMove();
}
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
TextBox textBox = Keyboard.FocusedElement as TextBox;
// unfocus text box if enter key is pressed
if (textBox != null)
{
if (e.Key == Key.Return)
{
if (e.Key == Key.Enter)
{
// Kill logical focus
FocusManager.SetFocusedElement(FocusManager.GetFocusScope(textBox), null);
// Kill keyboard focus
Keyboard.ClearFocus();
}
}
}
}
private void TextBox_GotKeyboardFocus(Object sender, KeyboardFocusChangedEventArgs e)
{
TextBox tb = (TextBox)sender;
tb.Dispatcher.BeginInvoke(new Action(() => tb.SelectAll()));
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox tb = (TextBox)sender;
tb.SelectAll();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
poppulatePowerSupplyTab();
poppulateDiscreteTab();
poppulateCoeTab();
}
catch (Exception ex)
{
_logger.Error(ex.Message + "\r\n" + ex.StackTrace);
}
}
private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (this.Visibility == Visibility.Visible)
{
List moduleList = Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.GetPowerModuleList();
foreach (string module in moduleList)
{
PowerModuleDataModel powerData = GetPowerModuleDataFromDatagridDataItems(module);
PowerModuleDataModel powerData2 = GetPowerModuleDataFromComboBoxDataItems(module);
if (powerData != null && !Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.IsPowerSupplyOn(module))
{
// update datagrid
_manualWindowViewModel._poweredModuleDataItems.Remove(powerData);
if (powerData2 != null)
powerData2.PowerLed = _manualWindowViewModel._imageToResourcePathDict[ManualWindowViewModel.Images.LED_OFF];
}
else if (Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.IsPowerSupplyOn(module))
{
// update datagrid
_manualWindowViewModel._poweredModuleDataItems.Add(powerData);
if (powerData2 != null)
powerData2.PowerLed = _manualWindowViewModel._imageToResourcePathDict[ManualWindowViewModel.Images.LED_ON];
}
}
if (powerModuleCb.SelectedItem != null)
{
string powerModule = ((PowerModuleDataModel)powerModuleCb.SelectedItem).Name;
if (Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.IsPowerSupplyOn(powerModule))
{
powerOnBtn.Content = "Power Off";
}
else
powerOnBtn.Content = "Power On";
}
}
}
///
/// Populate power supply tab
///
private void poppulatePowerSupplyTab()
{
string sectionName = "MANUAL_CONTROL_GUI.POWER_MODULES";
List keys = Program.Instance().ProgramSpecificConfig.ReadAllKeys(sectionName);
try
{
List moduleList = Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.GetPowerModuleList();
foreach (string module in moduleList)
{
PowerModuleDataModel data = new PowerModuleDataModel();
data.PowerLed = _manualWindowViewModel._imageToResourcePathDict[ManualWindowViewModel.Images.LED_OFF];
if (Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.IsPowerSupplyOn(module))
{
data.PowerLed = _manualWindowViewModel._imageToResourcePathDict[ManualWindowViewModel.Images.LED_ON];
}
data.Name = module;
data.ExpectedVoltage = Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.GetVoltageSetpoint(module).ToString("0.00");
_manualWindowViewModel._powerModuleComboBoxDataItems.Add(data);
}
}
catch (MalMeasurementManagerNullReferenceException ex)
{
_logger.Warn(ex.Message + "\r\n" + ex.StackTrace);
}
}
private void powerOnBtn_Click(object sender, RoutedEventArgs e)
{
bool success = true;
if (Program.Instance().IsUutPwrOn)
{
MessageBox.Show("Power to UUT is currently on. Please turn off power to UUT before manually controlling the power modules.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
try
{
((Button)sender).IsEnabled = false;
_manualWindowViewModel.GetPoweredModuleDataItemsSem().WaitOne();
string moduleName = ((PowerModuleDataModel)powerModuleCb.SelectedValue).Name;
PowerData data = Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.ReadPowerData(moduleName);
if (data.Voltage > 0.0)
{
Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.OutputDisable(moduleName);
}
string text = ((Button)sender).Content.ToString();
if (Regex.IsMatch(text, "power on", RegexOptions.IgnoreCase))
{
double voltageSetpoint = 0.0;
double ovp = 0.0;
double ocp = 0.0;
if (!Double.TryParse(voltageSetpointTb.Text, out voltageSetpoint))
{
MessageBox.Show("Voltage setpoint is invalid.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
success = false;
}
if (success)
{
if (!Double.TryParse(ovpTb.Text, out ovp))
{
MessageBox.Show("OVP is invalid.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
success = false;
}
}
if (success)
{
if (!Double.TryParse(ocpTb.Text, out ocp))
{
MessageBox.Show("OCP is invalid.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
success = false;
}
}
if (success)
{
Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.SetOverVoltageProtection(moduleName, ovp);
Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.SetVoltageSetpoint(moduleName, voltageSetpoint);
Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.SetOverCurrentProtection(moduleName, ocp);
Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.OutputEnable(moduleName);
((Button)sender).Content = "Power Off";
if (_powerSupplyReadThread == null)
{
_powerSupplyReadThread = new ManualGuiPowerSupplyReadThread();
_powerSupplyReadThread.Start();
}
}
}
else
{
Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.OutputDisable(moduleName);
((Button)sender).Content = "Power On";
}
}
catch (Exception ex)
{
_logger.Error(ex.Message + "\r\n" + ex.StackTrace);
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
_manualWindowViewModel.GetPoweredModuleDataItemsSem().Release();
((Button)sender).IsEnabled = true;
}
}
}
private void powerModuleCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string moduleName = ((PowerModuleDataModel)((ComboBox)sender).SelectedValue).Name;
double voltageSetpoint = 0.0;
double ovp = 0.0;
double ocp = 0.0;
voltageSetpoint = Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.GetVoltageSetpoint(moduleName);
ovp = Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.GetOverVoltageProtection(moduleName);
ocp = Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.GetOverCurrentProtection(moduleName);
voltageSetpointTb.Text = voltageSetpoint.ToString("0.00");
ovpTb.Text = ovp.ToString("0.00");
ocpTb.Text = ocp.ToString("0.00");
if (Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.IsPowerSupplyOn(moduleName))
{
powerOnBtn.Content = "Power Off";
}
else
powerOnBtn.Content = "Power On";
}
private void voltageSetpointTb_LostFocus(object sender, RoutedEventArgs e)
{
double voltageSetpoint = 0.0;
if (Double.TryParse(((TextBox)sender).Text, out voltageSetpoint) && powerModuleCb.SelectedItem != null)
{
string moduleName = ((PowerModuleDataModel)powerModuleCb.SelectedValue).Name;
PowerModuleDataModel data = GetPowerModuleDataFromComboBoxDataItems(moduleName);
string currentVoltageSetpoint = data.ExpectedVoltage;
if (voltageSetpoint.ToString("0.00") != currentVoltageSetpoint)
{
data.ExpectedVoltage = voltageSetpoint.ToString("0.00");
double ovp = voltageSetpoint + 0.5;
ovpTb.Text = ovp.ToString("0.00");
}
}
}
///
/// Get power module data from ComboBox
///
public PowerModuleDataModel GetPowerModuleDataFromComboBoxDataItems(string powerModuleName)
{
PowerModuleDataModel data = null;
foreach (PowerModuleDataModel item in _manualWindowViewModel._powerModuleComboBoxDataItems)
{
if (item.Name == powerModuleName)
data = item;
}
return data;
}
///
/// Get power module data from datagrid
///
public PowerModuleDataModel GetPowerModuleDataFromDatagridDataItems(string powerModuleName)
{
PowerModuleDataModel data = null;
foreach (PowerModuleDataModel item in _manualWindowViewModel._poweredModuleDataItems)
{
if (item.Name == powerModuleName)
data = item;
}
return data;
}
///
/// Populate Discrete tab
///
private void poppulateDiscreteTab()
{
try
{
foreach (KeyValuePair item in Program.Instance().MalMeasurementLibManager.DioMeasurementManager.SignalNameToChannelInfoMap)
{
if (item.Value.ioType == IODatatypes.IOType.DigitalInput)
{
inputDiscretesCb.Items.Add(item.Key);
}
else
outputDiscretesCb.Items.Add(item.Key);
}
}
catch (MalMeasurementManagerNullReferenceException ex)
{
_logger.Warn(ex.Message + "\r\n" + ex.StackTrace);
}
}
private void inputDiscreteReadBtn_Click(object sender, RoutedEventArgs e)
{
try
{
if (inputDiscretesCb.SelectedItem != null)
{
if (Program.Instance().MalMeasurementLibManager != null && Program.Instance().MalMeasurementLibManager.DioMeasurementManager != null)
{
IODatatypes.BitState state = Program.Instance().MalMeasurementLibManager.DioMeasurementManager.GetInputSignalState(inputDiscretesCb.SelectedValue.ToString());
inputDiscreteStatusTb.Text = state.ToString();
}
}
else
{
MessageBox.Show("Please select an input signal.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (Exception ex)
{
_logger.Error(ex.Message + "\r\n" + ex.StackTrace);
}
}
private void outputDiscreteWriteBtn_Click(object sender, RoutedEventArgs e)
{
bool success = true;
try
{
if (outputDiscretesCb.SelectedItem == null)
{
MessageBox.Show("Please select an output signal.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
success = false;
}
if (success)
{
if (outputDiscreteStateCb.SelectedItem == null)
{
MessageBox.Show("Please select an output signal state.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
success = false;
}
}
if (success)
{
if (Program.Instance().MalMeasurementLibManager != null && Program.Instance().MalMeasurementLibManager.DioMeasurementManager != null)
{
IODatatypes.BitState state;
if (Enum.TryParse(outputDiscreteStateCb.SelectedValue.ToString(), out state))
{
Program.Instance().MalMeasurementLibManager.DioMeasurementManager.SetOutputSignalState(outputDiscretesCb.SelectedValue.ToString(), state);
}
}
}
}
catch (Exception ex)
{
_logger.Error(ex.Message + "\r\n" + ex.StackTrace);
}
}
///
/// Populate COE tab
///
private void poppulateCoeTab()
{
try
{
ICollection