finish create module form

This commit is contained in:
Rene Schwarz
2021-08-09 00:26:03 +02:00
parent f8f28984a5
commit a4a944f0c6
62 changed files with 864 additions and 412 deletions

View File

@@ -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 {
}
}
}
}
}

View File

@@ -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
}
}
}

View File

@@ -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
}
}
}