finish create module form
This commit is contained in:
@@ -8,7 +8,7 @@ namespace Server_Dashboard {
|
||||
/// Base View Model all the other view models inherit from
|
||||
/// Makes me write the INotifyPropertyChanged only once
|
||||
/// </summary>
|
||||
class BaseViewModel : INotifyPropertyChanged {
|
||||
public class BaseViewModel : INotifyPropertyChanged {
|
||||
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
|
||||
|
||||
protected void OnPropertyChanged(string prop) {
|
||||
|
||||
@@ -1,24 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace Server_Dashboard {
|
||||
|
||||
/// <summary>
|
||||
/// View Model for the modules
|
||||
/// </summary>
|
||||
class DashboardModuleViewModel : BaseViewModel {
|
||||
internal class DashboardModuleViewModel : BaseViewModel {
|
||||
|
||||
//List with all Modules inside
|
||||
public ObservableCollection<DashboardModule> Modules { get; set; }
|
||||
|
||||
//Creates Default Modules, remove before release and when implementing the actual data comming from the socket
|
||||
public DashboardModuleViewModel() {
|
||||
public DashboardModuleViewModel(DataTable userdata) {
|
||||
GetModules(userdata);
|
||||
}
|
||||
|
||||
public void GetModules(DataTable userdata) {
|
||||
Modules = new ObservableCollection<DashboardModule>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
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();
|
||||
moduleIcon.BeginInit();
|
||||
moduleIcon.StreamSource = ms;
|
||||
moduleIcon.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
|
||||
moduleIcon.CacheOption = BitmapCacheOption.OnLoad;
|
||||
moduleIcon.EndInit();
|
||||
moduleIcon.Freeze();
|
||||
}
|
||||
Modules.Add(new DashboardModule(true) {
|
||||
ModuleName = "TestModule",
|
||||
Creator = "Username",
|
||||
ModuleIcon = "../../Assets/Images/PlaceHolderModuleLight.png",
|
||||
ModuleName = (string)row[4],
|
||||
Creator = (string)row[1],
|
||||
ModuleIcon = moduleIcon,
|
||||
CreationDate = DateTime.Now.ToString(),
|
||||
ServerInfo = new ServerInformation() {
|
||||
GpuUsage = "20",
|
||||
@@ -27,7 +47,7 @@ namespace Server_Dashboard {
|
||||
DeployDate = DateTime.Now.ToString(),
|
||||
GpuTemp = "69.69",
|
||||
ServerName = "Ubuntu",
|
||||
OSUserName = "crylia " + i,
|
||||
OSUserName = "crylia ",
|
||||
PrivateIpAdress = "192.168.1.1",
|
||||
PublicIpAdress = "85.69.102.58",
|
||||
Uptime = DateTime.Now.ToString()
|
||||
@@ -36,4 +56,4 @@ namespace Server_Dashboard {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,64 +5,30 @@ using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using Server_Dashboard_Socket;
|
||||
using System;
|
||||
using System.Data;
|
||||
|
||||
namespace Server_Dashboard {
|
||||
|
||||
/// <summary>
|
||||
/// View Model for the Dashboard
|
||||
/// </summary>
|
||||
class DashboardViewModel : BaseViewModel {
|
||||
internal class DashboardViewModel : BaseViewModel {
|
||||
|
||||
#region Private Values
|
||||
private readonly DashboardModuleViewModel dmvm = new DashboardModuleViewModel();
|
||||
#endregion
|
||||
|
||||
private readonly DashboardModuleViewModel dmvm;
|
||||
|
||||
#endregion Private Values
|
||||
|
||||
#region Properties
|
||||
|
||||
private string serverName;
|
||||
public string ServerName {
|
||||
get { return serverName; }
|
||||
set {
|
||||
if(serverName != value)
|
||||
serverName = value;
|
||||
OnPropertyChanged(nameof(serverName));
|
||||
}
|
||||
}
|
||||
|
||||
private string moduleName;
|
||||
public string ModuleName {
|
||||
get { return moduleName; }
|
||||
set {
|
||||
if (moduleName != value)
|
||||
moduleName = value;
|
||||
OnPropertyChanged(nameof(moduleName));
|
||||
}
|
||||
}
|
||||
|
||||
private string ipAdress;
|
||||
public string IPAdress {
|
||||
get { return ipAdress; }
|
||||
set {
|
||||
if (ipAdress != value)
|
||||
ipAdress = value;
|
||||
OnPropertyChanged(nameof(ipAdress));
|
||||
}
|
||||
}
|
||||
|
||||
private string port;
|
||||
public string Port {
|
||||
get { return port; }
|
||||
set {
|
||||
if (port != value)
|
||||
port = value;
|
||||
OnPropertyChanged(nameof(port));
|
||||
}
|
||||
}
|
||||
|
||||
//The Username displayed defaults to Username
|
||||
private string userName = "Username";
|
||||
private string userName;
|
||||
|
||||
public string UserName {
|
||||
get { return userName; }
|
||||
set {
|
||||
if(userName != value)
|
||||
set {
|
||||
if (userName != value)
|
||||
userName = value;
|
||||
OnPropertyChanged(nameof(userName));
|
||||
}
|
||||
@@ -70,37 +36,43 @@ namespace Server_Dashboard {
|
||||
|
||||
//List that contains every Module
|
||||
private ObservableCollection<DashboardModule> modules;
|
||||
|
||||
public ObservableCollection<DashboardModule> Modules {
|
||||
get { return modules; }
|
||||
set {
|
||||
if(value != modules)
|
||||
if (value != modules)
|
||||
modules = value;
|
||||
OnPropertyChanged(nameof(modules));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#endregion Properties
|
||||
|
||||
#region Constructor
|
||||
public DashboardViewModel() {
|
||||
//Creates a new echo server, remove b4 release
|
||||
EchoServer echoServer = new EchoServer();
|
||||
echoServer.Start();
|
||||
|
||||
public DashboardViewModel(string username) {
|
||||
UserName = username;
|
||||
//Command inits
|
||||
OpenLinkCommand = new RelayCommand(OpenLink);
|
||||
OpenNewModuleWindowCommand = new RelayCommand(OpenNewModuleWindow);
|
||||
CreateModuleCommand = new RelayCommand(CreateModule);
|
||||
|
||||
DataTable Userdata = DatabaseHandler.GetUserData(username);
|
||||
dmvm = new DashboardModuleViewModel(Userdata);
|
||||
//Sets the local module to the dashboardviewmodule modules
|
||||
Modules = dmvm.Modules;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#endregion Constructor
|
||||
|
||||
#region ICommands
|
||||
|
||||
public ICommand OpenLinkCommand { get; set; }
|
||||
public ICommand OpenNewModuleWindowCommand { get; set; }
|
||||
public ICommand CreateModuleCommand { get; set; }
|
||||
#endregion
|
||||
|
||||
#endregion ICommands
|
||||
|
||||
#region Commands
|
||||
|
||||
/// <summary>
|
||||
/// Opens a given link in the default browser
|
||||
/// </summary>
|
||||
@@ -108,7 +80,7 @@ namespace Server_Dashboard {
|
||||
private void OpenLink(object param) {
|
||||
Process.Start(new ProcessStartInfo((string)param) { UseShellExecute = true });
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new window to create a new Module
|
||||
/// </summary>
|
||||
@@ -116,25 +88,14 @@ namespace Server_Dashboard {
|
||||
private void OpenNewModuleWindow(object param) {
|
||||
//Creates a new CreateModulePopup and sets this view model as datacontext
|
||||
CreateModulePopup cmp = new CreateModulePopup {
|
||||
DataContext = this
|
||||
DataContext = new CreateModuleViewModel(UserName)
|
||||
};
|
||||
//Opens it in the middle of the screen, setting the parent window as owner causes the
|
||||
//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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// No function yes
|
||||
/// </summary>
|
||||
/// <param name="param">Nothing</param>
|
||||
private void CreateModule(object param) {
|
||||
if (!String.IsNullOrWhiteSpace(IPAdress)) {
|
||||
|
||||
} else {
|
||||
//error
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#endregion Commands
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace Server_Dashboard {
|
||||
|
||||
internal class CreateModuleViewModel : BaseViewModel, IWindowHelper {
|
||||
private readonly string username;
|
||||
|
||||
private string serverName;
|
||||
|
||||
public string ServerName {
|
||||
get { return serverName; }
|
||||
set {
|
||||
if (serverName != value)
|
||||
serverName = value;
|
||||
OnPropertyChanged(nameof(serverName));
|
||||
}
|
||||
}
|
||||
|
||||
private string moduleName;
|
||||
|
||||
public string ModuleName {
|
||||
get { return moduleName; }
|
||||
set {
|
||||
if (moduleName != value)
|
||||
moduleName = value;
|
||||
OnPropertyChanged(nameof(moduleName));
|
||||
}
|
||||
}
|
||||
|
||||
private string ipAdress;
|
||||
|
||||
public string IPAdress {
|
||||
get { return ipAdress; }
|
||||
set {
|
||||
if (ipAdress != value)
|
||||
ipAdress = value;
|
||||
OnPropertyChanged(nameof(ipAdress));
|
||||
}
|
||||
}
|
||||
|
||||
private string port;
|
||||
|
||||
public string Port {
|
||||
get { return port; }
|
||||
set {
|
||||
if (port != value)
|
||||
port = value;
|
||||
OnPropertyChanged(nameof(port));
|
||||
}
|
||||
}
|
||||
|
||||
private BitmapImage moduleIcon;
|
||||
|
||||
public BitmapImage ModuleIcon {
|
||||
get { return moduleIcon; }
|
||||
set {
|
||||
if (moduleIcon != value)
|
||||
moduleIcon = value;
|
||||
OnPropertyChanged(nameof(moduleIcon));
|
||||
}
|
||||
}
|
||||
|
||||
private string userInformationMessage;
|
||||
|
||||
public string UserInformationMessage {
|
||||
get { return userInformationMessage; }
|
||||
set {
|
||||
if (userInformationMessage != value)
|
||||
userInformationMessage = value;
|
||||
OnPropertyChanged(nameof(userInformationMessage));
|
||||
}
|
||||
}
|
||||
|
||||
public CreateModuleViewModel(string username) {
|
||||
this.username = username;
|
||||
CreateModuleCommand = new RelayCommand(CreateModuleAsync);
|
||||
SelectIconCommand = new RelayCommand(SelectIcon);
|
||||
RemoveIconCommand = new RelayCommand(RemoveIcon);
|
||||
TestConnectionCommand = new RelayCommand(TestConnection);
|
||||
}
|
||||
|
||||
public ICommand RemoveIconCommand { get; set; }
|
||||
public ICommand SelectIconCommand { get; set; }
|
||||
public ICommand CreateModuleCommand { get; set; }
|
||||
public ICommand TestConnectionCommand { get; set; }
|
||||
|
||||
public Action Close { get; set; }
|
||||
|
||||
//private readonly Regex moduleNameFilter = new Regex(@"^[A-Z][a-z][0-9]{0-20}$");
|
||||
//private readonly Regex serverNameFilter = new Regex(@"^[A-Z][a-z][0-9]{0-20}$");
|
||||
private readonly Regex ipFilter = new Regex(@"^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$");
|
||||
|
||||
/// <summary>
|
||||
/// First checks if the IP address and the other credentials are valid
|
||||
/// than asynchronously sends the data to the database where the module will be saved
|
||||
/// this also triggers a reload of all modules to make sure the newly created module
|
||||
/// will be shown without an application restart
|
||||
/// </summary>
|
||||
/// <param name="param">Nothing</param>
|
||||
private async void CreateModuleAsync(object param) {
|
||||
//Checks if the IP field is not empty and valid
|
||||
if (!String.IsNullOrWhiteSpace(ipAdress) && ipFilter.IsMatch(ipAdress)) {
|
||||
//Gives the Module a default name if the user doesnt name it
|
||||
if (String.IsNullOrWhiteSpace(moduleName))
|
||||
moduleName = "Module";
|
||||
//Gives the Server a default name is the user doesnt name it
|
||||
if (String.IsNullOrWhiteSpace(serverName))
|
||||
serverName = "Server";
|
||||
//Makes sure the name isnt any longer than characters
|
||||
if (moduleName.Length >= 20) {
|
||||
UserInformationMessage = "The Module Name is too long";
|
||||
return;
|
||||
}
|
||||
//Makes sure the name isnt any longer than characters
|
||||
if (serverName.Length >= 20) {
|
||||
UserInformationMessage = "The Server Name is too long";
|
||||
return;
|
||||
}
|
||||
//Clears the error message if there isnt any error
|
||||
UserInformationMessage = "";
|
||||
byte[] moduleIconStream = null;
|
||||
if (moduleIcon != null) {
|
||||
try {
|
||||
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
|
||||
encoder.Frames.Add(BitmapFrame.Create(moduleIcon));
|
||||
using MemoryStream ms = new MemoryStream();
|
||||
encoder.Save(ms);
|
||||
moduleIconStream = ms.ToArray();
|
||||
} catch { }
|
||||
}
|
||||
if (await Task.Run(() => DatabaseHandler.CreateNewModule(ipAdress, moduleName, serverName, username, moduleIconStream, port)) == 0) {
|
||||
Close?.Invoke();
|
||||
} else {
|
||||
UserInformationMessage = "Unknown error occured, please try again later";
|
||||
}
|
||||
} else {
|
||||
UserInformationMessage = "The IP Address is invalid";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a file dialog and lets the user select a .jpg, .jpeg or .png file as icon
|
||||
/// </summary>
|
||||
/// <param name="param"></param>
|
||||
private void SelectIcon(object param) {
|
||||
OpenFileDialog ofd = new OpenFileDialog {
|
||||
Title = "Choose an Image",
|
||||
Filter = "Supported format|*.jpg;*.jpeg;*.png"
|
||||
};
|
||||
if ((bool)ofd.ShowDialog()) {
|
||||
ModuleIcon = new BitmapImage(new Uri(ofd.FileName));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the selected ModuleIcon
|
||||
/// </summary>
|
||||
/// <param name="param"></param>
|
||||
private void RemoveIcon(object param) => ModuleIcon = null;
|
||||
|
||||
/// <summary>
|
||||
/// Tests the socket connection
|
||||
/// </summary>
|
||||
/// <param name="param"></param>
|
||||
private void TestConnection(object param) {
|
||||
//TODO: Test connection to the socket server
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,13 +6,17 @@ using System.Threading.Tasks;
|
||||
using Server_Dashboard_Socket;
|
||||
|
||||
namespace Server_Dashboard {
|
||||
|
||||
/// <summary>
|
||||
/// View Model for the Login Window
|
||||
/// </summary>
|
||||
class LoginViewModel : BaseViewModel, IWindowHelper {
|
||||
internal class LoginViewModel : BaseViewModel, IWindowHelper {
|
||||
|
||||
#region Properties
|
||||
|
||||
//Username Property
|
||||
private string username;
|
||||
|
||||
public string Username {
|
||||
get { return username; }
|
||||
set {
|
||||
@@ -21,8 +25,10 @@ namespace Server_Dashboard {
|
||||
OnPropertyChanged(nameof(username));
|
||||
}
|
||||
}
|
||||
|
||||
//Error Text displays an error to help the user to fill the form
|
||||
private string errorText;
|
||||
|
||||
public string ErrorText {
|
||||
get { return errorText; }
|
||||
set {
|
||||
@@ -31,18 +37,22 @@ namespace Server_Dashboard {
|
||||
OnPropertyChanged(nameof(errorText));
|
||||
}
|
||||
}
|
||||
|
||||
//Remember me button
|
||||
private bool rememberUser;
|
||||
|
||||
public bool RememberUser {
|
||||
get { return rememberUser; }
|
||||
set {
|
||||
if(value != rememberUser)
|
||||
if (value != rememberUser)
|
||||
rememberUser = value;
|
||||
OnPropertyChanged(nameof(rememberUser));
|
||||
}
|
||||
}
|
||||
|
||||
//Loading circle, gets hidden and shown when logging in
|
||||
private string loading;
|
||||
|
||||
public string Loading {
|
||||
get { return loading; }
|
||||
set {
|
||||
@@ -51,16 +61,20 @@ namespace Server_Dashboard {
|
||||
OnPropertyChanged(nameof(loading));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#endregion Properties
|
||||
|
||||
#region Public Values
|
||||
|
||||
//Close action for the Window to close properly
|
||||
public Action Close { get ; set; }
|
||||
#endregion
|
||||
public Action Close { get; set; }
|
||||
|
||||
#endregion Public Values
|
||||
|
||||
#region Constructor
|
||||
|
||||
public LoginViewModel() {
|
||||
SocketClient sc = new SocketClient();
|
||||
//SocketClient sc = new SocketClient();
|
||||
//Loading circle is hidden on startup
|
||||
Loading = "Hidden";
|
||||
//Command inits
|
||||
@@ -74,13 +88,17 @@ namespace Server_Dashboard {
|
||||
//TODO: Autologin
|
||||
//AutoLoginAsync();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#endregion Constructor
|
||||
|
||||
#region ICommands
|
||||
|
||||
public ICommand LoginCommand { get; set; }
|
||||
#endregion
|
||||
|
||||
#endregion ICommands
|
||||
|
||||
#region Commands
|
||||
|
||||
/// <summary>
|
||||
/// Async login
|
||||
/// </summary>
|
||||
@@ -105,6 +123,7 @@ namespace Server_Dashboard {
|
||||
//Sets the error text and exits function
|
||||
ErrorText = "Username or password is wrong.";
|
||||
return;
|
||||
|
||||
case 1:
|
||||
/*No idea why this is here, gonna let it be till the remember me and autologin is 100% done
|
||||
if (RememberUser && !String.IsNullOrEmpty(Settings.Default.Cookies)) {
|
||||
@@ -133,22 +152,26 @@ namespace Server_Dashboard {
|
||||
DatabaseHandler.AddCookie(Username, cookie);
|
||||
}
|
||||
//Creates a new Dashboard window and shows it
|
||||
DashboardWindow window = new DashboardWindow();
|
||||
DashboardWindow window = new DashboardWindow() {
|
||||
DataContext = new DashboardViewModel(Username)
|
||||
};
|
||||
window.Show();
|
||||
//When closed, close it correctly
|
||||
//Close window when dashboard is shown
|
||||
Close?.Invoke();
|
||||
return;
|
||||
|
||||
case 2:
|
||||
//Sets the error text
|
||||
ErrorText = "Server unreachable, connection timeout.";
|
||||
return;
|
||||
|
||||
default:
|
||||
//Sets the error text
|
||||
ErrorText = "An unknown error has occured";
|
||||
return;
|
||||
}
|
||||
//If the Username and password is blank
|
||||
//All these IF's could be one but i made multiple for the different errors so the user knows whats wrong
|
||||
//If the Username and password is blank
|
||||
//All these IF's could be one but i made multiple for the different errors so the user knows whats wrong
|
||||
} else if (String.IsNullOrWhiteSpace(Username) && String.IsNullOrWhiteSpace((parameter as IHavePassword).SecurePassword.Unsecure())) {
|
||||
//Sets the error text
|
||||
ErrorText = "Please provide a username and password";
|
||||
@@ -169,9 +192,11 @@ namespace Server_Dashboard {
|
||||
//If there is no error, clear the error text
|
||||
ErrorText = "";
|
||||
}
|
||||
#endregion
|
||||
|
||||
#endregion Commands
|
||||
|
||||
#region private functions
|
||||
|
||||
//TODO: Add autologin function that locks the UI untill the user hits the abort button or the login completes
|
||||
/*private async void AutoLoginAsync() {
|
||||
if (Settings.Default.RememberMe && !String.IsNullOrEmpty(Settings.Default.Username) && !String.IsNullOrEmpty(Settings.Default.Cookies)) {
|
||||
@@ -186,6 +211,7 @@ namespace Server_Dashboard {
|
||||
}
|
||||
}
|
||||
}*/
|
||||
#endregion
|
||||
|
||||
#endregion private functions
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user