From e29d962c5602fc9fc3a54fa4da8957609de7eea4 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Sun, 17 Feb 2019 15:52:11 +0200 Subject: Completed PPC watchdog ! --- .../PPC/Tango.PPC.WatchDog/App.xaml.cs | 12 +++ .../PPC/Tango.PPC.WatchDog/MainWindow.xaml | 37 ++++--- .../PPC/Tango.PPC.WatchDog/MainWindow.xaml.cs | 44 +++++++++ .../PPC/Tango.PPC.WatchDog/MainWindowVM.cs | 107 +++++++++++++++++++++ .../Tango.PPC.WatchDog/Tango.PPC.WatchDog.csproj | 16 ++- .../Visual_Studio/PPC/Tango.PPC.WatchDog/cat24.ico | Bin 0 -> 105237 bytes .../Visual_Studio/PPC/Tango.PPC.WatchDog/close.png | Bin 651 -> 0 bytes 7 files changed, 201 insertions(+), 15 deletions(-) create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.WatchDog/cat24.ico delete mode 100644 Software/Visual_Studio/PPC/Tango.PPC.WatchDog/close.png (limited to 'Software/Visual_Studio/PPC/Tango.PPC.WatchDog') diff --git a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/App.xaml.cs b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/App.xaml.cs index 3e39963bd..2247b95e5 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/App.xaml.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/App.xaml.cs @@ -5,6 +5,7 @@ using System.Data; using System.Linq; using System.Threading.Tasks; using System.Windows; +using Tango.Logging; namespace Tango.PPC.WatchDog { @@ -13,5 +14,16 @@ namespace Tango.PPC.WatchDog /// public partial class App : Application { + private LogManager LogManager = LogManager.Default; + + protected override void OnStartup(StartupEventArgs e) + { + LogManager.RegisterLogger(new FileLogger()); + LogManager.RegisterLogger(new VSOutputLogger()); + + LogManager.Log("Watchdog process started..."); + + base.OnStartup(e); + } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindow.xaml b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindow.xaml index 4e772e774..82056048e 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindow.xaml +++ b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindow.xaml @@ -3,31 +3,44 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:components="clr-namespace:Tango.SharedUI.Components;assembly=Tango.SharedUI" xmlns:local="clr-namespace:Tango.PPC.WatchDog" mc:Ignorable="d" - Title="Tango Watch Dog" Background="Transparent" AllowsTransparency="True" Height="250" Width="500" WindowStartupLocation="CenterScreen" WindowStyle="None" ResizeMode="NoResize" Icon="/cat.png" + Title="Tango WatchDog" Visibility="Visible" Background="Transparent" AllowsTransparency="True" Height="400" Width="700" WindowStartupLocation="CenterScreen" WindowStyle="None" ResizeMode="NoResize" Icon="/cat.png" d:DataContext="{d:DesignInstance Type=local:MainWindowVM, IsDesignTimeCreatable=False}"> - + - + - Tango Watch Dog - + Tango WatchDog + + + + + - + + + diff --git a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindow.xaml.cs b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindow.xaml.cs index 585fc3510..1e3c90342 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindow.xaml.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindow.xaml.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -7,11 +8,14 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; +using System.Windows.Forms; using System.Windows.Input; +using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using Tango.SharedUI.Helpers; namespace Tango.PPC.WatchDog { @@ -20,10 +24,50 @@ namespace Tango.PPC.WatchDog /// public partial class MainWindow : Window { + private NotifyIcon _icon; + public MainWindow() { InitializeComponent(); DataContext = new MainWindowVM(); + Visibility = Visibility.Hidden; + + var helper = new WindowInteropHelper(this); + helper.EnsureHandle(); + + _icon = new NotifyIcon(); + _icon.Icon = new System.Drawing.Icon(Core.Helpers.EmbeddedResourceHelper.GetEmbeddedResourceStream("Tango.PPC.WatchDog.cat24.ico")); + _icon.BalloonTipIcon = ToolTipIcon.Info; + _icon.BalloonTipTitle = Title; + _icon.BalloonTipText = "Working..."; + _icon.Click += _icon_Click; + _icon.Visible = true; + } + + private void _icon_Click(object sender, EventArgs e) + { + _icon.Visible = false; + Show(); + } + + private void Button_Click(object sender, RoutedEventArgs e) + { + _icon.Visible = true; + Hide(); + } + + protected override void OnClosing(CancelEventArgs e) + { + _icon.Visible = false; + _icon.Dispose(); + base.OnClosing(e); + } + + private void Button_Click_1(object sender, RoutedEventArgs e) + { + _icon.Visible = false; + _icon.Dispose(); + Environment.Exit(0); } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindowVM.cs b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindowVM.cs index a95a9503c..466c178a2 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindowVM.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindowVM.cs @@ -1,14 +1,121 @@ using System; using System.Collections.Generic; +using System.Diagnostics; +using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; +using Tango.Core.Helpers; +using Tango.Logging; +using Tango.PPC.Common.WatchDog; using Tango.SharedUI; +using Tango.SharedUI.Components; namespace Tango.PPC.WatchDog { public class MainWindowVM : ViewModel { + private const String tango_process_name = "Tango.PPC.UI"; + private WatchDogClient _watchdog; + public TextController Log { get; set; } + public MainWindowVM() + { + var logger = new SimpleStringLogger(); + logger.LogReceived += Logger_LogReceived; + LogManager.RegisterLogger(logger); + + _watchdog = new WatchDogClient(); + _watchdog.ServerNotResponding += _watchdog_ServerNotResponding; + Log = new TextController(); + + _watchdog.Start(); + } + + private void Logger_LogReceived(object sender, LogItemBase e) + { + if (e is ExceptionLogItem) + { + Log.WriteLine(e.ToString()); + } + else + { + Log.WriteLine($"[{e.TimeStamp.ToString()}] {e.Message}"); + } + } + + private void _watchdog_ServerNotResponding(object sender, EventArgs e) + { + LogManager.Log("PPC application failed to respond!", LogCategory.Warning); + LogManager.Log("Ensuring PPC application is down..."); + + var appProcess = Process.GetProcessesByName(tango_process_name).FirstOrDefault(); + Process p = new Process(); + p.StartInfo.CreateNoWindow = true; + p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; + p.StartInfo.FileName = "wmic"; + p.StartInfo.Arguments = String.Format("process where name='{0}' delete", tango_process_name); + + try + { + if (appProcess != null) + { + LogManager.Log("PPC application process found and seems to be frozen. Trying to kill the process..."); + appProcess.Kill(); + + appProcess = Process.GetProcessesByName(tango_process_name).FirstOrDefault(); + + if (appProcess != null) + { + LogManager.Log("Process kill failed. Trying again..."); + p.Start(); + Thread.Sleep(2000); + + appProcess = Process.GetProcessesByName(tango_process_name).FirstOrDefault(); + + if (appProcess == null) + { + LogManager.Log("PPC application is down."); + } + } + else + { + LogManager.Log("PPC application is down."); + } + } + } + catch + { + try + { + LogManager.Log("Process kill failed. Trying again..."); + p.Start(); + Thread.Sleep(2000); + + appProcess = Process.GetProcessesByName(tango_process_name).FirstOrDefault(); + + if (appProcess == null) + { + LogManager.Log("PPC application is down."); + } + } + catch { } + } + + LogManager.Log("Restarting PPC application..."); + + try + { + Process.Start(Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), tango_process_name + ".exe")); + LogManager.Log("PPC application started."); + Thread.Sleep(5000); + _watchdog.Start(); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error starting PPC application."); + } + } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/Tango.PPC.WatchDog.csproj b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/Tango.PPC.WatchDog.csproj index b9429e044..a1e6e5e88 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/Tango.PPC.WatchDog.csproj +++ b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/Tango.PPC.WatchDog.csproj @@ -37,6 +37,8 @@ + + @@ -101,18 +103,26 @@ - - - {a34ee0f0-649d-41c8-8489-b6f1cc6924ee} Tango.Core + + {BC932DBD-7CDB-488C-99E4-F02CF441F55E} + Tango.Logging + {8491d07b-c1f6-4b62-a412-41b9fd2d6538} Tango.SharedUI + + {0be74eee-22cb-4dba-b896-793b9e1a3ac0} + Tango.PPC.Common + + + + \ No newline at end of file diff --git a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/cat24.ico b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/cat24.ico new file mode 100644 index 000000000..6fa3cb368 Binary files /dev/null and b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/cat24.ico differ diff --git a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/close.png b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/close.png deleted file mode 100644 index 3a040a008..000000000 Binary files a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/close.png and /dev/null differ -- cgit v1.3.1