Files
GenericTeProgramLibrary/Source/Program/GUI/View/LiveDataWindow.xaml.cs
2025-10-24 15:18:11 -07:00

373 lines
15 KiB
C#

/*-------------------------------------------------------------------------
// 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.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using ProgramLib.GUI.ViewModel;
namespace ProgramLib.GUI.View
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
internal partial class LiveDataWindow : Window
{
private enum Events
{
GLOBAL_QUIT,
QUIT,
UUT_POWER_ON,
UUT_POWER_OFF,
// DO NOT change the name
// This must be the last member in the enum
EVENT_TIMED_OUT
}
internal LiveDataWindowViewModel LiveDataWindowViewModel { get; set; }
private DispatcherTimer _oneSecTimer;
private DateTime _testDateTime;
private DateTime? _powerOnDateTime = null;
EventGroup<Events, EventWaitHandle> _eventGroup;
public LiveDataWindow()
{
InitializeComponent();
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;
DateTime assemblyBuildDateTime = File.GetLastWriteTime(GetType().Assembly.Location);
string assemblyBuildDateTimeStr = assemblyBuildDateTime.ToString("MM/dd/yyyy");
Version appVersion = GetType().Assembly.GetName().Version;
this.txtBlockAppTitle.Text += $" - {assemblyBuildDateTimeStr} - Version " + appVersion.Major.ToString() + "." + appVersion.Minor.ToString();
LiveDataWindowViewModel = new LiveDataWindowViewModel(this);
DataContext = LiveDataWindowViewModel;
LiveDataWindowViewModel.LabelGradientAppControlledOrUnControlled = "Controlled";
if (!Program.Instance().IsAppControlled)
{
LiveDataWindowViewModel.LabelGradientAppControlledOrUnControlled = "UnControlled";
}
LiveDataWindowViewModel.UutPowerLedImagePath = LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_OFF];
LiveDataWindowViewModel.W1CableImagePath = LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_OFF];
LiveDataWindowViewModel.W2CableImagePath = LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_OFF];
LiveDataWindowViewModel.W3CableImagePath = LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_OFF];
LiveDataWindowViewModel.W4CableImagePath = LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_OFF];
LiveDataWindowViewModel.W5CableImagePath = LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_OFF];
if (Program.Instance().IsThereHardware)
{
LiveDataWindowViewModel.TePowerLedImagePath = LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_ON];
}
else
{
simModeBdr.BorderThickness = new Thickness(1);
runModeTb.Margin = new Thickness(5, 0, 5, 0);
runModeTb.Text = "Simulation Mode";
LiveDataWindowViewModel.TePowerLedImagePath = LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_OFF];
}
Dictionary<Events, EventWaitHandle> eventDict = new Dictionary<Events, EventWaitHandle>();
eventDict = new Dictionary<Events, EventWaitHandle>();
eventDict[Events.UUT_POWER_ON] = Program.Instance().EventManager[EventManager.Events.UUT_POWER_ON];
eventDict[Events.UUT_POWER_OFF] = Program.Instance().EventManager[EventManager.Events.UUT_POWER_OFF];
eventDict[Events.EVENT_TIMED_OUT] = null;
_eventGroup = new EventGroup<Events, EventWaitHandle>(eventDict);
_oneSecTimer = new DispatcherTimer();
_oneSecTimer.Interval = TimeSpan.FromMilliseconds(1000);
_oneSecTimer.Tick += new EventHandler(Timer_Tick);
_oneSecTimer.Start();
_testDateTime = DateTime.Now;
CheckDriveCapacity(Program.Instance().FileAndFolderManager.GetFolder(FileAndFolderManager.Folders.DATA_TEST));
ShowConnecterdUut();
ShowConnecterdCable();
}
~LiveDataWindow()
{
_oneSecTimer?.Stop();
}
/// <summary>
/// Show drive capacity
/// </summary>
private void CheckDriveCapacity(string driveNameOrPath)
{
bDataDriveTotalCapacityBarGraph.Width = 190;
tbDataDriveUsedCapacityBarGraph.Text = $"Can't get info for {driveNameOrPath}";
tbDataDriveUsedCapacityBarGraph.Foreground = new SolidColorBrush(Colors.Red);
string freeSpaceColorCode = "#007AD7";
if (driveNameOrPath.Length < 3)
{
driveNameOrPath += "\\";
}
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.IsReady && Regex.IsMatch(driveNameOrPath, Regex.Escape(drive.Name), RegexOptions.IgnoreCase))
{
// text color
tbDataDriveUsedCapacityBarGraph.Foreground = new SolidColorBrush(Colors.White);
long freeSpacePercentage = (long)(((double)drive.TotalFreeSpace / (double)drive.TotalSize) * 100.0);
int freeSpaceThreadHoldPercent = 20;
tbDataDriveUsedCapacityBarGraph.Width = ((double)(100 - freeSpacePercentage) / 100.0) * bDataDriveTotalCapacityBarGraph.Width;
if (freeSpacePercentage >= freeSpaceThreadHoldPercent)
{
tbDataDriveUsedCapacityBarGraph.Background = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(freeSpaceColorCode));
}
else
{
tbDataDriveUsedCapacityBarGraph.Background = new SolidColorBrush(Colors.Red);
}
tbDataDriveUsedCapacityBarGraph.Text = driveNameOrPath.Substring(0, 2) + "\\";
tbDataDriveCapacityDetails.Text = $"{DescribeLargestDriveStorageSize(drive.TotalFreeSpace)} free of {DescribeLargestDriveStorageSize(drive.TotalSize)}";
break;
}
}
}
/// <summary>
/// Describe largest unit size of drive storage
/// </summary>
private string DescribeLargestDriveStorageSize(long byteCount)
{
string size = "";
UInt64 oneKb = 1024;
UInt64 oneMb = oneKb * oneKb;
UInt64 oneGb = oneMb * oneKb;
UInt64 oneTb = oneGb * oneKb;
long kilobytes = (long)((double)((ulong)byteCount % oneMb) / (double)oneKb);
long megabytes = (long)((double)((ulong)byteCount % oneGb) / (double)oneMb);
long gigabytes = (long)((double)((ulong)byteCount % oneTb) / (double)oneGb);
long terabytes = (long)((double)(ulong)byteCount / (double)oneTb);
if (terabytes > 0)
{
size = $"{terabytes.ToString()} TB";
}
else if (gigabytes > 0)
{
size = $"{gigabytes.ToString()} GB";
}
else if (megabytes > 0)
{
size = $"{megabytes.ToString()} MB";
}
else if (kilobytes > 0)
{
size = $"{kilobytes.ToString()} KB";
}
else
size = $"{byteCount.ToString()} B";
return size;
}
/// <summary>
/// Show running test time and power on time
/// </summary>
private void Timer_Tick(object sender, EventArgs e)
{
TimeSpan ts = DateTime.Now - _testDateTime;
LiveDataWindowViewModel.TestTimeStr = ProgramLib.Util.DescribeTimeElapsed(ts);
Events id = _eventGroup.WaitAny(0);
if (id == Events.UUT_POWER_ON)
{
if (_powerOnDateTime == null)
{
_powerOnDateTime = DateTime.Now;
}
ts = DateTime.Now - (DateTime)_powerOnDateTime;
LiveDataWindowViewModel.PowerOnTimeStr = ProgramLib.Util.DescribeTimeElapsed(ts);
}
else if (id == Events.UUT_POWER_OFF && _powerOnDateTime != null)
{
_powerOnDateTime = null;
LiveDataWindowViewModel.PowerOnTimeStr = "---";
}
}
/// <summary>
/// Show which UUT is connected on th GUI
/// </summary>
private void ShowConnecterdUut()
{
string uutBuildLevel = "NO UUT";
if (Program.Instance().UutInfo.UutBuildLevel == UutInfo.BuildLevel.GMA)
{
uutBuildLevel = "Guided Missile Assem. (GMA)";
}
else if (Program.Instance().UutInfo.UutBuildLevel == UutInfo.BuildLevel.AUR)
{
uutBuildLevel = "All-Up-Round (AUR)";
}
else if (Program.Instance().UutInfo.UutBuildLevel == UutInfo.BuildLevel.SELF_TEST)
{
uutBuildLevel = "Self-Test Cable";
}
TextBlock textBlock = new TextBlock();
textBlock.Text = uutBuildLevel;
textBlock.FontWeight = FontWeights.Bold;
textBlock.FontSize = 16;
textBlock.HorizontalAlignment = HorizontalAlignment.Center;
connectedUutSp.Children.Add(textBlock);
if (Program.Instance().UutInfo.UutBuildLevel == UutInfo.BuildLevel.AUR || Program.Instance().UutInfo.UutBuildLevel == UutInfo.BuildLevel.GMA)
{
System.Windows.Controls.Image image = new System.Windows.Controls.Image();
BitmapImage myImageSource = new BitmapImage();
myImageSource.BeginInit();
textBlock.FontSize = 14;
int imageWidth = 154;
int imageHeight = 30;
if (Program.Instance().UutInfo.UutBuildLevel == UutInfo.BuildLevel.GMA)
{
myImageSource.UriSource = new Uri($"pack://application:,,,/{GetType().Assembly.GetName().Name};component/Resources/Images/gma.png");
imageWidth = 148;
imageHeight = 20;
}
else
myImageSource.UriSource = new Uri($"pack://application:,,,/{GetType().Assembly.GetName().Name};component/Resources/Images/aur.png");
myImageSource.EndInit();
image.Source = myImageSource;
image.Width = imageWidth;
image.Height = imageHeight;
image.Margin = new Thickness(0, 5, 0, 0);
connectedUutSp.Children.Add(image);
}
}
/// <summary>
/// Show which cable is connected on th GUI
/// </summary>
private void ShowConnecterdCable()
{
var brushConverter = new BrushConverter();
missileModeBdr.BorderThickness = new Thickness(0, 1, 0, 0);
if (Program.Instance().UutInfo.UniversalCableId == UutInfo.UniversalCable.W1)
{
LiveDataWindowViewModel.W1CableImagePath = LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_ON];
missileModeBdr.BorderBrush = (System.Windows.Media.Brush)brushConverter.ConvertFrom("#DCC3AD");
missileModeBdr.Background = (System.Windows.Media.Brush)brushConverter.ConvertFrom("#FDE6AE");
missileModeTb.Foreground = (System.Windows.Media.Brush)brushConverter.ConvertFrom("#A4530D");
missileModeTb.Text = "Maintenance Mode";
}
else if (Program.Instance().UutInfo.UniversalCableId == UutInfo.UniversalCable.W2)
{
LiveDataWindowViewModel.W2CableImagePath = LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_ON];
missileModeBdr.BorderBrush = (System.Windows.Media.Brush)brushConverter.ConvertFrom("#94D487");
missileModeBdr.Background = (System.Windows.Media.Brush)brushConverter.ConvertFrom("#DBFFD7");
missileModeTb.Foreground = (System.Windows.Media.Brush)brushConverter.ConvertFrom("#1C7F08");
missileModeTb.Text = "Tactical Mode";
}
if (Program.Instance().UutInfo.UutBuildLevel == UutInfo.BuildLevel.SELF_TEST)
{
LiveDataWindowViewModel.W5CableImagePath = LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_ON];
}
if (Program.Instance().UutInfo.SacrificialCableId == UutInfo.SacrificialCable.W3)
{
LiveDataWindowViewModel.W3CableImagePath = LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_ON];
}
else if (Program.Instance().UutInfo.SacrificialCableId == UutInfo.SacrificialCable.W4)
{
LiveDataWindowViewModel.W4CableImagePath = LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_ON];
}
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
this.Hide();
}
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)
{
DragMove();
}
private void dataLocationMi_Click(object sender, RoutedEventArgs e)
{
DataLocationWindow window = new DataLocationWindow();
window.ShowDialog();
}
private void manualControlGuiMi_Click(object sender, RoutedEventArgs e)
{
this.Hide();
((ManualControlWindow)Program.Instance().GuiManager[ProgramGuiManager.WINDOWS.MANUAL_CONTROL]).LiveDataWindow = this;
Program.Instance().GuiManager[ProgramGuiManager.WINDOWS.MANUAL_CONTROL].Show();
}
}
}