diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-02-17 15:52:11 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-02-17 15:52:11 +0200 |
| commit | e29d962c5602fc9fc3a54fa4da8957609de7eea4 (patch) | |
| tree | 857c8448611b79c3d3834f37edb00965ad5435ba /Software/Visual_Studio/PPC/Tango.PPC.WatchDog | |
| parent | 0cd0b590f62b31a8874ea21f225ba75c7a37053c (diff) | |
| download | Tango-e29d962c5602fc9fc3a54fa4da8957609de7eea4.tar.gz Tango-e29d962c5602fc9fc3a54fa4da8957609de7eea4.zip | |
Completed PPC watchdog !
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.WatchDog')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.WatchDog/App.xaml.cs | 12 | ||||
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindow.xaml | 37 | ||||
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindow.xaml.cs | 44 | ||||
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindowVM.cs | 107 | ||||
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.WatchDog/Tango.PPC.WatchDog.csproj | 16 | ||||
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.WatchDog/cat24.ico | bin | 0 -> 105237 bytes | |||
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.WatchDog/close.png | bin | 651 -> 0 bytes |
7 files changed, 201 insertions, 15 deletions
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 /// </summary> 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}"> <Grid> - <Border Margin="10" BorderThickness="1" BorderBrush="DimGray"> + <Border Margin="10" BorderThickness="1" BorderBrush="#1c63ea"> <Border.Effect> <DropShadowEffect ShadowDepth="0" BlurRadius="10" Color="Black" /> </Border.Effect> <DockPanel> - <Border DockPanel.Dock="Top" BorderThickness="0 0 0 1" BorderBrush="Gainsboro" Background="#202020" Padding="10"> + <Border DockPanel.Dock="Top" BorderThickness="0 0 0 1" BorderBrush="Gainsboro" Background="#1c63ea" Padding="10"> <DockPanel> <Image Source="/cat.png" Stretch="Uniform" Height="32" RenderOptions.BitmapScalingMode="Fant" /> - <TextBlock Margin="10 0 0 0" Foreground="Gainsboro" VerticalAlignment="Center">Tango Watch Dog</TextBlock> - <Button DockPanel.Dock="Right" Width="24" Height="24" HorizontalAlignment="Right" Cursor="Hand"> - <Button.Template> - <ControlTemplate TargetType="Button"> - <Image Source="/close.png" Width="12" Height="12"></Image> - </ControlTemplate> - </Button.Template> - </Button> + <TextBlock Margin="10 0 0 0" Foreground="Gainsboro" VerticalAlignment="Center" FontSize="14">Tango WatchDog</TextBlock> + + <StackPanel Orientation="Horizontal" DockPanel.Dock="Right" HorizontalAlignment="Right"> + <Button Height="24" HorizontalAlignment="Right" Cursor="Hand" Click="Button_Click_1" Margin="0 0 20 0"> + <Button.Template> + <ControlTemplate TargetType="Button"> + <TextBlock Foreground="White" VerticalAlignment="Center" TextDecorations="Underline">Terminate</TextBlock> + </ControlTemplate> + </Button.Template> + </Button> + <Button Height="24" HorizontalAlignment="Right" Cursor="Hand" Click="Button_Click"> + <Button.Template> + <ControlTemplate TargetType="Button"> + <TextBlock Foreground="White" VerticalAlignment="Center" TextDecorations="Underline">Hide</TextBlock> + </ControlTemplate> + </Button.Template> + </Button> + </StackPanel> </DockPanel> </Border> - <Grid Background="Gray"> + <Grid Background="#FFFFFF"> + <TextBox components:TextController.Controller="{Binding Log}" IsReadOnly="True" IsReadOnlyCaretVisible="True" BorderThickness="0" Background="Transparent" Padding="5" AcceptsReturn="True" TextWrapping="Wrap" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"> + </TextBox> </Grid> </DockPanel> </Border> 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 /// </summary> 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 @@ <ItemGroup> <Reference Include="System" /> <Reference Include="System.Data" /> + <Reference Include="System.Drawing" /> + <Reference Include="System.Windows.Forms" /> <Reference Include="System.Xml" /> <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Core" /> @@ -102,17 +104,25 @@ <Resource Include="cat.png" /> </ItemGroup> <ItemGroup> - <Resource Include="close.png" /> - </ItemGroup> - <ItemGroup> <ProjectReference Include="..\..\Tango.Core\Tango.Core.csproj"> <Project>{a34ee0f0-649d-41c8-8489-b6f1cc6924ee}</Project> <Name>Tango.Core</Name> </ProjectReference> + <ProjectReference Include="..\..\Tango.Logging\Tango.Logging.csproj"> + <Project>{BC932DBD-7CDB-488C-99E4-F02CF441F55E}</Project> + <Name>Tango.Logging</Name> + </ProjectReference> <ProjectReference Include="..\..\Tango.SharedUI\Tango.SharedUI.csproj"> <Project>{8491d07b-c1f6-4b62-a412-41b9fd2d6538}</Project> <Name>Tango.SharedUI</Name> </ProjectReference> + <ProjectReference Include="..\Tango.PPC.Common\Tango.PPC.Common.csproj"> + <Project>{0be74eee-22cb-4dba-b896-793b9e1a3ac0}</Project> + <Name>Tango.PPC.Common</Name> + </ProjectReference> + </ItemGroup> + <ItemGroup> + <EmbeddedResource Include="cat24.ico" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> </Project>
\ 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 Binary files differnew file mode 100644 index 000000000..6fa3cb368 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/cat24.ico diff --git a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/close.png b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/close.png Binary files differdeleted file mode 100644 index 3a040a008..000000000 --- a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/close.png +++ /dev/null |
