-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseConfigureDeviceCommand.cs
66 lines (58 loc) · 3.04 KB
/
BaseConfigureDeviceCommand.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
using CK.Core;
using System;
using System.Diagnostics;
namespace CK.DeviceModel;
/// <summary>
/// Non-generic base for the generic <see cref="ConfigureDeviceCommand{THost,TConfiguration}"/> (that
/// is the only one that can be used).
/// It exposes a base <see cref="DeviceConfiguration"/> and is useful when configuration type
/// is not statically available.
/// </summary>
public abstract class BaseConfigureDeviceCommand : DeviceCommandWithResult<DeviceApplyConfigurationResult>
{
private protected BaseConfigureDeviceCommand( DeviceConfiguration configuration, (string lockedName, string? lockedControllerKey)? locked = null )
: base( locked )
{
Throw.CheckNotNullArgument( configuration );
ExternalConfiguration = configuration;
ImmediateSending = true;
ShouldCallDeviceOnCommandCompleted = false;
}
/// <summary>
/// Transforms cancellation into <see cref="DeviceApplyConfigurationResult.ConfigurationCanceled"/> result.
/// </summary>
/// <param name="result">The result setter.</param>
protected override sealed void OnCanceled( ref CompletionSource<DeviceApplyConfigurationResult>.OnCanceled result )
{
result.SetResult( DeviceApplyConfigurationResult.ConfigurationCanceled );
}
/// <summary>
/// Transforms InvalidControllerKeyException into <see cref="DeviceApplyConfigurationResult.InvalidControllerKey"/>
/// and any other error into <see cref="DeviceApplyConfigurationResult.UnexpectedError"/> result.
/// </summary>
/// <param name="ex">The exception.</param>
/// <param name="result">The result setter.</param>
protected override sealed void OnError( Exception ex, ref CompletionSource<DeviceApplyConfigurationResult>.OnError result )
{
if( ex is InvalidControllerKeyException ) result.SetResult( DeviceApplyConfigurationResult.InvalidControllerKey );
else result.SetResult( DeviceApplyConfigurationResult.UnexpectedError );
}
/// <summary>
/// Returns <see cref="DeviceCommandStoppedBehavior.RunAnyway"/>: the configuration can obviously be applied while the device is stopped.
/// Note that this is not used: basic commands are always run by design.
/// </summary>
protected internal override sealed DeviceCommandStoppedBehavior StoppedBehavior => DeviceCommandStoppedBehavior.RunAnyway;
/// <summary>
/// Returns <see cref="DeviceImmediateCommandStoppedBehavior.RunAnyway"/>: the configuration can obviously be applied while the device is stopped.
/// Note that this is not used: basic commands are always run by design.
/// </summary>
protected internal override sealed DeviceImmediateCommandStoppedBehavior ImmediateStoppedBehavior => DeviceImmediateCommandStoppedBehavior.RunAnyway;
/// <summary>
/// Gets the configuration to apply.
/// <para>
/// This MUST not be changed once the command is sent (<see cref="BaseDeviceCommand.IsLocked"/> is true)
/// otherwise kitten will die.
/// </para>
/// </summary>
public DeviceConfiguration ExternalConfiguration { get; }
}