HamBook – Diff between revs 9 and 11

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 9 Rev 11
Line 27... Line 27...
27 } 27 }
Line 28... Line 28...
28   28  
29 public override async Task<bool> SetAsync(PowerState state, CancellationToken cancellationToken) 29 public override async Task<bool> SetAsync(PowerState state, CancellationToken cancellationToken)
30 { 30 {
-   31 var taskCompletionSource = new TaskCompletionSource<bool>();
-   32  
-   33 var id = new ID(SerialPort);
-   34 try
-   35 {
-   36 await id.ReadAsync(cancellationToken);
-   37 }
-   38 catch
-   39 {
-   40  
-   41 }
31 var taskCompletionSource = new TaskCompletionSource<bool>(); 42  
32 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 43 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
33 Task.Delay(Constants.POWER_ON_DELAY).ContinueWith(ca => 44 Task.Delay(POWER_ON_DELAY).ContinueWith(async task1 =>
-   45 {
34 { 46 byte[] payload = null;
35 switch(state) 47 switch(state)
36 { 48 {
37 case PowerState.ON: 49 case PowerState.ON:
-   50 payload = Constants.Encoding.GetBytes($"{Name}1{Generic.Constants.EOT}");
38 SerialPort.Write($"{Name}1{Generic.Constants.EOT}"); 51 await SerialPort.WriteAsync(payload, 0, payload.Length);
39 Task.Delay(Constants.TIME_TO_WARM).ContinueWith(cb => 52 Task.Delay(TIME_TO_WARM).ContinueWith(task2 =>
40 { 53 {
41 taskCompletionSource.TrySetResult(true); 54 taskCompletionSource.TrySetResult(true);
42 }); 55 });
43 return; 56 return;
44 case PowerState.OFF: 57 case PowerState.OFF:
-   58 payload = Constants.Encoding.GetBytes($"{Name}0{Generic.Constants.EOT}");
45 SerialPort.Write($"{Name}0{Generic.Constants.EOT}"); 59 await SerialPort.WriteAsync(payload, 0, payload.Length);
46 break; 60 break;
47 } 61 }
48 taskCompletionSource.TrySetResult(true); 62 taskCompletionSource.TrySetResult(true);
49 }); 63 });
Line 50... Line -...
50 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed -  
51   64 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
52 SerialPort.Write($"POWER"); 65  
53 await taskCompletionSource.Task; 66 await taskCompletionSource.Task;
Line 54... Line 67...
54 return await ReadAsync(cancellationToken) == state; 67 return await ReadAsync(cancellationToken) == state;
55 } 68 }
56   69  
-   70 public override async Task<PowerState> ReadAsync(CancellationToken cancellationToken)
-   71 {
-   72 var taskCompletionSource = new TaskCompletionSource<int>();
-   73  
-   74 var id = new ID(SerialPort);
-   75 try
-   76 {
-   77 await id.ReadAsync(cancellationToken);
-   78 }
-   79 catch
-   80 {
57 public override async Task<PowerState> ReadAsync(CancellationToken cancellationToken) 81  
58 { 82 }
59 var taskCompletionSource = new TaskCompletionSource<int>(); 83  
60 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 84 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
-   85 Task.Delay(POWER_ON_DELAY).ContinueWith(async _ =>
61 Task.Delay(Constants.POWER_ON_DELAY).ContinueWith(_ => 86 {
62 { -  
63 SerialPort.Write($"{Name}{Generic.Constants.EOT}"); -  
Line -... Line 87...
-   87 var payload = Constants.Encoding.GetBytes($"{Name}{Generic.Constants.EOT}");
-   88  
-   89 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
-   90  
-   91 var buffer = new byte[CatLength.Answer];
-   92 if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer)
-   93 {
-   94 throw new UnexpectedRadioResponseException(Name, buffer);
-   95 }
-   96 var answer = Constants.Encoding.GetString(buffer);
-   97 var match = readRegex.Match(answer);
-   98 if (!match.Success)
-   99 {
64 var result = SerialPort.ReadTo($"{Generic.Constants.EOT}"); 100 throw new UnmatchedRadioResponseException(Name, answer);
-   101 }
65 var match = readRegex.Match(result); 102  
66 var state = int.Parse(match.Result("${state}")); 103 var state = int.Parse(match.Result("${state}"));
67   -  
68 taskCompletionSource.TrySetResult(state); -  
69 }); 104 taskCompletionSource.TrySetResult(state);
70 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 105  
71   106 });
72 SerialPort.Write($"POWER"); 107 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
73 var onoff = await taskCompletionSource.Task; 108 var onoff = await taskCompletionSource.Task;