aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/ViewModels/LayoutViewVM.cs
blob: 4e6621b9ee737f49292d7a8c6334a82fbdde4671 (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
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.Core.Threading;
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;
using Tango.Transport;

namespace Tango.FSE.UI.ViewModels
{
    public class LayoutViewVM : FSEViewModel
    {
        private ActionTimer _transportOutActionTimer;
        private ActionTimer _transportInActionTimer;

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

        private bool _transportOut;
        public bool TransportOut
        {
            get { return _transportOut; }
            set { _transportOut = value; RaisePropertyChanged(nameof(TransportOut)); }
        }

        private bool _transportIn;
        public bool TransportIn
        {
            get { return _transportIn; }
            set { _transportIn = value; RaisePropertyChanged(nameof(TransportIn)); }
        }

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

            _transportOutActionTimer = new ActionTimer(TimeSpan.FromMilliseconds(50));
            _transportInActionTimer = new ActionTimer(TimeSpan.FromMilliseconds(50));
        }

        private void MachineProvider_MachineDisconnected(object sender, Common.Connection.MachineDisconnectedEventArgs e)
        {
            if (e.MachineOperator.Adapter != null)
            {
                e.MachineOperator.Adapter.PropertyChanged -= Adapter_PropertyChanged;
            }
        }

        private void MachineProvider_MachineConnected(object sender, Common.Connection.MachineConnectedEventArgs e)
        {
            e.MachineOperator.Adapter.PropertyChanged -= Adapter_PropertyChanged;
            e.MachineOperator.Adapter.PropertyChanged += Adapter_PropertyChanged;
        }

        private void Adapter_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(ITransportAdapter.TotalBytesSent))
            {
                TransportOut = true;
                _transportOutActionTimer.ResetReplace(() => TransportOut = false);
            }
            else if (e.PropertyName == nameof(ITransportAdapter.TotalBytesReceived))
            {
                TransportIn = true;
                _transportInActionTimer.ResetReplace(() => TransportIn = false);
            }
        }

        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;

            MachineProvider.MachineDisconnected += MachineProvider_MachineDisconnected;
            MachineProvider.MachineConnected += MachineProvider_MachineConnected;
        }

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

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