aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/Dialogs/MachineConnectionUsbViewVM.cs
blob: 9056e7f4ded809c0ad513c062a7881ae6ca27115 (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
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.Core.DI;
using Tango.FSE.BL;
using Tango.FSE.Common;
using Tango.FSE.Common.AutoComplete;
using Tango.Integration.ExternalBridge;

namespace Tango.FSE.UI.Dialogs
{
    /// <summary>
    /// Represents a machine USB connection view model.
    /// </summary>
    /// <seealso cref="Tango.FSE.UI.Dialogs.MachineConnectionBaseViewVM" />
    public class MachineConnectionUsbViewVM : MachineConnectionBaseViewVM
    {
        [TangoInject]
        protected FSEServicesContainer Services { get; set; }

        public IExternalBridgeClient Machine { get; set; }

        /// <summary>
        /// Gets or sets the machines completion source.
        /// </summary>
        public AutoCompleteSource<Machine> Machines { get; set; }

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

        /// <summary>
        /// Initializes a new instance of the <see cref="MachineConnectionUsbViewVM"/> class.
        /// </summary>
        public MachineConnectionUsbViewVM() : base()
        {
            TangoIOC.Default.Inject(this);
            Machines = new AutoCompleteSource<Machine>(AutoCompleteMachines);
        }

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

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

        /// <summary>
        /// Called when the dialog has been shown.
        /// </summary>
        public async override void OnShow()
        {
            base.OnShow();

            try
            {
                if (Settings.LastVirtualizedMachineSerialNumber != null)
                {
                    var machine = await Services.MachinesService.GetMachine(Settings.LastVirtualizedMachineSerialNumber);

                    if (SelectedMachine == null)
                    {
                        SelectedMachine = machine;
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error retrieving last virtualized machine.");
            }

            if (SelectedMachine == null)
            {
                this.SetFocus(() => SelectedMachine);
            }
        }

        /// <summary>
        /// Invokes the <see cref="E:Tango.SharedUI.DialogViewVM.Accepted" /> event.
        /// </summary>
        protected override void Accept()
        {
            Settings.LastVirtualizedMachineSerialNumber = SelectedMachine.SerialNumber;
            Settings.Save();

            base.Accept();
        }

        private List<Machine> AutoCompleteMachines(string key)
        {
            key = key ?? String.Empty;

            try
            {
                return Services.MachinesService.GetAllMachines().Result.Where(x => x.SerialNumber.ToLower().StartsWith(key)).Take(4).ToList();
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error on auto complete virtualized machine filter.");
                return new List<Machine>();
            }
        }
    }
}