diff ServerMonitor/Objects/Server.cs @ 4:3142e52cbe69

Lots more progress
author Brad Greco <brad@bgreco.net>
date Sun, 10 Feb 2019 20:51:26 -0500
parents 3e1a2131f897
children b6fe203af9d5
line wrap: on
line diff
--- a/ServerMonitor/Objects/Server.cs	Fri Jan 11 22:34:18 2019 -0500
+++ b/ServerMonitor/Objects/Server.cs	Sun Feb 10 20:51:26 2019 -0500
@@ -19,9 +19,11 @@
         private LoginType _loginType;
         private string _keyFile;
         private SshClient _sshClient;
+        private bool _enabled = true;
         private byte[] passwordHash;
 
         public event EventHandler CheckModified;
+        public event EventHandler EnabledChanged;
 
         public readonly BindingList<Check> Checks = new BindingList<Check>();
 
@@ -73,7 +75,17 @@
             }
         }
 
-        public bool Enabled { get; set; } = true;
+        public bool Enabled
+        {
+            get { return _enabled; }
+            set { _enabled = value; EnabledChanged?.Invoke(this, new EventArgs()); }
+        }
+
+        public CheckStatus Status => !Enabled ? CheckStatus.Disabled : Checks
+            .Where(c => c.Enabled)
+            .Select(c => c.LastRunStatus)
+            .DefaultIfEmpty(CheckStatus.Success)
+            .Max();
 
         public SshClient SshClient
         {
@@ -85,7 +97,7 @@
                     if (LoginType == LoginType.Password)
                         auth = new PasswordAuthenticationMethod(Username, Password);
                     else
-                        new PrivateKeyAuthenticationMethod(Username, new PrivateKeyFile(KeyFile));
+                        auth = new PrivateKeyAuthenticationMethod(Username, new PrivateKeyFile(KeyFile));
                     ConnectionInfo info = new ConnectionInfo(Host, Port, Username, auth);
                     _sshClient = new SshClient(info);
                 }
@@ -137,6 +149,13 @@
             CheckModified?.Invoke(check, new EventArgs());
         }
 
+        public bool IsEmpty()
+        {
+            return Name.IsNullOrEmpty()
+                && Host.IsNullOrEmpty()
+                && Checks.Count == 0;
+        }
+
         private void InvalidateSshConnection()
         {
             _sshClient?.Dispose();