Update led workers

pull/576/head
connor rigby 2018-07-10 14:48:16 -07:00 committed by Connor Rigby
parent 93c324d151
commit 40a2c4b00c
16 changed files with 91 additions and 211 deletions

View File

@ -5,7 +5,4 @@ known_formatted_files =
|> Enum.map(&String.trim(&1))
|> List.delete("")
[
inputs: ["mix.exs", ".formatter.exs"] ++ known_formatted_files,
line_length: 80
]
[inputs: ["mix.exs", ".formatter.exs"] ++ known_formatted_files]

View File

@ -20,6 +20,8 @@ config :farmbot, ecto_repos: [Farmbot.Repo, Farmbot.System.ConfigStorage]
# Configure your our init system.
config :farmbot, :init, [
Farmbot.Target.Leds.AleHandler,
# Autodetects if a Arduino is plugged in and configures accordingly.
Farmbot.Firmware.UartHandler.AutoDetector,
@ -44,8 +46,6 @@ config :farmbot, :init, [
# Debug stuff
Farmbot.System.Debug,
Farmbot.Target.Uevent.Supervisor,
Farmbot.Target.Leds.AleHandler
]
config :farmbot, :transport, [

View File

@ -20,6 +20,8 @@ config :farmbot, ecto_repos: [Farmbot.Repo, Farmbot.System.ConfigStorage]
# Configure your our init system.
config :farmbot, :init, [
Farmbot.Target.Leds.AleHandler,
# Autodetects if a Arduino is plugged in and configures accordingly.
Farmbot.Firmware.UartHandler.AutoDetector,
@ -42,8 +44,6 @@ config :farmbot, :init, [
# Helps with hot plugging of serial devices.
Farmbot.Target.Uevent.Supervisor,
Farmbot.Target.Leds.AleHandler
]
config :farmbot, :transport, [

View File

@ -72,8 +72,7 @@ defmodule Farmbot.Asset.FarmEvent do
|> elem(1)
|> DateTime.to_unix(:second)
end_time_seconds =
DateTime.from_iso8601(fe.end_time) |> elem(1) |> DateTime.to_unix(:second)
end_time_seconds = DateTime.from_iso8601(fe.end_time) |> elem(1) |> DateTime.to_unix(:second)
repeat = fe.repeat
repeat_frequency_seconds = time_unit_to_seconds(fe.time_unit)

View File

@ -1,31 +1,24 @@
defmodule Farmbot.BotState.LedWorker do
@moduledoc "Flashes leds based on connection status."
use GenServer
alias Farmbot.System.Registry, as: FBR
def start_link(args \\ []) do
GenServer.start_link(__MODULE__, args, name: __MODULE__)
end
def init([]) do
Farmbot.System.Registry.subscribe(self())
FBR.subscribe(self())
Farmbot.Leds.blue(:off)
{:ok, %{}}
end
def handle_info(
:bot_state,
%{informational_settings: %{connected: true}},
state
) do
def handle_info({FBR, {:bot_state, %{informational_settings: %{connected: true}}}}, state) do
Farmbot.Leds.blue(:solid)
{:noreply, state}
end
def handle_info(
:bot_state,
%{informational_settings: %{connected: false}},
state
) do
def handle_info({FBR, {:bot_state, %{informational_settings: %{connected: false}}}}, state) do
Farmbot.Leds.blue(:off)
{:noreply, state}
end

View File

@ -166,10 +166,7 @@ defmodule Farmbot.FarmEvent.Manager do
schedule_events(late_executables, now)
end
exit(
{:success,
%{new | events: Map.new(all_events, fn event -> {event.id, event} end)}}
)
exit({:success, %{new | events: Map.new(all_events, fn event -> {event.id, event} end)}})
end
defp do_checkup(list, time, late_events \\ [], state)
@ -184,8 +181,7 @@ defmodule Farmbot.FarmEvent.Manager do
# update state.
new_state = %{
state
| last_time_index:
Map.put(state.last_time_index, farm_event.id, last_time)
| last_time_index: Map.put(state.last_time_index, farm_event.id, last_time)
}
case new_late_executable do
@ -267,9 +263,7 @@ defmodule Farmbot.FarmEvent.Manager do
event,
_now
) do
maybe_farm_event_log(
"regimen #{event.name} (#{event.id}) should already be scheduled."
)
maybe_farm_event_log("regimen #{event.name} (#{event.id}) should already be scheduled.")
{nil, last_time}
end
@ -282,9 +276,9 @@ defmodule Farmbot.FarmEvent.Manager do
_
) do
maybe_farm_event_log(
"regimen #{event.name} (#{event.id}) is not scheduled yet. (#{
inspect(schedule_time)
}) (#{inspect(Timex.now())})"
"regimen #{event.name} (#{event.id}) is not scheduled yet. (#{inspect(schedule_time)}) (#{
inspect(Timex.now())
})"
)
{nil, last_time}
@ -323,8 +317,7 @@ defmodule Farmbot.FarmEvent.Manager do
event,
now
) do
{run?, next_time} =
should_run_sequence?(farm_event.calendar, last_time, now)
{run?, next_time} = should_run_sequence?(farm_event.calendar, last_time, now)
case run? do
true -> {event, next_time}
@ -359,9 +352,7 @@ defmodule Farmbot.FarmEvent.Manager do
event,
_now
) do
maybe_farm_event_log(
"sequence #{event.name} (#{event.id}) is not scheduled yet."
)
maybe_farm_event_log("sequence #{event.name} (#{event.id}) is not scheduled yet.")
{nil, last_time}
end
@ -384,9 +375,7 @@ defmodule Farmbot.FarmEvent.Manager do
# if there is no last time, check if time is passed now within 60 seconds.
defp should_run_sequence?([first_time | _], nil, now) do
maybe_farm_event_log(
"Checking sequence event that hasn't run before #{first_time}"
)
maybe_farm_event_log("Checking sequence event that hasn't run before #{first_time}")
# convert the first_time to a DateTime
dt = Timex.parse!(first_time, "{ISO:Extended}")

View File

@ -1,27 +1,24 @@
defmodule Farmbot.Firmware.LedWorker do
@moduledoc "Flashes leds based on e-stop status."
use GenServer
alias Farmbot.System.Registry, as: FBR
def start_link(args \\ []) do
GenServer.start_link(__MODULE__, args, name: __MODULE__)
end
def init([]) do
Farmbot.System.Registry.subscribe(self())
FBR.subscribe(self())
Farmbot.Leds.red(:solid)
{:ok, %{}}
end
def handle_info(:bot_state, %{informational_settings: %{locked: true}}, state) do
def handle_info({FBR, {:bot_state, %{informational_settings: %{locked: true}}}}, state) do
Farmbot.Leds.yellow(:slow_blink)
{:noreply, state}
end
def handle_info(
:bot_state,
%{informational_settings: %{locked: false}},
state
) do
def handle_info({FBR, {:bot_state, %{informational_settings: %{locked: false}}}}, state) do
Farmbot.Leds.yellow(:off)
{:noreply, state}
end

View File

@ -10,4 +10,7 @@ defmodule Farmbot.Leds.Handler do
@callback yellow(status) :: any
@callback white1(status) :: any
@callback white2(status) :: any
@callback white3(status) :: any
@callback white4(status) :: any
@callback white5(status) :: any
end

View File

@ -5,27 +5,13 @@ defmodule Farmbot.Leds do
@valid_status [:off, :solid, :slow_blink, :fast_blink]
def red(status) when status in @valid_status do
@led_handler.red(status)
end
def blue(status) when status in @valid_status do
@led_handler.blue(status)
end
def green(status) when status in @valid_status do
@led_handler.green(status)
end
def yellow(status) when status in @valid_status do
@led_handler.yellow(status)
end
def white1(status) when status in @valid_status do
@led_handler.white1(status)
end
def white2(status) when status in @valid_status do
@led_handler.white2(status)
end
def red(status) when status in @valid_status, do: @led_handler.red(status)
def blue(status) when status in @valid_status, do: @led_handler.blue(status)
def green(status) when status in @valid_status, do: @led_handler.green(status)
def yellow(status) when status in @valid_status, do: @led_handler.yellow(status)
def white1(status) when status in @valid_status, do: @led_handler.white1(status)
def white2(status) when status in @valid_status, do: @led_handler.white2(status)
def white3(status) when status in @valid_status, do: @led_handler.white3(status)
def white4(status) when status in @valid_status, do: @led_handler.white4(status)
def white5(status) when status in @valid_status, do: @led_handler.white5(status)
end

View File

@ -4,39 +4,25 @@ defmodule Farmbot.Leds.StubHandler do
@behaviour Farmbot.Leds.Handler
def red(status) do
do_debug(:red, status)
end
def blue(status) do
do_debug(:blue, status)
end
def green(status) do
do_debug(:green, status)
end
def yellow(status) do
do_debug(:yellow, status)
end
def white1(status) do
do_debug(:white, status)
end
def white2(status) do
do_debug(:white, status)
end
def red(status), do: do_debug(:red, status)
def blue(status), do: do_debug(:blue, status)
def green(status), do: do_debug(:green, status)
def yellow(status), do: do_debug(:yellow, status)
def white1(status), do: do_debug(:white, status)
def white2(status), do: do_debug(:white, status)
def white3(status), do: do_debug(:white, status)
def white4(status), do: do_debug(:white, status)
def white5(status), do: do_debug(:white, status)
defp do_debug(color, status) do
msg = ["LED STATUS: ",
msg = [IO.ANSI.reset(), "LED STATUS: ",
apply(IO.ANSI, color, []),
status_in(status),
to_string(color),
" ",
to_string(status),
status_out(status),
IO.ANSI.normal()
IO.ANSI.reset()
]
IO.puts(msg)
end

View File

@ -1,48 +1,28 @@
defmodule Farmbot.Repo.LedWorker do
@moduledoc "Flashes leds based on sync status."
use GenServer
alias Farmbot.System.Registry, as: FBR
def start_link(args \\ []) do
GenServer.start_link(__MODULE__, args, name: __MODULE__)
end
def init([]) do
Farmbot.System.Registry.subscribe(self())
FBR.subscribe(self())
{:ok, %{}}
end
def handle_info(
:bot_state,
%{informational_settings: %{sync_status: :sync_now}},
state
) do
Farmbot.Leds.green(:slow_blink)
{:noreply, state}
end
def handle_info(
:bot_state,
%{informational_settings: %{sync_status: :syncing}},
state
) do
def handle_info({FBR, {:bot_state, %{informational_settings: %{sync_status: :syncing}}}}, state) do
Farmbot.Leds.green(:fast_blink)
{:noreply, state}
end
def handle_info(
:bot_state,
%{informational_settings: %{sync_status: :synced}},
state
) do
def handle_info({FBR, {:bot_state, %{informational_settings: %{sync_status: :synced}}}}, state) do
Farmbot.Leds.green(:solid)
{:noreply, state}
end
def handle_info(
:bot_state,
%{informational_settings: %{sync_status: :sync_error}},
state
) do
def handle_info({FBR, {:bot_state, %{informational_settings: %{sync_status: _}}}}, state) do
Farmbot.Leds.green(:slow_blink)
{:noreply, state}
end

View File

@ -70,8 +70,7 @@ defmodule Farmbot.Repo.Worker do
syncing: true
})}
else
{:ok,
struct(State, %{stability_timer: stability_timer, requires_full: true})}
{:ok, struct(State, %{stability_timer: stability_timer, requires_full: true})}
end
end
@ -100,8 +99,7 @@ defmodule Farmbot.Repo.Worker do
pid = spawn(Farmbot.Repo, :fragment_sync, [verbosity])
ref = Process.monitor(pid)
timer =
refresh_or_start_sync_timeout(state.sync_timer, timeout_ms, ref, self())
timer = refresh_or_start_sync_timeout(state.sync_timer, timeout_ms, ref, self())
set_sync_status(:syncing)
@ -124,8 +122,7 @@ defmodule Farmbot.Repo.Worker do
pid = spawn(Farmbot.Repo, :full_sync, [verbosity])
ref = Process.monitor(pid)
timer =
refresh_or_start_sync_timeout(state.sync_timer, timeout_ms, ref, self())
timer = refresh_or_start_sync_timeout(state.sync_timer, timeout_ms, ref, self())
set_sync_status(:syncing)
@ -173,8 +170,7 @@ defmodule Farmbot.Repo.Worker do
%{
state
| requires_full: true,
stability_timer:
refresh_or_start_stability_timeout(state.stability_timer, self())
stability_timer: refresh_or_start_stability_timeout(state.stability_timer, self())
}}
end

View File

@ -84,8 +84,7 @@ defmodule Farmbot.Mixfile do
case System.get_env("ERL_EI_INCLUDE_DIR") do
nil ->
%{
"ERL_EI_INCLUDE_DIR" =>
Path.join([:code.root_dir(), "usr", "include"]),
"ERL_EI_INCLUDE_DIR" => Path.join([:code.root_dir(), "usr", "include"]),
"ERL_EI_LIBDIR" => Path.join([:code.root_dir(), "usr", "lib"]),
"MIX_TARGET" => @target
}
@ -111,8 +110,7 @@ defmodule Farmbot.Mixfile do
{:nerves_leds, "~> 0.8"},
{:cowboy, "~> 2.0"},
{:plug, "~> 1.6"},
{:ranch_proxy_protocol,
github: "heroku/ranch_proxy_protocol", override: true},
{:ranch_proxy_protocol, github: "heroku/ranch_proxy_protocol", override: true},
{:cors_plug, "~> 1.5"},
{:rsa, "~> 0.0.1"},
{:joken, "~> 1.5"},
@ -148,8 +146,7 @@ defmodule Farmbot.Mixfile do
{:nerves_firmware, "~> 0.4"},
{:nerves_init_gadget, "~> 0.4.0", only: :dev},
{:nerves_network, "~> 0.3"},
{:nerves_wpa_supplicant,
github: "nerves-project/nerves_wpa_supplicant", override: true},
{:nerves_wpa_supplicant, github: "nerves-project/nerves_wpa_supplicant", override: true},
{:dhcp_server, "~> 0.4.0"},
{:elixir_ale, "~> 1.0"},
{:mdns, "~> 1.0"}

View File

@ -47,6 +47,7 @@ defmodule Farmbot.Target.Bootstrap.Configurator.CaptivePortal do
wpa_pid = wait_for_wpa()
Nerves.WpaSupplicant.request(wpa_pid, {:AP_SCAN, 2})
Farmbot.Leds.blue(:slow_blink)
{:ok, %{dhcp_server: dhcp_server, dnsmasq: dnsmasq}}
end

View File

@ -8,29 +8,15 @@ defmodule Farmbot.Target.Leds.AleHandler do
# @valid_status [:off, :solid, :slow_blink, :fast_blink]
@moduledoc false
def red(status) do
GenServer.call(__MODULE__, {:red, status})
end
def blue(status) do
GenServer.call(__MODULE__, {:blue, status})
end
def green(status) do
GenServer.call(__MODULE__, {:green, status})
end
def yellow(status) do
GenServer.call(__MODULE__, {:yellow, status})
end
def white1(status) do
GenServer.call(__MODULE__, {:white1, status})
end
def white2(status) do
GenServer.call(__MODULE__, {:white2, status})
end
def red(status), do: GenServer.call(__MODULE__, {:red, status})
def blue(status), do: GenServer.call(__MODULE__, {:blue, status})
def green(status), do: GenServer.call(__MODULE__, {:green, status})
def yellow(status), do: GenServer.call(__MODULE__, {:yellow, status})
def white1(status), do: GenServer.call(__MODULE__, {:white1, status})
def white2(status), do: GenServer.call(__MODULE__, {:white2, status})
def white3(status), do: GenServer.call(__MODULE__, {:white3, status})
def white4(status), do: GenServer.call(__MODULE__, {:white4, status})
def white5(status), do: GenServer.call(__MODULE__, {:white5, status})
use GenServer
@ -39,8 +25,10 @@ defmodule Farmbot.Target.Leds.AleHandler do
end
def init([]) do
leds = [:red, :blue, :green, :yellow, :white1, :white2, :white3, :white4, :white5]
state =
Map.new([:red, :blue, :green, :yellow, :white1, :white2], fn color ->
Map.new(leds, fn color ->
{:ok, pid} = GPIO.start_link(color_to_pin(color), :output)
:ok = GPIO.write(pid, 0)
{color, %{pid: pid, status: :off, blink_timer: nil, state: 0}}
@ -54,10 +42,7 @@ defmodule Farmbot.Target.Leds.AleHandler do
:ok = cancel_timer(state[color].blink_timer)
{:reply, :ok,
%{
state
| color => %{state[color] | state: 0, blink_timer: nil, status: :off}
}}
update_color(state, color, %{state[color] | state: 0, blink_timer: nil, status: :off})}
end
def handle_call({color, :solid}, _from, state) do
@ -65,30 +50,21 @@ defmodule Farmbot.Target.Leds.AleHandler do
:ok = cancel_timer(state[color].blink_timer)
{:reply, :ok,
%{
state
| color => %{state[color] | state: 1, blink_timer: nil, status: :solid}
}}
update_color(state, color, %{state[color] | state: 1, blink_timer: nil, status: :off})}
end
def handle_call({color, :slow_blink}, _from, state) do
timer = restart_timer(state[color].blink_timer, color, @slow_blink_speed)
{:reply, :ok,
%{
state
| color => %{state[color] | blink_timer: timer, status: :slow_blink}
}}
update_color(state, color, %{state[color] | blink_timer: timer, status: :slow_blink})}
end
def handle_call({color, :fast_blink}, _from, state) do
timer = restart_timer(state[color].blink_timer, color, @fast_blink_speed)
{:reply, :ok,
%{
state
| color => %{state[color] | blink_timer: timer, status: :fast_blink}
}}
update_color(state, color, %{state[color] | blink_timer: timer, status: :slow_blink})}
end
def handle_info({:blink_timer, color}, state) do
@ -98,32 +74,14 @@ defmodule Farmbot.Target.Leds.AleHandler do
new_state =
case state[color] do
%{status: :slow_blink} ->
timer =
restart_timer(state[color].blink_timer, color, @slow_blink_speed)
%{
state
| color => %{
state[color]
| state: new_led_state,
blink_timer: timer,
status: :slow_blink
}
}
timer = restart_timer(state[color].blink_timer, color, @slow_blink_speed)
n = %{state[color] | state: new_led_state, blink_timer: timer, status: :slow_blink}
update_color(state, color, n)
%{status: :fast_blink} ->
timer =
restart_timer(state[color].blink_timer, color, @fast_blink_speed)
%{
state
| color => %{
state[color]
| state: new_led_state,
blink_timer: timer,
status: :fast_blink
}
}
timer = restart_timer(state[color].blink_timer, color, @fast_blink_speed)
n = %{state[color] | state: new_led_state, blink_timer: timer, status: :fast_blink}
update_color(state, color, n)
end
{:noreply, new_state}
@ -133,8 +91,11 @@ defmodule Farmbot.Target.Leds.AleHandler do
defp color_to_pin(:yellow), do: 23
defp color_to_pin(:green), do: 24
defp color_to_pin(:blue), do: 25
defp color_to_pin(:white1), do: 12
defp color_to_pin(:white2), do: 13
defp color_to_pin(:white1), do: 27
defp color_to_pin(:white2), do: 6
defp color_to_pin(:white3), do: 21
defp color_to_pin(:white4), do: 12
defp color_to_pin(:white5), do: 13
defp cancel_timer(nil), do: :ok
@ -150,12 +111,8 @@ defmodule Farmbot.Target.Leds.AleHandler do
defp invert(0), do: 1
defp invert(1), do: 0
defp update_color(state, color, new_color) do
%{state | color => new_color}
end
end
{:ok, h} =
Farmbot.Target.Leds.AleHandler.start_link(
[],
name: Farmbot.Target.Leds.AleHandler
)
Farmbot.Target.Leds.AleHandler.yellow(:solid)

View File

@ -31,8 +31,7 @@ defmodule Farmbot.Target.PinBinding.AleHandler do
end
def init([]) do
{:producer, struct(State, pins: %{}),
[dispatcher: GenStage.BroadcastDispatcher]}
{:producer, struct(State, pins: %{}), [dispatcher: GenStage.BroadcastDispatcher]}
end
def handle_demand(_amnt, state) do