-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMainWindow.xaml.cs
275 lines (238 loc) · 10.5 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using FontAwesome.WPF;
using log4net;
using MegatecUpsController.Properties;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Windows.Media;
namespace MegatecUpsController
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static readonly ILog applog = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static readonly ILog eventlog = LogManager.GetLogger("eventlog");
private readonly System.Threading.Timer timerUI;
private readonly NotifyIcon trayIcon = new NotifyIcon();
private readonly double[] xAxis = new double[60];
public MainWindow()
{
InitializeComponent();
LoadSettings();
SetupTrayIcon();
TimerCallback tm = new TimerCallback(TimerActionRefreshUI);
timerUI = new System.Threading.Timer(tm, null, 0, 1000);
eventlog.Info("Приложение запущено");
}
private void LoadSettings()
{
log4net.Config.XmlConfigurator.Configure();
for (int i = 0; i < xAxis.Length; i++) //заполняем ось X (время) графика от 0 до 59
xAxis[i] = i;
if (Settings.Default.alwaysOnTop)
{
Topmost = true;
}
else
{
Topmost = false;
}
}
private void SetupTrayIcon()
{
MenuItem MainMenuItem = new MenuItem("Главное окно", new EventHandler(ShowMainWindow));
MenuItem SettingsMenuItem = new MenuItem("Настройки", new EventHandler(ShowSettingsWindow));
MenuItem AboutMenuItem = new MenuItem("О программе", new EventHandler(ShowAboutWindow));
MenuItem ExitMenuItem = new MenuItem("Завершить", new EventHandler(ExitApp));
trayIcon.Icon = new System.Drawing.Icon(System.Windows.Application.GetResourceStream(new Uri("pack://application:,,,/Resources/AppIcon.ico")).Stream);
trayIcon.Visible = true;
trayIcon.ContextMenu = new ContextMenu(new MenuItem[] { MainMenuItem, SettingsMenuItem, AboutMenuItem, ExitMenuItem });
trayIcon.DoubleClick +=
delegate (object sender, EventArgs args)
{
Show();
WindowState = WindowState.Normal;
};
}
private void TimerActionRefreshUI(object obj) //TODO переделать на Binding
{
System.Windows.Application.Current.Dispatcher.Invoke(delegate
{
if ((DateTime.Now - UpsData.LastUpdated).TotalSeconds > 10) //иногда у ИБП случается затуп и он ничего не шлёт в ответ секунд 7. Больше уж да, что-то не то
{
Lbl_InputVoltage.Content = "???";
Lbl_OutputVoltage.Content = "???";
Lbl_Temperature.Content = "???";
Lbl_Hz.Content = "???";
Lbl_LoadPercent.Content = "???";
Lbl_CurAmper.Content = "???";
Lbl_CurWatt.Content = "???";
Lbl_CurVA.Content = "???";
Pb_BatteryLevel.Value = 0;
Lbl_BatteryVoltage.Content = "???";
Lbl_PowerInfo.Content = "Нет данных от ИБП";
UpsData.InputVoltageHistory.Enqueue(0);
UpsData.OutputVoltageHistory.Enqueue(0);
VoltageInputGraph.Plot(xAxis, UpsData.InputVoltageHistory);
VoltageOutputGraph.PlotBars(xAxis, UpsData.OutputVoltageHistory);
}
else
{
Lbl_InputVoltage.Content = UpsData.InputVoltage + " В";
Lbl_OutputVoltage.Content = UpsData.OutputVoltage + " В";
Lbl_Temperature.Content = UpsData.Temperature + "°";
Lbl_Hz.Content = UpsData.Hz + " Гц";
Lbl_LoadPercent.Content = UpsData.LoadPercent + " %";
Lbl_CurAmper.Content = UpsData.CurAmper + " А";
Lbl_CurWatt.Content = UpsData.CurWatt + " ВТ";
Lbl_CurVA.Content = UpsData.CurVA + " ВА";
Pb_BatteryLevel.Value = UpsData.BatteryPercent;
Lbl_BatteryVoltage.Content = UpsData.BatteryVoltage + " В";
if (UpsData.IsActiveAVR)
{
El_AvrStatus.Fill = Brushes.Yellow;
Lbl_AvrStatus.Content = "AVR: вкл.";
}
else
{
El_AvrStatus.Fill = Brushes.Gray;
Lbl_AvrStatus.Content = "AVR: выкл.";
}
if (UpsData.IsBeeperOn)
{
Fa_UpsSoundSwitch.Icon = FontAwesomeIcon.VolumeUp;
Btn_UpsSoundSwitch.ToolTip = "Пищалка ИБП (включёна)";
}
else
{
Fa_UpsSoundSwitch.Icon = FontAwesomeIcon.VolumeOff;
Btn_UpsSoundSwitch.ToolTip = "Пищалка ИБП (выключена)";
}
if (UpsData.IsUtilityFail)
{
Lbl_PowerInfo.FontWeight = FontWeights.Bold;
Lbl_PowerInfo.Foreground = Brushes.Red;
Lbl_PowerInfo.Content = "Питание: ОТ БАТАРЕИ";
}
else
{
Lbl_PowerInfo.FontWeight = FontWeights.Normal;
Lbl_PowerInfo.Foreground = Brushes.Black;
Lbl_PowerInfo.Content = "Питание: от розетки";
}
if (UpsData.IsBatteryLow)
{
Lbl_LowBattery.Visibility = Visibility.Visible;
}
else
{
Lbl_LowBattery.Visibility = Visibility.Collapsed;
}
VoltageInputGraph.Plot(xAxis, UpsData.InputVoltageHistory);
VoltageOutputGraph.PlotBars(xAxis, UpsData.OutputVoltageHistory);
}
if (UpsData.ConnectStatus)
{
El_UpsStatus.Fill = Brushes.LimeGreen;
Lbl_BottomStatus.Content = "ИБП работает";
Lbl_RawInputData.Content = UpsData.RawInputData;
}
else
{
El_UpsStatus.Fill = Brushes.Red;
Lbl_BottomStatus.Content = "ИБП недоступен";
Lbl_RawInputData.Content = "";
}
trayIcon.Text = Lbl_BottomStatus.Content + ". " + Lbl_PowerInfo.Content + ". " + Lbl_AvrStatus.Content;
});
}
private void Btn_UpsSoundSwitch_Click(object sender, RoutedEventArgs e)
{
Settings.Default.isBeeperOn = !UpsData.IsBeeperOn;
Settings.Default.Save();
UsbOps.SwitchUpsBeeper();
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
Message m = new Message
{
HWnd = hwnd,
Msg = msg,
WParam = wParam,
LParam = lParam
};
UsbOps.usb.ParseMessages(ref m);
return IntPtr.Zero;
}
private void MainForm_SourceInitialized(object sender, EventArgs e)
{
var handle = new WindowInteropHelper(this).Handle; //это и ещё две строки в MainForm_Loaded - для нормальной обработки потери связи по USB
UsbOps.usb.RegisterHandle(handle);
}
private void MainForm_Loaded(object sender, RoutedEventArgs e)
{
HwndSource src = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
src.AddHook(new HwndSourceHook(WndProc));
UsbOps.SetupUsbDevice(int.Parse(Settings.Default.vid, NumberStyles.AllowHexSpecifier), int.Parse(Settings.Default.pid, NumberStyles.AllowHexSpecifier));
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
Hide();
base.OnClosing(e);
}
private void ShowMainWindow(object sender, EventArgs e)
{
Show();
WindowState = WindowState.Normal;
}
private void ShowSettingsWindow(object sender, EventArgs e)
{
SettingsWindow settingsWindow = new SettingsWindow();
settingsWindow.ShowDialog();
LoadSettings();
}
private void ShowAboutWindow(object sender, EventArgs e)
{
AboutWindow aboutWindow = new AboutWindow();
aboutWindow.ShowDialog();
}
private void ExitApp(object sender, EventArgs e)
{
UsbOps.StopUsbTimer();
timerUI.Dispose();
trayIcon.Dispose();
eventlog.Info("Приложение закрыто");
App.Current.Shutdown();
}
private void Menu_About_Click(object sender, RoutedEventArgs e)
{
ShowAboutWindow(null, null);
}
private void Menu_Settings_Click(object sender, RoutedEventArgs e)
{
ShowSettingsWindow(null, null);
}
private void Menu_Help_Click(object sender, RoutedEventArgs e)
{
Process.Start("https://github.com/Galakart/MegatecUpsController/wiki");
}
private void Menu_Update_Click(object sender, RoutedEventArgs e)
{
UpdateWindow updateWindow = new UpdateWindow();
updateWindow.ShowDialog();
}
private void Menu_Close_Click(object sender, RoutedEventArgs e)
{
ExitApp(null, null);
}
}
}