clean up warnings, fix tests.

pull/301/head
connor rigby 2017-05-04 07:03:29 -07:00
parent 1b8a09b96e
commit fc91829b0f
6 changed files with 63 additions and 24 deletions

View File

@ -11,7 +11,7 @@
"lib/downloader.ex",
"lib/farmbot/sysformatter.ex",
"lib/logger/backends/farmbot_logger.ex",
"lib/farmbot/sync/syncable.ex"
"lib/farmbot/sync/syncable.ex",
]
}
}

View File

@ -3,6 +3,8 @@
"skip_files": [
"lib/mix",
"lib/farmbot/system/",
"lib/farmbot/easter_eggs.ex"
"lib/farmbot/easter_eggs.ex",
"lib/farmbot/debug_log.ex",
"lib/farmbot/debug_log"
]
}

View File

@ -52,7 +52,6 @@ defmodule Farmbot do
supervise(children, opts)
end
# This has to be at runtime because you cant access your own apps
# priv dir during Mix.Config.
if Mix.env == :prod do
@ -73,7 +72,6 @@ defmodule Farmbot do
]
end
defp setup_nerves_fw do
Logger.info ">> Disabling firmware signing!"
Application.put_env(:nerves_firmware, :pub_key_path, nil)

View File

@ -4,7 +4,7 @@ Provides a `debug_log/1` function.
"""
use_logger? = fn() ->
if Mix.env() == :dev do
if Mix.env() == :dev || Mix.env() == :test do
(System.get_env("DEBUG_LOG") || false)
else
Mix.shell.info "Not allowing `DebugLog` in `:prod` build."

View File

@ -23,7 +23,33 @@ defmodule Farmbot.Serial.Handler do
"""
@type nerves :: handler
@type state :: {:hey, :fixme}
@typedoc """
Status of the arduino
"""
@type status :: :busy | :done
@typedoc """
State for this GenServer
"""
@type state :: %{
nerves: nerves,
tty: binary,
current: current,
timeouts: integer,
status: status,
initialized: boolean
}
@typedoc """
The current message being handled
"""
@type current :: %{
timer: reference,
reply: tuple,
status: status,
from: {pid, reference},
q: binary
} | nil
@default_timeout_ms 10_000
@max_timeouts 5
@ -213,23 +239,11 @@ defmodule Farmbot.Serial.Handler do
results = handle_gcode(parsed)
debug_log "Handling results: #{inspect results}"
case results do
{:status, :done} ->
debug_log "replying to #{inspect current.from} with: #{inspect current.reply}"
GenServer.reply(current.from, current.reply)
Process.cancel_timer(current.timer)
nil
{:status, :busy} ->
debug_log "refreshing timer."
Process.cancel_timer(current.timer)
timer = Process.send_after(self(), :timeout, 5000)
%{current | status: :busy, timer: timer}
{:status, :done} -> handle_done(current)
{:status, :busy} -> handle_busy(current)
{:status, status} -> %{current | status: status}
{:reply, reply} -> %{current | reply: reply}
thing ->
unless is_nil(thing) do
debug_log "Unexpected thing: #{inspect thing}"
end
current
thing -> handle_other(thing, current)
end
end
@ -238,6 +252,28 @@ defmodule Farmbot.Serial.Handler do
nil
end
@spec handle_busy(current) :: current
defp handle_busy(current) do
debug_log "refreshing timer."
Process.cancel_timer(current.timer)
timer = Process.send_after(self(), :timeout, 5000)
%{current | status: :busy, timer: timer}
end
defp handle_other(thing, current) do
unless is_nil(thing) do
debug_log "Unexpected thing: #{inspect thing}"
end
current
end
defp handle_done(current) do
debug_log "replying to #{inspect current.from} with: #{inspect current.reply}"
GenServer.reply(current.from, current.reply)
Process.cancel_timer(current.timer)
nil
end
@spec generate_handshake :: binary
defp generate_handshake, do: "Q#{:rand.uniform(99)}"

View File

@ -46,12 +46,15 @@ defmodule Farmbot.Serial.Supervisor do
# If running in the host environment the proper tty is expected to be in
# the environment
@tty System.get_env("ARDUINO_TTY") || Application.get_env(:farmbot, :tty)
defp get_tty do
System.get_env("ARDUINO_TTY") || Application.get_env(:farmbot, :tty)
end
@spec open_ttys(atom | pid, [binary]) :: :ok | no_return
def open_ttys(supervisor, list \\ nil)
def open_ttys(supervisor, _) do
if @tty do
thing = {@tty, [name: Farmbot.Serial.Handler]}
if get_tty() do
thing = {get_tty(), [name: Farmbot.Serial.Handler]}
try_open([thing], supervisor)
else
Logger.warn ">> EXPORT ARDUINO_TTY to initialize arduino in Host mode"