Skip to content

Commit

Permalink
Remove cache (#10)
Browse files Browse the repository at this point in the history
* Remove cache

* Remove cache

* Network readiness

* {:error, :unable_to_get_nodes}
  • Loading branch information
mattludwigs authored Jul 11, 2019
1 parent 3def161 commit 1467037
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 525 deletions.
16 changes: 9 additions & 7 deletions lib/grizzly.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ defmodule Grizzly do
@doc """
List the nodes on the Z-Wave network
"""
@spec list_nodes() :: [Node.t()]
defdelegate list_nodes(), to: Grizzly.Network
@spec get_nodes() :: {:ok, [Node.t()]} | {:error, :unable_to_get_nodes}
defdelegate get_nodes(), to: Grizzly.Network

@doc """
Check to see if the network is busy
Expand Down Expand Up @@ -134,8 +134,8 @@ defmodule Grizzly do
@doc """
Update the command class version of a node
"""
@spec update_command_class_versions(Node.t(), :sync | :async) :: :ok
defdelegate update_command_class_versions(zw_node, mode \\ :async), to: Node
@spec update_command_class_versions(Node.t()) :: Node.t()
defdelegate update_command_class_versions(zw_node), to: Node

@doc """
Put the controller in learn mode for a few seconds
Expand All @@ -144,10 +144,12 @@ defmodule Grizzly do
defdelegate start_learn_mode(opts \\ []), to: Grizzly.Inclusion

@doc """
Get the version of a node's command class, possibly sticking to the already cached version
Get the version of a node's command class, if the node does not have a version for
this command class this function will try to get it from the Z-Wave network.
"""
@spec command_class_version(Node.t(), atom, boolean) :: {:ok, non_neg_integer} | {:error, atom}
defdelegate command_class_version(node, command_class_name, use_cached \\ false), to: Node
@spec get_command_class_version(Node.t(), atom) ::
{:ok, non_neg_integer} | {:error, atom}
defdelegate get_command_class_version(node, command_class_name), to: Node

@doc """
Whether a node has a given command class
Expand Down
9 changes: 8 additions & 1 deletion lib/grizzly/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Grizzly.Application do
{Grizzly.SeqNumber, Enum.random(0..255)},
{DynamicSupervisor, name: Grizzly.Conn.Supervisor, strategy: :one_for_one},
{Grizzly.UnsolicitedServer, unsolicited_server_config()},
Grizzly.Controller.Supervisor,
{Grizzly.Controller, get_grizzly_config()},
Grizzly.UnsolicitedServer.Socket.Supervisor,
Grizzly.Inclusion,
Grizzly.Network.State
Expand Down Expand Up @@ -48,4 +48,11 @@ defmodule Grizzly.Application do
children_list
end
end

defp get_grizzly_config() do
case Application.get_env(:grizzly, Grizzly.Controller) do
nil -> Grizzly.config()
opts -> Grizzly.Conn.Config.new(opts)
end
end
end
18 changes: 10 additions & 8 deletions lib/grizzly/command_class.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ defmodule Grizzly.CommandClass do
Identifies a command class by name and version
"""

alias __MODULE__

@type version :: non_neg_integer | :no_version_number
@type name :: atom
@type t :: %__MODULE__{name: name, version: version}
Expand All @@ -16,17 +14,21 @@ defmodule Grizzly.CommandClass do
struct(__MODULE__, opts)
end

@spec name(t()) :: name()
def name(%__MODULE__{name: name}), do: name

@spec version(t()) :: version()
def version(%__MODULE__{version: version}), do: version

@spec versioned?(t) :: boolean
def versioned?(%CommandClass{version: version}) do
def versioned?(%__MODULE__{version: version}) do
version != :no_version_number
end

@spec set_version(t, non_neg_integer | :no_version_number) :: t
def set_version(%CommandClass{} = command_class, version) when is_integer(version) do
%CommandClass{command_class | version: version}
end
def set_version(%__MODULE__{version: version} = command_class, version), do: command_class

def set_version(%CommandClass{} = command_class, :no_version_number) do
command_class
def set_version(%__MODULE__{} = command_class, version) when is_integer(version) do
%__MODULE__{command_class | version: version}
end
end
2 changes: 1 addition & 1 deletion lib/grizzly/command_class/command_class_version/get.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule Grizzly.CommandClass.CommandClassVersion.Get do
@spec handle_response(t, Packet.t()) ::
{:continue, t}
| {:done, {:error, :nack_response}}
| {:done, non_neg_integer}
| {:done, {:ok, non_neg_integer}}
| {:retry, t}
def handle_response(%__MODULE__{seq_number: seq_number} = command, %Packet{
seq_number: seq_number,
Expand Down
2 changes: 2 additions & 0 deletions lib/grizzly/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule Grizzly.Controller do
alias Grizzly
alias Grizzly.{Conn, Notifications}
alias Grizzly.Conn.Config
alias Grizzly.Network.State, as: NetworkState

defmodule State do
@moduledoc false
Expand Down Expand Up @@ -73,6 +74,7 @@ defmodule Grizzly.Controller do
{:connection_established, %Config{ip: ip}},
%State{conn: %Conn{ip_address: ip}} = state
) do
NetworkState.set(:idle)
Notifications.broadcast(:controller_connected)
{:noreply, state}
end
Expand Down
24 changes: 0 additions & 24 deletions lib/grizzly/controller/supervisor.ex

This file was deleted.

103 changes: 86 additions & 17 deletions lib/grizzly/network.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@ defmodule Grizzly.Network do
@moduledoc """
Module for working with the Z-Wave network
"""
alias Grizzly.Network.Server
alias Grizzly.Node
require Logger

alias Grizzly.{Node, SeqNumber, Controller}
alias Grizzly.Network.State, as: NetworkState

@doc """
List the nodes that known to the network
"""
@spec list_nodes() :: [Node.t()]
def list_nodes() do
Server.all()
end
alias Grizzly.CommandClass.NetworkManagementProxy.{
NodeListGet,
NodeInfoCache
}

alias Grizzly.CommandClass.NetworkManagementBasic.DefaultSet

@doc """
Reset the network
"""
@spec reset() :: :ok | {:error, :network_busy}
def reset() do
Server.reset()
end
seq_number = SeqNumber.get_and_inc()

@doc """
Get nodes from network
"""
@spec get_node(Node.node_id()) :: {:ok, Node.t()} | {:error, :node_not_found}
def get_node(node_id) do
Server.get_node(node_id)
Grizzly.send_command(
Controller.conn(),
DefaultSet,
seq_number: seq_number
)
end

@doc """
Expand Down Expand Up @@ -61,4 +59,75 @@ defmodule Grizzly.Network do
def set_state(state) do
NetworkState.set(state)
end

@doc """
Get a list of nodes from the network
"""
@spec get_nodes() :: {:ok, [Node.t()]} | {:error, :unable_to_get_node_list}
def get_nodes() do
seq_number = SeqNumber.get_and_inc()

node_list =
Grizzly.send_command(
Controller.conn(),
NodeListGet,
seq_number: seq_number
)

case node_list do
{:ok, node_list} ->
nodes =
node_list
|> Enum.filter(&(&1 != 1))
|> Enum.map(fn node_id ->
case get_node(node_id) do
{:ok, zw_node} ->
zw_node

{:error, reason} ->
_ = Logger.warn("Error getting node info for #{node_id} reason: #{reason}")
Node.new(id: node_id)
end
end)

{:ok, nodes}

{:error, _} ->
{:error, :unable_to_get_node_list}
end
end

@doc """
Get a Node from the Z-Wave network
"""
@spec get_node(Node.node_id()) :: {:ok, Node.t()} | {:error, :nack_response}
def get_node(node_id) do
with {:ok, node_info} <- get_node_info(node_id),
{:ok, ip_address} <- Node.get_ip(node_id) do
zw_node =
node_info
|> Map.put(:id, node_id)
|> Map.put(:ip_address, ip_address)
|> Map.to_list()
|> Node.new()

{:ok, zw_node}
else
{:error, _} = error ->
error
end
end

@spec get_node_info(Node.node_id()) :: {:ok, map()} | {:error, :nack_response}
def get_node_info(node_id) do
seq_number = SeqNumber.get_and_inc()

Grizzly.send_command(
Controller.conn(),
NodeInfoCache,
cached_minutes_passed: 0x01,
node_id: node_id,
seq_number: seq_number
)
end
end
54 changes: 0 additions & 54 deletions lib/grizzly/network/commands.ex

This file was deleted.

Loading

0 comments on commit 1467037

Please sign in to comment.