aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs
blob: a81db331c58aa098c1e249dbd84a1e1511bed375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Core.Commands;
using Tango.Core.Helpers;
using Tango.PPC.Common;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.MachineSetup;
using Tango.PPC.Common.Navigation;
using Tango.PPC.UI.ViewsContracts;
using Tango.Settings;
using Tango.SharedUI.Helpers;
using Tango.SQLExaminer;

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,
            Working,
            Completed,
            Failed,
        }

        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; }
            set { _serialNumber = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private String _hostAddress;
        /// <summary>
        /// Gets or sets the host address.
        /// </summary>
        public String HostAddress
        {
            get { return _hostAddress; }
            set { _hostAddress = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private String _log;
        /// <summary>
        /// Gets or sets the log.
        /// </summary>
        public String Log
        {
            get { return _log; }
            set { _log = value; RaisePropertyChangedAuto(); }
        }

        private MachineSetupStates _state;
        /// <summary>
        /// Gets or sets the state.
        /// </summary>
        public MachineSetupStates State
        {
            get { return _state; }
            set { _state = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        #endregion

        #region Commands

        /// <summary>
        /// Gets or sets the start command.
        /// </summary>
        public RelayCommand StartCommand { get; set; }

        /// <summary>
        /// Gets or sets the complete command.
        /// </summary>
        public RelayCommand CompleteCommand { 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.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;
        }

        #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);
        }

        #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"))
            {
                InvokeUI(() =>
                {
                    View.AppendLog(msg + Environment.NewLine);
                });
            }
        }

        /// <summary>
        /// Starts the setup.
        /// </summary>
        private async void StartSetup()
        {
            LogManager.Log("Starting machine setup...");

            State = MachineSetupStates.Working;

            try
            {
                _setup_result = await MachineSetupManager.Setup(SerialNumber, HostAddress);
                Settings.ApplicationState = ApplicationStates.SemiSetup;
                Settings.Save();
                State = MachineSetupStates.Completed;

                LogManager.Log("Machine setup completed.");
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Machine setup failed.");
                State = MachineSetupStates.Failed;
                await NotificationProvider.ShowInfo(ex.Message);
            }
        }

        /// <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);
        }

        #endregion
    }
}