-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPicoConnect.cs
160 lines (131 loc) · 4.87 KB
/
PicoConnect.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace HeadsetBatteryInfo
{
internal class PicoConnect : BatteryTracking
{
private enum Side
{
Left,
Right
}
private class BatteryInfoCallback
{
public DeviceType deviceType { get; set; }
public Side side { get; set; }
public bool active { get; set; }
public int percentage { get; set; }
}
private const string fileName = "pico_connect*.log";
private readonly string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "PICO Connect\\logs");
private bool picoConnectAppFound = false;
public override async void Init()
{
base.Init();
while (!isTeardown)
{
var processes = Process.GetProcessesByName("PICO Connect");
if (processes.Length > 0)
{
MainWindow.Instance.SetCompany("pico");
picoConnectAppFound = true;
break;
}
await Task.Delay(TimeSpan.FromMilliseconds(1000));
}
}
protected override async Task Listen()
{
await Task.Delay(TimeSpan.FromMilliseconds(3000));
if (!CanListen())
return;
var batteryInfo = await GetBatteryInfo();
foreach (var battery in batteryInfo)
{
if (Settings._config.predictCharging && battery.deviceType == DeviceType.Headset)
PredictChargeState(battery.percentage);
if (battery.deviceType == DeviceType.ControllerLeft)
{
BatteryInfoReceiver.OnReceiveBatteryLevel(battery.percentage, battery.side == Side.Left ? DeviceType.ControllerLeft : DeviceType.ControllerRight);
}
else
{
BatteryInfoReceiver.OnReceiveBatteryLevel(battery.percentage, battery.deviceType);
}
SetBattery(battery.deviceType, battery.percentage, battery.deviceType == DeviceType.Headset ? -1 : 0);
}
}
private bool CanListen()
{
if (isTeardown || !picoConnectAppFound)
return false;
return true;
}
private async Task<BatteryInfoCallback[]> GetBatteryInfo()
{
var batteryInfo = new BatteryInfoCallback[0];
string file = GetLatestFileName(path, fileName);
if (!File.Exists(file))
return batteryInfo;
await Task.Run(() =>
{
batteryInfo = ParseLog(file);
});
return batteryInfo;
}
private BatteryInfoCallback[] ParseLog(string fileName)
{
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
BatteryInfoCallback[] percentage = new BatteryInfoCallback[3];
StreamReader sr = new StreamReader(fs);
string[] strings = sr.ReadToEnd().Split('\n');
string pattern = "(.*?) \\[info\\] update battery info callback, battery_info: (.+)\\r";
for (int i = strings.Length-1; i > 0; i--)
{
string s = strings[i];
if (s.Contains("update battery info callback"))
{
var res = Regex.Match(s, pattern).Result("$2");
BatteryInfoCallback[] info = JsonSerializer.Deserialize<BatteryInfoCallback[]>(res);
return info;
}
}
return percentage;
}
}
private class FileTimeInfo
{
public string FileName;
public DateTime FileCreateTime;
}
private string GetLatestFileName(string path, string fileName)
{
DirectoryInfo d = new DirectoryInfo(path);
List<FileTimeInfo> list = new List<FileTimeInfo>();
foreach (FileInfo file in d.GetFiles(fileName))
{
list.Add(new FileTimeInfo()
{
FileName = file.FullName,
FileCreateTime = file.CreationTime
});
}
var qry = from x in list
orderby x.FileCreateTime
select x;
return qry.LastOrDefault().FileName;
}
public override void Terminate()
{
base.Terminate();
picoConnectAppFound = false;
}
}
}