-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptronIPolarSetup.cs
282 lines (222 loc) · 8.87 KB
/
OptronIPolarSetup.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
276
277
278
279
280
281
282
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
using System.Windows.Automation;
using System.Threading;
using System.IO;
using System.Globalization;
namespace AutoPolarAlign
{
public class OptronIPolarSetup
{
public class Settings
{
public bool CenterXFound = false;
public float CenterX;
public bool CenterYFound = false;
public float CenterY;
}
[DllImport("user32.dll", SetLastError = true)]
private static extern int SendMessageTimeoutA(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam, int fuFlags, int uTimeout, out IntPtr lpdwResult);
private static readonly int SMTO_NORMAL = 0x0000;
private static readonly int SMTO_ABORTIFHUNG = 0x0002;
private static readonly int BM_CLICK = 0xF5;
private static readonly int WM_CLOSE = 0x0010;
private static readonly int ERROR_TIMEOUT = 0x5B4;
public double Timeout { get; set; } = 10;
public bool AutoLoadDark { get; set; } = true;
public bool AutoLoadSettings { get; set; } = true;
public string ProcessName { get; set; } = "iOptron iPolar";
public string ApplicationPath { get; set; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "iOptron iPolar", "iOptron iPolar.exe");
public bool Run(out Settings settings)
{
StartIPolar();
return ConnectIPolar(out settings);
}
public void StopIPolar()
{
foreach (var process in Process.GetProcessesByName(ProcessName))
{
process.Kill();
}
}
public void StartIPolar()
{
// Once iPolar has connected UI automation doesn't work anymore
// for some reason, so we just restart iPolar to run the setup
StopIPolar();
var startInfo = new ProcessStartInfo
{
FileName = ApplicationPath,
WorkingDirectory = Path.GetDirectoryName(ApplicationPath)
};
Process.Start(startInfo);
}
public bool ConnectIPolar(out Settings settings)
{
settings = new Settings();
DateTime startTime = DateTime.Now;
bool CheckTimeout()
{
return (DateTime.Now - startTime).TotalSeconds > Timeout;
}
var window = FindAndCheckWindow(ProcessName, "MainPanel", Timeout);
if (CheckTimeout())
{
return false;
}
if (!FindAndClickButton(window, "buttonSettings", Timeout))
{
return false;
}
if (CheckTimeout())
{
return false;
}
if (AutoLoadDark || AutoLoadSettings)
{
var settingsWindow = FindAndCheckWindow(ProcessName, "Settings", Timeout);
if (CheckTimeout())
{
return false;
}
if (AutoLoadSettings)
{
if (FindFloatElement(settingsWindow, "maskedTextBoxX", out float centerX) && centerX > float.Epsilon)
{
settings.CenterX = centerX;
settings.CenterXFound = true;
}
if (FindFloatElement(settingsWindow, "maskedTextBoxY", out float centerY) && centerY > float.Epsilon)
{
settings.CenterY = centerY;
settings.CenterYFound = true;
}
}
if (AutoLoadDark)
{
if (!FindAndClickButton(settingsWindow, "buttonLoadLastDarkFrame", Timeout))
{
return false;
}
if (CheckTimeout())
{
return false;
}
}
SendMessageTimeoutA(new IntPtr(settingsWindow.Current.NativeWindowHandle), WM_CLOSE, IntPtr.Zero, IntPtr.Zero, SMTO_NORMAL | SMTO_ABORTIFHUNG, (int)Math.Ceiling(Timeout * 1000), out var _);
if (CheckTimeout())
{
return false;
}
}
// blocking: true would be better, but seems to hang once iPolar has connected
if (!FindAndClickButton(window, "buttonConnect", Timeout, blocking: false))
{
return false;
}
return true;
}
private AutomationElement FindAndCheckWindow(string processName, string automationId, double timeout)
{
var window = FindWindow(processName, automationId, timeout);
if (window == null)
{
throw new Exception("iPolar application is not running");
}
return window;
}
private AutomationElement FindWindow(string processName, string automationId, double timeout)
{
DateTime startTime = DateTime.Now;
while ((DateTime.Now - startTime).TotalSeconds <= timeout)
{
var windows = AutomationElement.RootElement.FindAll(TreeScope.Children, automationId != null ? new PropertyCondition(AutomationElement.AutomationIdProperty, automationId) : Condition.TrueCondition);
foreach (AutomationElement window in windows)
{
try
{
var process = Process.GetProcessById(window.Current.ProcessId);
if (process.ProcessName == processName)
{
return window;
}
}
catch (ArgumentException)
{
// Process not running
}
}
Thread.Sleep(100);
}
return null;
}
private bool FindAndClickButton(AutomationElement parent, string automationId, double timeout, bool blocking = true)
{
DateTime startTime = DateTime.Now;
while ((DateTime.Now - startTime).TotalSeconds <= timeout)
{
var handle = FindElementHandle(parent, automationId);
if (handle != IntPtr.Zero)
{
int result = SendMessageTimeoutA(handle, BM_CLICK, IntPtr.Zero, IntPtr.Zero, SMTO_NORMAL | SMTO_ABORTIFHUNG, !blocking ? 1 : (int)Math.Ceiling(timeout * 1000), out var _);
if (result != 0)
{
return true;
}
else
{
var lastError = Marshal.GetLastWin32Error();
if (!blocking && lastError == ERROR_TIMEOUT)
{
return true;
}
return false;
}
}
Thread.Sleep(100);
}
return false;
}
private AutomationElement FindElement(AutomationElement parent, string automationId)
{
return parent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
}
private IntPtr FindElementHandle(AutomationElement parent, string automationId)
{
AutomationElement element = FindElement(parent, automationId);
if (element != null)
{
return new IntPtr(element.Current.NativeWindowHandle);
}
return IntPtr.Zero;
}
private bool FindFloatElement(AutomationElement parent, string automationId, out float value)
{
var element = FindElement(parent, automationId);
if (element != null)
{
string valueStr = element.Current.Name;
if (float.TryParse(valueStr.Replace(",", "."), NumberStyles.Float, CultureInfo.InvariantCulture, out value))
{
return true;
}
}
value = 0;
return false;
}
private void InspectWindow(AutomationElement window)
{
if (window != null)
{
foreach (AutomationElement element in window.FindAll(TreeScope.Descendants, Condition.TrueCondition))
{
Console.WriteLine("> " + element.Current.ControlType.ProgrammaticName);
Console.WriteLine(" " + element.Current.Name);
Console.WriteLine(" " + element.Current.AutomationId);
Console.WriteLine();
}
}
}
}
}