-
Notifications
You must be signed in to change notification settings - Fork 9
/
powerlevel_test_node_report.ex
68 lines (54 loc) · 2.03 KB
/
powerlevel_test_node_report.ex
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
defmodule Grizzly.ZWave.Commands.PowerlevelTestNodeReport do
@moduledoc """
This command is used to report the latest result of a test frame transmission started by the Powerlevel
Test Node Set Command.
Params:
* `:test_node_id` - The id of the node that received the test frames.
* `:test_frame_count` - The number of test frames transmitted to the test node
* `:status_of_operation` The result of the last test
"""
@behaviour Grizzly.ZWave.Command
alias Grizzly.ZWave.{Command, DecodeError}
alias Grizzly.ZWave.CommandClasses.Powerlevel
@type param ::
{:test_node_id, Grizzly.node_id()}
| {:status_of_operation, Powerlevel.status_of_operation()}
| {:test_frame_count, non_neg_integer()}
@impl true
@spec new([param()]) :: {:ok, Command.t()}
def new(params) do
command = %Command{
name: :powerlevel_test_node_report,
command_byte: 0x06,
command_class: Powerlevel,
params: params,
impl: __MODULE__
}
{:ok, command}
end
@impl true
@spec encode_params(Command.t()) :: binary()
def encode_params(command) do
test_node_id = Command.param!(command, :test_node_id)
status_of_operation_byte =
Command.param!(command, :status_of_operation) |> Powerlevel.status_of_operation_to_byte()
test_frame_count = Command.param!(command, :test_frame_count)
<<test_node_id, status_of_operation_byte, test_frame_count::size(16)>>
end
@impl true
@spec decode_params(binary()) :: {:ok, [param()]} | {:error, DecodeError.t()}
def decode_params(<<test_node_id, status_of_operation_byte, test_frame_count::size(16)>>) do
with {:ok, status_of_operation} <-
Powerlevel.status_of_operation_from_byte(status_of_operation_byte) do
{:ok,
[
test_node_id: test_node_id,
status_of_operation: status_of_operation,
test_frame_count: test_frame_count
]}
else
{:error, %DecodeError{} = error} ->
%DecodeError{error | command: :powerlevel_test_node_report}
end
end
end