aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2019-02-17 15:52:11 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2019-02-17 15:52:11 +0200
commite29d962c5602fc9fc3a54fa4da8957609de7eea4 (patch)
tree857c8448611b79c3d3834f37edb00965ad5435ba /Software/Visual_Studio/PPC
parent0cd0b590f62b31a8874ea21f225ba75c7a37053c (diff)
downloadTango-e29d962c5602fc9fc3a54fa4da8957609de7eea4.tar.gz
Tango-e29d962c5602fc9fc3a54fa4da8957609de7eea4.zip
Completed PPC watchdog !
Diffstat (limited to 'Software/Visual_Studio/PPC')
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs6
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogClient.cs87
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogServer.cs108
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs1
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/MainWindow.xaml.cs2
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs44
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs4
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineUpdateViewVM.cs2
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MainViewVM.cs1
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.WatchDog/App.xaml.cs12
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindow.xaml37
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindow.xaml.cs44
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindowVM.cs107
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.WatchDog/Tango.PPC.WatchDog.csproj16
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.WatchDog/cat24.icobin0 -> 105237 bytes
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.WatchDog/close.pngbin651 -> 0 bytes
16 files changed, 433 insertions, 38 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs
index 7b617ddaa..409dd1cfc 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs
@@ -72,6 +72,11 @@ namespace Tango.PPC.Common
public DeploymentSlot DeploymentSlot { get; set; }
/// <summary>
+ /// Gets or sets a value indicating whether to enable the watch dog process.
+ /// </summary>
+ public bool EnableWatchDog { get; set; }
+
+ /// <summary>
/// Gets the machine service address.
/// </summary>
/// <returns></returns>
@@ -91,6 +96,7 @@ namespace Tango.PPC.Common
ExternalBridgePassword = "Aa123456";
HotSpotPassword = "Aa123456";
DeploymentSlot = DeploymentSlot.TEST;
+ EnableWatchDog = true;
}
}
}
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogClient.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogClient.cs
index 47a24ea85..bbdfbf7e2 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogClient.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogClient.cs
@@ -1,12 +1,97 @@
using System;
using System.Collections.Generic;
+using System.IO;
+using System.IO.Pipes;
using System.Linq;
using System.Text;
+using System.Threading;
using System.Threading.Tasks;
+using Tango.Core;
namespace Tango.PPC.Common.WatchDog
{
- public class WatchDogClient
+ public class WatchDogClient : ExtendedObject
{
+ public event EventHandler ServerNotResponding;
+
+ private NamedPipeClientStream _client;
+ private StreamReader _reader;
+ private StreamWriter _writer;
+ private Thread _thread;
+
+ public bool IsStarted { get; private set; }
+
+ public TimeSpan Interval { get; set; }
+
+ public WatchDogClient()
+ {
+ Interval = TimeSpan.FromSeconds(5);
+ }
+
+ public void Start()
+ {
+ if (!IsStarted)
+ {
+ LogManager.Log("Starting watchdog client...");
+
+ IsStarted = true;
+
+ _thread = new Thread(ThreadMethod);
+ _thread.IsBackground = true;
+ _thread.Start();
+ }
+ }
+
+ private void ThreadMethod()
+ {
+ try
+ {
+ LogManager.Log("Watchdog client started.");
+
+ while (IsStarted)
+ {
+ _client = new NamedPipeClientStream("Tango_Watch_Dog_Pipe");
+ _client.Connect(5000);
+
+ _reader = new StreamReader(_client);
+ _writer = new StreamWriter(_client);
+
+ _writer.WriteLine("ping");
+ _writer.Flush();
+
+ var line = _reader.ReadLine();
+
+ if (line != "ping")
+ {
+ LogManager.Log("Watchdog server not found. Raising notification...");
+ try
+ {
+ IsStarted = false;
+ _client.Dispose();
+ }
+ catch { }
+
+ ServerNotResponding?.Invoke(this, new EventArgs());
+ return;
+ }
+
+ _client.Dispose();
+
+ Thread.Sleep(Interval);
+ }
+ }
+ catch (Exception ex)
+ {
+ IsStarted = false;
+ LogManager.Log(ex, "Error in watchdog client.");
+ ServerNotResponding?.Invoke(this, new EventArgs());
+ }
+ }
+
+ public void Stop()
+ {
+ IsStarted = false;
+ _client.Dispose();
+ }
}
}
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogServer.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogServer.cs
index 77bca2e30..84ff68d08 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogServer.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogServer.cs
@@ -1,26 +1,60 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
+using System.Windows.Threading;
+using Tango.Core;
+using Tango.Core.Helpers;
namespace Tango.PPC.Common.WatchDog
{
- public class WatchDogServer
+ public class WatchDogServer : ExtendedObject, IDisposable
{
private NamedPipeServerStream _server;
+ private StreamReader _reader;
+ private StreamWriter _writer;
private Thread _thread;
+ private Dispatcher _dispatcher;
+ private String _watchdogAppPath;
+ private Process _watchdogProcess;
public bool IsStarted { get; private set; }
+ public WatchDogServer(Dispatcher uiDispatcher)
+ {
+ _watchdogAppPath = Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), "Tango.PPC.WatchDog.exe");
+
+ _dispatcher = uiDispatcher;
+ }
+
public void Start()
{
- _thread = new Thread(ThreadMethod);
- _thread.IsBackground = true;
- _thread.Start();
+ LogManager.Log("Starting watchdog server...");
+
+ try
+ {
+ _watchdogProcess = Process.GetProcessesByName("Tango.PPC.WatchDog").FirstOrDefault();
+
+ _server = new NamedPipeServerStream("Tango_Watch_Dog_Pipe");
+ _reader = new StreamReader(_server);
+ _writer = new StreamWriter(_server);
+
+ IsStarted = true;
+ _thread = new Thread(ThreadMethod);
+ _thread.IsBackground = true;
+ _thread.Start();
+
+ LogManager.Log("Watchdog server started.");
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, "Error starting watchdog server!");
+ }
}
private void ThreadMethod()
@@ -29,27 +63,73 @@ namespace Tango.PPC.Common.WatchDog
try
{
- _server = new NamedPipeServerStream("Tango_Watch_Dog_Pipe");
- _server.WaitForConnection();
- StreamReader reader = new StreamReader(_server);
- StreamWriter writer = new StreamWriter(_server);
-
while (IsStarted)
{
- var line = reader.ReadLine();
- writer.WriteLine(line);
- writer.Flush();
+ if (_watchdogProcess == null)
+ {
+ LogManager.Log($"Starting watchdog process at '{_watchdogAppPath}'...");
+
+ try
+ {
+ _watchdogProcess = Process.Start(_watchdogAppPath);
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, "Error starting watchdog process!");
+ return;
+ }
+ }
+
+ _server.WaitForConnection();
+
+ var line = _reader.ReadLine();
+
+ _dispatcher.Invoke(() =>
+ {
+ _writer.WriteLine(line);
+ _writer.Flush();
+ });
+
+ _server.Disconnect();
}
}
- catch
+ catch (Exception ex)
{
+ LogManager.Log(ex, "Error in watchdog server. Restarting watchdog...");
IsStarted = false;
+
+ try
+ {
+ _server.Dispose();
+ }
+ catch { }
+
+ Start();
+ return;
}
}
public void Stop()
{
- _server.Dispose();
+ if (IsStarted)
+ {
+ try
+ {
+ _watchdogProcess.Kill();
+ _watchdogProcess = null;
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, "Error killing watchdog process!");
+ }
+ IsStarted = false;
+ _server.Dispose();
+ }
+ }
+
+ public void Dispose()
+ {
+ Stop();
}
}
}
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs
index 98ed016ae..0eb982d08 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs
@@ -12,6 +12,7 @@ using Tango.Core;
using Tango.Core.Helpers;
using Tango.Logging;
using Tango.PPC.Common;
+using Tango.PPC.Common.WatchDog;
using Tango.Settings;
namespace Tango.PPC.UI
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/MainWindow.xaml.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/MainWindow.xaml.cs
index 4ca4a1fb7..edc7cce52 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.UI/MainWindow.xaml.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/MainWindow.xaml.cs
@@ -89,7 +89,7 @@ namespace Tango.PPC.UI
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
- Environment.Exit(0);
+ TangoIOC.Default.GetInstance<IPPCApplicationManager>().ShutDown();
}
protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs
index 39ce8cd30..43b0cc047 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs
@@ -27,6 +27,7 @@ using System.Diagnostics;
using Tango.PPC.Common.EventLogging;
using Tango.BL.Enumerations;
using Tango.PPC.Common.Notifications;
+using Tango.PPC.Common.WatchDog;
namespace Tango.PPC.UI.PPCApplication
{
@@ -44,6 +45,7 @@ namespace Tango.PPC.UI.PPCApplication
private IEventLogger _eventLogger;
private IPPCModuleLoader _moduleLoader;
private INotificationProvider _notificationProvider;
+ private WatchDogServer _watchdogServer;
/// <summary>
/// Occurs when a system restart is required.
@@ -157,6 +159,14 @@ namespace Tango.PPC.UI.PPCApplication
LogManager.Log(settings.ToJsonString());
+ //Start watchdog
+ _watchdogServer = new WatchDogServer(Application.Current.Dispatcher);
+
+ if (settings.EnableWatchDog)
+ {
+ _watchdogServer.Start();
+ }
+
LogManager.Log("Reading Core settings...");
var coreSettings = SettingsManager.Default.GetOrCreate<CoreSettings>();
@@ -323,16 +333,23 @@ namespace Tango.PPC.UI.PPCApplication
/// </summary>
public void ShutDown()
{
- //TODO: Needs some work on logging and shutdown procedures! Do I really need this?
-
- LogManager.Log("Shutting down application...");
+ if (IsShuttingDown) return;
IsShuttingDown = true;
- foreach (var vm in TangoIOC.Default.GetAllInstancesByBase<PPCViewModel>())
+ try
{
- vm.OnApplicationShuttingDown();
+ LogManager.Log("Shutting down application...");
+ _watchdogServer.Dispose();
+
+ foreach (var vm in TangoIOC.Default.GetAllInstancesByBase<PPCViewModel>())
+ {
+ vm.OnApplicationShuttingDown();
+ }
}
+ catch { }
+
+ Environment.Exit(0);
}
/// <summary>
@@ -340,6 +357,23 @@ namespace Tango.PPC.UI.PPCApplication
/// </summary>
public void Restart()
{
+ if (IsShuttingDown) return;
+
+ IsShuttingDown = true;
+
+ try
+ {
+ LogManager.Log("Restarting the application...");
+
+ _watchdogServer.Dispose();
+
+ foreach (var vm in TangoIOC.Default.GetAllInstancesByBase<PPCViewModel>())
+ {
+ vm.OnApplicationShuttingDown();
+ }
+ }
+ catch { }
+
Process.Start(Application.ResourceAssembly.Location);
Environment.Exit(0);
}
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs
index ee1b39ca6..48aae2b8f 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs
@@ -59,6 +59,7 @@ namespace Tango.PPC.UI.ViewModels
private MachineSetupResult _setup_result;
private IOperationSystemManager _operationSystemManager;
+ private IPPCApplicationManager _appManager;
#region Properties
@@ -193,6 +194,7 @@ namespace Tango.PPC.UI.ViewModels
/// <param name="machineSetupManager">The machine setup manager.</param>
public MachineSetupViewVM(IPPCApplicationManager applicationManager, IMachineSetupManager machineSetupManager, IOperationSystemManager operationSystemManager)
{
+ _appManager = applicationManager;
MachineSetupManager = machineSetupManager;
DeploymentSlot = Settings.DeploymentSlot;
@@ -282,7 +284,7 @@ namespace Tango.PPC.UI.ViewModels
LogManager.Log($"Executing '{updater_exe}' with arguments '{PathHelper.GetStartupPath()}'...");
Process.Start(updater_exe, PathHelper.GetStartupPath());
LogManager.Log("Terminating application process!");
- Environment.Exit(0);
+ _appManager.ShutDown();
}
/// <summary>
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineUpdateViewVM.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineUpdateViewVM.cs
index 07d034964..2f6199ecb 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineUpdateViewVM.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineUpdateViewVM.cs
@@ -220,7 +220,7 @@ namespace Tango.PPC.UI.ViewModels
LogManager.Log($"Executing '{updater_exe}' with arguments '{PathHelper.GetStartupPath()}'...");
Process.Start(updater_exe, PathHelper.GetStartupPath());
LogManager.Log("Terminating application process!");
- Environment.Exit(0);
+ ApplicationManager.ShutDown();
}
else
{
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MainViewVM.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MainViewVM.cs
index b01be3734..a4f550a39 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MainViewVM.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MainViewVM.cs
@@ -13,6 +13,7 @@ using Tango.PPC.Common.ExternalBridge;
using Tango.PPC.Common.Modules;
using Tango.PPC.Common.Navigation;
using Tango.PPC.Common.Notifications;
+using Tango.PPC.Common.WatchDog;
using Tango.SharedUI;
namespace Tango.PPC.UI.ViewModels
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
new file mode 100644
index 000000000..6fa3cb368
--- /dev/null
+++ b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/cat24.ico
Binary files 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
--- a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/close.png
+++ /dev/null
Binary files differ