aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.Diagnostics/ViewModels/DiagnosticsViewVM.cs
blob: 75c623c62b016be7885dbc4e669df7ef5d8672a8 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.FSE.Common;
using Tango.FSE.Common.Diagnostics;
using Tango.FSE.Diagnostics.Project;
using Tango.PMR.Diagnostics;

namespace Tango.FSE.Diagnostics.ViewModels
{
    public class DiagnosticsViewVM : FSEViewModel
    {
        private Dictionary<String, PropertyInfo> _monitorsProperties;
        private bool _isLoaded;
        private string _diagnosticsProjectFile;
        private FileSystemWatcher _diagnosticsProjectFileWatcher;

        private bool _isLoadingProject;
        public bool IsLoadingProject
        {
            get { return _isLoadingProject; }
            set { _isLoadingProject = value; RaisePropertyChangedAuto(); }
        }

        private DiagnosticsProject _project;
        public DiagnosticsProject Project
        {
            get { return _project; }
            set { _project = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<DiagnosticsTabViewVM> _tabs;
        public ObservableCollection<DiagnosticsTabViewVM> Tabs
        {
            get { return _tabs; }
            set { _tabs = value; RaisePropertyChangedAuto(); }
        }

        private DiagnosticsTabViewVM _selectedTab;
        public DiagnosticsTabViewVM SelectedTab
        {
            get { return _selectedTab; }
            set { _selectedTab = value; RaisePropertyChangedAuto(); OnSelectedTabChanged(); }
        }

        public RelayCommand ExportProjectCommand { get; set; }

        public DiagnosticsViewVM()
        {
            _monitorsProperties = new Dictionary<string, PropertyInfo>();

            foreach (var prop in typeof(DiagnosticsMonitors).GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList())
            {
                _monitorsProperties.Add(prop.Name, prop);
            }

            Tabs = new ObservableCollection<DiagnosticsTabViewVM>();
            Project = new DiagnosticsProject();

            ExportProjectCommand = new RelayCommand(ExportProject);
        }

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

            _diagnosticsProjectFileWatcher = new FileSystemWatcher(ApplicationManager.StartPath, "diagnostics.json");
            _diagnosticsProjectFileWatcher.Changed += _diagnosticsProjectFileWatcher_Changed;
            _diagnosticsProjectFileWatcher.EnableRaisingEvents = true;
        }

        private void _diagnosticsProjectFileWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            InvokeUI(async () =>
            {
                int selectedIndex = 0;

                if (SelectedTab != null)
                {
                    selectedIndex = Tabs.IndexOf(SelectedTab);
                }

                await Task.Delay(200);
                await LoadProject();

                if (selectedIndex > 0 && Tabs.Count > selectedIndex)
                {
                    SelectedTab = Tabs[selectedIndex];
                }
            });
        }

        private void DiagnosticsProvider_FrameReceived(object sender, DiagnosticsFrameReceivedEventArgs e)
        {
            PopulateDiagnosticsData(e.Frame);
        }

        public async override void OnApplicationReady()
        {
            base.OnApplicationReady();

            if (!_isLoaded)
            {
                _diagnosticsProjectFile = Path.Combine(ApplicationManager.StartPath, "diagnostics.json");
                await LoadProject();
            }
        }

        private async Task LoadProject()
        {
            Project = DiagnosticsProject.FromFile(_diagnosticsProjectFile);

            try
            {
                IsLoadingProject = true;
                _isLoaded = false;

                await Services.TechComponentsService.Preload();

                foreach (var widget in Project.Tabs.SelectMany(x => x.Widgets))
                {
                    try
                    {
                        await widget.Init();
                    }
                    catch (Exception ex)
                    {
                        NotificationProvider.PushErrorReportingSnackbar(ex, "Diagnostics Module Error", "Error initializing diagnostics widget.");
                    }
                }

                Tabs = new ObservableCollection<DiagnosticsTabViewVM>();
                foreach (var tab in Project.Tabs)
                {
                    Tabs.Add(new DiagnosticsTabViewVM() { Tab = tab });
                }

                SelectedTab = Tabs.FirstOrDefault();

                _isLoaded = true;
            }
            catch (Exception ex)
            {
                NotificationProvider.PushErrorReportingSnackbar(ex, "Diagnostics Module Error", "Error initializing diagnostics module.");
                return;
            }
            finally
            {
                IsLoadingProject = false;
            }
        }

        private void OnSelectedTabChanged()
        {

        }

        private void PopulateDiagnosticsData(DiagnosticsFrame frame)
        {
            if (_isLoaded)
            {
                foreach (var tab in Tabs.ToList())
                {
                    tab.PopulateDiagnosticsData(new DiagnosticsPackage()
                    {
                        Frame = frame,
                        MonitorsProperties = _monitorsProperties
                    });
                }
            }
        }

        private async void ExportProject()
        {
            var result = await StorageProvider.SaveFile("Export diagnostics project", "Diagnostics Projects|*.json", "diagnostics.json", ".json");

            if (result.Confirmed)
            {
                try
                {
                    Project.ToFile(result.SelectedItem);
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error exporting diagnostics project.");
                    await NotificationProvider.ShowError($"Error exporting diagnostics project\n{ex.FlattenMessage()}");
                }
            }
        }
    }
}