-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
152 lines (131 loc) · 5.49 KB
/
MainWindow.xaml.cs
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
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Timers;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using UDPLogger.Configuration;
using UDPLogger.SQLite;
using UDPLogger.UDP;
using UDPLogger.Utility;
namespace UDPLogger
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public const string VERSION = "1.0.0";
public class GridData(string name, object value, DateTime lastUpdate)
{
public string Name { get; set; } = name;
public object Value { get; set; } = value;
public DateTime LastUpdate { get; set; } = lastUpdate;
}
private readonly ObservableCollection<GridData> gridDataItemSource = [];
public ConfigurationFile Configuration { get; init; }
private readonly SQLiteHandler databaseHandler;
private readonly UDPSocketHandler udpSocketHandler;
private bool lastValuesLoaded = false;
public MainWindow()
{
this.Configuration = ConfigurationFile.Load();
this.Configuration.Save();
InitializeComponent(); //Since components are binded to configuration directly, this assure they are loaded with current values.
this.databaseHandler = new();
this.databaseHandler.QueryResult += (sender, args) =>
{
foreach (var record in args.RecordList)
{
var convertedValue = UDPTypeConverter.ConvertFromString((byte)record.Identifier, record.StringValue);
if(convertedValue != null)
{
gridDataItemSource.Add(new(record.Name, convertedValue, record.Time));
}
}
this.DataGrid.Items.Refresh();
lastValuesLoaded = true;
};
this.databaseHandler.StartWorker(Configuration.GetFullDatabasePath());
this.udpSocketHandler = new();
this.udpSocketHandler.ReceivedDataEvent += (sender, args) =>
{
var now = DateTime.Now;
var receivedDataList = args.ReceivedDataList;
var anyChanged = false;
foreach (var receivedData in receivedDataList)
{
bool isChanged = false;
var gridData = gridDataItemSource.Where(data => receivedData.Name == data.Name).FirstOrDefault();
if (gridData == null)
{
gridDataItemSource.Add(new(receivedData.Name, receivedData.Data, now));
isChanged = true;
}
else if (gridData.Value == null || !gridData.Value.Equals(receivedData.Data))
{
gridData.Value = receivedData.Data;
gridData.LastUpdate = now;
isChanged = true;
}
if(isChanged || receivedData.Flags.ForceInsert)
{
this.databaseHandler.AddRecord(new(receivedData.Name, receivedData.DataIdentifier, "" + receivedData.Data, now));
}
anyChanged |= isChanged;
}
if (anyChanged)
{
this.DataGrid.Items.Refresh();
}
};
this.udpSocketHandler.ConnectionStatusEvent += (sender, args) =>
{
if (args.ConnectionStatus)
{
StartButton.Foreground = new SolidColorBrush(Colors.Lime);
StopButton.Foreground = new SolidColorBrush(Colors.Black);
}
else
{
StartButton.Foreground = new SolidColorBrush(Colors.Black);
StopButton.Foreground = new SolidColorBrush(Colors.Red);
}
};
this.udpSocketHandler.StartWorker();
this.StartButton.Click += (sender, args) =>
{
if(lastValuesLoaded)
{
udpSocketHandler.Connect(this.Configuration.IPAddress, this.Configuration.RemotePort, this.Configuration.LocalPort);
}
};
this.StopButton.Click += (sender, args) => udpSocketHandler.Disconnect();
this.PurgeDatabaseButton.Click += (sender, args) =>
{
new PurgeDatabaseWindow(databaseHandler) { Owner = this }.ShowDialog();
};
this.DataGrid.ItemsSource = gridDataItemSource;
this.databaseHandler.QueryLastValues();
DispatcherTimer dispatcherTimer = new() { Interval = TimeSpan.FromSeconds(1) };
dispatcherTimer.Tick += (sender, args) =>
{
try
{
var fileInfo = new FileInfo(Configuration.GetFullDatabasePath());
this.DatabaseSizeTextBox.Text = fileInfo.FormatBytes();
}
catch (Exception ex)
{
LoggerTXT.AddException("Database Size Handlig Exception", ex);
}
};
dispatcherTimer.Start();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
new ConfigurationWindow(this.Configuration) { Owner = this }.ShowDialog();
}
}
}