aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/Dialogs/MachineConnectionWifiViewVM.cs
blob: 5af64464c101d7cacbfaee4293bb4655088d5808 (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
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.FSE.Common;
using Tango.FSE.Common.Helpers;
using Tango.Integration.ExternalBridge;
using Tango.PMR.Integration;
using Tango.SharedUI;

namespace Tango.FSE.UI.Dialogs
{
    /// <summary>
    /// Represents a machine TCP connection view model.
    /// </summary>
    /// <seealso cref="Tango.FSE.UI.Dialogs.MachineConnectionBaseViewVM" />
    public class MachineConnectionWifiViewVM : MachineConnectionBaseViewVM
    {
        private String _password;
        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        public String Password
        {
            get { return _password; }
            set { _password = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private ExternalBridgeLoginIntent _selectedIntent;
        /// <summary>
        /// Gets or sets the selected intent.
        /// </summary>
        public ExternalBridgeLoginIntent SelectedIntent
        {
            get { return _selectedIntent; }
            set { _selectedIntent = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Gets or sets the available intents.
        /// </summary>
        public List<ExternalBridgeLoginIntent> Intents { get; set; }

        private bool _requireSafetyLevelOperations;
        /// <summary>
        /// Gets or sets a value indicating whether to require safety level operations on the remote machine.
        /// </summary>
        public bool RequireSafetyLevelOperations
        {
            get { return _requireSafetyLevelOperations; }
            set { _requireSafetyLevelOperations = value; RaisePropertyChangedAuto(); }
        }

        private bool _autoReconnection;
        /// <summary>
        /// Gets or sets a value indicating whether keep trying to reconnect.
        /// </summary>
        public bool AutoReconnection
        {
            get { return _autoReconnection; }
            set { _autoReconnection = value; RaisePropertyChangedAuto(); }
        }


        private ExternalBridgeTcpClient _machine;
        /// <summary>
        /// Gets or sets the selected machine.
        /// </summary>
        public ExternalBridgeTcpClient Machine
        {
            get { return _machine; }
            set { _machine = value; RaisePropertyChangedAuto(); }
        }

        private bool _rememberMachinePassword;
        /// <summary>
        /// Gets or sets a value indicating whether to save the machine password.
        /// </summary>
        public bool RememberMachinePassword
        {
            get { return _rememberMachinePassword; }
            set { _rememberMachinePassword = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MachineConnectionWifiViewVM"/> class.
        /// </summary>
        /// <param name="machine">The machine.</param>
        public MachineConnectionWifiViewVM(ExternalBridgeTcpClient machine)
        {
            Machine = machine;
            Password = String.Empty;

            Intents = new List<ExternalBridgeLoginIntent>()
            {
                 ExternalBridgeLoginIntent.Diagnostics,
                 ExternalBridgeLoginIntent.FullControl
            };

            SelectedIntent = ExternalBridgeLoginIntent.Diagnostics;

            var connectionSettings = Settings.StoredMachinesConnectionSettings.FirstOrDefault(x => x.SerialNumber == machine.SerialNumber);

            if (connectionSettings != null)
            {
                Password = EncryptionHelper.Decrypt(connectionSettings.Password);
                SelectedIntent = connectionSettings.Intent;
                RequireSafetyLevelOperations = connectionSettings.RequireSafetyLevelOperations;
                AutoReconnection = connectionSettings.AutoReconnection;
                RememberMachinePassword = true;
            }
        }

        /// <summary>
        /// Invokes the <see cref="E:Tango.SharedUI.DialogViewVM.Accepted" /> event.
        /// </summary>
        protected override void Accept()
        {
            if (Machine != null)
            {
                if (RememberMachinePassword)
                {
                    FSESettings.MachineConnectionSettings connectionSettings = null;
                    connectionSettings = Settings.StoredMachinesConnectionSettings.FirstOrDefault(x => x.SerialNumber == Machine.SerialNumber);

                    if (connectionSettings == null)
                    {
                        connectionSettings = new FSESettings.MachineConnectionSettings();
                        Settings.StoredMachinesConnectionSettings.Add(connectionSettings);
                    }

                    connectionSettings.SerialNumber = Machine.SerialNumber;
                    connectionSettings.Password = EncryptionHelper.Encrypt(Password);
                    connectionSettings.Intent = SelectedIntent;
                    connectionSettings.RequireSafetyLevelOperations = RequireSafetyLevelOperations;
                    connectionSettings.AutoReconnection = AutoReconnection;
                }
                else
                {
                    Settings.StoredMachinesConnectionSettings.RemoveAll(x => x.SerialNumber == Machine.SerialNumber);
                }

                Settings.Save();
            }

            base.Accept();
        }

        /// <summary>
        /// Determines whether this instance can invoke the OK command.
        /// </summary>
        /// <returns></returns>
        protected override bool CanOK()
        {
            return Password.IsNotNullOrEmpty();
        }

        /// <summary>
        /// Gets the machine serial number.
        /// </summary>
        /// <returns></returns>
        public override string GetMachineSerialNumber()
        {
            return Machine.SerialNumber;
        }

        /// <summary>
        /// Called when the dialog has been shown.
        /// </summary>
        public override void OnShow()
        {
            base.OnShow();
            this.SetFocus(() => Password);
        }
    }
}