clean up some unused code.
parent
33cac0ef8b
commit
926f9ce156
|
@ -44,4 +44,4 @@ fwup-key.priv
|
|||
# secret config stuffs for dev environment.
|
||||
config/auth_secret.exs
|
||||
flash_fw.sh
|
||||
./tmp
|
||||
tmp
|
||||
|
|
|
@ -49,7 +49,7 @@ defmodule Farmbot.Bootstrap.Authorization do
|
|||
|> Poison.encode!
|
||||
|> RSA.encrypt({:public, rsa_key})
|
||||
|> Base.encode64
|
||||
payload = %{user: %{credentials: secret}} |> Poison.encode
|
||||
%{user: %{credentials: secret}} |> Poison.encode
|
||||
end
|
||||
|
||||
defp request_token(server, payload) do
|
||||
|
@ -71,5 +71,5 @@ defmodule Farmbot.Bootstrap.Authorization do
|
|||
{:error, msg}
|
||||
end
|
||||
|
||||
defp handle_error({:error, reason} = error), do: error
|
||||
defp handle_error({:error, _reason} = error), do: error
|
||||
end
|
||||
|
|
|
@ -74,7 +74,6 @@ defmodule Farmbot.Bootstrap.Supervisor do
|
|||
end
|
||||
|
||||
def init(args) do
|
||||
Logger.info "Beginning Authorization."
|
||||
# try to find the creds.
|
||||
case get_creds() do
|
||||
# do the actual supervisor init if we have creds. This may still fail.
|
||||
|
@ -103,14 +102,13 @@ defmodule Farmbot.Bootstrap.Supervisor do
|
|||
end
|
||||
|
||||
defp actual_init(args, email, pass, server) do
|
||||
Logger.info "Beginning authorization: #{email} - #{server}"
|
||||
# get a token
|
||||
case @auth_task.authorize(email, pass, server) do
|
||||
{:ok, token} ->
|
||||
children = [
|
||||
supervisor(Farmbot.BotState.Supervisor, [token, [name: Farmbot.BotState.Supervisor ]]),
|
||||
supervisor(Farmbot.HTTP.Supervisor, [token, [name: Farmbot.HTTP.Supervisor]]),
|
||||
|
||||
# supervisor(Farmbot.Transport.Supervisor, [token, [name: Farmbot.Transport.Supervisor]])
|
||||
]
|
||||
opts = [strategy: :one_for_all]
|
||||
supervise(children, opts)
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
defmodule Farmbot.CeleryScript.Command do
|
||||
@moduledoc "Deleteme"
|
||||
def do_command(_, _), do: :ok
|
||||
end
|
|
@ -1,36 +0,0 @@
|
|||
defmodule Farmbot.FactoryResetWatcher do
|
||||
@moduledoc "Watches a pid and factory resets if it dies."
|
||||
use GenServer
|
||||
use Farmbot.DebugLog
|
||||
alias Farmbot.Context
|
||||
|
||||
def start_link(%Context{} = ctx, server, opts \\ []) do
|
||||
GenServer.start_link(__MODULE__, {ctx, server}, opts)
|
||||
end
|
||||
|
||||
def init({ctx, server}) when is_atom(server) do
|
||||
debug_log "Watching #{server}"
|
||||
case Process.whereis(server) do
|
||||
pid when is_pid(pid) -> init({ctx, pid})
|
||||
_ -> {:stop, {:error, :no_proc}}
|
||||
end
|
||||
end
|
||||
|
||||
def init({_ctx, server}) when is_pid(server) do
|
||||
Process.flag(:trap_exit, true)
|
||||
{:ok, server}
|
||||
end
|
||||
|
||||
def handle_info({:EXIT, _from, reason}, server) when reason in [:normal, :shutdown] do
|
||||
{:stop, reason, server}
|
||||
end
|
||||
|
||||
def handle_info({:EXIT, from, reason}, server) when from == server do
|
||||
msg = """
|
||||
Unexpected exit from a watched process.
|
||||
#{inspect reason}
|
||||
"""
|
||||
Farmbot.System.factory_reset msg
|
||||
{:noreply, server}
|
||||
end
|
||||
end
|
|
@ -10,6 +10,7 @@ defmodule Farmbot.Firmware do
|
|||
LocationData
|
||||
}
|
||||
|
||||
@doc "Public API for handling a gcode."
|
||||
def handle_gcode(firmware, code), do: GenServer.call(firmware, {:handle_gcode, code})
|
||||
|
||||
def start_link(bot_state, informational_settings, configuration, location_data, mcu_params, handler_mod, opts) do
|
||||
|
|
|
@ -1,180 +0,0 @@
|
|||
defmodule Farmbot.Regimen.Runner do
|
||||
@moduledoc """
|
||||
Runs a regimen
|
||||
"""
|
||||
|
||||
alias Farmbot.Regimen.Supervisor, as: RegSup
|
||||
alias Farmbot.{Database, Context}
|
||||
alias Database.Syncable.{Regimen, Sequence}
|
||||
alias Farmbot.CeleryScript.Command
|
||||
use GenServer
|
||||
require Logger
|
||||
|
||||
defmodule Error do
|
||||
@moduledoc false
|
||||
defexception [:epoch, :regimen, :message]
|
||||
end
|
||||
|
||||
defmodule Item do
|
||||
@moduledoc false
|
||||
@type t :: %__MODULE__{
|
||||
name: binary,
|
||||
time_offset: integer,
|
||||
sequence: Farmbot.CeleryScript.Ast.t
|
||||
}
|
||||
|
||||
defstruct [:time_offset, :sequence, :name]
|
||||
|
||||
def parse(%{"time_offset" => offset, "sequence_id" => sequence_id},
|
||||
%Context{} = ctx)
|
||||
do
|
||||
sequence = fetch_sequence(ctx, sequence_id)
|
||||
%__MODULE__{
|
||||
name: sequence.name,
|
||||
time_offset: offset,
|
||||
sequence: Farmbot.CeleryScript.Ast.parse(sequence)}
|
||||
end
|
||||
|
||||
def fetch_sequence(%Context{} = ctx, id) do
|
||||
db_obj = Database.get_by_id(ctx, Sequence, id)
|
||||
unless db_obj do
|
||||
raise "Could not find sequence by id: #{inspect id}"
|
||||
end
|
||||
db_obj.body
|
||||
end
|
||||
end
|
||||
|
||||
def start_link(%Context{} = ctx, regimen, time) do
|
||||
GenServer.start_link(__MODULE__,
|
||||
[ctx, regimen, time],
|
||||
name: :"regimen-#{regimen.id}")
|
||||
end
|
||||
|
||||
def init([ctx, regimen, time]) do
|
||||
# parse and sort the regimen items
|
||||
items = filter_items(regimen, ctx)
|
||||
first_item = List.first(items)
|
||||
regimen = %{regimen | regimen_items: items}
|
||||
epoch = build_epoch(ctx, time) || raise Error,
|
||||
message: "Could not determine EPOCH because no timezone was supplied.",
|
||||
epoch: :error, regimen: regimen
|
||||
|
||||
initial_state = %{
|
||||
next_execution: nil,
|
||||
regimen: regimen,
|
||||
context: ctx,
|
||||
epoch: epoch,
|
||||
timer: nil
|
||||
}
|
||||
|
||||
if first_item do
|
||||
state = build_next_state(regimen, first_item, ctx, self(), initial_state)
|
||||
{:ok, state}
|
||||
else
|
||||
Logger.warn "[#{regimen.name}] has no items on regimen."
|
||||
{:ok, :finished}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_info(:execute, state) do
|
||||
{item, regimen} = pop_item(state.regimen)
|
||||
if item do
|
||||
do_item(item, regimen, state)
|
||||
else
|
||||
Logger.info "[#{regimen.name}] is complete!"
|
||||
spawn fn() ->
|
||||
RegSup.remove_child(state.context, regimen)
|
||||
end
|
||||
{:noreply, :finished}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_info(:skip, state) do
|
||||
{item, regimen} = pop_item(state.regimen)
|
||||
if item do
|
||||
do_item(nil, regimen, state)
|
||||
else
|
||||
Logger.info "[#{regimen.name}] is complete!"
|
||||
spawn fn() ->
|
||||
RegSup.remove_child(state.context, regimen)
|
||||
end
|
||||
{:noreply, :finished}
|
||||
end
|
||||
end
|
||||
|
||||
defp filter_items(regimen, %Context{} = ctx) do
|
||||
regimen.regimen_items
|
||||
|> Enum.map(&Item.parse(&1, ctx))
|
||||
|> Enum.sort(&(&1.time_offset <= &2.time_offset))
|
||||
end
|
||||
|
||||
defp do_item(item, regimen, state) do
|
||||
context =
|
||||
if item do
|
||||
Logger.info "[#{regimen.name}] is going to execute: #{item.name}"
|
||||
Command.do_command(item.sequence, state.context)
|
||||
else
|
||||
state.context
|
||||
end
|
||||
next_item = List.first(regimen.regimen_items)
|
||||
if next_item do
|
||||
new_state = build_next_state(regimen, next_item, context, self(), state)
|
||||
{:noreply, new_state}
|
||||
else
|
||||
Logger.info "[#{regimen.name}] is complete!"
|
||||
spawn fn() ->
|
||||
RegSup.remove_child(context, regimen)
|
||||
end
|
||||
{:noreply, :finished}
|
||||
end
|
||||
end
|
||||
|
||||
def build_next_state(
|
||||
%Regimen{} = regimen,
|
||||
%Item{} = nx_itm, %Context{} = context,
|
||||
pid, state)
|
||||
do
|
||||
next_dt = Timex.shift(state.epoch, milliseconds: nx_itm.time_offset)
|
||||
now = Timex.now(Farmbot.BotState.get_config(context, :timezone))
|
||||
offset_from_now = Timex.diff(next_dt, now, :milliseconds)
|
||||
|
||||
timer = if (offset_from_now < 0) and (offset_from_now < -60_000) do
|
||||
Logger.info "[#{regimen.name}] #{[nx_itm.name]} has been scheduled " <>
|
||||
"to happen more than one minute ago: #{offset_from_now} Skipping it."
|
||||
Process.send_after(pid, :skip, 1000)
|
||||
else
|
||||
{msg, real_offset} = ensure_not_negative(offset_from_now)
|
||||
Process.send_after(pid, msg, real_offset)
|
||||
end
|
||||
|
||||
timestr = "#{next_dt.month}/#{next_dt.day}/#{next_dt.year} " <>
|
||||
"at: #{next_dt.hour}:#{next_dt.minute} (#{offset_from_now} milliseconds)"
|
||||
|
||||
Logger.info "[#{regimen.name}] next item will execute on #{timestr}"
|
||||
|
||||
%{state | timer: timer,
|
||||
context: context,
|
||||
regimen: regimen,
|
||||
next_execution: next_dt}
|
||||
end
|
||||
|
||||
defp ensure_not_negative(offset) when offset < -60_000, do: {:skip, 1000}
|
||||
defp ensure_not_negative(offset) when offset < 0, do: {:execute, 1000}
|
||||
defp ensure_not_negative(offset), do: {:execute, offset}
|
||||
|
||||
@spec pop_item(Regimen.t) :: {Item.t | nil, Regimen.t}
|
||||
# when there is more than one item pop the top one
|
||||
defp pop_item(%Regimen{regimen_items: [do_this_one | items ]} = r) do
|
||||
{do_this_one, %Regimen{r | regimen_items: items}}
|
||||
end
|
||||
|
||||
# returns midnight of today
|
||||
@spec build_epoch(Context.t, DateTime.t) :: DateTime.t
|
||||
def build_epoch(%Context{} = context, time) do
|
||||
tz = Farmbot.BotState.get_config(context, :timezone)
|
||||
if tz do
|
||||
n = Timex.Timezone.convert(time, tz)
|
||||
Timex.shift(n, hours: -n.hour, seconds: -n.second, minutes: -n.minute)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,39 +0,0 @@
|
|||
defmodule Farmbot.Regimen.Supervisor do
|
||||
@moduledoc """
|
||||
Supervisor for Regimens
|
||||
"""
|
||||
use Supervisor
|
||||
@behaviour Farmbot.EventSupervisor
|
||||
alias Farmbot.Context
|
||||
|
||||
@type supervisor :: pid | atom
|
||||
|
||||
@doc """
|
||||
Start a Regimen Supervisor
|
||||
"""
|
||||
def start_link(%Context{} = context, opts),
|
||||
do: Supervisor.start_link(__MODULE__, context, opts)
|
||||
|
||||
def init(_) do
|
||||
children = []
|
||||
opts = [strategy: :one_for_one]
|
||||
supervise(children, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Add a child to this supervisor
|
||||
"""
|
||||
def add_child(%Context{} = context, regimen, time) do
|
||||
Supervisor.start_child(context.regimen_supervisor,
|
||||
worker(Farmbot.Regimen.Runner, [context, regimen, time],
|
||||
[restart: :transient, id: regimen.id]))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Remove a child from this supervisor.
|
||||
"""
|
||||
def remove_child(%Context{} = context, regimen) do
|
||||
Supervisor.terminate_child(context.regimen_supervisor, regimen.id)
|
||||
Supervisor.delete_child(context.regimen_supervisor, regimen.id)
|
||||
end
|
||||
end
|
|
@ -1,52 +0,0 @@
|
|||
defmodule Farmbot.Sequence.Manager do
|
||||
@moduledoc "Manages a sequence tree"
|
||||
alias Farmbot.{Context, DebugLog, CeleryScript, Sequence}
|
||||
alias Sequence.Runner
|
||||
alias CeleryScript.Ast
|
||||
use GenServer
|
||||
use DebugLog, name: SequenceManager
|
||||
|
||||
@doc """
|
||||
Starts managing a sequence. If you want to wait on a sequence, you should do
|
||||
a recieve loop and wait for info there.
|
||||
"""
|
||||
def start_link(%Context{} = ctx, %Ast{} = sequence, caller, opts \\ []) do
|
||||
GenServer.start_link(__MODULE__, {ctx, sequence, caller}, opts)
|
||||
end
|
||||
|
||||
def init({ctx, sequence_ast, caller}) do
|
||||
case Runner.start_link(ctx, sequence_ast, self()) do
|
||||
{:ok, sequence_pid} ->
|
||||
Process.flag(:trap_exit, true)
|
||||
Process.link(sequence_pid)
|
||||
{:ok, %{context: ctx, caller: caller, sequence_pid: sequence_pid}}
|
||||
:ignore ->
|
||||
send(caller, {self(), ctx})
|
||||
:ignore
|
||||
err -> {:stop, err}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_info({_pid, %Context{} = ctx}, %{sequence_pid: _sequence_pid} = state) do
|
||||
{:noreply, %{state | context: ctx}}
|
||||
end
|
||||
|
||||
def handle_info({_pid, {:error, ex}}, state) do
|
||||
debug_log "Sequence error."
|
||||
send state.caller, {self(), {:error, ex}}
|
||||
{:stop, :normal, state}
|
||||
end
|
||||
|
||||
def handle_info({:EXIT, _pid, :normal}, %{sequence_pid: _sequence_pid} = state) do
|
||||
debug_log "Sequence completed successfully."
|
||||
send state.caller, {self(), state.context}
|
||||
{:stop, :normal, state}
|
||||
end
|
||||
|
||||
def handle_info({:EXIT, _pid, reason}, %{sequence_pid: _sequence_pid} = state) do
|
||||
debug_log "Caught sequence exit error: #{inspect reason}"
|
||||
send state.caller, {self(), {:error, reason}}
|
||||
{:stop, :normal, state}
|
||||
end
|
||||
|
||||
end
|
|
@ -1,95 +0,0 @@
|
|||
defmodule Farmbot.Sequence.Runner do
|
||||
@moduledoc """
|
||||
Runs a sequence
|
||||
"""
|
||||
use GenServer
|
||||
alias Farmbot.{CeleryScript, Context, DebugLog}
|
||||
alias CeleryScript.Ast
|
||||
import CeleryScript.Command, only: [do_command: 2]
|
||||
require Logger
|
||||
use DebugLog
|
||||
|
||||
@type context :: Context.t
|
||||
@type sequence_pid :: pid
|
||||
|
||||
@type block :: {reference, pid}
|
||||
|
||||
@type state :: %{
|
||||
blocks: [block],
|
||||
body: [Ast.t]
|
||||
}
|
||||
|
||||
@step_timeout 15_000
|
||||
|
||||
@doc """
|
||||
Starts a sequence.
|
||||
"""
|
||||
def start_link(%Context{} = ctx, %Ast{} = ast, caller) do
|
||||
GenServer.start_link(__MODULE__, {ast, ctx, caller}, [])
|
||||
end
|
||||
|
||||
def init({%{body: []}, context, caller}) do
|
||||
send caller, {self(), context}
|
||||
:ignore
|
||||
end
|
||||
|
||||
def init({ast, first_context, caller}) do
|
||||
debug_log "[#{inspect self()}] Sequence init."
|
||||
# Setup the firt step
|
||||
[first | rest] = ast.body
|
||||
pid = spawn __MODULE__, :work, [{first, first_context}, self()]
|
||||
timer = Process.send_after(:timeout, self(), @step_timeout)
|
||||
state = %{
|
||||
body: rest,
|
||||
context: first_context,
|
||||
caller: caller,
|
||||
worker: pid,
|
||||
timer: timer
|
||||
}
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
def handle_cast({:error, ex}, state) do
|
||||
Process.cancel_timer(state.timer)
|
||||
send(state.caller, {self(), {:error, ex}})
|
||||
{:stop, :normal, state}
|
||||
end
|
||||
|
||||
# When a stop finishes and there is no more steps
|
||||
def handle_cast({:finished, next_context}, %{body: []} = state) do
|
||||
Process.cancel_timer(state.timer)
|
||||
send(state.caller, {self(), next_context})
|
||||
{:stop, :normal, %{state | context: next_context, timer: nil}}
|
||||
end
|
||||
|
||||
def handle_cast({:finished, next_context}, %{body: rest} = state) do
|
||||
Process.cancel_timer(state.timer)
|
||||
send(state.caller, {self(), next_context})
|
||||
[first | rest] = rest
|
||||
pid = spawn __MODULE__, :work, [{first, next_context}, self()]
|
||||
Process.link(pid)
|
||||
timer = Process.send_after(:timeout, self(), @step_timeout)
|
||||
new_state = %{state | body: rest,
|
||||
context: next_context,
|
||||
worker: pid,
|
||||
timer: timer
|
||||
}
|
||||
{:noreply, new_state}
|
||||
end
|
||||
|
||||
def handle_info(:timeout, state), do: {:stop, :timeout, %{state | timer: nil}}
|
||||
|
||||
@spec work({Ast.t, Context.t}, sequence_pid) :: :ok
|
||||
def work({ast, context}, sequence) do
|
||||
debug_log "[#{inspect self()}] doing work: #{inspect ast}"
|
||||
Process.sleep(1000)
|
||||
# this might raise.
|
||||
|
||||
try do
|
||||
new_context = do_command(ast, context)
|
||||
GenServer.cast(sequence, {:finished, new_context})
|
||||
rescue
|
||||
e -> GenServer.cast(sequence, {:error, e})
|
||||
end
|
||||
end
|
||||
end
|
|
@ -36,14 +36,7 @@ defmodule Farmbot.System do
|
|||
@doc "Remove all configuration data, and reboot."
|
||||
@spec factory_reset(unparsed_reason) :: no_return
|
||||
def factory_reset(reason) do
|
||||
try do
|
||||
raise "Doing factory reset: #{inspect reason}", System.stacktrace()
|
||||
rescue
|
||||
e ->
|
||||
debug_log "Doing factory reset: #{inspect e}"
|
||||
debug_log "#{inspect System.stacktrace()}"
|
||||
@system_tasks.factory_reset(reason)
|
||||
end
|
||||
@system_tasks.factory_reset(reason)
|
||||
end
|
||||
|
||||
@doc "Reboot."
|
||||
|
|
|
@ -2,7 +2,7 @@ defmodule Farmbot.Host.Authorization do
|
|||
@moduledoc "Host implementation for Farmbot Authorization"
|
||||
@behaviour Farmbot.Bootstrap.Authorization
|
||||
|
||||
def authorize(email, password, server) do
|
||||
def authorize(_email, _password, _server) do
|
||||
{:ok, "this wont work"}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -44,7 +44,7 @@ defmodule Farmbot.Test.SystemTasks do
|
|||
{:ok, []}
|
||||
end
|
||||
|
||||
def handle_call({action, reason} = thing, _, state) do
|
||||
def handle_call({_action, _reason} = thing, _, state) do
|
||||
{:reply, :ok, [thing | state]}
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue