From 8651169fac81e34685f52f8bb9147115f7ac8765 Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Fri, 13 Nov 2020 03:13:31 +0200 Subject: Working on data store ! --- .../Converters/EventTypeToStringConverter.cs | 29 +++++ .../Utilities/Tango.MachineEM.UI/InputWindow.xaml | 25 +++++ .../Tango.MachineEM.UI/InputWindow.xaml.cs | 56 ++++++++++ .../Utilities/Tango.MachineEM.UI/MainWindow.xaml | 2 +- .../Tango.MachineEM.UI/Tango.MachineEM.UI.csproj | 10 +- .../Tango.MachineEM.UI/ViewModels/MainViewVM.cs | 122 +++++++++++++++++++++ .../Tango.MachineEM.UI/Views/MainView.xaml | 26 ++++- 7 files changed, 263 insertions(+), 7 deletions(-) create mode 100644 Software/Visual_Studio/Utilities/Tango.MachineEM.UI/Converters/EventTypeToStringConverter.cs create mode 100644 Software/Visual_Studio/Utilities/Tango.MachineEM.UI/InputWindow.xaml create mode 100644 Software/Visual_Studio/Utilities/Tango.MachineEM.UI/InputWindow.xaml.cs (limited to 'Software/Visual_Studio/Utilities/Tango.MachineEM.UI') diff --git a/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/Converters/EventTypeToStringConverter.cs b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/Converters/EventTypeToStringConverter.cs new file mode 100644 index 000000000..de771f2a8 --- /dev/null +++ b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/Converters/EventTypeToStringConverter.cs @@ -0,0 +1,29 @@ +using Google.Protobuf.Reflection; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Data; +using Tango.PMR.Diagnostics; + +namespace Tango.MachineEM.UI.Converters +{ + public class EventTypeToStringConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + EventType eventType = (EventType)value; + FieldInfo fi = value.GetType().GetField(eventType.ToString()); + String name = fi.GetCustomAttribute().Name; + return $"{(int)eventType} {name}"; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/InputWindow.xaml b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/InputWindow.xaml new file mode 100644 index 000000000..05a4b33cd --- /dev/null +++ b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/InputWindow.xaml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + diff --git a/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/InputWindow.xaml.cs b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/InputWindow.xaml.cs new file mode 100644 index 000000000..191b53072 --- /dev/null +++ b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/InputWindow.xaml.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Shapes; + +namespace Tango.MachineEM.UI +{ + /// + /// Interaction logic for InputWindow.xaml + /// + public partial class InputWindow : Window + { + public String Value { get; set; } + public String Message { get; set; } + + public InputWindow() + { + InitializeComponent(); + + btnOK.Click += BtnOK_Click; + btnCancel.Click += BtnCancel_Click; + Loaded += InputWindow_Loaded; + } + + private void InputWindow_Loaded(object sender, RoutedEventArgs e) + { + txtValue.Text = Value; + txtMessage.Text = Message; + + txtValue.Focus(); + Keyboard.Focus(txtValue); + } + + private void BtnCancel_Click(object sender, RoutedEventArgs e) + { + DialogResult = false; + Close(); + } + + private void BtnOK_Click(object sender, RoutedEventArgs e) + { + Value = txtValue.Text; + DialogResult = true; + Close(); + } + } +} diff --git a/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/MainWindow.xaml b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/MainWindow.xaml index 7bfc7f0fd..8f066800e 100644 --- a/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/MainWindow.xaml +++ b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/MainWindow.xaml @@ -9,7 +9,7 @@ xmlns:local="clr-namespace:Tango.MachineEM.UI" xmlns:views="clr-namespace:Tango.MachineEM.UI.Views" mc:Ignorable="d" - Title="Tango Embedded Emulator" Height="720" Width="1300" TitleCaps="False" BorderBrush="Gray" BorderThickness="1" WindowStartupLocation="CenterScreen" Background="#202020" Foreground="Gainsboro" DataContext="{Binding RelativeSource={RelativeSource Self}}"> + Title="Tango Embedded Emulator" Height="820" Width="1300" TitleCaps="False" BorderBrush="Gray" BorderThickness="1" WindowStartupLocation="CenterScreen" Background="#202020" Foreground="Gainsboro" DataContext="{Binding RelativeSource={RelativeSource Self}}"> diff --git a/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/Tango.MachineEM.UI.csproj b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/Tango.MachineEM.UI.csproj index 705194057..c6f382afe 100644 --- a/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/Tango.MachineEM.UI.csproj +++ b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/Tango.MachineEM.UI.csproj @@ -83,6 +83,10 @@ InkFillingStatusView.xaml + + + InputWindow.xaml + MainView.xaml @@ -91,6 +95,10 @@ MSBuild:Compile Designer + + Designer + MSBuild:Compile + MSBuild:Compile Designer @@ -200,7 +208,7 @@ - + \ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/ViewModels/MainViewVM.cs b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/ViewModels/MainViewVM.cs index b46b89a4a..c993be2d7 100644 --- a/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/ViewModels/MainViewVM.cs @@ -13,6 +13,8 @@ using Tango.Transport.Transporters; using Tango.Core; using Tango.Settings; using Tango.PMR.ThreadLoading; +using Tango.PMR.DataStore; +using System.Windows; namespace Tango.MachineEM.UI.ViewModels { @@ -138,6 +140,10 @@ namespace Tango.MachineEM.UI.ViewModels /// public RelayCommand AbortJobCommand { get; set; } + public RelayCommand GetDataStoreItemCommand { get; set; } + + public RelayCommand PutDataStoreItemCommand { get; set; } + public RelayCommand PerformDataStoreTestCommand { get; set; } #endregion @@ -191,6 +197,8 @@ namespace Tango.MachineEM.UI.ViewModels SelectedPort = Ports.First(); PerformDataStoreTestCommand = new RelayCommand(Emulator.PerformDataStoreTest); + GetDataStoreItemCommand = new RelayCommand(GetDataStoreItem); + PutDataStoreItemCommand = new RelayCommand(PutDataStoreItem); } #endregion @@ -282,6 +290,120 @@ namespace Tango.MachineEM.UI.ViewModels InvalidateRelayCommands(); } + private void GetDataStoreItem() + { + try + { + var result = GetUserInput("Get Data Store Item", "Enter collection name", String.Empty); + if (!result.Confirmed) return; + var collectionName = result.Value; + + result = GetUserInput("Get Data Store Item", "Enter item key", String.Empty); + if (!result.Confirmed) return; + var key = result.Value; + + Emulator.GetDataStoreItem(new PMR.DataStore.GetDataStoreItemRequest() + { + Collection = collectionName, + Key = key + }); + } + catch { } + } + + private void PutDataStoreItem() + { + try + { + var result = GetUserInput("Put Data Store Item", "Enter collection name", String.Empty); + if (!result.Confirmed) return; + var collectionName = result.Value; + + result = GetUserInput("Put Data Store Item", "Enter item key", String.Empty); + if (!result.Confirmed) return; + var key = result.Value; + + result = GetUserInput("Put Data Store Item", "Enter data type", "Int32"); + if (!result.Confirmed) return; + DataType dataType = (DataType)Enum.Parse(typeof(DataType), result.Value); + + result = GetUserInput("Put Data Store Item", "Enter value", "10"); + if (!result.Confirmed) return; + var value = result.Value; + + DataStoreItem item = new DataStoreItem(); + item.DataType = dataType; + + switch (dataType) + { + case DataType.Int32: + item.Int32Value = int.Parse(value); + break; + case DataType.Float: + item.FloatValue = float.Parse(value); + break; + case DataType.Double: + item.DoubleValue = double.Parse(value); + break; + case DataType.Boolean: + item.BooleanValue = bool.Parse(value); + break; + case DataType.String: + item.StringValue = value; + break; + default: + LogManager.Log("Data type not supported by this emulator."); + throw new NotSupportedException(); + } + + Emulator.PutDataStoreItem(new PutDataStoreItemRequest() + { + Collection = collectionName, + Key = key, + Item = item + }); + } + catch { } + } + + #endregion + + #region Private Methods + + private class InputResult + { + public bool Confirmed { get; set; } + public T Value { get; set; } + } + + private InputResult GetUserInput(String title, String message, T defaultValue) + { + InputWindow window = new InputWindow(); + window.Title = title; + window.Message = message; + window.Value = defaultValue.ToStringSafe(); + window.Owner = Application.Current.MainWindow; + window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner; + window.WindowStyle = WindowStyle.ToolWindow; + + try + { + if (window.ShowDialog().Value) + { + return new InputResult() { Confirmed = true, Value = (T)Convert.ChangeType(window.Value, typeof(T)) }; + } + else + { + return new InputResult(); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error parsing input."); + throw ex; + } + } + #endregion } } diff --git a/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/Views/MainView.xaml b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/Views/MainView.xaml index f553ec588..c0c4b7b97 100644 --- a/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/Views/MainView.xaml +++ b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/Views/MainView.xaml @@ -11,13 +11,15 @@ xmlns:vm="clr-namespace:Tango.MachineEM.UI.ViewModels" xmlns:pmr="clr-namespace:Tango.PMR.MachineStatus;assembly=Tango.PMR" xmlns:threadLoading="clr-namespace:Tango.PMR.ThreadLoading;assembly=Tango.PMR" + xmlns:localConverters="clr-namespace:Tango.MachineEM.UI.Converters" xmlns:local="clr-namespace:Tango.MachineEM.UI.Views" mc:Ignorable="d" - d:DesignHeight="720" d:DesignWidth="1300" Foreground="Gainsboro" d:DataContext="{d:DesignInstance Type=vm:MainViewVM, IsDesignTimeCreatable=False}" > + d:DesignHeight="780" d:DesignWidth="1300" Foreground="Gainsboro" d:DataContext="{d:DesignInstance Type=vm:MainViewVM, IsDesignTimeCreatable=False}" > + @@ -150,14 +152,28 @@ - + + + + @@ -183,7 +199,7 @@ - + -- cgit v1.3.1