using Google.Protobuf;
using Google.Protobuf.Collections;
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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 System.Windows.Input;
using System.Windows.Threading;
using Tango.Core;
using Tango.Core.Commands;
using Tango.Integration.Operation;
using Tango.PMR;
using Tango.Scripting;
using Tango.Settings;
using Tango.SharedUI;
using Tango.Stubs.Views;
using Tango.Transport;
using Tango.Transport.Adapters;
namespace Tango.Stubs.ViewModels
{
///
/// Represents the script execution utility main view model.
///
///
public class StubsViewVM : ViewModel
{
private StubManager _stubManager;
private TextBox _logTextBox;
private StubsSettings _settings;
private Core.Threading.IntervalMessageDispatcher _consoleDispatcher;
#region Properties
public ITransportAdapter OverrideAdapter { get; set; }
public List CreateGroups { get; set; }
public List Examples { get; set; }
private IMachineOperator _machineOperator;
///
/// Gets or sets the machine operator.
///
public IMachineOperator MachineOperator
{
get { return _machineOperator; }
set { _machineOperator = value; RaisePropertyChangedAuto(); }
}
///
/// 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;
///
/// Gets or sets a value indicating whether the logs automatically.
///
public bool AppendLogAuto
{
get { return _appendLogAuto; }
set { _appendLogAuto = value; RaisePropertyChangedAuto(); }
}
private UsbSerialBaudRates _baudRate;
///
/// Gets or sets the baud rate.
///
public UsbSerialBaudRates BaudRate
{
get { return _baudRate; }
set { _baudRate = value; RaisePropertyChangedAuto(); }
}
private ConnectionMode _connectionMode;
///
/// Gets or sets the connection mode.
///
public ConnectionMode ConnectionMode
{
get { return _connectionMode; }
set { _connectionMode = value; RaisePropertyChangedAuto(); }
}
private bool _displayConnectionPane;
///
/// Gets or sets a value indicating whether [hide connection pane].
///
public bool DisplayConnectionPane
{
get { return _displayConnectionPane; }
set { _displayConnectionPane = 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 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 StubsViewVM()
{
_consoleDispatcher = new Core.Threading.IntervalMessageDispatcher(OnConsoleLog);
_consoleDispatcher.Start();
DisplayConnectionPane = true;
_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