-
Notifications
You must be signed in to change notification settings - Fork 9
/
statistics_report.ex
58 lines (45 loc) · 1.65 KB
/
statistics_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
defmodule Grizzly.ZWave.Commands.StatisticsReport do
@moduledoc """
This command is used to query Installation and Maintenance statistics from a node.
Params:
* `:node_id` - the NodeID for which statistics are requested
* `:statistics` - statistics collected about the node
"""
@behaviour Grizzly.ZWave.Command
alias Grizzly.ZWave.{Command, DecodeError}
alias Grizzly.ZWave.CommandClasses.NetworkManagementInstallationMaintenance
@type param ::
{:node_id, byte} | {:statistics, NetworkManagementInstallationMaintenance.statistics()}
@impl true
@spec new([param()]) :: {:ok, Command.t()}
def new(params) do
command = %Command{
name: :statistics_report,
command_byte: 0x05,
command_class: NetworkManagementInstallationMaintenance,
params: params,
impl: __MODULE__
}
{:ok, command}
end
@impl true
@spec encode_params(Command.t()) :: binary()
def encode_params(command) do
node_id = Command.param!(command, :node_id)
statistics_binary =
Command.param!(command, :statistics)
|> NetworkManagementInstallationMaintenance.statistics_to_binary()
<<node_id>> <> statistics_binary
end
@impl true
@spec decode_params(binary()) :: {:ok, [param()]} | {:error, DecodeError.t()}
def decode_params(<<node_id, statistics_binary::binary>>) do
with {:ok, statistics} <-
NetworkManagementInstallationMaintenance.statistics_from_binary(statistics_binary) do
{:ok, [node_id: node_id, statistics: statistics]}
else
{:error, %DecodeError{} = decode_error} ->
{:error, %DecodeError{decode_error | command: :statistics_report}}
end
end
end