-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
119 lines (96 loc) · 3.3 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using MicroPods.service;
using System.Threading;
namespace MicroPods
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private PordService ps;
private static readonly object Lock = new object();
private Dictionary<string, Object> status;
public delegate void UpdateUIDelegate(Dictionary<string, Object> status);
public UpdateUIDelegate updateUIDelegate;
public MainWindow()
{
InitializeComponent();
Init();
}
public void Init()
{
this.ps = new PordService();
var that = this;
this.ps.updateUIDelegate += (status) =>
{
that.status = status;
};
this.updateUIDelegate += (status) =>
{
int validTime = int.Parse(status.GetValueOrDefault("validTime", -1).ToString());
bool valid = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds() < validTime;
that.valid.Content = valid ? "已连接" : "未连接";
that.leftStatus.Content = getBatteryStatus(status, "leftStatus", 0) + "0%";
that.rightStatus.Content = getBatteryStatus(status, "rightStatus", 0) + "0%";
that.caseStatus.Content = getBatteryStatus(status, "caseStatus", 0) + "0%";
};
new Thread(() =>
{
while (true)
{
if(status != null)
{
this.Dispatcher.Invoke(updateUIDelegate, status);
}
Thread.Sleep(500);//睡眠500毫秒,也就是0.5秒
}
}).Start();
}
private void DrawOver(object sender, DragEventArgs e)
{
this.DragMove();
}
private void Drag(object sender, MouseEventArgs e)
{
//判断鼠标是否拖动窗口
if (e.LeftButton == MouseButtonState.Pressed)
{
//执行移动方法
this.DragMove();
}
}
private void minimizeClick(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void closeClick(object sender, RoutedEventArgs e)
{
//终止所有线程
Environment.Exit(Environment.ExitCode);
}
private int hexStrToInt(string hexStr)
{
return Int32.Parse(hexStr, System.Globalization.NumberStyles.HexNumber);
}
private int getBatteryStatus(Dictionary<string, Object> status, string key, int defaultValue)
{
string v = status.GetValueOrDefault(key, defaultValue).ToString();
return hexStrToInt(v);
}
}
}