aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs
diff options
context:
space:
mode:
authorAvi Levkovich <avi@twine-s.com>2018-08-21 09:17:46 +0300
committerAvi Levkovich <avi@twine-s.com>2018-08-21 09:17:46 +0300
commit193f7f5e096751da747b0fe9d145bd43a02d7340 (patch)
treece0aa138252ca76426fbbc84e1753d7e410011a9 /Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs
parent98493042a5dace2535dd4343c047eaf1bd784ae3 (diff)
parentf468145c16b5c677201327feeb4f207c21df0b0b (diff)
downloadTango-193f7f5e096751da747b0fe9d145bd43a02d7340.tar.gz
Tango-193f7f5e096751da747b0fe9d145bd43a02d7340.zip
merge after conflict
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs')
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs282
1 files changed, 272 insertions, 10 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs
index 2877d52b3..57c2200c6 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs
@@ -8,6 +8,9 @@ using System.Threading.Tasks;
using Tango.Core;
using Tango.Core.Commands;
using Tango.Core.Helpers;
+using Tango.Integration.Operation;
+using Tango.Logging;
+using Tango.PMR.Connection;
using Tango.PPC.Common;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.MachineSetup;
@@ -16,11 +19,19 @@ using Tango.PPC.UI.ViewsContracts;
using Tango.Settings;
using Tango.SharedUI.Helpers;
using Tango.SQLExaminer;
+using Tango.Transport.Adapters;
namespace Tango.PPC.UI.ViewModels
{
+ /// <summary>
+ /// Represents the machine setup view ViewModel.
+ /// </summary>
+ /// <seealso cref="Tango.PPC.Common.PPCViewModel{Tango.PPC.UI.ViewsContracts.IMachineSetupView}" />
public class MachineSetupViewVM : PPCViewModel<IMachineSetupView>
{
+ /// <summary>
+ /// Represents the various machine setup view states.
+ /// </summary>
public enum MachineSetupStates
{
None,
@@ -29,11 +40,32 @@ namespace Tango.PPC.UI.ViewModels
Failed,
}
+ public enum MachineSetupView
+ {
+ WelcomeView,
+ WiFiSelectionView,
+ WiFiTestView,
+ EmbeddedWelcomeView,
+ EmbeddedTestView,
+ SetupWelcomeView,
+ SetupProgressView,
+ SetupCompletedView,
+ SetupFailedView,
+ }
+
private MachineSetupResult _setup_result;
+ #region Properties
+
+ /// <summary>
+ /// Gets or sets the machine setup manager.
+ /// </summary>
public IMachineSetupManager MachineSetupManager { get; set; }
private String _serialNumber;
+ /// <summary>
+ /// Gets or sets the serial number.
+ /// </summary>
public String SerialNumber
{
get { return _serialNumber; }
@@ -41,6 +73,9 @@ namespace Tango.PPC.UI.ViewModels
}
private String _hostAddress;
+ /// <summary>
+ /// Gets or sets the host address.
+ /// </summary>
public String HostAddress
{
get { return _hostAddress; }
@@ -48,6 +83,9 @@ namespace Tango.PPC.UI.ViewModels
}
private String _log;
+ /// <summary>
+ /// Gets or sets the log.
+ /// </summary>
public String Log
{
get { return _log; }
@@ -55,36 +93,124 @@ namespace Tango.PPC.UI.ViewModels
}
private MachineSetupStates _state;
+ /// <summary>
+ /// Gets or sets the state.
+ /// </summary>
public MachineSetupStates State
{
get { return _state; }
set { _state = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
- public RelayCommand StartCommand { get; set; }
+ private String _machineConnectionStatus;
+ public String MachineConnectionStatus
+ {
+ get { return _machineConnectionStatus; }
+ set { _machineConnectionStatus = value; RaisePropertyChangedAuto(); }
+ }
+
+ #endregion
+ #region Commands
+
+ /// <summary>
+ /// Gets or sets the complete command.
+ /// </summary>
public RelayCommand CompleteCommand { get; set; }
+ /// <summary>
+ /// Gets or sets the navigate to WiFi command.
+ /// </summary>
+ public RelayCommand NavigateToWiFiCommand { get; set; }
+
+ /// <summary>
+ /// Gets or sets the skip embedded test command.
+ /// </summary>
+ public RelayCommand SkipEmbeddedTestCommand { get; set; }
+
+ /// <summary>
+ /// Gets or sets the perform embedded test command.
+ /// </summary>
+ public RelayCommand PerformEmbeddedTestCommand { get; set; }
+
+ /// <summary>
+ /// Gets or sets the install command.
+ /// </summary>
+ public RelayCommand InstallCommand { get; set; }
+
+ /// <summary>
+ /// Gets or sets the restart command.
+ /// </summary>
+ public RelayCommand RestartCommand { get; set; }
+
+ #endregion
+
+ #region Constructors
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="MachineSetupViewVM"/> class.
+ /// </summary>
+ /// <param name="applicationManager">The application manager.</param>
+ /// <param name="machineSetupManager">The machine setup manager.</param>
public MachineSetupViewVM(IPPCApplicationManager applicationManager, IMachineSetupManager machineSetupManager)
{
MachineSetupManager = machineSetupManager;
- MachineSetupManager.ProgressLog += (x, msg) => AppendLog(msg);
+ //MachineSetupManager.ProgressLog += (x, msg) => AppendLog(msg);
MachineSetupManager.ProgressStep += (x, step) => AppendLog(Environment.NewLine + "-----------" + step.ToDescription().ToUpper() + "-----------" + Environment.NewLine);
HostAddress = "localhost\\SQLEXPRESS";
SerialNumber = "1111";
- StartCommand = new RelayCommand(StartSetup, () => !String.IsNullOrWhiteSpace(HostAddress) && !String.IsNullOrWhiteSpace(SerialNumber) && State == MachineSetupStates.None);
CompleteCommand = new RelayCommand(CompleteSetup, () => State == MachineSetupStates.Completed);
applicationManager.SetupRequired += ApplicationManager_SetupRequired;
+
+ NavigateToWiFiCommand = new RelayCommand(EnsureWiFi);
+
+ PerformEmbeddedTestCommand = new RelayCommand(PerformEmbeddedTest);
+ SkipEmbeddedTestCommand = new RelayCommand(SkipEmbeddedTest);
+ InstallCommand = new RelayCommand(Install);
+ RestartCommand = new RelayCommand(() => { NavigateTo(MachineSetupView.WelcomeView); });
+
+ if (!LogManager.Categories.Contains(LogCategory.Debug))
+ {
+ LogManager.Categories.Add(LogCategory.Debug);
+ }
+
+ var logger = new SimpleStringLogger();
+ LogManager.RegisterLogger(logger);
+ logger.LogReceived += Logger_LogReceived;
}
+ #endregion
+
+ #region Event Handlers
+
+ /// <summary>
+ /// Handles the SetupRequired event of the ApplicationManager.
+ /// </summary>
+ /// <param name="sender">The source of the event.</param>
+ /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void ApplicationManager_SetupRequired(object sender, EventArgs e)
{
+ LogManager.Log("SetupRequired event received. Navigating to MachineSetupView...");
NavigationManager.NavigateTo(NavigationView.MachineSetupView);
}
+
+ private void Logger_LogReceived(object sender, LogItemBase e)
+ {
+ AppendLog(e.Message);
+ }
+
+ #endregion
+
+ #region Private Methods
+
+ /// <summary>
+ /// Appends the log.
+ /// </summary>
+ /// <param name="msg">The MSG.</param>
private void AppendLog(String msg)
{
if (msg != null && !msg.Contains("SQL Examiner"))
@@ -96,8 +222,145 @@ namespace Tango.PPC.UI.ViewModels
}
}
- private async void StartSetup()
+ /// <summary>
+ /// Completes the setup.
+ /// </summary>
+ private void CompleteSetup()
{
+ String updater_exe = AssemblyHelper.GetCurrentAssemblyFolder() + "\\Tango.PPC.Updater.exe";
+
+ LogManager.Log("Completing machine setup...");
+ LogManager.Log($"Executing '{updater_exe}' with arguments '{_setup_result.UpdatePackagePath}'...");
+ Process.Start(updater_exe, _setup_result.UpdatePackagePath);
+ LogManager.Log("Terminating application process!");
+ Environment.Exit(0);
+ }
+
+ /// <summary>
+ /// Navigates to the specified view.
+ /// </summary>
+ /// <param name="view">The view.</param>
+ private Task NavigateTo(MachineSetupView view)
+ {
+ return View.NavigateTo(view);
+ }
+
+ #endregion
+
+ #region WiFi
+
+ private async void EnsureWiFi()
+ {
+ await NavigateTo(MachineSetupView.WiFiTestView);
+
+ await Task.Delay(2000);
+
+ bool connected = await ConnectivityProvider.CheckInternetConnection();
+
+ if (connected)
+ {
+ await NavigateTo(MachineSetupView.EmbeddedWelcomeView);
+ }
+ else
+ {
+ await NavigateTo(MachineSetupView.WiFiSelectionView);
+ await ConnectivityProvider.RefreshAvailableWiFiNetworks();
+
+ ConnectivityProvider.ConnectionStateChanged += ConnectivityProvider_ConnectionStateChanged;
+ }
+ }
+
+ private void ConnectivityProvider_ConnectionStateChanged(object sender, Common.Connectivity.ConnectionStateEventArgs e)
+ {
+ if (e.IsConnected)
+ {
+ ConnectivityProvider.ConnectionStateChanged -= ConnectivityProvider_ConnectionStateChanged;
+ EnsureWiFi();
+ }
+ }
+
+ #endregion
+
+ #region Embedded
+
+ private async void PerformEmbeddedTest()
+ {
+ await NavigateTo(MachineSetupView.EmbeddedTestView);
+
+ try
+ {
+ MachineConnectionStatus = "Scanning for the machine...";
+
+ LogManager.Log("Starting machine connection procedure...");
+
+ TimeSpan timeout = TimeSpan.FromSeconds(SettingsManager.Default.GetOrCreate<PPCSettings>().MachineScanningTimeoutSeconds);
+
+ LogManager.Log("Scanning for machine on available serial ports...");
+ Transport.Discovery.UsbCommunicationScanner<ConnectRequest, ConnectResponse> scanner = new Transport.Discovery.UsbCommunicationScanner<ConnectRequest, ConnectResponse>(UsbSerialBaudRates.BR_9600);
+
+ scanner.ScanningPort += (port) =>
+ {
+ MachineConnectionStatus = $"Scanning for the machine on {port}...";
+ };
+
+ var response = await scanner.Scan(new ConnectRequest() { Password = "1234" }, timeout);
+
+ MachineConnectionStatus = "Machine discovered on port: " + response.Adapter.Address + ", trying to connect...";
+
+ LogManager.Log("Machine discovered on port: " + response.Adapter.Address);
+ LogManager.Log("Device Information:");
+ LogManager.Log(response.Response.DeviceInformation.ToJsonString());
+
+ LogManager.Log("Disconnecting machine operator...");
+
+ IMachineOperator op = new MachineOperator(response.Adapter);
+
+ op.EnableDiagnostics = false;
+ op.EnableEmbeddedDebugging = false;
+ op.EnableEventsNotification = false;
+
+ LogManager.Log("Connecting machine operator...");
+ await op.Connect();
+
+ MachineConnectionStatus = "Test completed successfully!";
+
+ await Task.Delay(1000);
+
+ try
+ {
+ await op.Disconnect();
+ }
+ catch { }
+
+ await NavigateTo(MachineSetupView.SetupWelcomeView);
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, "Error while trying to scan and connect to the machine.");
+ MachineConnectionStatus = "Test Failed!";
+ await Task.Delay(2000);
+ await NavigateTo(MachineSetupView.EmbeddedWelcomeView);
+ }
+ }
+
+ private async void SkipEmbeddedTest()
+ {
+ if (await NotificationProvider.ShowQuestion("Are you sure you want to skip the machine communication test?"))
+ {
+
+ }
+ }
+
+ #endregion
+
+ #region Setup
+
+ private async void Install()
+ {
+ await NavigateTo(MachineSetupView.SetupProgressView);
+
+ LogManager.Log("Starting machine setup...");
+
State = MachineSetupStates.Working;
try
@@ -106,18 +369,17 @@ namespace Tango.PPC.UI.ViewModels
Settings.ApplicationState = ApplicationStates.SemiSetup;
Settings.Save();
State = MachineSetupStates.Completed;
+ LogManager.Log("Machine setup completed.");
+ await NavigateTo(MachineSetupView.SetupCompletedView);
}
catch (Exception ex)
{
+ LogManager.Log(ex, "Machine setup failed.");
State = MachineSetupStates.Failed;
- await NotificationProvider.ShowInfo(ex.Message);
+ await NavigateTo(MachineSetupView.SetupFailedView);
}
}
- private void CompleteSetup()
- {
- Process.Start(AssemblyHelper.GetCurrentAssemblyFolder() + "\\Tango.PPC.Updater.exe", _setup_result.UpdatePackagePath);
- Environment.Exit(0);
- }
+ #endregion
}
}