aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/Dialogs/MachineConnectionWifiViewVM.cs
blob: d93b80f16b36a61c383ab240555c2b87e8496819 (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
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
{
    public class MachineConnectionWifiViewVM : MachineConnectionBaseViewVM
    {
        private String _password;
        public String Password
        {
            get { return _password; }
            set { _password = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private ExternalBridgeLoginIntent _intent;
        public ExternalBridgeLoginIntent SelectedIntent
        {
            get { return _intent; }
            set { _intent = value; RaisePropertyChangedAuto(); }
        }

        public List<ExternalBridgeLoginIntent> Intents { get; set; }

        private ExternalBridgeTcpClient _machine;
        public ExternalBridgeTcpClient Machine
        {
            get { return _machine; }
            set { _machine = value; RaisePropertyChangedAuto(); }
        }

        private bool _rememberMachinePassword;
        public bool RememberMachinePassword
        {
            get { return _rememberMachinePassword; }
            set { _rememberMachinePassword = value; RaisePropertyChangedAuto(); }
        }

        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;
                RememberMachinePassword = true;
            }
        }

        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;
                }
                else
                {
                    Settings.StoredMachinesConnectionSettings.RemoveAll(x => x.SerialNumber == Machine.SerialNumber);
                }

                Settings.Save();
            }

            base.Accept();
        }

        protected override bool CanOK()
        {
            return Password.IsNotNullOrEmpty();
        }

        public override string GetMachineSerialNumber()
        {
            return Machine.SerialNumber;
        }

        public override void OnShow()
        {
            base.OnShow();
            this.SetFocus(() => Password);
        }
    }
}