Repository navigation
Expand file tree
/
Copy pathSessionViewModel.cs
More file actions
595 lines (498 loc) · 15.4 KB
/
Copy pathSessionViewModel.cs
File metadata and controls
595 lines (498 loc) · 15.4 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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using KdrEnet.Services;
namespace KdrEnet;
public sealed class SessionViewModel : INotifyPropertyChanged
{
private string _techIp = "127.0.0.1";
private string _techName = "This laptop";
private string _techExtra = "";
private string _techHint = "The other person types this code. Radmin is not used.";
private string _sessionCode = "—";
private string _codeInput = "";
private string _radminLine = "";
private string _nextTitle = "Do this now";
private string _nextDetail = "Plug the cable into the laptop that is with the car.";
private string _statusLine = "Checking the cable";
private string _lastLine = "";
private bool _lastLineAlert;
private string _versionText = "1.0";
private bool _isBusy;
private bool _sessionOn;
private bool _isCarSide;
private bool _relayReady;
private string? _vehicleIp;
public SessionViewModel()
{
Cards = new[] { Cable, Power, Car, Session };
}
public event PropertyChangedEventHandler? PropertyChanged;
public CardModel Cable { get; } = new("Cable", "Checking");
public CardModel Power { get; } = new("Power", "Checking");
public CardModel Car { get; } = new("Car", "Waiting");
public CardModel Session { get; } = new("Session", "Off");
public IReadOnlyList<CardModel> Cards { get; }
public ObservableCollection<LogLine> Lines { get; } = new();
public string TechIp
{
get => _techIp;
set => Set(ref _techIp, value);
}
public string TechName
{
get => _techName;
set => Set(ref _techName, value);
}
public string TechExtra
{
get => _techExtra;
set => Set(ref _techExtra, value);
}
public string TechHint
{
get => _techHint;
set => Set(ref _techHint, value);
}
public string SessionCode
{
get => _sessionCode;
set
{
if (!Set(ref _sessionCode, value))
return;
NotifyCommands();
}
}
public string CodeInput
{
get => _codeInput;
set
{
var digits = SessionLink.Digits(value);
if (digits.Length > 6)
digits = digits[..6];
if (digits == _codeInput)
return;
_identifyText = "";
_identifyTone = "missing";
Set(ref _codeInput, digits);
NotifyCommands();
}
}
public bool IsCarSide
{
get => _isCarSide;
set
{
if (!Set(ref _isCarSide, value))
return;
NotifyCommands();
}
}
public bool IsTechSide => !IsCarSide;
public bool RelayReady
{
get => _relayReady;
set
{
if (!Set(ref _relayReady, value))
return;
NotifyCommands();
}
}
public string SideSwitchLabel => IsCarSide ? "This laptop has E-Sys" : "This laptop has the car";
public string RadminLine
{
get => _radminLine;
set => Set(ref _radminLine, value);
}
public string NextTitle
{
get => _nextTitle;
set => Set(ref _nextTitle, value);
}
public string NextDetail
{
get => _nextDetail;
set => Set(ref _nextDetail, value);
}
private string _checkWord = "Looking for the cable.";
private string _checkTone = "missing";
public string CheckWord
{
get => _checkWord;
set
{
if (!Set(ref _checkWord, value))
return;
OnPropertyChanged(nameof(ShowCheck));
}
}
public string CheckTone
{
get => _checkTone;
set => Set(ref _checkTone, value);
}
public bool ShowCheck => CheckWord.Length > 0;
private string _pingText = "Measuring the server…";
private string _pingTone = "missing";
public string PingText
{
get => _pingText;
set => Set(ref _pingText, value);
}
public string PingTone
{
get => _pingTone;
set => Set(ref _pingTone, value);
}
public void NotePing(int? milliseconds)
{
if (milliseconds is null)
{
PingText = "The server did not answer";
PingTone = "wrong";
OnPropertyChanged(nameof(PingIsFar));
OnPropertyChanged(nameof(ShowOpenRadmin));
return;
}
PingText = milliseconds + " ms to the server";
PingTone = milliseconds >= 200 ? "wrong" : "good";
OnPropertyChanged(nameof(PingIsFar));
OnPropertyChanged(nameof(ShowOpenRadmin));
}
public bool PingIsFar => PingTone == "wrong" && PingText.Contains(" ms", StringComparison.Ordinal);
public bool ShowOpenRadmin => UseRadmin || PingIsFar;
private bool _useRadmin;
private string _radminAddress = "";
public bool UseRadmin
{
get => _useRadmin;
set
{
if (!Set(ref _useRadmin, value))
return;
NotifyCommands();
}
}
public string RadminAddress
{
get => _radminAddress;
set
{
if (!Set(ref _radminAddress, value))
return;
OnPropertyChanged(nameof(RadminReadout));
NotifyCommands();
}
}
public string RadminReadout => string.IsNullOrEmpty(RadminAddress)
? "Waiting for Radmin VPN"
: "tcp://" + RadminAddress + ":6801";
public bool ShowCarCode => IsCarSide && !UseRadmin;
public bool ShowCarRadmin => IsCarSide && UseRadmin;
public bool ShowTechCode => !IsCarSide && !UseRadmin;
public bool ShowTechRadmin => !IsCarSide && UseRadmin;
public string StatusLine
{
get => _statusLine;
set => Set(ref _statusLine, value);
}
private string _vinLine = "";
private string _carFactsLine = "";
public string VinLine
{
get => _vinLine;
set
{
if (!Set(ref _vinLine, value))
return;
OnPropertyChanged(nameof(ShowVin));
}
}
public string CarFactsLine
{
get => _carFactsLine;
set
{
if (!Set(ref _carFactsLine, value))
return;
OnPropertyChanged(nameof(ShowCarFacts));
}
}
public bool ShowVin => IsCarSide && !SessionOn && VinLine.Length > 0;
public bool ShowCarFacts => IsCarSide && !SessionOn && CarFactsLine.Length > 0;
private string _versionLine = "";
public string VersionLine
{
get => _versionLine;
set
{
if (!Set(ref _versionLine, value))
return;
OnPropertyChanged(nameof(ShowVersion));
}
}
public bool ShowVersion => IsCarSide && !SessionOn && VersionLine.Length > 0;
public string PeerFrom { get; set; } = "";
private string _linkState = "Not connected";
private string _linkTone = "missing";
private string _linkHint = "";
private string _linkAddress = "";
public bool PeerLive { get; set; }
public string LinkState
{
get => _linkState;
set => Set(ref _linkState, value);
}
public string LinkTone
{
get => _linkTone;
set => Set(ref _linkTone, value);
}
public string LinkHint
{
get => _linkHint;
set
{
if (!Set(ref _linkHint, value))
return;
OnPropertyChanged(nameof(ShowLinkAddress));
}
}
public string LinkAddress
{
get => _linkAddress;
set
{
if (!Set(ref _linkAddress, value))
return;
OnPropertyChanged(nameof(ShowLinkAddress));
}
}
public bool ShowLinkAddress => LinkAddress.Length > 0;
private string _linkDetail = "";
public string LinkDetail
{
get => _linkDetail;
set
{
if (!Set(ref _linkDetail, value))
return;
OnPropertyChanged(nameof(ShowLinkDetail));
}
}
public bool ShowLinkDetail => IsCarSide && LinkDetail.Length > 0;
public string LastLine
{
get => _lastLine;
set => Set(ref _lastLine, value);
}
public bool LastLineAlert
{
get => _lastLineAlert;
set => Set(ref _lastLineAlert, value);
}
public string VersionText
{
get => _versionText;
set => Set(ref _versionText, value);
}
public bool CanCopy => UseRadmin
? IsCarSide && !IsBusy && !string.IsNullOrEmpty(RadminAddress)
: IsCarSide ? SessionOn && SessionLink.Digits(SessionCode).Length == 6 : true;
public bool IsBusy
{
get => _isBusy;
set
{
if (!Set(ref _isBusy, value))
return;
NotifyCommands();
}
}
public bool SessionOn
{
get => _sessionOn;
set
{
if (!Set(ref _sessionOn, value))
return;
NotifyCommands();
}
}
public string? VehicleIp
{
get => _vehicleIp;
set
{
if (!Set(ref _vehicleIp, value))
return;
NotifyCommands();
}
}
public string StartLabel => IsBusy ? "Working…" : UseRadmin ? "Start" : "Get code";
public string NewCodeLabel => IsBusy ? "Working…" : "New code";
public bool CanNewCode => IsCarSide && !UseRadmin && !IsBusy && SessionOn && !string.IsNullOrEmpty(VehicleIp);
public string JoinLabel => IsBusy ? "Working…" : "Join";
public string CheckLabel => IsBusy ? "Identifying…" : "Check code";
private string _identifyText = "";
private string _identifyTone = "missing";
public string IdentifyText
{
get => _identifyText;
set => Set(ref _identifyText, value);
}
public string IdentifyTone
{
get => _identifyTone;
set => Set(ref _identifyTone, value);
}
public bool CanCheck => !UseRadmin && !IsBusy && !SessionOn && IsTechSide && RelayReady && SessionLink.Digits(CodeInput).Length == 6;
public string DisplayCode => SessionLink.Digits(SessionCode).Length == 6 ? SessionLink.FormatCode(SessionCode) : "—";
public bool CodeReady => SessionLink.Digits(SessionCode).Length == 6;
public string Shown0 => ShownAt(0);
public string Shown1 => ShownAt(1);
public string Shown2 => ShownAt(2);
public string Shown3 => ShownAt(3);
public string Shown4 => ShownAt(4);
public string Shown5 => ShownAt(5);
private string ShownAt(int index)
{
var digits = SessionLink.Digits(SessionCode);
return digits.Length == 6 ? digits[index].ToString() : "–";
}
public string CarHelp => SessionLink.Digits(SessionCode).Length == 6
? "Read this code to the E-Sys laptop. Leave this window open."
: "Click Get code. The number shows here. Read it to the E-Sys laptop.";
public string CopyLabel => UseRadmin || !IsCarSide ? "Copy address" : "Copy code";
public bool ShowRadminCopy => UseRadmin && IsCarSide && !string.IsNullOrEmpty(RadminAddress);
public bool ShowCopy => !UseRadmin && (!IsCarSide || SessionLink.Digits(SessionCode).Length == 6);
public bool CanStart => !IsBusy && !SessionOn && IsCarSide && !string.IsNullOrEmpty(VehicleIp);
public bool CanJoin => !UseRadmin && !IsBusy && !SessionOn && IsTechSide && RelayReady && SessionLink.Digits(CodeInput).Length == 6;
public bool CanSwitch => !IsBusy && !SessionOn;
public bool CanStop => !IsBusy && SessionOn;
private string _driverUrl = "";
private bool _showDriver;
public string DriverUrl
{
get => _driverUrl;
set => Set(ref _driverUrl, value);
}
public bool ShowDriver
{
get => _showDriver;
set => Set(ref _showDriver, value);
}
public void AddLog(string text, bool alert = false)
{
Lines.Add(new LogLine(text, alert));
while (Lines.Count > 200)
Lines.RemoveAt(0);
if (!alert)
return;
LastLine = text;
LastLineAlert = true;
}
public void NotifyCommands()
{
OnPropertyChanged(nameof(StartLabel));
OnPropertyChanged(nameof(NewCodeLabel));
OnPropertyChanged(nameof(JoinLabel));
OnPropertyChanged(nameof(CheckLabel));
OnPropertyChanged(nameof(DisplayCode));
OnPropertyChanged(nameof(CodeReady));
OnPropertyChanged(nameof(Shown0));
OnPropertyChanged(nameof(Shown1));
OnPropertyChanged(nameof(Shown2));
OnPropertyChanged(nameof(Shown3));
OnPropertyChanged(nameof(Shown4));
OnPropertyChanged(nameof(Shown5));
OnPropertyChanged(nameof(CarHelp));
OnPropertyChanged(nameof(CopyLabel));
OnPropertyChanged(nameof(ShowCopy));
OnPropertyChanged(nameof(CanStart));
OnPropertyChanged(nameof(CanNewCode));
OnPropertyChanged(nameof(CanJoin));
OnPropertyChanged(nameof(CanCheck));
OnPropertyChanged(nameof(CanStop));
OnPropertyChanged(nameof(CanSwitch));
OnPropertyChanged(nameof(CanCopy));
OnPropertyChanged(nameof(IsTechSide));
OnPropertyChanged(nameof(ShowCarCode));
OnPropertyChanged(nameof(ShowCarRadmin));
OnPropertyChanged(nameof(ShowTechCode));
OnPropertyChanged(nameof(ShowTechRadmin));
OnPropertyChanged(nameof(ShowVin));
OnPropertyChanged(nameof(ShowCarFacts));
OnPropertyChanged(nameof(ShowVersion));
OnPropertyChanged(nameof(ShowRadminCopy));
OnPropertyChanged(nameof(RadminReadout));
OnPropertyChanged(nameof(SideSwitchLabel));
OnPropertyChanged(nameof(ShowOpenRadmin));
}
private bool Set<T>(ref T field, T value, [CallerMemberName] string? name = null)
{
if (EqualityComparer<T>.Default.Equals(field, value))
return false;
field = value;
OnPropertyChanged(name);
return true;
}
private void OnPropertyChanged([CallerMemberName] string? name = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public sealed class CardModel : INotifyPropertyChanged
{
private string _detail;
private string _state = "wait";
public CardModel(string title, string detail)
{
Title = title;
_detail = detail;
}
public event PropertyChangedEventHandler? PropertyChanged;
public string Title { get; private set; }
public void Rename(string title)
{
if (Title == title)
return;
Title = title;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Title)));
}
public string Detail
{
get => _detail;
set
{
if (_detail == value)
return;
_detail = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Detail)));
}
}
public string State
{
get => _state;
set
{
if (_state == value)
return;
_state = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(State)));
}
}
}
public sealed class LogLine
{
public LogLine(string text, bool isAlert)
{
TimeText = DateTime.Now.ToString("HH:mm");
Text = text;
IsAlert = isAlert;
}
public string TimeText { get; }
public string Text { get; }
public bool IsAlert { get; }
}