From 856773f7eafb9d04500ede0cfae9c0e75231418b Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Tue, 3 Jul 2018 16:36:54 +0300 Subject: Implemented StubsView & VM !!! --- .../Properties/Resources.Designer.cs | 29 -- .../Properties/Resources.resx | 4 - .../Resources/CodeTabTemplate.cs | 20 - .../Tango.MachineStudio.Stubs/StubManager.cs | 181 -------- .../StubOnExecuteParameters.cs | 30 -- .../Tango.MachineStudio.Stubs.csproj | 5 - .../ViewModels/CodeTabVM.cs | 100 ----- .../ViewModels/MainViewVM.cs | 463 +-------------------- .../ViewModels/StubSnippetVM.cs | 47 --- .../Tango.MachineStudio.Stubs/Views/MainView.xaml | 325 +-------------- .../Views/MainView.xaml.cs | 21 - 11 files changed, 16 insertions(+), 1209 deletions(-) delete mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Resources/CodeTabTemplate.cs delete mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/StubManager.cs delete mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/StubOnExecuteParameters.cs delete mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/CodeTabVM.cs delete mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/StubSnippetVM.cs (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs') diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Properties/Resources.Designer.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Properties/Resources.Designer.cs index e712344b4..646376036 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Properties/Resources.Designer.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Properties/Resources.Designer.cs @@ -59,34 +59,5 @@ namespace Tango.MachineStudio.Stubs.Properties { resourceCulture = value; } } - - /// - /// Looks up a localized string similar to using System; - ///using System.Text; - ///using System.Linq; - ///using System.Drawing; - ///using System.Diagnostics; - ///using System.Windows.Forms; - ///using System.Threading; - ///using System.Threading.Tasks; - ///using System.Collections.Generic; - ///using Tango.Stubs.UI; - /// - ///public void OnExecute(StubManager stubManager) - ///{ - /// for (int i = 0; i < 10; i++) - /// { - /// stubManager.Run("calculate", 10, 5); - /// Thread.Sleep(10); - /// } - ///} - /// - ///. - /// - internal static string CodeTabTemplate { - get { - return ResourceManager.GetString("CodeTabTemplate", resourceCulture); - } - } } } diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Properties/Resources.resx b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Properties/Resources.resx index 7693cadac..1af7de150 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Properties/Resources.resx +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Properties/Resources.resx @@ -117,8 +117,4 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - ..\Resources\CodeTabTemplate.cs;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 - \ No newline at end of file diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Resources/CodeTabTemplate.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Resources/CodeTabTemplate.cs deleted file mode 100644 index b21e6e758..000000000 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Resources/CodeTabTemplate.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Text; -using System.Linq; -using System.Drawing; -using System.Diagnostics; -using System.Windows.Forms; -using System.Threading; -using System.Threading.Tasks; -using System.Collections.Generic; -using Tango.MachineStudio.Stubs; - -public void OnExecute(StubManager stubManager) -{ - for (int i = 0; i < 10; i++) - { - stubManager.Run("calculate", 10, 5); - Thread.Sleep(10); - } -} - diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/StubManager.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/StubManager.cs deleted file mode 100644 index 1808e2a53..000000000 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/StubManager.cs +++ /dev/null @@ -1,181 +0,0 @@ -using Google.Protobuf; -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using Tango.PMR; -using Tango.PMR.Common; -using Tango.Stubs; -using Tango.Transport; -using Tango.Transport.Adapters; - -namespace Tango.MachineStudio.Stubs -{ - /// - /// Represents a manager capable of executing stub scripts asynchronously. - /// - public class StubManager - { - public ITransportAdapter Adapter { get; private set; } - - /// - /// Occurs when the stub has failed to execute. - /// - public event EventHandler Failed; - - /// - /// Occurs when the stub has completed successfully. - /// - public event EventHandler Completed; - - /// - /// Occurs when the stub has been initialized and executed. - /// - public event EventHandler Executed; - - /// - /// Gets a value indicating whether this is aborted. - /// - internal bool Aborted { get; private set; } - - /// - /// Initializes a new instance of the class. - /// - /// The adapter. - public StubManager(ITransportAdapter adapter) - { - Adapter = adapter; - } - - /// - /// Aborts the current script. - /// - internal void Abort() - { - Aborted = true; - } - - /// - /// Runs the specified stub name. - /// - /// Name of the stub. - /// The arguments. - public void Run(String stubName, params Object[] args) - { - if (Aborted) return; - - var stubType = StubBase.GetAvailableRequestStubs().SingleOrDefault(x => x.Name.ToLower() == stubName.ToLower() || x.Name.Replace("Request", "").ToLower() == stubName.ToLower()); - if (stubType == null) - { - OnFailed(new ArgumentException("Invalid stub '" + stubName + "'.")); - return; - } - - var stubProps = stubType.GetProperties(BindingFlags.Public | BindingFlags.Instance); - - if (stubProps.Length > args.Length) - { - OnFailed(new ArgumentOutOfRangeException("Not enough arguments for " + stubType.Name + ".")); - return; - } - - Executed?.Invoke(this, stubType.Name); - - try - { - MessageContainer container = new MessageContainer(); - container.Token = Guid.NewGuid().ToString(); - container.Type = MessageFactory.ParseMessageType(stubType.Name); - - Object request = Activator.CreateInstance(stubType); - - int argIndex = 0; - foreach (var prop in stubProps) - { - String arg = args[argIndex++].ToString(); - - if (prop.PropertyType == typeof(UInt32)) - { - prop.SetValue(request, UInt32.Parse(arg)); - } - else if (prop.PropertyType == typeof(bool)) - { - prop.SetValue(request, bool.Parse(arg)); - } - else - { - object converted = Convert.ChangeType(arg, prop.PropertyType); - prop.SetValue(request, converted); - } - } - - container.Data = typeof(IMessage).GetExtensionMethod(typeof(ByteString).Assembly, "ToByteString").Invoke(request, new object[] { request }) as ByteString; - - byte[] requestData = container.ToByteArray(); - - bool done = false; - - Task.Factory.StartNew(() => - { - Adapter.Write(requestData); - - DateTime startTime = DateTime.Now; - - MessageContainer responseContainer = null; - - Adapter.DataAvailable += (sender, data) => - { - responseContainer = MessageFactory.ParseContainer(data); - }; - - while (responseContainer == null) - { - Thread.Sleep(2); - - if (DateTime.Now > startTime.AddSeconds(2)) - { - done = true; - OnFailed(new TimeoutException("Response has failed to arrive after 2 seconds.")); - return; - } - } - - IMessage message = MessageFactory.ExtractMessageFromContainer(responseContainer); - OnCompleted(JsonConvert.SerializeObject(message, Formatting.Indented)); - done = true; - }); - - while (!done) - { - Thread.Sleep(2); - } - } - catch (Exception ex) - { - OnFailed(ex); - } - } - - /// - /// Raises the event. - /// - /// The exception. - protected virtual void OnFailed(Exception ex) - { - Failed?.Invoke(this, ex); - } - - /// - /// Raises the event. - /// - /// The response. - protected virtual void OnCompleted(String response) - { - Completed?.Invoke(this, response); - } - } -} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/StubOnExecuteParameters.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/StubOnExecuteParameters.cs deleted file mode 100644 index 761375818..000000000 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/StubOnExecuteParameters.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.Scripting; - -namespace Tango.MachineStudio.Stubs -{ - /// - /// Represents the global object which will be sent to the scripting engine. - /// - /// - public class StubOnExecuteParameters : OnExecuteParameters - { - /// - /// Provides access to the script stub manager. - /// - public StubManager stubManager; - - /// - /// Initializes a new instance of the class. - /// - /// The manager. - public StubOnExecuteParameters(StubManager manager) - { - stubManager = manager; - } - } -} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Tango.MachineStudio.Stubs.csproj b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Tango.MachineStudio.Stubs.csproj index a3618de3a..99ba89122 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Tango.MachineStudio.Stubs.csproj +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Tango.MachineStudio.Stubs.csproj @@ -68,17 +68,12 @@ - MachineStudio.cs - - - - MainView.xaml diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/CodeTabVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/CodeTabVM.cs deleted file mode 100644 index 451e1a1f0..000000000 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/CodeTabVM.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.Core.Commands; -using Tango.SharedUI; - -namespace Tango.MachineStudio.Stubs.ViewModels -{ - /// - /// Represents a single script editor tab view model; - /// - /// - public class CodeTabVM : ViewModel - { - private String _title; - /// - /// Gets or sets the script title/file name. - /// - public String Title - { - get - { - return File != null ? Path.GetFileName(File) : _title; - } - set - { - _title = value; - RaisePropertyChanged(nameof(Title)); - } - } - - private String _file; - /// - /// Gets or sets the full script file path. - /// - public String File - { - get { return _file; } - set - { - _file = value; - RaisePropertyChanged(nameof(File)); - RaisePropertyChanged(nameof(Title)); - } - } - - private String _code; - /// - /// Gets or sets the script code. - /// - public String Code - { - get { return _code; } - set { _code = value; RaisePropertyChanged(nameof(Code)); } - } - - private bool _isRunning; - /// - /// Gets or sets a value indicating whether this instance is running. - /// - public bool IsRunning - { - get { return _isRunning; } - set { _isRunning = value; RaisePropertyChangedAuto(); } - } - - private RelayCommand _insertCodeSnippetCommand; - /// - /// Gets or sets the insert snippet command. (Inserts stub snippet to editor) - /// - public RelayCommand InsertSnippetCommand - { - get { return _insertCodeSnippetCommand; } - set { _insertCodeSnippetCommand = value; RaisePropertyChanged(nameof(InsertSnippetCommand)); } - } - - /// - /// Initializes a new instance of the class. - /// - public CodeTabVM() - { - Title = "untitled"; - Code = Properties.Resources.CodeTabTemplate; - } - - /// - /// Returns a that represents this instance. - /// - /// - /// A that represents this instance. - /// - public override string ToString() - { - return Title; - } - } -} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs index afd5e4d70..f222b7584 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs @@ -12,10 +12,12 @@ using Tango.Core.Commands; using Tango.MachineStudio.Common; using Tango.MachineStudio.Common.Notifications; using Tango.MachineStudio.Common.StudioApplication; +using Tango.PMR; using Tango.Scripting; using Tango.Settings; using Tango.SharedUI; using Tango.Stubs; +using Tango.Stubs.ViewModels; using Tango.Transport; using Tango.Transport.Adapters; @@ -27,8 +29,6 @@ namespace Tango.MachineStudio.Stubs.ViewModels /// public class MainViewVM : StudioViewModel { - private UsbTransportAdapter _adapter; //Holds the USB transport adapter. - private StubManager _stubManager; private INotificationProvider _notification; private StubsModuleSettings _settings; @@ -58,162 +58,18 @@ namespace Tango.MachineStudio.Stubs.ViewModels } } + private StubsViewVM _stubsViewVM; /// - /// Gets or sets the code tabs. + /// Gets or sets the stubs view vm. /// - public ObservableCollection CodeTabs { get; set; } - - /// - /// Gets or sets the additional highlight C# types. - /// - public ObservableCollection> HighlightTypes { get; set; } - - /// - /// Gets or sets the collection of stub snippets. - /// - public ObservableCollection StubSnippets { get; set; } - - private StubSnippetVM _selectedStubSnippet; - /// - /// Gets or sets the selected stub snippet. - /// - public StubSnippetVM SelectedStubSnippet - { - get { return _selectedStubSnippet; } - set { _selectedStubSnippet = value; RaisePropertyChanged(nameof(SelectedStubSnippet)); } - } - - private String _log; - /// - /// Gets or sets the current response log. - /// - public String Log + public StubsViewVM StubsViewVM { - get { return _log; } - set { _log = value; RaisePropertyChanged(nameof(Log)); } - } - - private CodeTabVM _selectedCodeTab; - /// - /// Gets or sets the selected code tab. - /// - public CodeTabVM SelectedCodeTab - { - get { return _selectedCodeTab; } - set { _selectedCodeTab = value; RaisePropertyChanged(nameof(SelectedCodeTab)); InvalidateRelayCommands(); } - } - - private bool _isConnected; - /// - /// Gets or sets a value indicating whether the USB adapter is connected. - /// - public bool IsConnected - { - get { return _isConnected; } - set { _isConnected = value; RaisePropertyChanged(nameof(IsConnected)); InvalidateRelayCommands(); } - } - - private List _ports; - /// - /// Gets or sets the available USB ports. - /// - public List Ports - { - get { return _ports; } - set { _ports = value; RaisePropertyChanged(nameof(Ports)); } - } - - private String _selectedPort; - /// - /// Gets or sets the selected USB port. - /// - public String SelectedPort - { - get { return _selectedPort; } - set { _selectedPort = value; RaisePropertyChanged(nameof(SelectedPort)); InvalidateRelayCommands(); } - } - - private String _status; - /// - /// Gets or sets the current status bar text. - /// - public String Status - { - get { return _status; } - set { _status = value; RaisePropertyChanged(nameof(Status)); } - } - - private bool _isRunning; - /// - /// Gets or sets a value indicating whether a stub is currently running. - /// - public bool IsRunning - { - get { return _isRunning; } - set { _isRunning = value; RaisePropertyChanged(nameof(IsRunning)); InvalidateRelayCommands(); } + get { return _stubsViewVM; } + set { _stubsViewVM = value; RaisePropertyChangedAuto(); } } #endregion - #region Commands - - /// - /// Gets or sets the new command. - /// - public RelayCommand NewCommand { get; set; } - - /// - /// Gets or sets the close tab command. - /// - public RelayCommand CloseTabCommand { get; set; } - - /// - /// Gets or sets the run command. - /// - public RelayCommand RunCommand { get; set; } - - /// - /// Gets or sets the stop command. - /// - public RelayCommand StopCommand { get; set; } - - /// - /// Gets or sets the toggle connection command. - /// - public RelayCommand ToggleConnectionCommand { get; set; } - - /// - /// Gets or sets the open command. - /// - public RelayCommand OpenCommand { get; set; } - - /// - /// Gets or sets the save command. - /// - public RelayCommand SaveCommand { get; set; } - - /// - /// Gets or sets the save as command. - /// - public RelayCommand SaveAsCommand { get; set; } - - /// - /// Gets or sets the stub snippet selected command. - /// - public RelayCommand StubSnippetSelectedCommand { get; set; } - - /// - /// Gets or sets the insert snippet command. - /// - public RelayCommand InsertSnippetCommand { get; set; } - - /// - /// Gets or sets the exit command. - /// - public RelayCommand ExitCommand { get; set; } - - #endregion - #region Constructors /// @@ -221,310 +77,14 @@ namespace Tango.MachineStudio.Stubs.ViewModels /// public MainViewVM(IStudioApplicationManager applicationManager, INotificationProvider notification) { + StubsViewVM = new StubsViewVM(ConnectionMode.External); + _settings = SettingsManager.Default.GetOrCreate(); ApplicationManager = applicationManager; _notification = notification; - CodeTabs = new ObservableCollection(); - - NewCommand = new RelayCommand(CreateNewTab); - CloseTabCommand = new RelayCommand(OnTabClosing); - RunCommand = new RelayCommand(RunTab, (x) => (IsConnected || UseConnectedMachine) && !IsRunning && SelectedCodeTab != null); - StopCommand = new RelayCommand(StopTab, (x) => (IsConnected || UseConnectedMachine) && IsRunning && SelectedCodeTab != null); - InsertSnippetCommand = new RelayCommand((x) => { }); - - HighlightTypes = new ObservableCollection>(); - HighlightTypes.Add(new KeyValuePair("stubManager", typeof(StubManager))); - - StubSnippets = new ObservableCollection(); - - foreach (var stubType in StubBase.GetAvailableRequestStubs()) - { - StubSnippetVM snippet = new StubSnippetVM(); - snippet.Name = stubType.Name.Replace("Stub_", ""); - snippet.Code = String.Format("stubManager.Run(\"{0}\" ,{1});", stubType.Name, String.Join(", ", stubType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Select(x => x.PropertyType.Name == "string" ? "\"string\"" : x.PropertyType.Name))); - StubSnippets.Add(snippet); - } - - ToggleConnectionCommand = new RelayCommand(ToggleConnection, (x) => !IsRunning); - OpenCommand = new RelayCommand(OpenFile); - SaveCommand = new RelayCommand(SaveFile); - SaveAsCommand = new RelayCommand(SaveAsFile); - StubSnippetSelectedCommand = new RelayCommand(OnStubSnippetSelected); - ExitCommand = new RelayCommand(() => Application.Current.Shutdown()); - - Ports = new List() - { - "COM1", - "COM2", - "COM3", - "COM4", - "COM5", - "COM6", - "COM7", - "COM8", - "COM9", - }; - - SelectedPort = _settings.SelectedPort != null ? _settings.SelectedPort : Ports.First(); - - Status = "Ready"; - - if (_settings.LastTabs.Count > 0) - { - foreach (var file in _settings.LastTabs) - { - if (File.Exists(file)) - { - OpenFile(file); - } - } - } - else - { - CreateNewTab(); - } - } - - #endregion - - #region Virtual Methods - - /// - /// Called when a stub snippet is double clicked. - /// - protected virtual void OnStubSnippetSelected() - { - if (SelectedStubSnippet != null) - { - if (InsertSnippetCommand != null) - { - InsertSnippetCommand.Execute(SelectedStubSnippet.Code); - } - } - } - - /// - /// Called when user closes a script tab. - /// - /// The code tab. - protected virtual void OnTabClosing(CodeTabVM codeTab) - { - CodeTabs.Remove(codeTab); - } - - #endregion - - #region Private Methods - - /// - /// Saves the selected script file. - /// - private void SaveFile() - { - if (SelectedCodeTab != null) - { - if (SelectedCodeTab.File == null) - { - SaveAsFile(); - } - else - { - File.WriteAllText(SelectedCodeTab.File, SelectedCodeTab.Code); - } - } - } - - /// - /// Saves the selected script file. - /// - private void SaveAsFile() - { - if (SelectedCodeTab != null) - { - SaveFileDialog dlg = new SaveFileDialog(); - dlg.Filter = "C# Script Files|*.cs"; - dlg.DefaultExt = ".cs"; - if (dlg.ShowDialog().Value) - { - File.WriteAllText(dlg.FileName, SelectedCodeTab.Code); - SelectedCodeTab.File = dlg.FileName; - } - } - } - - /// - /// Opens a script from HD. - /// - private void OpenFile() - { - OpenFileDialog dlg = new OpenFileDialog(); - dlg.Filter = "C# Script Files|*.cs"; - dlg.Multiselect = true; - if (dlg.ShowDialog().Value) - { - foreach (var file in dlg.FileNames) - { - OpenFile(file); - } - } - } - - /// - /// Opens the file. - /// - /// The file. - private void OpenFile(String file) - { - var newTab = new CodeTabVM(); - newTab.File = file; - newTab.Code = File.ReadAllText(file); - CodeTabs.Add(newTab); - SelectedCodeTab = newTab; - } - - /// - /// Toggles the USB adapter connection. - /// - private void ToggleConnection() - { - try - { - if (!IsConnected) - { - _adapter = new UsbTransportAdapter(SelectedPort); - _adapter.Connect().Wait(); - IsConnected = true; - } - else - { - _adapter.Disconnect().Wait(); - IsConnected = false; - } - } - catch (Exception ex) - { - MessageBox.Show(ex.ToString(), "Tango"); - } - } - - /// - /// Creates a new script tab. - /// - private void CreateNewTab() - { - var newTab = new CodeTabVM(); - CodeTabs.Add(newTab); - SelectedCodeTab = newTab; - } - - /// - /// Runs the selected script tab. - /// - private async void RunTab() - { - if (UseConnectedMachine && !ApplicationManager.IsMachineConnected) - { - _notification.ShowError("Cannot execute stub while 'Connected Machine' is set but no machine connected."); - return; - } - - IsRunning = true; - SelectedCodeTab.IsRunning = true; - Log = (DateTime.Now.ToTimeString() + ": ") + "Executing script '" + SelectedCodeTab.Title + "'..." + Environment.NewLine; - - await Task.Factory.StartNew(async () => - { - try - { - ITransportAdapter adapter = _adapter; - - if (ApplicationManager.IsMachineConnected && UseConnectedMachine) - { - adapter = ApplicationManager.ConnectedMachine.Adapter; - } - - _stubManager = new StubManager(adapter); - var thisStubManager = _stubManager; - _stubManager.Completed += Manager_Completed; - _stubManager.Failed += Manager_Failed; - _stubManager.Executed += Manager_Executed; - - ScriptEngine engine = new ScriptEngine(new StubOnExecuteParameters(_stubManager)); - - engine.ReferencedAssemblies.Add(this.GetType()); - await engine.Run(SelectedCodeTab.Code,null); - - if (!thisStubManager.Aborted) - { - IsRunning = false; - SelectedCodeTab.IsRunning = false; - } - } - catch (Exception ex) - { - IsRunning = false; - SelectedCodeTab.IsRunning = false; - MessageBox.Show(ex.Message, "Tango"); - } - }); - } - - /// - /// Stops the currently current script. - /// - private void StopTab() - { - if (_stubManager != null) - { - _stubManager.Abort(); - IsRunning = false; - SelectedCodeTab.IsRunning = false; - Status = "Stopped!"; - Log += (DateTime.Now.ToTimeString() + ": ") + "Stopped!" + Environment.NewLine; - } - } - - #endregion - - #region Event Handlers - - /// - /// Handled the Executed event. - /// - /// The sender. - /// Name of the stub. - private void Manager_Executed(object sender, string stubName) - { - Log += (DateTime.Now.ToTimeString() + ": ") + "Executing '" + stubName + "'..." + Environment.NewLine; - Status = "Executing " + stubName + "..."; - } - - /// - /// Handled the Failed event. - /// - /// The sender. - /// The exception. - private void Manager_Failed(object sender, Exception ex) - { - if (IsRunning) - { - Log += (DateTime.Now.ToTimeString() + ": ") + ex.Message + Environment.NewLine; - Status = "Failed!"; - } - } - - /// - /// Handled the Completed event. - /// - /// The sender. - /// The response. - private void Manager_Completed(object sender, string response) - { - Log += (DateTime.Now.ToTimeString() + ": ") + "Response Received:" + Environment.NewLine; - Log += (DateTime.Now.ToTimeString() + ": ") + response + Environment.NewLine; - Status = "Completed"; + StubsViewVM.Bind(nameof(StubsViewVM.MachineOperator), ApplicationManager, nameof(ApplicationManager.ConnectedMachine)); } #endregion @@ -537,8 +97,7 @@ namespace Tango.MachineStudio.Stubs.ViewModels /// public override void OnShuttingDown() { - _settings.SelectedPort = SelectedPort; - _settings.LastTabs = CodeTabs.Select(x => x.File).ToList(); + } #endregion diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/StubSnippetVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/StubSnippetVM.cs deleted file mode 100644 index d99906f20..000000000 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/StubSnippetVM.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.SharedUI; - -namespace Tango.MachineStudio.Stubs.ViewModels -{ - /// - /// Represents a single stub snippet view model. - /// - /// - public class StubSnippetVM : ViewModel - { - private String _name; - /// - /// Gets or sets the stub name. - /// - public String Name - { - get { return _name; } - set { _name = value; RaisePropertyChanged(nameof(Name)); } - } - - private String _code; - /// - /// Gets or sets the snippet code. - /// - public String Code - { - get { return _code; } - set { _code = value; RaisePropertyChanged(nameof(Code)); } - } - - /// - /// Returns a that represents this instance. - /// - /// - /// A that represents this instance. - /// - public override string ToString() - { - return Name; - } - } -} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Views/MainView.xaml b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Views/MainView.xaml index f1254db47..bac93bff3 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Views/MainView.xaml +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Views/MainView.xaml @@ -2,18 +2,16 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - xmlns:mahapps="http://metro.mahapps.com/winfx/xaml/controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:views="clr-namespace:Tango.MachineStudio.Stubs.Views" - xmlns:fa="http://schemas.fontawesome.io/icons/" xmlns:global="clr-namespace:Tango.MachineStudio.Stubs" + xmlns:stubs="clr-namespace:Tango.Stubs.Views;assembly=Tango.Stubs" + xmlns:local="clr-namespace:Tango.MachineStudio.Stubs.Views" - xmlns:converters="clr-namespace:Tango.SharedUI.Converters;assembly=Tango.SharedUI" - xmlns:controls="clr-namespace:Tango.SharedUI.Controls;assembly=Tango.SharedUI" mc:Ignorable="d" d:DesignHeight="720" d:DesignWidth="1280" Background="#202020" Foreground="Gainsboro" DataContext="{x:Static global:ViewModelLocator.MainViewVM}"> - + @@ -28,324 +26,11 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Use Connected Machine - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Views/MainView.xaml.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Views/MainView.xaml.cs index 2d099ef16..238772f07 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Views/MainView.xaml.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/Views/MainView.xaml.cs @@ -26,27 +26,6 @@ namespace Tango.MachineStudio.Stubs.Views public MainView() : base() { InitializeComponent(); - this.Loaded += MainView_Loaded; - } - - private void MainView_Loaded(object sender, RoutedEventArgs e) - { - //ServiceLocator.Current.GetInstance().CodeTabs.RemoveAt(0); - } - - //Auto scroll to bottom of response log each time it is changed. - private void TextBox_TextChanged(object sender, TextChangedEventArgs e) - { - Task.Factory.StartNew(() => - { - Thread.Sleep(50); - - this.Dispatcher.Invoke(() => - { - txtLog.SelectionStart = txtLog.Text.Length; - txtLog.ScrollToEnd(); - }); - }); } } } -- cgit v1.3.1 From 39c49d522d68a591d60aecdeab8149f1bb04415c Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Tue, 3 Jul 2018 17:15:11 +0300 Subject: Stubs working good on Machine Studio... --- Software/DB/Tango.mdf | Bin 75497472 -> 75497472 bytes Software/DB/Tango_log.ldf | Bin 1572864 -> 1572864 bytes .../ViewModels/MainViewVM.cs | 2 +- .../Tango.Stubs/ViewModels/StubsViewVM.cs | 35 ++++++++++++--------- .../Tango.Stubs/Views/StubsView.xaml.cs | 5 ++- 5 files changed, 25 insertions(+), 17 deletions(-) (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs') diff --git a/Software/DB/Tango.mdf b/Software/DB/Tango.mdf index fd19b6140..3d183bcd4 100644 Binary files a/Software/DB/Tango.mdf and b/Software/DB/Tango.mdf differ diff --git a/Software/DB/Tango_log.ldf b/Software/DB/Tango_log.ldf index db39d922f..e31a2abc0 100644 Binary files a/Software/DB/Tango_log.ldf and b/Software/DB/Tango_log.ldf differ diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs index f222b7584..b1ffd9071 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs @@ -97,7 +97,7 @@ namespace Tango.MachineStudio.Stubs.ViewModels /// public override void OnShuttingDown() { - + StubsViewVM.SaveSettings(); } #endregion diff --git a/Software/Visual_Studio/Tango.Stubs/ViewModels/StubsViewVM.cs b/Software/Visual_Studio/Tango.Stubs/ViewModels/StubsViewVM.cs index dbcf0887b..64f5a2913 100644 --- a/Software/Visual_Studio/Tango.Stubs/ViewModels/StubsViewVM.cs +++ b/Software/Visual_Studio/Tango.Stubs/ViewModels/StubsViewVM.cs @@ -419,8 +419,6 @@ namespace Tango.Stubs.ViewModels Examples = Examples.OrderBy(x => x.Name).ToList(); - Application.Current.Exit += Current_Exit; - LogManager.RegisterLogger(new Logging.FileLogger() { Enabled = true }); LogManager.Log("Application Started!"); } @@ -546,6 +544,8 @@ namespace Tango.Stubs.ViewModels File.WriteAllText(SelectedCodeTab.File, SelectedCodeTab.Code); await Task.Delay(1000); Status = "Ready"; + + SaveSettings(); } } } @@ -567,10 +567,21 @@ namespace Tango.Stubs.ViewModels SelectedCodeTab.File = dlg.FileName; await Task.Delay(1000); Status = "Ready"; + + SaveSettings(); } } } + public void SaveSettings() + { + _settings.AutoLogResponse = AppendLogAuto; + _settings.SelectedPort = SelectedPort; + _settings.BaudRate = BaudRate; + _settings.LastTabs = CodeTabs.Select(x => x.File).ToList(); + _settings.Save(); + } + /// /// Opens a script from HD. /// @@ -623,7 +634,7 @@ namespace Tango.Stubs.ViewModels } catch (Exception ex) { - MessageBox.Show(ex.ToString(), "Tango"); + AppendTextLog(ex.Message); } } @@ -646,6 +657,12 @@ namespace Tango.Stubs.ViewModels if (SelectedCodeTab.Errors.Count > 0) return; + if (MachineOperator == null || MachineOperator.State != TransportComponentState.Connected) + { + AppendTextLog("Machine operator is not initialized or connected. Could not execute script."); + return; + } + IsRunning = true; SelectedCodeTab.IsRunning = true; _logTextBox.Text = (DateTime.Now.ToTimeString() + ": ") + "Executing script '" + SelectedCodeTab.Title + "'..." + Environment.NewLine; @@ -687,7 +704,6 @@ namespace Tango.Stubs.ViewModels { IsRunning = false; SelectedCodeTab.IsRunning = false; - MessageBox.Show(ex.Message, "Tango"); } }); } @@ -807,17 +823,6 @@ namespace Tango.Stubs.ViewModels Status = "Completed"; } - private void Current_Exit(object sender, ExitEventArgs e) - { - LogManager.OverrideQueue = true; - LogManager.Log("Application Terminated"); - _settings.AutoLogResponse = AppendLogAuto; - _settings.SelectedPort = SelectedPort; - _settings.BaudRate = BaudRate; - _settings.LastTabs = CodeTabs.Select(x => x.File).ToList(); - _settings.Save(); - } - private void AppendTextLog(String log) { LogManager.Log(log); diff --git a/Software/Visual_Studio/Tango.Stubs/Views/StubsView.xaml.cs b/Software/Visual_Studio/Tango.Stubs/Views/StubsView.xaml.cs index 0f772f4ba..8c0619fdf 100644 --- a/Software/Visual_Studio/Tango.Stubs/Views/StubsView.xaml.cs +++ b/Software/Visual_Studio/Tango.Stubs/Views/StubsView.xaml.cs @@ -34,7 +34,10 @@ namespace Tango.Stubs.Views _vm = DataContext as StubsViewVM; _vm.SetLogTextBox(txtLog); - + var source = tabControl.ItemsSource; + tabControl.ItemsSource = null; + tabControl.ItemsSource = source; + tabControl.SelectedIndex = 0; }; } -- cgit v1.3.1 From 33a7f16d7f6f7d0be58fb5ac77dcb84934114d48 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Tue, 3 Jul 2018 17:49:38 +0300 Subject: Embedded StubsView in Stubs UI. --- Software/DB/Tango.mdf | Bin 75497472 -> 75497472 bytes Software/DB/Tango_log.ldf | Bin 1572864 -> 1572864 bytes .../ViewModels/MainViewVM.cs | 25 - .../Tango.Settings/SettingsManager.cs | 5 + .../Tango.Stubs/ViewModels/StubsViewVM.cs | 19 +- .../Visual_Studio/Tango.Stubs/Views/StubsView.xaml | 164 +++-- .../Utilities/Tango.Stubs.UI/App.xaml | 5 - .../Tango.Stubs.UI/Examples/BasicRequest.cs | 35 - .../Tango.Stubs.UI/Examples/BasicRequestPolling.cs | 28 - .../Tango.Stubs.UI/Examples/ContinuousResponse.cs | 25 - .../Examples/ContinuousResponseAdvanced.cs | 35 - .../Examples/InvokeResponseWindow.cs | 28 - .../Examples/ManualRequestInitialization.cs | 26 - .../Tango.Stubs.UI/Examples/RequestTimeout.cs | 19 - .../Utilities/Tango.Stubs.UI/Examples/Sleep.cs | 30 - .../Tango.Stubs.UI/Examples/WriteToFile.cs | 29 - .../Utilities/Tango.Stubs.UI/MainWindow.xaml | 4 +- .../Properties/Resources.Designer.cs | 24 - .../Tango.Stubs.UI/Properties/Resources.resx | 4 - .../Tango.Stubs.UI/Resources/CodeTabTemplate.cs | 28 - .../Utilities/Tango.Stubs.UI/StubManager.cs | 481 ------------- .../Tango.Stubs.UI/StubOnExecuteParameters.cs | 30 - .../Utilities/Tango.Stubs.UI/StubsUISettings.cs | 42 -- .../Utilities/Tango.Stubs.UI/Tango.Stubs.UI.csproj | 27 - .../Tango.Stubs.UI/ViewModels/CodeTabVM.cs | 113 --- .../Tango.Stubs.UI/ViewModels/CreateGroupVM.cs | 21 - .../Tango.Stubs.UI/ViewModels/CreateItemVM.cs | 16 - .../Tango.Stubs.UI/ViewModels/ExampleVM.cs | 15 - .../Tango.Stubs.UI/ViewModels/MainViewVM.cs | 757 +-------------------- .../Tango.Stubs.UI/ViewModels/StubSnippetVM.cs | 47 -- .../Utilities/Tango.Stubs.UI/Views/MainView.xaml | 401 +---------- .../Tango.Stubs.UI/Views/MainView.xaml.cs | 41 +- .../Tango.Stubs.UI/Windows/TextInputWindow.xaml | 25 - .../Tango.Stubs.UI/Windows/TextInputWindow.xaml.cs | 53 -- 34 files changed, 113 insertions(+), 2489 deletions(-) delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/BasicRequest.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/BasicRequestPolling.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/ContinuousResponse.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/ContinuousResponseAdvanced.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/InvokeResponseWindow.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/ManualRequestInitialization.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/RequestTimeout.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/Sleep.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/WriteToFile.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/Resources/CodeTabTemplate.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubManager.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubOnExecuteParameters.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubsUISettings.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CodeTabVM.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CreateGroupVM.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CreateItemVM.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/ExampleVM.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/StubSnippetVM.cs delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/Windows/TextInputWindow.xaml delete mode 100644 Software/Visual_Studio/Utilities/Tango.Stubs.UI/Windows/TextInputWindow.xaml.cs (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs') diff --git a/Software/DB/Tango.mdf b/Software/DB/Tango.mdf index 3d183bcd4..aa316f3a2 100644 Binary files a/Software/DB/Tango.mdf and b/Software/DB/Tango.mdf differ diff --git a/Software/DB/Tango_log.ldf b/Software/DB/Tango_log.ldf index e31a2abc0..fb24dd6b9 100644 Binary files a/Software/DB/Tango_log.ldf and b/Software/DB/Tango_log.ldf differ diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs index b1ffd9071..fb05a0b94 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs @@ -30,34 +30,11 @@ namespace Tango.MachineStudio.Stubs.ViewModels public class MainViewVM : StudioViewModel { private INotificationProvider _notification; - private StubsModuleSettings _settings; #region Properties public IStudioApplicationManager ApplicationManager { get; set; } - private bool _useConnectedMachine; - /// - /// Gets or sets a value indicating whether [use connected machine]. - /// - public bool UseConnectedMachine - { - get { return _useConnectedMachine; } - set - { - _useConnectedMachine = value; - - if (_useConnectedMachine && !ApplicationManager.IsMachineConnected) - { - _notification.ShowError("No connected machine found."); - _useConnectedMachine = false; - } - - RaisePropertyChangedAuto(); - InvalidateRelayCommands(); - } - } - private StubsViewVM _stubsViewVM; /// /// Gets or sets the stubs view vm. @@ -79,8 +56,6 @@ namespace Tango.MachineStudio.Stubs.ViewModels { StubsViewVM = new StubsViewVM(ConnectionMode.External); - _settings = SettingsManager.Default.GetOrCreate(); - ApplicationManager = applicationManager; _notification = notification; diff --git a/Software/Visual_Studio/Tango.Settings/SettingsManager.cs b/Software/Visual_Studio/Tango.Settings/SettingsManager.cs index 34ac2ddfe..cd8b97c92 100644 --- a/Software/Visual_Studio/Tango.Settings/SettingsManager.cs +++ b/Software/Visual_Studio/Tango.Settings/SettingsManager.cs @@ -63,6 +63,11 @@ namespace Tango.Settings { TypeNameHandling = TypeNameHandling.All, Formatting = Formatting.Indented, + Error = (sender, args) => + { + args.ErrorContext.Handled = true; + Logging.LogManager.Default.Log(args.ErrorContext.Error.Message); + } }; _jsonSettings.Converters.Add(new StringEnumConverter(false)); diff --git a/Software/Visual_Studio/Tango.Stubs/ViewModels/StubsViewVM.cs b/Software/Visual_Studio/Tango.Stubs/ViewModels/StubsViewVM.cs index 64f5a2913..34052cb7a 100644 --- a/Software/Visual_Studio/Tango.Stubs/ViewModels/StubsViewVM.cs +++ b/Software/Visual_Studio/Tango.Stubs/ViewModels/StubsViewVM.cs @@ -248,11 +248,6 @@ namespace Tango.Stubs.ViewModels /// public RelayCommand InsertSnippetCommand { get; set; } - /// - /// Gets or sets the exit command. - /// - public RelayCommand ExitCommand { get; set; } - /// /// Gets or sets the create item command. /// @@ -348,7 +343,6 @@ namespace Tango.Stubs.ViewModels SaveCommand = new RelayCommand(SaveFile); SaveAsCommand = new RelayCommand(SaveAsFile); StubSnippetSelectedCommand = new RelayCommand(OnStubSnippetSelected); - ExitCommand = new RelayCommand(() => Application.Current.Shutdown()); ClearCommand = new RelayCommand(ClearLog); Ports = new List(); @@ -418,9 +412,6 @@ namespace Tango.Stubs.ViewModels } Examples = Examples.OrderBy(x => x.Name).ToList(); - - LogManager.RegisterLogger(new Logging.FileLogger() { Enabled = true }); - LogManager.Log("Application Started!"); } public StubsViewVM(ConnectionMode connectionMode) : this() @@ -615,7 +606,7 @@ namespace Tango.Stubs.ViewModels /// /// Toggles the USB adapter connection. /// - private void ToggleConnection() + private async void ToggleConnection() { try { @@ -623,18 +614,18 @@ namespace Tango.Stubs.ViewModels { _machineOperator = new MachineOperator(); _machineOperator.Adapter = new UsbTransportAdapter(SelectedPort, BaudRate); - _machineOperator.Connect().Wait(); + await _machineOperator.Connect(); IsConnected = true; } else { - _machineOperator.Disconnect().Wait(); IsConnected = false; + await _machineOperator.Disconnect(); } } catch (Exception ex) { - AppendTextLog(ex.Message); + AppendTextLog(ex.Message + Environment.NewLine); } } @@ -659,7 +650,7 @@ namespace Tango.Stubs.ViewModels if (MachineOperator == null || MachineOperator.State != TransportComponentState.Connected) { - AppendTextLog("Machine operator is not initialized or connected. Could not execute script."); + AppendTextLog("Machine operator is not initialized or connected. Could not execute script." + Environment.NewLine); return; } diff --git a/Software/Visual_Studio/Tango.Stubs/Views/StubsView.xaml b/Software/Visual_Studio/Tango.Stubs/Views/StubsView.xaml index c27109232..456a6718f 100644 --- a/Software/Visual_Studio/Tango.Stubs/Views/StubsView.xaml +++ b/Software/Visual_Studio/Tango.Stubs/Views/StubsView.xaml @@ -14,8 +14,8 @@ xmlns:transport="clr-namespace:Tango.Transport.Adapters;assembly=Tango.Transport" mc:Ignorable="d" d:DesignHeight="720" d:DesignWidth="1280" Background="#202020" Foreground="Gainsboro" d:DataContext="{d:DesignInstance Type=vm:StubsViewVM, IsDesignTimeCreatable=False}"> - - + + @@ -176,12 +176,6 @@ - - - - - - @@ -318,83 +312,87 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - - - - - + + + + + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/App.xaml b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/App.xaml index 68bec1795..93766373c 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/App.xaml +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/App.xaml @@ -19,11 +19,6 @@ - - - - - diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/BasicRequest.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/BasicRequest.cs deleted file mode 100644 index b1a1902ef..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/BasicRequest.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Text; -using System.Linq; -using System.Drawing; -using System.Diagnostics; -using System.Windows.Forms; -using System.Threading; -using System.Threading.Tasks; -using System.Collections.Generic; -using Tango.PMR.Stubs; -using Tango.Stubs.UI; - -public void OnExecute(StubManager stubManager) -{ - - // Request ---- - // Double : A - // Double : B - - // Response ---- - // Double : Sum - var response = stubManager.Run("CalculateRequest", 10, 5); - - if (response.Sum == 15) - { - - stubManager.WriteLine("Sum is " + response.Sum); - - } - else - { - stubManager.WriteLine("Sum is incorrect!"); - } - -} \ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/BasicRequestPolling.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/BasicRequestPolling.cs deleted file mode 100644 index feba9cbe3..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/BasicRequestPolling.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Text; -using System.Linq; -using System.Drawing; -using System.Diagnostics; -using System.Windows.Forms; -using System.Threading; -using System.Threading.Tasks; -using System.Collections.Generic; -using Tango.PMR.Stubs; -using Tango.Stubs.UI; - -public void OnExecute(StubManager stubManager) -{ - int sum = 0; - int a = 0; - - while (sum < 100) - { - a = a + 1; - var response = stubManager.Run("CalculateRequest", 0, a); - sum = response.Sum; - stubManager.WriteLine(sum); - } - - stubManager.WriteLine("Polling completed!"); - -} \ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/ContinuousResponse.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/ContinuousResponse.cs deleted file mode 100644 index 86587abcc..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/ContinuousResponse.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Text; -using System.Linq; -using System.Drawing; -using System.Diagnostics; -using System.Windows.Forms; -using System.Threading; -using System.Threading.Tasks; -using System.Collections.Generic; -using Tango.PMR.Stubs; -using Tango.Stubs.UI; - -public void OnExecute(StubManager stubManager) -{ - - - // Request ---- - // Int32 : Amount - // Int32 : Delay - - // Response ---- - // Double : Progress - stubManager.RunContinuous("ProgressRequest", null, 100, 1); //Put null in the callback and let our application log the response automatically.. - -} \ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/ContinuousResponseAdvanced.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/ContinuousResponseAdvanced.cs deleted file mode 100644 index 3e744ba4b..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/ContinuousResponseAdvanced.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Text; -using System.Linq; -using System.Drawing; -using System.Diagnostics; -using System.Windows.Forms; -using System.Threading; -using System.Threading.Tasks; -using System.Collections.Generic; -using Tango.PMR.Stubs; -using Tango.Stubs.UI; - -public void OnExecute(StubManager stubManager) -{ - - // Request ---- - // Int32 : Amount - // Int32 : Delay - - // Response ---- - // Double : Progress - - - ProgressRequest request = new ProgressRequest(); - request.Amount = 100; - request.Delay = 1; - - stubManager.RunContinuous(request, (response) => - { - - stubManager.WriteLine(response.Progress); - - }); - -} \ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/InvokeResponseWindow.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/InvokeResponseWindow.cs deleted file mode 100644 index 3b702bd91..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/InvokeResponseWindow.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Text; -using System.Linq; -using System.Drawing; -using System.Diagnostics; -using System.Windows.Forms; -using System.Threading; -using System.Threading.Tasks; -using System.Collections.Generic; -using Tango.PMR.Stubs; -using Tango.Stubs.UI; - -public void OnExecute(StubManager stubManager) -{ - String response = null; - - //Invoke the response window with default parameters. - response = stubManager.ShowResponseWindow(); - stubManager.WriteLine(response); - - //Invoke the response window with custom message. - response = stubManager.ShowResponseWindow("Please enter response:"); - stubManager.WriteLine(response); - - //Invoke the response window with custom message and default response. - response = stubManager.ShowResponseWindow("Please enter response:", "Default response"); - stubManager.WriteLine(response); -} \ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/ManualRequestInitialization.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/ManualRequestInitialization.cs deleted file mode 100644 index 75e204a1d..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/ManualRequestInitialization.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Text; -using System.Linq; -using System.Drawing; -using System.Diagnostics; -using System.Windows.Forms; -using System.Threading; -using System.Threading.Tasks; -using System.Collections.Generic; -using Tango.PMR.Stubs; -using Tango.Stubs.UI; - -public void OnExecute(StubManager stubManager) -{ - - - CalculateRequest calculateRequest = new CalculateRequest(); - calculateRequest.A = 10; - calculateRequest.B = 15; - - var response = stubManager.Run(calculateRequest); - - stubManager.WriteLine("The response sum is: " + response.Sum); - - -} \ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/RequestTimeout.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/RequestTimeout.cs deleted file mode 100644 index ca6fea674..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/RequestTimeout.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Text; -using System.Linq; -using System.Drawing; -using System.Diagnostics; -using System.Windows.Forms; -using System.Threading; -using System.Threading.Tasks; -using System.Collections.Generic; -using Tango.PMR.Stubs; -using Tango.Stubs.UI; - -public void OnExecute(StubManager stubManager) -{ - - stubManager.RequestTimeout = 5000; //Set request timeout to 5 seconds. - - var response = stubManager.Run("CalculateRequest", 10, 5); -} \ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/Sleep.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/Sleep.cs deleted file mode 100644 index 9179e46ec..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/Sleep.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Text; -using System.Linq; -using System.Drawing; -using System.Diagnostics; -using System.Windows.Forms; -using System.Threading; -using System.Threading.Tasks; -using System.Collections.Generic; -using Tango.PMR.Stubs; -using Tango.Stubs.UI; - -public void OnExecute(StubManager stubManager) -{ - int sum = 0; - int a = 0; - - while (sum < 100) - { - a = a + 1; - var response = stubManager.Run("CalculateRequest", 0, a); - sum = response.Sum; - stubManager.WriteLine(sum); - - Thread.Sleep(10); //Sleep for 10 milli. - } - - stubManager.WriteLine("Polling completed!"); - -} \ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/WriteToFile.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/WriteToFile.cs deleted file mode 100644 index 1d580fd5d..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Examples/WriteToFile.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Text; -using System.Linq; -using System.Drawing; -using System.Diagnostics; -using System.Windows.Forms; -using System.Threading; -using System.Threading.Tasks; -using System.Collections.Generic; -using Tango.PMR.Stubs; -using Tango.Stubs.UI; - -private const string FILE_PATH = "D:\\logFile.txt"; - -public void OnExecute(StubManager stubManager) -{ - //This will overwrite existing file. - stubManager.WriteToFile(FILE_PATH, "Writing to file..."); - - for (int i = 0; i < 10; i++) - { - //This will append the contents to the end of the file. - stubManager.AppendToFile(FILE_PATH, i.ToString() + " Some text..."); - } - - stubManager.AppendToFile(FILE_PATH, "Done"); - - stubManager.WriteLine("Done writing to the file."); -} \ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/MainWindow.xaml b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/MainWindow.xaml index 4674e860e..61c79481e 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/MainWindow.xaml +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/MainWindow.xaml @@ -7,7 +7,7 @@ xmlns:views="clr-namespace:Tango.Stubs.UI.Views" xmlns:local="clr-namespace:Tango.Stubs.UI" mc:Ignorable="d" - Title="Tango Stubs Execution Utility" Height="720" Width="1280" TitlebarHeight="40" WindowState="Maximized" TitleCaps="False" BorderBrush="Gray" BorderThickness="1" WindowStartupLocation="CenterScreen" Background="#202020" Foreground="Gainsboro" DataContext="{Binding RelativeSource={RelativeSource Self}}"> + Title="Tango Stubs Execution Utility" Height="720" Width="1280" TitlebarHeight="40" WindowState="Maximized" TitleCaps="False" BorderBrush="Gray" BorderThickness="1" WindowStartupLocation="CenterScreen" Background="#202020" Foreground="Gainsboro"> @@ -20,6 +20,6 @@ - + diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Properties/Resources.Designer.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Properties/Resources.Designer.cs index 0f1dfb64d..0c9738076 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Properties/Resources.Designer.cs +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Properties/Resources.Designer.cs @@ -59,29 +59,5 @@ namespace Tango.Stubs.UI.Properties { resourceCulture = value; } } - - /// - /// Looks up a localized string similar to using System; - ///using System.Text; - ///using System.Linq; - ///using System.Drawing; - ///using System.Diagnostics; - ///using System.Windows.Forms; - ///using System.Threading; - ///using System.Threading.Tasks; - ///using System.Collections.Generic; - /// - ///public void OnExecute(MediaManager mediaManager, Preset preset, CanvasScriptItem item) - ///{ - /// CanvasVideoItem canvasVideoItem = new CanvasVideoItem(); - /// canvasVideoItem.VideoFile = "C:\\video1.mp4"; - /// mediaManager.Invoke(() => mediaManager.Play(canvasVideoItem)); - /// Thread.Sl [rest of string was truncated]";. - /// - internal static string CodeTabTemplate { - get { - return ResourceManager.GetString("CodeTabTemplate", resourceCulture); - } - } } } diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Properties/Resources.resx b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Properties/Resources.resx index 7693cadac..1af7de150 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Properties/Resources.resx +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Properties/Resources.resx @@ -117,8 +117,4 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - ..\Resources\CodeTabTemplate.cs;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 - \ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Resources/CodeTabTemplate.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Resources/CodeTabTemplate.cs deleted file mode 100644 index bce014952..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Resources/CodeTabTemplate.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Text; -using System.Linq; -using System.Drawing; -using System.Diagnostics; -using System.Windows.Forms; -using System.Threading; -using System.Threading.Tasks; -using System.Collections.Generic; -using Tango.PMR.Stubs; -using Tango.Stubs.UI; - -public void OnExecute(StubManager stubManager) -{ - for (int i = 0; i < 10; i++) - { - var response = stubManager.Run("calculate", 1, i); - - if (response.Sum == 10) - { - stubManager.WriteLine("OK!"); - } - else - { - stubManager.WriteLine(response.Sum); - } - } -} \ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubManager.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubManager.cs deleted file mode 100644 index 328318e93..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubManager.cs +++ /dev/null @@ -1,481 +0,0 @@ -using Google.Protobuf; -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Controls; -using Tango.PMR; -using Tango.PMR.Common; -using Tango.Stubs.UI.Windows; -using Tango.Transport.Adapters; - -namespace Tango.Stubs.UI -{ - /// - /// Represents a manager capable of executing stub scripts asynchronously. - /// - public class StubManager - { - private UsbTransportAdapter _adapter; //Holds the USB transport adapter. - private Action _writeLine; - private Action _write; - private Action _clear; - - /// - /// Occurs when the stub has failed to execute. - /// - public event EventHandler Failed; - - /// - /// Occurs when the stub has completed successfully. - /// - public event EventHandler Completed; - - /// - /// Occurs when the stub has been initialized and executed. - /// - public event EventHandler Executed; - - /// - /// Gets a value indicating whether this is aborted. - /// - internal bool Aborted { get; private set; } - - /// - /// Gets or sets a value indicating whether [automatic log]. - /// - public bool AutoLog { get; set; } - - /// - /// Gets or sets the request timeout. - /// - public int RequestTimeout { get; set; } - - /// - /// Initializes a new instance of the class. - /// - /// The adapter. - public StubManager(UsbTransportAdapter adapter, Action writeLine, Action write, Action clear) - { - _writeLine = writeLine; - _write = write; - _clear = clear; - - _adapter = adapter; - RequestTimeout = 2; - } - - /// - /// Aborts the current script. - /// - internal void Abort() - { - Aborted = true; - } - - /// - /// Runs the specified stub name. - /// - /// Name of the stub. - /// The arguments. - public IMessage Run(String stubName, params Object[] args) - { - if (Aborted) return null; - - var stubType = MessageFactory.GetAvailableRequestStubs().SingleOrDefault(x => x.Name.ToLower() == stubName.ToLower() || x.Name.Replace("Request", "").ToLower() == stubName.ToLower()); - if (stubType == null) - { - OnFailed(new ArgumentException("Invalid stub '" + stubName + "'.")); - return null; - } - - var stubProps = stubType.GetProperties(BindingFlags.Public | BindingFlags.Instance); - - if (stubProps.Length > args.Length) - { - OnFailed(new ArgumentOutOfRangeException("Not enough arguments for " + stubType.Name + ".")); - return null; - } - - Executed?.Invoke(this, stubType.Name); - - try - { - Object request = Activator.CreateInstance(stubType); - - int argIndex = 0; - foreach (var prop in stubProps) - { - Object arg = args[argIndex++]; - - if (prop.PropertyType == typeof(UInt32)) - { - prop.SetValue(request, UInt32.Parse(arg.ToString())); - } - else if (prop.PropertyType == typeof(bool)) - { - prop.SetValue(request, bool.Parse(arg.ToString())); - } - else if (prop.PropertyType.IsPrimitive) - { - object converted = Convert.ChangeType(arg, prop.PropertyType); - prop.SetValue(request, converted); - } - else - { - prop.SetValue(request, arg); - } - } - - return Run(request as IMessage); - } - catch (Exception ex) - { - OnFailed(ex); - } - - return null; - } - - /// - /// Runs the specified stub name. - /// - /// Name of the stub. - /// The arguments. - public IMessage Run(IMessage stub) - { - if (Aborted) return null; - - Executed?.Invoke(this, stub.GetType().Name); - - EventHandler handler = null; - - try - { - MessageContainer container = new MessageContainer(); - container.Token = Guid.NewGuid().ToString(); - container.Type = MessageFactory.ParseMessageType(stub.GetType().Name); - - container.Data = (stub as IMessage).ToByteString(); - - byte[] requestData = container.ToByteArray(); - - bool done = false; - - IMessage message = null; - - Task.Factory.StartNew(() => - { - _adapter.Write(requestData); - - DateTime startTime = DateTime.Now; - - MessageContainer responseContainer = null; - - handler = new EventHandler((sender, data) => - { - responseContainer = MessageFactory.ParseContainer(data); - }); - - _adapter.DataAvailable += handler; - - while (responseContainer == null) - { - Thread.Sleep(2); - - if (DateTime.Now > startTime.AddSeconds(RequestTimeout)) - { - done = true; - OnFailed(new TimeoutException(String.Format("Response has failed to arrive after {0} seconds.", TimeSpan.FromMilliseconds(RequestTimeout).TotalSeconds))); - return; - } - - if (Aborted) - { - done = true; - return; - } - } - - message = MessageFactory.ExtractMessageFromContainer(responseContainer); - OnCompleted(JsonConvert.SerializeObject(message, Formatting.Indented)); - done = true; - }); - - while (!done) - { - Thread.Sleep(2); - } - - if (handler != null) - { - _adapter.DataAvailable -= handler; - } - - return message; - } - catch (Exception ex) - { - if (handler != null) - { - _adapter.DataAvailable -= handler; - } - - OnFailed(ex); - } - - return null; - } - - public T Run(String stubName, params Object[] args) where T : class, IMessage - { - return Run(stubName, args) as T; - } - - public T Run(IMessage stub) where T : class, IMessage - { - return Run(stub) as T; - } - - /// - /// Runs the specified stub name. - /// - /// Name of the stub. - /// The arguments. - public void RunContinuous(IMessage stub, Action callback) where T : class, IMessage - { - if (Aborted) return; - - Type stubType = stub.GetType(); - - Executed?.Invoke(this, stubType.Name); - - EventHandler handler = null; - - try - { - MessageContainer container = new MessageContainer(); - container.Token = Guid.NewGuid().ToString(); - container.Continuous = true; - container.Type = MessageFactory.ParseMessageType(stubType.Name); - - container.Data = (stub as IMessage).ToByteString(); - - byte[] requestData = container.ToByteArray(); - - bool done = false; - - IMessage message = null; - - Task.Factory.StartNew(() => - { - _adapter.Write(requestData); - - DateTime startTime = DateTime.Now; - - MessageContainer responseContainer = null; - - handler = new EventHandler((sender, data) => - { - responseContainer = MessageFactory.ParseContainer(data); - message = MessageFactory.ExtractMessageFromContainer(responseContainer); - - callback?.Invoke(message as T); - - if (AutoLog) - { - WriteLine(JsonConvert.SerializeObject(message, Formatting.Indented)); - } - - if (responseContainer.Completed || Aborted) - { - _adapter.DataAvailable -= handler; - done = true; - return; - } - }); - - _adapter.DataAvailable += handler; - }); - - while (!done) - { - Thread.Sleep(2); - } - - OnCompleted("Continuous request completed."); - - if (handler != null) - { - _adapter.DataAvailable -= handler; - } - - return; - } - catch (Exception ex) - { - if (handler != null) - { - _adapter.DataAvailable -= handler; - } - - OnFailed(ex); - } - - return; - } - - /// - /// - /// Runs the specified stub name. - /// - /// Name of the stub. - /// The arguments. - public void RunContinuous(String stubName, Action callback, params Object[] args) where T : class, IMessage - { - if (Aborted) return; - - var stubType = MessageFactory.GetAvailableRequestStubs().SingleOrDefault(x => x.Name.ToLower() == stubName.ToLower() || x.Name.Replace("Request", "").ToLower() == stubName.ToLower()); - if (stubType == null) - { - OnFailed(new ArgumentException("Invalid stub '" + stubName + "'.")); - return; - } - - var stubProps = stubType.GetProperties(BindingFlags.Public | BindingFlags.Instance); - - if (stubProps.Length > args.Length) - { - OnFailed(new ArgumentOutOfRangeException("Not enough arguments for " + stubType.Name + ".")); - return; - } - - try - { - Object request = Activator.CreateInstance(stubType); - - int argIndex = 0; - foreach (var prop in stubProps) - { - Object arg = args[argIndex++]; - - if (prop.PropertyType == typeof(UInt32)) - { - prop.SetValue(request, UInt32.Parse(arg.ToString())); - } - else if (prop.PropertyType == typeof(bool)) - { - prop.SetValue(request, bool.Parse(arg.ToString())); - } - else if (prop.PropertyType.IsPrimitive) - { - object converted = Convert.ChangeType(arg, prop.PropertyType); - prop.SetValue(request, converted); - } - else - { - prop.SetValue(request, arg); - } - } - - RunContinuous(request as IMessage, callback); - } - catch (Exception ex) - { - OnFailed(ex); - } - } - - public void WriteLine(Object obj) - { - _writeLine(obj != null ? obj.ToString() : "null"); - } - - public void Write(Object obj) - { - _write(obj != null ? obj.ToString() : "null"); - } - - public void WriteLineHex(Object number, int digits) - { - _writeLine("#" + Convert.ToInt32(number).ToString("X" + digits.ToString())); - } - - public void WriteHex(Object number, int digits) - { - _write("#" + Convert.ToInt32(number).ToString("X" + digits.ToString())); - } - - public void Clear() - { - _clear(); - } - - public String ShowResponseWindow(String message, String defaultResponse) - { - String response = null; - bool closed = false; - - Application.Current.Dispatcher.Invoke(() => - { - TextInputWindow dlg = new TextInputWindow(message, defaultResponse); - dlg.WindowStartupLocation = WindowStartupLocation.CenterOwner; - dlg.Owner = Application.Current.MainWindow; - dlg.ShowDialog(); - response = dlg.Response; - closed = true; - }); - - while (!closed) - { - Thread.Sleep(10); - } - - return response; - } - - public String ShowResponseWindow(String message) - { - return ShowResponseWindow(message, null); - } - - public String ShowResponseWindow() - { - return ShowResponseWindow(null); - } - - public void WriteToFile(String filePath, String content) - { - File.WriteAllText(filePath, content + Environment.NewLine); - } - - public void AppendToFile(String filePath, String content) - { - File.AppendAllText(filePath, content + Environment.NewLine); - } - - /// - /// Raises the event. - /// - /// The exception. - protected virtual void OnFailed(Exception ex) - { - Failed?.Invoke(this, ex); - } - - /// - /// Raises the event. - /// - /// The response. - protected virtual void OnCompleted(String response) - { - Completed?.Invoke(this, response); - } - } -} diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubOnExecuteParameters.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubOnExecuteParameters.cs deleted file mode 100644 index 14b69a0b7..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubOnExecuteParameters.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.Scripting; - -namespace Tango.Stubs.UI -{ - /// - /// Represents the global object which will be sent to the scripting engine. - /// - /// - public class StubOnExecuteParameters : OnExecuteParameters - { - /// - /// Provides access to the script stub manager. - /// - public StubManager stubManager; - - /// - /// Initializes a new instance of the class. - /// - /// The manager. - public StubOnExecuteParameters(StubManager manager) - { - stubManager = manager; - } - } -} diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubsUISettings.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubsUISettings.cs deleted file mode 100644 index 62d77a012..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubsUISettings.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.Settings; -using Tango.Transport.Adapters; - -namespace Tango.Stubs.UI -{ - public class StubsUISettings : SettingsBase - { - /// - /// Gets or sets the selected port. - /// - public String SelectedPort { get; set; } - - /// - /// Gets or sets the baud rate. - /// - public UsbSerialBaudRates BaudRate { get; set; } - - /// - /// Gets or sets the last tabs. - /// - public List LastTabs { get; set; } - - /// - /// Gets or sets a value indicating whether [automatic log response]. - /// - public bool AutoLogResponse { get; set; } - - /// - /// Initializes a new instance of the class. - /// - public StubsUISettings() - { - BaudRate = UsbSerialBaudRates.BR_9600; - LastTabs = new List(); - } - } -} diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Tango.Stubs.UI.csproj b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Tango.Stubs.UI.csproj index 5867800db..de5ba2c69 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Tango.Stubs.UI.csproj +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Tango.Stubs.UI.csproj @@ -78,27 +78,7 @@ MSBuild:Compile Designer - - - - TextInputWindow.xaml - - - - - - - - - - - - - - - - MainView.xaml @@ -118,10 +98,6 @@ Designer MSBuild:Compile - - Designer - MSBuild:Compile - @@ -195,9 +171,6 @@ - - - if $(ConfigurationName) == Debug $(TargetDir)linkgen.exe -s "$(TargetPath)" -d "$(TargetDir)Utilities\Stubs Execution GUI.lnk" diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CodeTabVM.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CodeTabVM.cs deleted file mode 100644 index a56ad7aca..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CodeTabVM.cs +++ /dev/null @@ -1,113 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.Core.Commands; -using Tango.Scripting; -using Tango.SharedUI; - -namespace Tango.Stubs.UI.ViewModels -{ - /// - /// Represents a single script editor tab view model; - /// - /// - public class CodeTabVM : ViewModel - { - private String _title; - /// - /// Gets or sets the script title/file name. - /// - public String Title - { - get - { - return File != null ? Path.GetFileName(File) : _title; - } - set - { - _title = value; - RaisePropertyChanged(nameof(Title)); - } - } - - private String _file; - /// - /// Gets or sets the full script file path. - /// - public String File - { - get { return _file; } - set - { - _file = value; - RaisePropertyChanged(nameof(File)); - RaisePropertyChanged(nameof(Title)); - } - } - - private String _code; - /// - /// Gets or sets the script code. - /// - public String Code - { - get { return _code; } - set { _code = value; RaisePropertyChanged(nameof(Code)); } - } - - private bool _isRunning; - /// - /// Gets or sets a value indicating whether this instance is running. - /// - public bool IsRunning - { - get { return _isRunning; } - set { _isRunning = value; RaisePropertyChangedAuto(); } - } - - private RelayCommand _insertCodeSnippetCommand; - /// - /// Gets or sets the insert snippet command. (Inserts stub snippet to editor) - /// - public RelayCommand InsertSnippetCommand - { - get { return _insertCodeSnippetCommand; } - set { _insertCodeSnippetCommand = value; RaisePropertyChanged(nameof(InsertSnippetCommand)); } - } - - private ObservableCollection _errors; - /// - /// Gets or sets the errors. - /// - public ObservableCollection Errors - { - get { return _errors; } - set { _errors = value; RaisePropertyChangedAuto(); } - } - - /// - /// Initializes a new instance of the class. - /// - public CodeTabVM() - { - Title = "untitled"; - Code = Properties.Resources.CodeTabTemplate; - Errors = new ObservableCollection(); - } - - /// - /// Returns a that represents this instance. - /// - /// - /// A that represents this instance. - /// - public override string ToString() - { - return Title; - } - } -} diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CreateGroupVM.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CreateGroupVM.cs deleted file mode 100644 index c2ed051db..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CreateGroupVM.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.Core; - -namespace Tango.Stubs.UI.ViewModels -{ - public class CreateGroupVM : ExtendedObject - { - public String Name { get; set; } - - public List Items { get; set; } - - public CreateGroupVM() - { - Items = new List(); - } - } -} diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CreateItemVM.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CreateItemVM.cs deleted file mode 100644 index be96edee8..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CreateItemVM.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.Core; - -namespace Tango.Stubs.UI.ViewModels -{ - public class CreateItemVM : ExtendedObject - { - public Type Type { get; set; } - - public String Name { get; set; } - } -} diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/ExampleVM.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/ExampleVM.cs deleted file mode 100644 index b98c9cd12..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/ExampleVM.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Tango.Stubs.UI.ViewModels -{ - public class ExampleVM - { - public String Name { get; set; } - - public String Code { get; set; } - } -} diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/MainViewVM.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/MainViewVM.cs index e5e053b36..4027af171 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/MainViewVM.cs @@ -20,6 +20,7 @@ using Tango.Scripting; using Tango.Settings; using Tango.SharedUI; using Tango.Stubs.UI.Views; +using Tango.Stubs.ViewModels; using Tango.Transport.Adapters; namespace Tango.Stubs.UI.ViewModels @@ -30,770 +31,30 @@ namespace Tango.Stubs.UI.ViewModels /// public class MainViewVM : ViewModel { - private UsbTransportAdapter _adapter; //Holds the USB transport adapter. - private StubManager _stubManager; - private TextBox _logTextBox; - private StubsUISettings _settings; - - #region Properties - - public List CreateGroups { get; set; } - - public List Examples { get; set; } - - /// - /// Gets or sets the code tabs. - /// - public ObservableCollection CodeTabs { get; set; } - - /// - /// Gets or sets the additional highlight C# types. - /// - public ObservableCollection> HighlightTypes { get; set; } - - /// - /// Gets or sets the intellisense types. - /// - public ObservableCollection> IntellisenseTypes { get; set; } - - /// - /// Gets or sets the collection of stub snippets. - /// - public ObservableCollection StubSnippets { get; set; } - - private StubSnippetVM _selectedStubSnippet; - /// - /// Gets or sets the selected stub snippet. - /// - public StubSnippetVM SelectedStubSnippet - { - get { return _selectedStubSnippet; } - set { _selectedStubSnippet = value; RaisePropertyChanged(nameof(SelectedStubSnippet)); } - } - - private CodeTabVM _selectedCodeTab; - /// - /// Gets or sets the selected code tab. - /// - public CodeTabVM SelectedCodeTab - { - get { return _selectedCodeTab; } - set { _selectedCodeTab = value; RaisePropertyChanged(nameof(SelectedCodeTab)); InvalidateRelayCommands(); } - } - - private bool _isConnected; - /// - /// Gets or sets a value indicating whether the USB adapter is connected. - /// - public bool IsConnected - { - get { return _isConnected; } - set { _isConnected = value; RaisePropertyChanged(nameof(IsConnected)); InvalidateRelayCommands(); } - } - - private List _ports; - /// - /// Gets or sets the available USB ports. - /// - public List Ports - { - get { return _ports; } - set { _ports = value; RaisePropertyChanged(nameof(Ports)); } - } - - private String _selectedPort; - /// - /// Gets or sets the selected USB port. - /// - public String SelectedPort - { - get { return _selectedPort; } - set { _selectedPort = value; RaisePropertyChanged(nameof(SelectedPort)); InvalidateRelayCommands(); } - } - - private String _status; - /// - /// Gets or sets the current status bar text. - /// - public String Status - { - get { return _status; } - set { _status = value; RaisePropertyChanged(nameof(Status)); } - } - - private bool _isRunning; - /// - /// Gets or sets a value indicating whether a stub is currently running. - /// - public bool IsRunning - { - get { return _isRunning; } - set { _isRunning = value; RaisePropertyChanged(nameof(IsRunning)); InvalidateRelayCommands(); } - } - - private bool _appendLogAuto; - - public bool AppendLogAuto - { - get { return _appendLogAuto; } - set { _appendLogAuto = value; RaisePropertyChangedAuto(); } - } - - private UsbSerialBaudRates _baudRate; + private StubsViewVM _stubsViewVM; /// - /// Gets or sets the baud rate. + /// Gets or sets the stubs view vm. /// - public UsbSerialBaudRates BaudRate + public StubsViewVM StubsViewVM { - get { return _baudRate; } - set { _baudRate = value; RaisePropertyChangedAuto(); } + get { return _stubsViewVM; } + set { _stubsViewVM = value; RaisePropertyChangedAuto(); } } - #endregion - - #region Commands - - /// - /// Gets or sets the new command. - /// - public RelayCommand NewCommand { get; set; } - - /// - /// Gets or sets the close tab command. - /// - public RelayCommand CloseTabCommand { get; set; } - - /// - /// Gets or sets the build command. - /// - public RelayCommand BuildCommand { get; set; } - - /// - /// Gets or sets the run command. - /// - public RelayCommand RunCommand { get; set; } - - /// - /// Gets or sets the stop command. - /// - public RelayCommand StopCommand { get; set; } - - /// - /// Gets or sets the toggle connection command. - /// - public RelayCommand ToggleConnectionCommand { get; set; } - - /// - /// Gets or sets the open command. - /// - public RelayCommand OpenCommand { get; set; } - - /// - /// Gets or sets the save command. - /// - public RelayCommand SaveCommand { get; set; } - - /// - /// Gets or sets the save as command. - /// - public RelayCommand SaveAsCommand { get; set; } - - /// - /// Gets or sets the clear command. - /// - public RelayCommand ClearCommand { get; set; } - - /// - /// Gets or sets the stub snippet selected command. - /// - public RelayCommand StubSnippetSelectedCommand { get; set; } - - /// - /// Gets or sets the insert snippet command. - /// - public RelayCommand InsertSnippetCommand { get; set; } - - /// - /// Gets or sets the exit command. - /// - public RelayCommand ExitCommand { get; set; } - - /// - /// Gets or sets the create item command. - /// - public RelayCommand CreateItemCommand { get; set; } - - /// - /// Gets or sets the create example command. - /// - public RelayCommand CreateExampleCommand { get; set; } - #endregion - - #region Constructors - /// /// Initializes a new instance of the class. /// public MainViewVM() { - _settings = SettingsManager.Default.GetOrCreate(); - - Examples = new List(); - CodeTabs = new ObservableCollection(); - NewCommand = new RelayCommand(CreateNewTab); - CloseTabCommand = new RelayCommand(OnTabClosing); - RunCommand = new RelayCommand(RunTab, (x) => IsConnected && !IsRunning && SelectedCodeTab != null); - BuildCommand = new RelayCommand(async () => await BuildTab(), (x) => !IsRunning && SelectedCodeTab != null); - StopCommand = new RelayCommand(StopTab, (x) => IsConnected && IsRunning && SelectedCodeTab != null); - InsertSnippetCommand = new RelayCommand((x) => { }); - CreateExampleCommand = new RelayCommand(CreateExample); - - HighlightTypes = new ObservableCollection>(); - IntellisenseTypes = new ObservableCollection>(); - - IntellisenseTypes.Add(new KeyValuePair("stubManager", typeof(StubManager))); - - foreach (var stubType in typeof(PMR.Common.MessageContainer).Assembly.GetTypes().Where(x => typeof(IMessage).IsAssignableFrom(x))) - { - HighlightTypes.Add(new KeyValuePair(stubType.Name, stubType)); - } - - HighlightTypes.Add(new KeyValuePair("Thread", typeof(Thread))); - HighlightTypes.Add(new KeyValuePair("DateTime", typeof(DateTime))); - HighlightTypes.Add(new KeyValuePair("TimeSpan", typeof(TimeSpan))); - HighlightTypes.Add(new KeyValuePair("Dispatcher", typeof(Dispatcher))); - HighlightTypes.Add(new KeyValuePair("Task", typeof(Task))); - HighlightTypes.Add(new KeyValuePair("List", typeof(IList))); - HighlightTypes.Add(new KeyValuePair("int", typeof(Int32))); - HighlightTypes.Add(new KeyValuePair("double", typeof(Double))); - HighlightTypes.Add(new KeyValuePair("String", typeof(String))); - HighlightTypes.Add(new KeyValuePair("string", typeof(String))); - - foreach (var item in HighlightTypes) - { - IntellisenseTypes.Add(item); - } - - StubSnippets = new ObservableCollection(); - - foreach (var stubType in MessageFactory.GetAvailableRequestStubs()) - { - StubSnippetVM snippet = new StubSnippetVM(); - snippet.Name = stubType.Name.Replace("Stub", "").Replace("Request", "").ToWords(); - - snippet.Code = String.Empty; - - snippet.Code += "// " + "Request ----" + Environment.NewLine; - - foreach (var prop in stubType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) - { - snippet.Code += "// " + prop.PropertyType.Name + " : " + prop.Name + Environment.NewLine; - } - - Type responseType = MessageFactory.GetAvailableRequestResponseStubs().SingleOrDefault(x => x.Name == stubType.Name.Replace("Request", "Response")); - - if (responseType != null) - { - snippet.Code += Environment.NewLine + "// " + "Response ----" + Environment.NewLine; - - foreach (var prop in responseType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) - { - snippet.Code += "// " + prop.PropertyType.Name + " : " + prop.Name + Environment.NewLine; - } - } - - snippet.Code += String.Format("var response = stubManager.Run<{2}>(\"{0}\" ,{1});", stubType.Name, String.Join(", ", stubType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Select(x => x.PropertyType.Name == "string" ? "\"string\"" : x.PropertyType.Name)), stubType.Name.Replace("Request", "Response")); - StubSnippets.Add(snippet); - } - - ToggleConnectionCommand = new RelayCommand(ToggleConnection, (x) => !IsRunning); - OpenCommand = new RelayCommand(OpenFile); - SaveCommand = new RelayCommand(SaveFile); - SaveAsCommand = new RelayCommand(SaveAsFile); - StubSnippetSelectedCommand = new RelayCommand(OnStubSnippetSelected); - ExitCommand = new RelayCommand(() => Application.Current.Shutdown()); - ClearCommand = new RelayCommand(ClearLog); - - Ports = new List(); - - for (int i = 1; i < 100; i++) - { - Ports.Add("COM" + i); - } - - SelectedPort = _settings.SelectedPort != null ? _settings.SelectedPort : Ports.First(); - BaudRate = _settings.BaudRate; - AppendLogAuto = _settings.AutoLogResponse; - - Status = "Ready"; - - if (_settings.LastTabs.Count > 0) - { - foreach (var file in _settings.LastTabs) - { - if (File.Exists(file)) - { - OpenFile(file); - } - } - } - else - { - CreateNewTab(); - } - - CreateGroups = new List(); - - foreach (var typesGroup in typeof(PMR.Common.MessageContainer).Assembly.GetTypes().Where(x => x.IsClass && !x.IsGenericType && !x.Name.Contains("Reflection") && typeof(IMessage).IsAssignableFrom(x)).GroupBy(x => x.Namespace)) - { - CreateGroupVM group = new CreateGroupVM(); - group.Name = typesGroup.First().Namespace.Split('.').Last(); - - foreach (var type in typesGroup) - { - group.Items.Add(new CreateItemVM() - { - Name = type.Name, - Type = type, - }); - } - - CreateGroups.Add(group); - } - - CreateItemCommand = new RelayCommand(CreateItem); - - foreach (var name in typeof(MainViewVM).Assembly.GetManifestResourceNames()) - { - if (name.Contains(".Examples.")) - { - using (Stream stream = typeof(MainViewVM).Assembly.GetManifestResourceStream(name)) - { - StreamReader reader = new StreamReader(stream); - - ExampleVM example = new ExampleVM(); - String[] str = name.Split('.'); - example.Name = str[str.Length - 2].ToWords(); - example.Code = reader.ReadToEnd(); - Examples.Add(example); - } - } - } - - Examples = Examples.OrderBy(x => x.Name).ToList(); - - Application.Current.Exit += Current_Exit; - LogManager.RegisterLogger(new Logging.FileLogger() { Enabled = true }); - LogManager.Log("Application Started!"); - } - - #endregion - - #region Virtual Methods - - /// - /// Called when a stub snippet is double clicked. - /// - protected virtual void OnStubSnippetSelected() - { - if (SelectedStubSnippet != null) - { - if (InsertSnippetCommand != null) - { - InsertSnippetCommand.Execute(SelectedStubSnippet.Code); - } - } - } - - /// - /// Called when user closes a script tab. - /// - /// The code tab. - protected virtual void OnTabClosing(CodeTabVM codeTab) - { - CodeTabs.Remove(codeTab); - } - - #endregion - - #region Private Methods - - private void CreateExample(ExampleVM example) - { - CreateNewTab(); - SelectedCodeTab.Code = example.Code; - SelectedCodeTab.Title = example.Name; - } - - private void CreateItem(CreateItemVM item) - { - if (item != null) - { - if (InsertSnippetCommand != null) - { - String code = String.Empty; - - FormatProperties(item.Type, ref code); - - InsertSnippetCommand.Execute(code); - } - } - } - - private void FormatProperties(Type type, ref String code) - { - code += Environment.NewLine + String.Format("{0} {1} = new {0}();", type.Name, type.Name.ToCamelCase()) + Environment.NewLine; - - foreach (var prop in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) - { - if (prop.PropertyType == typeof(String)) - { - code += String.Format("{0}.{1} = {2};", type.Name.ToCamelCase(), prop.Name, "null") + Environment.NewLine; - } - else if (prop.PropertyType.IsEnum) - { - code += String.Format("{0}.{1} = {2};", type.Name.ToCamelCase(), prop.Name, Activator.CreateInstance(prop.PropertyType).GetType().FullName + "." + Activator.CreateInstance(prop.PropertyType).ToString()) + Environment.NewLine; - } - else if (!prop.PropertyType.IsClass) - { - code += String.Format("{0}.{1} = {2};", type.Name.ToCamelCase(), prop.Name, Activator.CreateInstance(prop.PropertyType).ToString().ToLower()) + Environment.NewLine; - } - else if (prop.PropertyType.IsGenericType) - { - Type genericType = prop.PropertyType.GenericTypeArguments[0]; - FormatProperties(genericType, ref code); - code += String.Format("{0}.{1}.Add({2});", type.Name.ToCamelCase(), prop.Name, genericType.Name.ToCamelCase()) + Environment.NewLine; - } - else - { - FormatProperties(prop.PropertyType, ref code); - code += Environment.NewLine + String.Format("{0}.{1} = {2};", type.Name.ToCamelCase(), prop.Name, prop.Name.ToCamelCase()) + Environment.NewLine; - } - } - } - - /// - /// Clears the log. - /// - private void ClearLog() - { - _logTextBox.Clear(); - } - - /// - /// Saves the selected script file. - /// - private async void SaveFile() - { - if (SelectedCodeTab != null) - { - if (SelectedCodeTab.File == null) - { - SaveAsFile(); - } - else - { - Status = "Saving " + SelectedCodeTab.File + "..."; - File.WriteAllText(SelectedCodeTab.File, SelectedCodeTab.Code); - await Task.Delay(1000); - Status = "Ready"; - } - } - } - - /// - /// Saves the selected script file. - /// - private async void SaveAsFile() - { - if (SelectedCodeTab != null) - { - SaveFileDialog dlg = new SaveFileDialog(); - dlg.Filter = "C# Script Files|*.cs"; - dlg.DefaultExt = ".cs"; - if (dlg.ShowDialog().Value) - { - Status = "Saving " + dlg.FileName + "..."; - File.WriteAllText(dlg.FileName, SelectedCodeTab.Code); - SelectedCodeTab.File = dlg.FileName; - await Task.Delay(1000); - Status = "Ready"; - } - } - } - - /// - /// Opens a script from HD. - /// - private void OpenFile() - { - OpenFileDialog dlg = new OpenFileDialog(); - dlg.Filter = "C# Script Files|*.cs"; - dlg.Multiselect = true; - if (dlg.ShowDialog().Value) - { - foreach (var file in dlg.FileNames) - { - OpenFile(file); - } - } - } - - /// - /// Opens the file. - /// - /// The file. - private void OpenFile(String file) - { - var newTab = new CodeTabVM(); - newTab.File = file; - newTab.Code = File.ReadAllText(file); - CodeTabs.Add(newTab); - SelectedCodeTab = newTab; - } - - /// - /// Toggles the USB adapter connection. - /// - private void ToggleConnection() - { - try - { - if (!IsConnected) - { - _adapter = new UsbTransportAdapter(SelectedPort); - _adapter.BaudRate = BaudRate; - _adapter.Connect().Wait(); - IsConnected = true; - } - else - { - _adapter.Disconnect().Wait(); - IsConnected = false; - } - } - catch (Exception ex) - { - MessageBox.Show(ex.ToString(), "Tango"); - } - } + StubsViewVM = new StubsViewVM(ConnectionMode.Internal); - /// - /// Creates a new script tab. - /// - private void CreateNewTab() - { - var newTab = new CodeTabVM(); - CodeTabs.Add(newTab); - SelectedCodeTab = newTab; - } - - /// - /// Runs the selected script tab. - /// - private async void RunTab() - { - await BuildTab(); - - if (SelectedCodeTab.Errors.Count > 0) return; - - IsRunning = true; - SelectedCodeTab.IsRunning = true; - _logTextBox.Text = (DateTime.Now.ToTimeString() + ": ") + "Executing script '" + SelectedCodeTab.Title + "'..." + Environment.NewLine; - - await Task.Factory.StartNew(async () => - { - try - { - _stubManager = new StubManager(_adapter, (txt) => - { - AppendTextLog(txt + Environment.NewLine); - }, (txt) => - { - AppendTextLog(txt); - }, () => - { - - }); - var thisStubManager = _stubManager; - _stubManager.Completed += Manager_Completed; - _stubManager.Failed += Manager_Failed; - _stubManager.Executed += Manager_Executed; - _stubManager.AutoLog = AppendLogAuto; - - ScriptEngine engine = new ScriptEngine(new StubOnExecuteParameters(_stubManager)); - - engine.ReferencedAssemblies.Add(this.GetType()); - engine.ReferencedAssemblies.Add(typeof(PMR.Stubs.CalculateRequest)); - engine.ReferencedAssemblies.Add(typeof(IMessage)); - await engine.Run(SelectedCodeTab.Code, Path.GetDirectoryName(SelectedCodeTab.File)); - - if (!thisStubManager.Aborted) - { - IsRunning = false; - SelectedCodeTab.IsRunning = false; - } - } - catch (Exception ex) - { - IsRunning = false; - SelectedCodeTab.IsRunning = false; - MessageBox.Show(ex.Message, "Tango"); - } - }); - } - - /// - /// Builds the tab. - /// - private Task BuildTab() - { - return Task.Factory.StartNew(() => - { - try - { - Status = "Compiling " + SelectedCodeTab.Title + "..."; - - var thisStubManager = _stubManager; - - ScriptEngine engine = new ScriptEngine(new StubOnExecuteParameters(_stubManager)); - - engine.ReferencedAssemblies.Add(this.GetType()); - engine.ReferencedAssemblies.Add(typeof(PMR.Stubs.CalculateRequest)); - engine.ReferencedAssemblies.Add(typeof(IMessage)); - var results = engine.Compile(SelectedCodeTab.Code, Path.GetDirectoryName(SelectedCodeTab.File)).Result; - - if (results.Count == 0) - { - SelectedCodeTab.Errors = new ObservableCollection(); - Status = "Compiled successfully."; - } - else - { - SelectedCodeTab.Errors = results.ToObservableCollection(); - Status = results.Count + " compilation errors found!"; - } - } - catch (Exception ex) - { - Status = "Error compiling!"; - SelectedCodeTab.Errors = new ObservableCollection() { new CompilerError() { Error = ex.Message } }; - } - }); - } - - /// - /// Stops the currently current script. - /// - private void StopTab() - { - if (_stubManager != null) - { - _stubManager.Abort(); - IsRunning = false; - SelectedCodeTab.IsRunning = false; - Status = "Stopped!"; - AppendTextLog((DateTime.Now.ToTimeString() + ": ") + "Stopped!" + Environment.NewLine); - } - } - - #endregion - - #region Public Methods - - public void SetLogTextBox(TextBox logTextBox) - { - _logTextBox = logTextBox; - } - - #endregion - - #region Event Handlers - - /// - /// Handled the Executed event. - /// - /// The sender. - /// Name of the stub. - private void Manager_Executed(object sender, string stubName) - { - if (AppendLogAuto) - { - AppendTextLog((DateTime.Now.ToTimeString() + ": ") + "Executing '" + stubName + "'..." + Environment.NewLine); - } - - Status = "Executing " + stubName + "..."; - } - - /// - /// Handled the Failed event. - /// - /// The sender. - /// The exception. - private void Manager_Failed(object sender, Exception ex) - { - if (IsRunning) - { - if (AppendLogAuto) - { - AppendTextLog((DateTime.Now.ToTimeString() + ": ") + ex.Message + Environment.NewLine); - } - - Status = "Failed!"; - } - } - - /// - /// Handled the Completed event. - /// - /// The sender. - /// The response. - private void Manager_Completed(object sender, string response) - { - if (AppendLogAuto) - { - AppendTextLog((DateTime.Now.ToTimeString() + ": ") + "Response Received:" + Environment.NewLine); - AppendTextLog((DateTime.Now.ToTimeString() + ": ") + response + Environment.NewLine); - } - Status = "Completed"; + Application.Current.Exit += Current_Exit; } private void Current_Exit(object sender, ExitEventArgs e) { - LogManager.OverrideQueue = true; - LogManager.Log("Application Terminated"); - _settings.AutoLogResponse = AppendLogAuto; - _settings.SelectedPort = SelectedPort; - _settings.BaudRate = BaudRate; - _settings.LastTabs = CodeTabs.Select(x => x.File).ToList(); - _settings.Save(); + StubsViewVM.SaveSettings(); } - - private void AppendTextLog(String log) - { - LogManager.Log(log); - - InvokeUI(() => - { - if (_logTextBox.Text.Length > 99999) - { - _logTextBox.Clear(); - } - _logTextBox.AppendText(log); - }); - } - - private void ClearTextLog() - { - LogManager.Log("Log Cleared -----------------------------------------------------------------"); - - InvokeUI(() => - { - _logTextBox.Text = String.Empty; - }); - } - - #endregion } } diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/StubSnippetVM.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/StubSnippetVM.cs deleted file mode 100644 index 32964473a..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/StubSnippetVM.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.SharedUI; - -namespace Tango.Stubs.UI.ViewModels -{ - /// - /// Represents a single stub snippet view model. - /// - /// - public class StubSnippetVM : ViewModel - { - private String _name; - /// - /// Gets or sets the stub name. - /// - public String Name - { - get { return _name; } - set { _name = value; RaisePropertyChanged(nameof(Name)); } - } - - private String _code; - /// - /// Gets or sets the snippet code. - /// - public String Code - { - get { return _code; } - set { _code = value; RaisePropertyChanged(nameof(Code)); } - } - - /// - /// Returns a that represents this instance. - /// - /// - /// A that represents this instance. - /// - public override string ToString() - { - return Name; - } - } -} diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Views/MainView.xaml b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Views/MainView.xaml index 5113a972b..8cba3b74b 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Views/MainView.xaml +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Views/MainView.xaml @@ -3,409 +3,12 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mahapps="http://metro.mahapps.com/winfx/xaml/controls" + xmlns:stubs="clr-namespace:Tango.Stubs.Views;assembly=Tango.Stubs" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" - xmlns:views="clr-namespace:Tango.Stubs.UI.Views" - xmlns:fa="http://schemas.fontawesome.io/icons/" - xmlns:local="clr-namespace:Tango.Stubs.UI.Views" - xmlns:vm="clr-namespace:Tango.Stubs.UI.ViewModels" - xmlns:System="clr-namespace:System;assembly=mscorlib" - xmlns:converters="clr-namespace:Tango.SharedUI.Converters;assembly=Tango.SharedUI" - xmlns:controls="clr-namespace:Tango.SharedUI.Controls;assembly=Tango.SharedUI" - xmlns:transport="clr-namespace:Tango.Transport.Adapters;assembly=Tango.Transport" mc:Ignorable="d" d:DesignHeight="720" d:DesignWidth="1280"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - '' - - - - - - - (,) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Auto Logging - Scroll To End - - - - - - - - - + diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Views/MainView.xaml.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Views/MainView.xaml.cs index 06cc3d76c..8bf67e1c1 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Views/MainView.xaml.cs +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Views/MainView.xaml.cs @@ -18,38 +18,15 @@ using Tango.Stubs.UI.ViewModels; namespace Tango.Stubs.UI.Views { - /// - /// Interaction logic for MainView.xaml - /// - public partial class MainView : UserControl + /// + /// Interaction logic for MainView.xaml + /// + public partial class MainView : UserControl + { + public MainView() : base() { - private MainViewVM _vm; - - public MainView() : base() - { - InitializeComponent(); - Loaded += (_, __) => - { - _vm = DataContext as MainViewVM; - _vm.SetLogTextBox(txtLog); - - - }; - } - - //Auto scroll to bottom of response log each time it is changed. - private void TextBox_TextChanged(object sender, TextChangedEventArgs e) - { - if (chkScroll.IsChecked.Value) - { - txtLog.SelectionStart = txtLog.Text.Length; - txtLog.ScrollToEnd(); - } - } - - private void txtLog_PreviewMouseUp(object sender, MouseButtonEventArgs e) - { - chkScroll.IsChecked = false; - } + InitializeComponent(); + DataContext = new MainViewVM(); } + } } diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Windows/TextInputWindow.xaml b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Windows/TextInputWindow.xaml deleted file mode 100644 index cae2f94fe..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Windows/TextInputWindow.xaml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - The current operation requires you to provide some response in string format. - - - - - - - diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Windows/TextInputWindow.xaml.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Windows/TextInputWindow.xaml.cs deleted file mode 100644 index e15c690ec..000000000 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Windows/TextInputWindow.xaml.cs +++ /dev/null @@ -1,53 +0,0 @@ -using MahApps.Metro.Controls; -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.Stubs.UI.Windows -{ - /// - /// Interaction logic for TextInputWindow.xaml - /// - public partial class TextInputWindow : MetroWindow - { - public String Response { get; set; } - - public TextInputWindow() - { - InitializeComponent(); - this.Loaded += TextInputWindow_Loaded; - } - - private void TextInputWindow_Loaded(object sender, RoutedEventArgs e) - { - txtText.Focus(); - } - - public TextInputWindow(String message, String defaultResponse) : this() - { - txtText.Text = defaultResponse; - - if (message != null) - { - lbMessage.Text = message; - } - } - - private void Button_Click(object sender, RoutedEventArgs e) - { - Response = txtText.Text; - DialogResult = true; - Close(); - } - } -} -- cgit v1.3.1