-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBasicCommandClass.cs
More file actions
187 lines (156 loc) · 5.73 KB
/
BasicCommandClass.cs
File metadata and controls
187 lines (156 loc) · 5.73 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
using Microsoft.Extensions.Logging;
namespace ZWave.CommandClasses;
public enum BasicCommand : byte
{
/// <summary>
/// Set a value in a supporting device
/// </summary>
Set = 0x01,
/// <summary>
/// Request the status of a supporting device
/// </summary>
Get = 0x02,
/// <summary>
/// Advertise the status of the primary functionality of the device.
/// </summary>
Report = 0x03,
}
/// <summary>
/// Represents a Basic Report received from a device.
/// </summary>
public readonly record struct BasicReport(
/// <summary>
/// The current value of the device hardware
/// </summary>
GenericValue CurrentValue,
/// <summary>
/// The target value of an ongoing transition or the most recent transition.
/// </summary>
GenericValue? TargetValue,
/// <summary>
/// The time needed to reach the Target Value at the actual transition rate.
/// </summary>
DurationReport? Duration);
[CommandClass(CommandClassId.Basic)]
public sealed class BasicCommandClass : CommandClass<BasicCommand>
{
internal BasicCommandClass(
CommandClassInfo info,
IDriver driver,
IEndpoint endpoint,
ILogger logger)
: base(info, driver, endpoint, logger)
{
}
/// <summary>
/// Gets the last report received from the device.
/// </summary>
public BasicReport? LastReport { get; private set; }
/// <summary>
/// Event raised when a Basic Report is received, both solicited and unsolicited.
/// </summary>
public event Action<BasicReport>? OnBasicReportReceived;
/// <inheritdoc />
public override bool? IsCommandSupported(BasicCommand command)
=> command switch
{
BasicCommand.Set => true,
BasicCommand.Get => true,
_ => false,
};
internal override async Task InterviewAsync(CancellationToken cancellationToken)
{
_ = await GetAsync(cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Request the status of a supporting device
/// </summary>
public async Task<BasicReport> GetAsync(CancellationToken cancellationToken)
{
BasicGetCommand command = BasicGetCommand.Create();
await SendCommandAsync(command, cancellationToken).ConfigureAwait(false);
CommandClassFrame reportFrame = await AwaitNextReportAsync<BasicReportCommand>(cancellationToken).ConfigureAwait(false);
BasicReport report = BasicReportCommand.Parse(reportFrame, Logger);
LastReport = report;
OnBasicReportReceived?.Invoke(report);
return report;
}
/// <summary>
/// Set a value in a supporting device
/// </summary>
public async Task SetAsync(GenericValue targetValue, CancellationToken cancellationToken)
{
var command = BasicSetCommand.Create(targetValue);
await SendCommandAsync(command, cancellationToken).ConfigureAwait(false);
}
protected override void ProcessUnsolicitedCommand(CommandClassFrame frame)
{
switch ((BasicCommand)frame.CommandId)
{
case BasicCommand.Report:
{
BasicReport report = BasicReportCommand.Parse(frame, Logger);
LastReport = report;
OnBasicReportReceived?.Invoke(report);
break;
}
}
}
internal readonly struct BasicSetCommand : ICommand
{
public BasicSetCommand(CommandClassFrame frame)
{
Frame = frame;
}
public static CommandClassId CommandClassId => CommandClassId.Basic;
public static byte CommandId => (byte)BasicCommand.Set;
public CommandClassFrame Frame { get; }
public static BasicSetCommand Create(GenericValue value)
{
ReadOnlySpan<byte> commandParameters = [value.Value];
CommandClassFrame frame = CommandClassFrame.Create(CommandClassId, CommandId, commandParameters);
return new BasicSetCommand(frame);
}
}
internal readonly struct BasicGetCommand : ICommand
{
public BasicGetCommand(CommandClassFrame frame)
{
Frame = frame;
}
public static CommandClassId CommandClassId => CommandClassId.Basic;
public static byte CommandId => (byte)BasicCommand.Get;
public CommandClassFrame Frame { get; }
public static BasicGetCommand Create()
{
CommandClassFrame frame = CommandClassFrame.Create(CommandClassId, CommandId);
return new BasicGetCommand(frame);
}
}
internal readonly struct BasicReportCommand : ICommand
{
public BasicReportCommand(CommandClassFrame frame)
{
Frame = frame;
}
public static CommandClassId CommandClassId => CommandClassId.Basic;
public static byte CommandId => (byte)BasicCommand.Report;
public CommandClassFrame Frame { get; }
public static BasicReport Parse(CommandClassFrame frame, ILogger logger)
{
if (frame.CommandParameters.Length < 1)
{
logger.LogWarning("Basic Report frame is too short ({Length} bytes)", frame.CommandParameters.Length);
ZWaveException.Throw(ZWaveErrorCode.InvalidPayload, "Basic Report frame is too short");
}
GenericValue currentValue = frame.CommandParameters.Span[0];
GenericValue? targetValue = frame.CommandParameters.Length > 1
? frame.CommandParameters.Span[1]
: null;
DurationReport? duration = frame.CommandParameters.Length > 2
? frame.CommandParameters.Span[2]
: null;
return new BasicReport(currentValue, targetValue, duration);
}
}
}