aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/ViewModels/LayoutViewVM.cs
blob: 29d7e13944e69d624fc5b72542492d00865ba31e (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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.FSE.Common;
using Tango.FSE.Common.Navigation;
using Tango.FSE.Common.Notifications;
using Tango.FSE.UI.Panes;
using Tango.Integration.ExternalBridge;
using Tango.SharedUI.Helpers;

namespace Tango.FSE.UI.ViewModels
{
    public class LayoutViewVM : FSEViewModel
    {
        private bool _isConnectionPaneOpened;
        public bool IsConnectionPaneOpened
        {
            get { return _isConnectionPaneOpened; }
            set { _isConnectionPaneOpened = value; RaisePropertyChangedAuto(); OnConnectionPaneToggleChanged(); }
        }

        private MachineConnectionPaneVM _connectionPaneVM;
        public MachineConnectionPaneVM ConnectionPaneVM
        {
            get { return _connectionPaneVM; }
            set { _connectionPaneVM = value; RaisePropertyChangedAuto(); }
        }

        private ConnectedMachinePaneVM _connectedMachinePaneVM;
        public ConnectedMachinePaneVM ConnectedMachinePaneVM
        {
            get { return _connectedMachinePaneVM; }
            set { _connectedMachinePaneVM = value; RaisePropertyChangedAuto(); }
        }

        private LogViewerPaneVM _logViewPaneVM;
        public LogViewerPaneVM LogViewerPaneVM
        {
            get { return _logViewPaneVM; }
            set { _logViewPaneVM = value; RaisePropertyChangedAuto(); }
        }

        private bool _isMenuOpened;
        public bool IsMenuOpened
        {
            get { return _isMenuOpened; }
            set { _isMenuOpened = value; RaisePropertyChangedAuto(); }
        }

        private bool _isLogsOpened;
        public bool IsLogsOpened
        {
            get { return _isLogsOpened; }
            set { _isLogsOpened = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand ToggleConnectionPaneCommand { get; set; }

        public RelayCommand LogoutCommand { get; set; }

        public LayoutViewVM()
        {
            ToggleConnectionPaneCommand = new RelayCommand(() => IsConnectionPaneOpened = !IsConnectionPaneOpened);
            ConnectionPaneVM = new MachineConnectionPaneVM();
            ConnectionPaneVM.MachineSelected += ConnectionPaneVM_MachineSelected;

            ConnectedMachinePaneVM = new ConnectedMachinePaneVM();
            ConnectedMachinePaneVM.DisconnectedFromMachine += ConnectedMachinePaneVM_DisconnectedFromMachine;

            LogViewerPaneVM = new LogViewerPaneVM();

            LogoutCommand = new RelayCommand(Logout);
        }

        private async void ConnectedMachinePaneVM_DisconnectedFromMachine(object sender, EventArgs e)
        {
            IsConnectionPaneOpened = false;
            await MachineProvider.DisconnectMachine();
        }

        private async void ConnectionPaneVM_MachineSelected(object sender, IExternalBridgeClient machine)
        {
            IsConnectionPaneOpened = false;

            try
            {
                if (await MachineProvider.ConnectToMachine(machine))
                {
                    NotificationProvider.PushSnackbarItem(MessageType.Success, "Machine successfully connected", true, $"{MachineProvider.Machine.SerialNumber}, {MachineProvider.Machine.Name}", TimeSpan.FromSeconds(5));
                }
            }
            catch (Exception ex)
            {
                await NotificationProvider.ShowError($"An error occurred while trying to connect to the specified machine.\n{ex.FlattenMessage()}");
                LogManager.Log(ex, "Error connecting to machine.");
            }
        }

        private void OnConnectionPaneToggleChanged()
        {
            if (IsConnectionPaneOpened)
            {
                ConnectionPaneVM.Start();
            }
            else
            {
                ConnectionPaneVM.Stop();
            }
        }

        public override void OnApplicationStarted()
        {
            DiagnosticsProvider.FrameReceived += DiagnosticsProvider_FrameReceived;
        }

        private void DiagnosticsProvider_FrameReceived(object sender, Common.Diagnostics.DiagnosticsFrameReceivedEventArgs e)
        {
            Debug.WriteLine("Diagnostics Received...");
        }

        private void Logout()
        {
            AuthenticationProvider.LogOut();
        }
    }
}