-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloWorldWindow.xaml.cs
More file actions
499 lines (422 loc) · 17.8 KB
/
HelloWorldWindow.xaml.cs
File metadata and controls
499 lines (422 loc) · 17.8 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GelSight.Api.Client;
using GelSight.Api.Client.EventArgs;
using GelSight.Api.Client.Shapes;
using ConnectionState = GelSight.Api.Client.ConnectionState;
using Point = GelSight.Api.Client.Shapes.Point;
namespace GelSight.Api.HelloWorld
{
/// <summary>
/// Interaction logic for HelloWorldWindow.xaml
/// </summary>
public partial class HelloWorldWindow
{
private readonly ConnectionManager _connectionManager;
public HelloWorldWindow()
{
InitializeComponent();
DataContext = this;
// Subscribe to the window closing event so we can notify the server that the client is going away
Closing += OnClosing;
var host = Utility.GetLocalIpAddress();
const ushort port = 9002;
const string password = "password";
// Update the user interface
IpAddress.Text = host;
Port.Text = port.ToString();
Password.Text = password;
// Create the ConnectionManager object
_connectionManager = new ConnectionManager(host, port, password);
SubscribeToServerEvents(_connectionManager);
ConnectToServer();
}
private void OnClosing(object? sender, CancelEventArgs e)
{
// Close the connection to the server
_connectionManager.Close();
UnsubscribeFromServerEvents(_connectionManager);
}
private void AddLine(string message)
{
Dispatcher.Invoke(() =>
{
MessageList.Items.Add(message);
});
}
private void SubscribeToServerEvents(ConnectionManager? cm)
{
if (cm == null)
return;
cm.ConnectionStateChanged += ConnectionStateChanged;
cm.StatusChanged += StatusChanged;
cm.ErrorMessageReceived += ErrorMessageReceived;
cm.GelChanged += GelChanged;
cm.ScanCompleted += ScanCompleted;
cm.AnalysisSaved += AnalysisSaved;
cm.HeightmapStarted += HeightmapStarted;
cm.HeightmapCanceled += HeightmapCanceled;
cm.HeightmapCompleted += HeightmapCompleted;
cm.ScanDeleted += ScanDeleted;
cm.GelStateChanged += GelStateChanged;
cm.ImageAcquired += ImageAcquired;
cm.WarningReceived += OnWarningReceived;
}
private void OnWarningReceived(object sender, string e)
{
AddLine($"Warning received: {e}");
}
private void UnsubscribeFromServerEvents(ConnectionManager? cm)
{
if (cm == null)
return;
cm.ConnectionStateChanged -= ConnectionStateChanged;
cm.StatusChanged -= StatusChanged;
cm.ErrorMessageReceived -= ErrorMessageReceived;
cm.GelChanged -= GelChanged;
cm.ScanCompleted -= ScanCompleted;
cm.AnalysisSaved -= AnalysisSaved;
cm.HeightmapStarted -= HeightmapStarted;
cm.HeightmapCanceled -= HeightmapCanceled;
cm.HeightmapCompleted -= HeightmapCompleted;
cm.ScanDeleted -= ScanDeleted;
cm.GelStateChanged -= GelStateChanged;
cm.ImageAcquired -= ImageAcquired;
cm.WarningReceived -= OnWarningReceived;
}
//
// Server event handlers
//
private void ScanDeleted(object? sender, ScanDeletedEventArgs args)
{
AddLine($"Scan deleted: ScanFolder = {args.ScanFolder}, Success = {args.Success}");
}
private void HeightmapCompleted(object? sender, HeightmapCompletedEventArgs args)
{
AddLine($"Heightmap completed: ScanFolder = {args.ScanFolder}, Success = {args.Success}");
}
private void HeightmapCanceled(object? sender, EventArgs args)
{
AddLine("Heightmap canceled.");
}
private void HeightmapStarted(object? sender, string scanFolder)
{
AddLine("Heightmap started.");
}
private void ScanCompleted(object? sender, ScanCompletedEventArgs args)
{
AddLine($"Scan completed: ScanFolder = {args.ScanFolder}, ScanMetadata = {args.ScanMetadata}");
}
private void AnalysisSaved(object? sender, AnalysisSavedEventArgs args)
{
// Demonstration of extracting properties from an analysis method
// using the Nuget package, Newtonsoft's Json.NET
// https://www.newtonsoft.com/json
//var json = args.Results;
//dynamic data = JObject.Parse(json);
//foreach (var routine in data.Routines)
//{
// if (routine["name"] != "Surface Roughness")
// continue;
// var sa = routine["Sa"];
// AddLine($"Sa = {sa}");
//}
AddLine($"Analysis saved: ScanFolder = {args.ScanFolder}, Success = {args.Success}, Results = {args.Results}, ErrorMessage = {args.ErrorMessage}, RequestId = {args.RequestId}");
}
private void GelChanged(object? sender, GelInfo gelInfo)
{
Dispatcher.Invoke(() =>
{
var serial = $"Gel Serial: {gelInfo.Serial ?? " Unknown"}";
var name = $"Gel Type: {gelInfo.Type ?? " Unknown"}";
var useBy = $"Gel Use by: {gelInfo.UseBy?.ToShortDateString() ?? " Unknown"}";
GelInfo.Content = string.Join("\t", serial, name, useBy);
});
AddLine($"Gel info changed: Serial = {gelInfo.Serial}, Type = {gelInfo.Type}, UseBy = {gelInfo.UseBy}");
}
private void GelStateChanged(object? sender, string args)
{
AddLine($"Gel state changed: state = {args}");
}
private void ImageAcquired(object? sender, MessageImageAcquiredEventArgs args)
{
// Call the helper method to convert the array of bytes representing
// a jpeg image to a BitmapImage object to display in the UI
var image = BitmapImageFromJpegArray(args.JpegImage);
Dispatcher.Invoke(() =>
{
ScanImage.Source = image;
});
}
/// <summary>
/// Helper method to convert an array of bytes representing a jpeg image to a BitmapImage object.
/// </summary>
private static BitmapImage? BitmapImageFromJpegArray(byte[] data)
{
if (data.Length == 0)
return null;
using var ms = new MemoryStream(data);
var img = new BitmapImage();
img.BeginInit();
img.CacheOption = BitmapCacheOption.OnLoad;
img.StreamSource = ms;
img.EndInit();
if (img.CanFreeze)
img.Freeze();
return img;
}
private void ErrorMessageReceived(object? sender, ErrorMessageReceivedEventArgs args)
{
AddLine($"Error message received: Message = {args.Message}, RequestId = {args.RequestId}");
}
private void StatusChanged(object? sender, Status status)
{
Dispatcher.Invoke(() =>
{
SensorPresent.IsChecked = status.IsSensorPresent;
SensorLive.IsChecked = status.IsSensorLive;
ScanPossible.IsChecked = status.IsScanPossible;
});
DeviceSerialNumber = status.Serial ?? string.Empty;
AddLine($"Status changed: IsSensorPresent = {status.IsSensorPresent}, IsSensorLive = {status.IsSensorLive}, IsScanPossible = {status.IsScanPossible}, " +
$"Model = {status.Model}, DisplaySerial = {status.DisplaySerial}, Serial = {status.Serial}, Configuration = {status.Configuration}, " +
$"FirmwareVersion = {status.FirmwareVersion}, ImageWidth = {status.ImageWidth}, ImageHeight = {status.ImageHeight}");
}
private void UpdateConnectionState()
{
var connectionState = _connectionManager.ConnectionState;
Dispatcher.Invoke(() =>
{
Status.Content = connectionState.GetDescription();
var color = connectionState.GetColor();
var converted = Color.FromArgb(color.A, color.R, color.G, color.B);
Status.Background = new SolidColorBrush(converted);
ConnectBtn.IsEnabled = CanConnect;
IpAddress.IsEnabled = CanConnect;
Port.IsEnabled = CanConnect;
Password.IsEnabled = CanConnect;
DisconnectBtn.IsEnabled = CanDisconnect;
});
// If we are connected and validated, update the UI with the GSM server version info.
if (connectionState == ConnectionState.ConnectedAndValidated)
{
var version = _connectionManager.GsmServerVersion;
AddLine($"Gsm Version = {version}");
}
}
private void ConnectionStateChanged(object? sender, ConnectionState connectionState)
{
UpdateConnectionState();
}
//
// UI helper functions
//
private bool CanDisconnect => _connectionManager.ConnectionState == ConnectionState.ConnectedAndValidated ||
_connectionManager.ConnectionState == ConnectionState.Connected ||
_connectionManager.ConnectionState == ConnectionState.Connecting;
private bool CanConnect => _connectionManager.ConnectionState == ConnectionState.NotStarted ||
_connectionManager.ConnectionState == ConnectionState.ValidationFailed ||
_connectionManager.ConnectionState == ConnectionState.VersionMismatch;
//
// Button click event handlers to demonstrate server request actions.
//
private void RequestHeightmap_OnClick(object sender, RoutedEventArgs e)
{
try
{
var scanFolder = @"C:\Users\Public\Documents\GelSight\Scans\New Scans\Scan008";
var requestId = _connectionManager.RequestHeightmap(scanFolder, true, true, true, 4);
AddLine($"Message sent: RequestId = {requestId}");
}
catch (Exception ex)
{
AddLine(ex.Message);
}
}
private void TryDeleteScan_OnClick(object sender, RoutedEventArgs e)
{
try
{
var scanFolder = @"C:\Users\Public\Documents\GelSight\Scans\New Scans\Scan008";
var requestId = _connectionManager.TryDeleteScan(scanFolder);
AddLine($"Message sent: RequestId = {requestId}");
}
catch (Exception ex)
{
AddLine(ex.Message);
}
}
private void RequestLiveView_OnClick(object sender, RoutedEventArgs e)
{
try
{
var requestId = _connectionManager.RequestLiveView(true);
AddLine($"Message sent: RequestId = {requestId}");
}
catch (Exception ex)
{
AddLine(ex.Message);
}
}
private void StopLiveView_OnClick(object sender, RoutedEventArgs e)
{
try
{
var requestId = _connectionManager.RequestLiveView(false);
AddLine($"Message sent: RequestId = {requestId}");
}
catch (Exception ex)
{
AddLine(ex.Message);
}
}
private void SubscribeGelState_OnClick(object sender, RoutedEventArgs e)
{
try
{
SubscribeToGelState = true;
var requestId = _connectionManager.UpdateSubscriptions(SubscribeToLiveImages, SubscribeToGelState, DeviceSerialNumber);
AddLine($"Message sent: RequestId = {requestId}");
}
catch (Exception ex)
{
AddLine(ex.Message);
}
}
private void UnsubscribeGelState_OnClick(object sender, RoutedEventArgs e)
{
try
{
SubscribeToGelState = false;
var requestId = _connectionManager.UpdateSubscriptions(SubscribeToLiveImages, SubscribeToGelState, DeviceSerialNumber);
AddLine($"Message sent: RequestId = {requestId}");
}
catch (Exception ex)
{
AddLine(ex.Message);
}
}
private void SubscribeLiveView_OnClick(object sender, RoutedEventArgs e)
{
try
{
SubscribeToLiveImages = true;
var requestId = _connectionManager.UpdateSubscriptions(SubscribeToLiveImages, SubscribeToGelState, DeviceSerialNumber);
AddLine($"Message sent: RequestId = {requestId}");
}
catch (Exception ex)
{
AddLine(ex.Message);
}
}
private void UnsubscribeLiveView_OnClick(object sender, RoutedEventArgs e)
{
try
{
SubscribeToLiveImages = false;
var requestId = _connectionManager.UpdateSubscriptions(SubscribeToLiveImages, SubscribeToGelState, DeviceSerialNumber);
AddLine($"Message sent: RequestId = {requestId}");
}
catch (Exception ex)
{
AddLine(ex.Message);
}
}
private void RequestScan_OnClick(object sender, RoutedEventArgs e)
{
try
{
var scanPath = @"C:\Users\Public\Documents\GelSight\Scans\New Scans";
var scanPrefixName = "Test";
var requestId = _connectionManager.RequestScan(scanPath, scanPrefixName);
AddLine($"Message sent: RequestId = {requestId}");
}
catch (Exception ex)
{
AddLine(ex.Message);
}
}
private void RequestAnalysis_OnClick(object sender, RoutedEventArgs e)
{
try
{
var scanFolder = @"C:\Users\Public\Documents\GelSight\Scans\AutoScratch\SmallDent";
//
// Profile Roughness example
//
var analysisName = "Profile Roughness";
var line = new LineApi(new Point(100, 100), new Point(1000, 1000));
var shapes = new List<ShapeApi> { line };
var parameters = new Dictionary<string, string>
{
{ "lambdac", "0.8" },
{ "numprofiles", "5" },
{ "profilewidth", "1.0" }
};
var requestId = _connectionManager.RequestAnalysis(scanFolder, analysisName, parameters, shapes);
AddLine($"Message sent: RequestId = {requestId}");
}
catch (Exception ex)
{
AddLine(ex.Message);
}
}
//
// Connect button click handler
//
private void Connect_OnClick(object sender, RoutedEventArgs e)
{
ConnectToServer();
}
private void ConnectToServer()
{
try
{
if (!ushort.TryParse(Port.Text, out var port))
return;
var host = IpAddress.Text;
var password = Password.Text;
_connectionManager.Port = port;
_connectionManager.Host = host;
_connectionManager.Password = password;
// _connectionManager.DisableHeartbeat = true;
// Attempt to connect to the GelSight server
_ = _connectionManager.ConnectAsync();
}
catch (Exception ex)
{
AddLine(ex.Message);
}
}
//
// Disconnect button click handler
//
private void Disconnect_OnClick(object sender, RoutedEventArgs e)
{
try
{
// Disconnect from the GelSight server
_connectionManager.Close();
}
catch (Exception ex)
{
AddLine(ex.Message);
}
}
//
// Clear messages button click handler
//
private void Clear_OnClick(object sender, RoutedEventArgs e)
{
MessageList.Items.Clear();
}
private bool SubscribeToLiveImages { get; set; }
private bool SubscribeToGelState { get; set; }
private string DeviceSerialNumber { get; set; } = string.Empty;
}
}