diff --git a/.vs/Server Dashboard/DesignTimeBuild/.dtbcache.v2 b/.vs/Server Dashboard/DesignTimeBuild/.dtbcache.v2 index 82db2c5..cf13afc 100644 Binary files a/.vs/Server Dashboard/DesignTimeBuild/.dtbcache.v2 and b/.vs/Server Dashboard/DesignTimeBuild/.dtbcache.v2 differ diff --git a/.vs/Server Dashboard/v16/.suo b/.vs/Server Dashboard/v16/.suo index 1a139e2..ee03118 100644 Binary files a/.vs/Server Dashboard/v16/.suo and b/.vs/Server Dashboard/v16/.suo differ diff --git a/Server Dashboard/DashboardModules/ServerInformation.cs b/Server Dashboard/DashboardModules/ServerInformation.cs deleted file mode 100644 index 399e567..0000000 --- a/Server Dashboard/DashboardModules/ServerInformation.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Text; - -namespace Server_Dashboard { - - /// - /// Server information class, this will probably scale pretty big later on - /// This will hold all the information the socket will gather - /// - internal class ServerInformation { - - //The ServerName - public string ServerName { get; set; } - - //The unix or windows username - public string OSUserName { get; set; } - - //Cpu Temp in C - public string CpuTemp { get; set; } - - //Gpu Temp in C - public string GpuTemp { get; set; } - - //Server uptime - public string Uptime { get; set; } - - //When the server was first deployed - public string DeployDate { get; set; } - - //Public IPv4 Adress - public string PublicIpAdress { get; set; } - - //Private IP adress from the servers network - public string PrivateIpAdress { get; set; } - - //GPU usage in % - public string GpuUsage { get; set; } - - //CPU usage in % - public string CpuUsage { get; set; } - } -} \ No newline at end of file diff --git a/Server Dashboard/Database/DatabaseHandler.cs b/Server Dashboard/Database/DatabaseHandler.cs index bdcc3e5..8bc1a22 100644 --- a/Server Dashboard/Database/DatabaseHandler.cs +++ b/Server Dashboard/Database/DatabaseHandler.cs @@ -5,6 +5,7 @@ using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Reflection; +using System.Threading.Tasks; using System.Windows.Media.Imaging; namespace Server_Dashboard { @@ -65,12 +66,77 @@ namespace Server_Dashboard { //Open the connection con.Open(); //SQL Query - string query = "SELECT UserID, Username, Email, CreationTime, ModuleName, MI.Image FROM UserData LEFT JOIN ModuleData MD on UserData.ID = MD.UserID LEFT JOIN ModuleIcon MI on MD.ID = MI.Module WHERE Username = @username"; + string query = "SELECT ID, Username, Email, RegistrationDate FROM UserData WHERE Username = @username"; //Creates a new command using SqlCommand com = new SqlCommand(query, con);//For security reasons the values are added with this function //this will avoid SQL Injections com.Parameters.AddWithValue("@username", username); //Execute query and return number of rows affected + DataTable resultTable = new DataTable() { TableName = "Userdata" }; + using SqlDataAdapter sda = new SqlDataAdapter(com); + sda.Fill(resultTable); + return resultTable; + //Checks if there are any rows successful + //If the query returns 0 the query wasn't successful + //if its any number above 0 it was successfull + //Catch any error + } catch (SqlException ex) { + return null; + } finally { + //Always close the connection + con.Close(); + } + } + + public static DataTable GetUserModuleData(int uid) { + //Creates the database connection + using SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerDashboardDB"].ConnectionString); + try { + //Open the connection + con.Open(); + //SQL Query + string query = "SELECT Creator, CreationTime, ModuleName, MI.Image FROM ModuleData LEFT JOIN ModuleIcon MI on ModuleData.ID = MI.Module WHERE UserID = @userID"; + //Creates a new command + using SqlCommand com = new SqlCommand(query, con);//For security reasons the values are added with this function + //this will avoid SQL Injections + com.Parameters.AddWithValue("@userID", uid); + //Execute query and return number of rows affected + DataTable resultTable = new DataTable(); + using SqlDataAdapter sda = new SqlDataAdapter(com); + sda.Fill(resultTable); + return resultTable; + //Checks if there are any rows successful + //If the query returns 0 the query wasn't successful + //if its any number above 0 it was successfull + //Catch any error + } catch (SqlException ex) { + return null; + } finally { + //Always close the connection + con.Close(); + } + } + + /// + /// This function will fetch every Serverdata for each module + /// This will need some optimization, for now we just asynchronously + /// fetch the serverdata for each module + /// + /// ModuleID to fetch the data from + /// + public static DataTable GetServerData(string mid) { + //Creates the database connection + using SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerDashboardDB"].ConnectionString); + try { + //Open the connection + con.Open(); + //SQL Query + string query = "SELECT * FROM ServerData WHERE ModuleID = @mid"; + //Creates a new command + using SqlCommand com = new SqlCommand(query, con);//For security reasons the values are added with this function + //this will avoid SQL Injections + com.Parameters.AddWithValue("@mid", mid); + //Execute query and return number of rows affected DataTable resultTable = new DataTable(); using SqlDataAdapter sda = new SqlDataAdapter(com); sda.Fill(resultTable); @@ -113,7 +179,10 @@ namespace Server_Dashboard { com.Parameters.AddWithValue("@time", DateTime.Now); com.Parameters.AddWithValue("@moduleName", moduleName); com.Parameters.AddWithValue("@serverName", serverName); - com.Parameters.Add("@moduleIcon", SqlDbType.VarBinary, int.MaxValue).Value = moduleIcon; + com.Parameters.Add("@moduleIcon", SqlDbType.VarBinary, -1).Value = moduleIcon; + if (moduleIcon == null) + com.Parameters["@moduleIcon"].Value = DBNull.Value; + //com.Parameters.AddWithValue("@moduleIcon", moduleIcon); com.Parameters.AddWithValue("@ipAdress", ipAdress); com.Parameters.AddWithValue("@port", port); //Execute query and return number of rows affected @@ -150,21 +219,19 @@ namespace Server_Dashboard { //Open the connection con.Open(); //SQL Query - string query = "EXEC CheckUserCookie @Cookie = @cookie, @UserName = @username, @Valid = @valid OUTPUT"; + string query = "((SELECT Cookie FROM UserData WHERE Username = @username) = @cookie)"; //Creates a new command using SqlCommand com = new SqlCommand(query, con); //For security reasons the values are added with this function //this will avoid SQL Injections com.Parameters.AddWithValue("@cookie", cookie); com.Parameters.AddWithValue("@username", username); - com.Parameters.Add("@valid", SqlDbType.Bit); - com.Parameters["@valid"].Direction = ParameterDirection.Output; //Execute query and return number of rows affected int sqlResponse = com.ExecuteNonQuery(); //Checks if there are any rows successful //If the query returns 0 the query wasn't successful //if its any number above 0 it was successfull - if ((int)com.Parameters["@Valid"].Value == 0) { + if (sqlResponse == 0) { //Error, not successful return 1; } else { @@ -191,7 +258,7 @@ namespace Server_Dashboard { //Open the connection con.Open(); //SQL Query - string query = "EXEC DeleteUserCookie @Username = @username"; + string query = "UPDATE UserData SET Cookie = null WHERE Username = @username"; //Creates a new command using SqlCommand com = new SqlCommand(query, con); //For security reasons the values are added with this function @@ -224,19 +291,19 @@ namespace Server_Dashboard { /// The delicious locally stored cookie /// The User who deserves a cookie :3 /// [0] is false, [1] is true, [2] connection error - public static int AddCookie(string cookie, string username) { + public static int AddCookie(string username, string cookie) { //Creates the database connection using SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerDashboardDB"].ConnectionString); try { //Open the connection con.Open(); //SQL Query - string query = "EXEC AddCookieToUser @Cookie = @cookie, @UserName = @username"; + string query = "UPDATE UserData SET Cookie = @cookie WHERE Username = @username"; //Creates a new command using SqlCommand com = new SqlCommand(query, con); //For security reasons the values are added with this function //this will avoid SQL Injections - com.Parameters.AddWithValue("@cookie", cookie); + com.Parameters.Add("@cookie", SqlDbType.NVarChar, -1).Value = cookie; com.Parameters.AddWithValue("@username", username); //Execute query and return number of rows affected int sqlResponse = com.ExecuteNonQuery(); diff --git a/Server Dashboard/DashboardModules/DashboardModule.cs b/Server Dashboard/User/ModuleData/ModuleData.cs similarity index 76% rename from Server Dashboard/DashboardModules/DashboardModule.cs rename to Server Dashboard/User/ModuleData/ModuleData.cs index d046943..213ca75 100644 --- a/Server Dashboard/DashboardModules/DashboardModule.cs +++ b/Server Dashboard/User/ModuleData/ModuleData.cs @@ -5,10 +5,7 @@ using System.Windows.Media.Imaging; namespace Server_Dashboard { - /// - /// Dashboard Module class that holds all the information that gets displayed - /// - internal class DashboardModule { + internal class ModuleData { //The name the user gives the module public string ModuleName { get; set; } @@ -16,9 +13,6 @@ namespace Server_Dashboard { //The user who created it public string Creator { get; set; } - //All the information that the server had - public ServerInformation ServerInfo { get; set; } - //The status indicator public string StatusIndicator { get; set; } @@ -32,13 +26,16 @@ namespace Server_Dashboard { public BitmapImage ModuleIcon { get; set; } //Creation date with System.DateTime.Now - public string CreationDate { get; set; } + public DateTime CreationDate { get; set; } + + //List that contains all the serverinformation over a period of time(lifespan of the module) + public List ServerInformation { get; set; } /// /// This will set the Module status indicator red or green if the server is available or not /// /// - public DashboardModule(bool serverAvailable) { + public ModuleData(bool serverAvailable) { ServerAvailable = serverAvailable; StatusIndicator = ServerAvailable ? "#20c657" : "#e53935"; StatusIndicatorBG = ServerAvailable ? "#94eeb0" : "#ef9a9a"; diff --git a/Server Dashboard/User/ModuleData/ServerData/ServerInformation.cs b/Server Dashboard/User/ModuleData/ServerData/ServerInformation.cs new file mode 100644 index 0000000..fb2cdbe --- /dev/null +++ b/Server Dashboard/User/ModuleData/ServerData/ServerInformation.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Text; + +namespace Server_Dashboard { + + /// + /// Server information class, this will probably scale pretty big later on + /// This will hold all the information the socket will gather + /// + internal class ServerInformation { + public string ServerName { get; set; } + public string OSUserName { get; set; } + public double CpuTemp { get; set; } + public double GpuTemp { get; set; } + public DateTime Uptime { get; set; } + public DateTime DeployDate { get; set; } + public string PublicIpAdress { get; set; } + public string PrivateIpAdress { get; set; } + public int GpuUsage { get; set; } + public int CpuUsage { get; set; } + public double NetworkUP { get; set; } + public double NetworkDown { get; set; } + } +} \ No newline at end of file diff --git a/Server Dashboard/User/ModuleData/ServerData/ServerModules/CPU.cs b/Server Dashboard/User/ModuleData/ServerData/ServerModules/CPU.cs new file mode 100644 index 0000000..96d8db0 --- /dev/null +++ b/Server Dashboard/User/ModuleData/ServerData/ServerModules/CPU.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Server_Dashboard { + + /// + /// This class represents a single CPU and will be added + /// to a list to account for a multi CPU system + /// + internal class CPU { + } +} \ No newline at end of file diff --git a/Server Dashboard/User/ModuleData/ServerData/ServerModules/Drives.cs b/Server Dashboard/User/ModuleData/ServerData/ServerModules/Drives.cs new file mode 100644 index 0000000..1bca239 --- /dev/null +++ b/Server Dashboard/User/ModuleData/ServerData/ServerModules/Drives.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Server_Dashboard { + + /// + /// This class represents a single drive on a server, this will be added + /// to a list to account for multiple drives + /// + internal class Drives { + } +} \ No newline at end of file diff --git a/Server Dashboard/User/ModuleData/ServerData/ServerModules/GPU.cs b/Server Dashboard/User/ModuleData/ServerData/ServerModules/GPU.cs new file mode 100644 index 0000000..0330175 --- /dev/null +++ b/Server Dashboard/User/ModuleData/ServerData/ServerModules/GPU.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Server_Dashboard { + + /// + /// This class represents a single GPU and will be added to a list + /// to account for multiple GPU's + /// + internal class GPU { + } +} \ No newline at end of file diff --git a/Server Dashboard/User/ModuleData/ServerData/ServerModules/Motherboard.cs b/Server Dashboard/User/ModuleData/ServerData/ServerModules/Motherboard.cs new file mode 100644 index 0000000..aba76f6 --- /dev/null +++ b/Server Dashboard/User/ModuleData/ServerData/ServerModules/Motherboard.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Server_Dashboard { + + /// + /// This class represents the mainboard + /// It will hold information about the chipset, number of hardware etc... + /// + internal class Motherboard { + } +} \ No newline at end of file diff --git a/Server Dashboard/User/ModuleData/ServerData/ServerModules/NetworkInterface.cs b/Server Dashboard/User/ModuleData/ServerData/ServerModules/NetworkInterface.cs new file mode 100644 index 0000000..efd380e --- /dev/null +++ b/Server Dashboard/User/ModuleData/ServerData/ServerModules/NetworkInterface.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Server_Dashboard { + + /// + /// This class represents a single network interface and will be added + /// to a list to account for multiple networking interfaces + /// + internal class NetworkInterface { + } +} \ No newline at end of file diff --git a/Server Dashboard/User/ModuleData/ServerData/ServerModules/RAM.cs b/Server Dashboard/User/ModuleData/ServerData/ServerModules/RAM.cs new file mode 100644 index 0000000..a41bcf4 --- /dev/null +++ b/Server Dashboard/User/ModuleData/ServerData/ServerModules/RAM.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Server_Dashboard { + + /// + /// This class represents RAM + /// TODO: figure out if it makes sense to create a class for the entire system memory + /// or for each and every stick + /// + internal class RAM { + } +} \ No newline at end of file diff --git a/Server Dashboard/User/User.cs b/Server Dashboard/User/User.cs new file mode 100644 index 0000000..eec1ddd --- /dev/null +++ b/Server Dashboard/User/User.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Text; + +namespace Server_Dashboard { + + /// + /// User class to store user informations + /// + internal class User { + public int UID { get; set; } + public string UserName { get; set; } + public string Email { get; set; } + public DateTime RegistrationDate { get; set; } + + public User(DataTable userData) { + foreach (DataRow row in userData.Rows) { + UID = (int)row[0]; + UserName = (string)row[1]; + Email = (string)row[2]; + RegistrationDate = (DateTime)row[3]; + } + } + } +} \ No newline at end of file diff --git a/Server Dashboard/ViewModels/Dashboard/DashboardModuleViewModel.cs b/Server Dashboard/ViewModels/Dashboard/DashboardModuleViewModel.cs index 3ab69ab..f14c8a0 100644 --- a/Server Dashboard/ViewModels/Dashboard/DashboardModuleViewModel.cs +++ b/Server Dashboard/ViewModels/Dashboard/DashboardModuleViewModel.cs @@ -14,46 +14,40 @@ namespace Server_Dashboard { internal class DashboardModuleViewModel : BaseViewModel { //List with all Modules inside - public ObservableCollection Modules { get; set; } + public ObservableCollection Modules { get; set; } //Creates Default Modules, remove before release and when implementing the actual data comming from the socket - public DashboardModuleViewModel(DataTable userdata) { - GetModules(userdata); + public DashboardModuleViewModel(DataTable moduleData) { + Modules = new ObservableCollection(); + foreach (DataRow row in moduleData.Rows) { + if (row[0] != null) { + byte[] iconBytes = row[3] == DBNull.Value ? null : (byte[])row[3]; + Modules.Add(new ModuleData(true) { + ModuleName = (string)row?[2], + Creator = (string)row?[0], + ModuleIcon = ConvertByteToBitmapImage(iconBytes), + CreationDate = (DateTime)row?[1], + ServerInformation = null + }); + } + } } - public void GetModules(DataTable userdata) { - Modules = new ObservableCollection(); - foreach (DataRow row in userdata.Rows) { - BitmapImage moduleIcon = null; - if (row[5] != DBNull.Value) { - using MemoryStream ms = new MemoryStream((byte[])row[5]); - moduleIcon = new BitmapImage(); + private BitmapImage ConvertByteToBitmapImage(byte[] icon) { + if (icon != null) { + try { + using MemoryStream ms = new MemoryStream(icon); + BitmapImage moduleIcon = new BitmapImage(); moduleIcon.BeginInit(); moduleIcon.StreamSource = ms; moduleIcon.CreateOptions = BitmapCreateOptions.PreservePixelFormat; moduleIcon.CacheOption = BitmapCacheOption.OnLoad; moduleIcon.EndInit(); moduleIcon.Freeze(); - } - Modules.Add(new DashboardModule(true) { - ModuleName = (string)row[4], - Creator = (string)row[1], - ModuleIcon = moduleIcon, - CreationDate = DateTime.Now.ToString(), - ServerInfo = new ServerInformation() { - GpuUsage = "20", - CpuUsage = "20", - CpuTemp = "88.88", - DeployDate = DateTime.Now.ToString(), - GpuTemp = "69.69", - ServerName = "Ubuntu", - OSUserName = "crylia ", - PrivateIpAdress = "192.168.1.1", - PublicIpAdress = "85.69.102.58", - Uptime = DateTime.Now.ToString() - } - }); + return moduleIcon; + } catch { } } + return null; } } } \ No newline at end of file diff --git a/Server Dashboard/ViewModels/Dashboard/DashboardViewModel.cs b/Server Dashboard/ViewModels/Dashboard/DashboardViewModel.cs index 140d0b1..909f52f 100644 --- a/Server Dashboard/ViewModels/Dashboard/DashboardViewModel.cs +++ b/Server Dashboard/ViewModels/Dashboard/DashboardViewModel.cs @@ -6,6 +6,8 @@ using System.Windows.Input; using Server_Dashboard_Socket; using System; using System.Data; +using System.Collections.Generic; +using System.Linq; namespace Server_Dashboard { @@ -16,28 +18,28 @@ namespace Server_Dashboard { #region Private Values - private readonly DashboardModuleViewModel dmvm; + private DashboardModuleViewModel dmvm; #endregion Private Values #region Properties //The Username displayed defaults to Username - private string userName; + private User user; - public string UserName { - get { return userName; } + public User User { + get { return user; } set { - if (userName != value) - userName = value; - OnPropertyChanged(nameof(userName)); + if (user != value) + user = value; + OnPropertyChanged(nameof(user)); } } //List that contains every Module - private ObservableCollection modules; + private ObservableCollection modules; - public ObservableCollection Modules { + public ObservableCollection Modules { get { return modules; } set { if (value != modules) @@ -51,15 +53,13 @@ namespace Server_Dashboard { #region Constructor public DashboardViewModel(string username) { - UserName = username; //Command inits OpenLinkCommand = new RelayCommand(OpenLink); OpenNewModuleWindowCommand = new RelayCommand(OpenNewModuleWindow); - DataTable Userdata = DatabaseHandler.GetUserData(username); - dmvm = new DashboardModuleViewModel(Userdata); - //Sets the local module to the dashboardviewmodule modules - Modules = dmvm.Modules; + DataTable userData = DatabaseHandler.GetUserData(username); + User = new User(userData); + GetModules(); } #endregion Constructor @@ -88,12 +88,20 @@ namespace Server_Dashboard { private void OpenNewModuleWindow(object param) { //Creates a new CreateModulePopup and sets this view model as datacontext CreateModulePopup cmp = new CreateModulePopup { - DataContext = new CreateModuleViewModel(UserName) + DataContext = new CreateModuleViewModel(User.UserName) }; //Opens it in the middle of the screen, setting the parent window as owner causes the //application to crash when NOT in debug mode(???) cmp.WindowStartupLocation = WindowStartupLocation.CenterScreen; cmp.ShowDialog(); + GetModules(); + } + + private void GetModules() { + DataTable moduleData = DatabaseHandler.GetUserModuleData(User.UID); + dmvm = new DashboardModuleViewModel(moduleData); + //Sets the local module to the dashboardviewmodule modules + Modules = dmvm.Modules; } #endregion Commands diff --git a/Server Dashboard/ViewModels/Login/LoginViewModel.cs b/Server Dashboard/ViewModels/Login/LoginViewModel.cs index ad09c4f..c20bb79 100644 --- a/Server Dashboard/ViewModels/Login/LoginViewModel.cs +++ b/Server Dashboard/ViewModels/Login/LoginViewModel.cs @@ -140,9 +140,9 @@ namespace Server_Dashboard { DatabaseHandler.DeleteCookie(Username); } //If the remember user option is checked and the cookie is not set save everything locally - if (RememberUser && String.IsNullOrEmpty(Settings.Default.Cookies)) { + if (RememberUser && Settings.Default.Username != Username) { //Creates a new GUID with the username at the end, this is the cookie - var cookie = new Guid().ToString() + Username; + var cookie = $"{Guid.NewGuid().ToString()}+user:{Username}"; //Saves cookie, Username Remember me option to the local storage (Settings.settings) Settings.Default.Cookies = cookie; Settings.Default.Username = Username; diff --git a/Server Dashboard/Views/DashboardWindow.xaml b/Server Dashboard/Views/DashboardWindow.xaml index 717c80c..4d22f1b 100644 --- a/Server Dashboard/Views/DashboardWindow.xaml +++ b/Server Dashboard/Views/DashboardWindow.xaml @@ -67,7 +67,7 @@ -