cleanup credo
parent
08a0ac8c5a
commit
6b0c34a5ab
|
@ -24,15 +24,33 @@ defmodule Farmbot do
|
|||
children = [
|
||||
worker(Farmbot.DebugLog, [], restart: :permanent),
|
||||
supervisor(Registry, [:duplicate, Farmbot.Registry]),
|
||||
supervisor(FBSYS, [context, [name: FBSYS ]], restart: :permanent),
|
||||
worker(Farmbot.Auth, [context, [name: Farmbot.Auth ]], restart: :permanent),
|
||||
worker(Farmbot.Database, [context, [name: Farmbot.Database ]], restart: :permanent),
|
||||
supervisor(Farmbot.BotState.Supervisor, [context, [name: Farmbot.BotState.Supervisor ]], restart: :permanent),
|
||||
supervisor(Farmbot.FarmEvent.Supervisor, [context, [name: Farmbot.FarmEvent.Supervisor ]], restart: :permanent),
|
||||
supervisor(Farmbot.Transport.Supervisor, [context, [name: Farmbot.Transport.Supervisor ]], restart: :permanent),
|
||||
supervisor(Farmware.Supervisor, [context, [name: Farmware.Supervisor ]], restart: :permanent),
|
||||
worker(Farmbot.ImageWatcher, [context, [name: Farmbot.ImageWatcher ]], restart: :permanent),
|
||||
worker(Task, [Farmbot.Serial.Handler.OpenTTY, :open_ttys, [__MODULE__]], restart: :transient),
|
||||
supervisor(FBSYS,
|
||||
[context, [name: FBSYS ]], restart: :permanent),
|
||||
|
||||
worker(Farmbot.Auth,
|
||||
[context, [name: Farmbot.Auth ]], restart: :permanent),
|
||||
|
||||
worker(Farmbot.Database,
|
||||
[context, [name: Farmbot.Database ]], restart: :permanent),
|
||||
|
||||
supervisor(Farmbot.BotState.Supervisor,
|
||||
[context, [name: Farmbot.BotState.Supervisor ]], restart: :permanent),
|
||||
|
||||
supervisor(Farmbot.FarmEvent.Supervisor,
|
||||
[context, [name: Farmbot.FarmEvent.Supervisor ]], restart: :permanent),
|
||||
|
||||
supervisor(Farmbot.Transport.Supervisor,
|
||||
[context, [name: Farmbot.Transport.Supervisor ]], restart: :permanent),
|
||||
|
||||
supervisor(Farmware.Supervisor,
|
||||
[context, [name: Farmware.Supervisor ]], restart: :permanent),
|
||||
|
||||
worker(Farmbot.ImageWatcher,
|
||||
[context, [name: Farmbot.ImageWatcher ]], restart: :permanent),
|
||||
|
||||
worker(Task, [Farmbot.Serial.Handler.OpenTTY, :open_ttys, [__MODULE__]],
|
||||
restart: :transient),
|
||||
|
||||
supervisor(Farmbot.Configurator, [], restart: :permanent),
|
||||
]
|
||||
opts = [strategy: :one_for_one]
|
||||
|
|
|
@ -14,7 +14,8 @@ defmodule Farmbot.BotState do
|
|||
Gets the current position of the bot. Returns [x,y,z]
|
||||
"""
|
||||
@spec get_current_pos(context) :: [integer, ...]
|
||||
def get_current_pos(%Context{} = context), do: GenServer.call(context.hardware, :get_current_pos)
|
||||
def get_current_pos(%Context{} = context),
|
||||
do: GenServer.call(context.hardware, :get_current_pos)
|
||||
|
||||
@doc """
|
||||
Sets the position to givin position.
|
||||
|
@ -29,7 +30,8 @@ defmodule Farmbot.BotState do
|
|||
Sets a pin under the given value
|
||||
"""
|
||||
@spec set_pin_value(context, integer, integer) :: :ok
|
||||
def set_pin_value(%Context{} = context, pin, value) when is_integer(pin) and is_integer(value) do
|
||||
def set_pin_value(%Context{} = context, pin, value)
|
||||
when is_integer(pin) and is_integer(value) do
|
||||
GenServer.cast(context.hardware, {:set_pin_value, {pin, value}})
|
||||
end
|
||||
|
||||
|
@ -64,7 +66,9 @@ defmodule Farmbot.BotState do
|
|||
Gets the map of every param.
|
||||
Useful for resetting params if the arduino flops
|
||||
"""
|
||||
@spec get_all_mcu_params(context) :: Farmbot.BotState.Hardware.State.mcu_params
|
||||
@spec get_all_mcu_params(context)
|
||||
:: Farmbot.BotState.Hardware.State.mcu_params
|
||||
|
||||
def get_all_mcu_params(%Context{} = context) do
|
||||
GenServer.call(context.hardware, :get_all_mcu_params)
|
||||
end
|
||||
|
@ -82,26 +86,30 @@ defmodule Farmbot.BotState do
|
|||
This is just a shortcut
|
||||
"""
|
||||
@spec get_fw_version(context) :: String.t
|
||||
def get_fw_version(%Context{} = context), do: GenServer.call(context.configuration, :get_fw_version)
|
||||
def get_fw_version(%Context{} = context),
|
||||
do: GenServer.call(context.configuration, :get_fw_version)
|
||||
|
||||
@doc """
|
||||
Set the version
|
||||
"""
|
||||
@spec set_fw_version(context, binary) :: no_return
|
||||
def set_fw_version(%Context{} = context, v),
|
||||
do: GenServer.cast(context.configuration, {:update_info, :firmware_version, v})
|
||||
def set_fw_version(%Context{} = context, v) do
|
||||
GenServer.cast(context.configuration, {:update_info, :firmware_version, v})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the current controller version
|
||||
"""
|
||||
@spec get_os_version(context) :: String.t
|
||||
def get_os_version(%Context{} = context), do: GenServer.call(context.configuration, :get_version)
|
||||
def get_os_version(%Context{} = context),
|
||||
do: GenServer.call(context.configuration, :get_version)
|
||||
|
||||
@doc """
|
||||
Gets the value of a hardware param
|
||||
"""
|
||||
@spec get_param(context, atom) :: integer | nil
|
||||
def get_param(%Context{} = context, param), do: GenServer.call(context.hardware, {:get_param, param})
|
||||
def get_param(%Context{} = context, param),
|
||||
do: GenServer.call(context.hardware, {:get_param, param})
|
||||
|
||||
@doc """
|
||||
Update a config under key
|
||||
|
@ -167,14 +175,25 @@ defmodule Farmbot.BotState do
|
|||
@spec set_sync_msg(context, sync_msg) :: :ok
|
||||
def set_sync_msg(context, sync_msg)
|
||||
|
||||
def set_sync_msg(%Context{} = ctx, :sync_error = thing), do: do_set_sync_msg(ctx, thing)
|
||||
def set_sync_msg(%Context{} = ctx, :sync_now = thing), do: do_set_sync_msg(ctx, thing)
|
||||
def set_sync_msg(%Context{} = ctx, :syncing = thing), do: do_set_sync_msg(ctx, thing)
|
||||
def set_sync_msg(%Context{} = ctx, :unknown = thing), do: do_set_sync_msg(ctx, thing)
|
||||
def set_sync_msg(%Context{} = ctx, :locked = thing), do: do_set_sync_msg(ctx, thing)
|
||||
def set_sync_msg(%Context{} = ctx, :synced = thing), do: do_set_sync_msg(ctx, thing)
|
||||
def set_sync_msg(%Context{} = ctx, :sync_error = thing),
|
||||
do: do_set_sync_msg(ctx, thing)
|
||||
|
||||
defp do_set_sync_msg(%Context{} = context, thing),
|
||||
do: GenServer.cast(context.configuration, {:update_info, :sync_status, thing})
|
||||
def set_sync_msg(%Context{} = ctx, :sync_now = thing),
|
||||
do: do_set_sync_msg(ctx, thing)
|
||||
|
||||
def set_sync_msg(%Context{} = ctx, :syncing = thing),
|
||||
do: do_set_sync_msg(ctx, thing)
|
||||
|
||||
def set_sync_msg(%Context{} = ctx, :unknown = thing),
|
||||
do: do_set_sync_msg(ctx, thing)
|
||||
|
||||
def set_sync_msg(%Context{} = ctx, :locked = thing),
|
||||
do: do_set_sync_msg(ctx, thing)
|
||||
|
||||
def set_sync_msg(%Context{} = ctx, :synced = thing),
|
||||
do: do_set_sync_msg(ctx, thing)
|
||||
|
||||
defp do_set_sync_msg(%Context{} = context, thing) do
|
||||
GenServer.cast(context.configuration, {:update_info, :sync_status, thing})
|
||||
end
|
||||
end
|
||||
|
|
|
@ -57,7 +57,7 @@ defmodule Farmbot.BotState.Monitor do
|
|||
GenStage.async_notify(new_state.context.monitor, new_state)
|
||||
{:noreply, [], new_state}
|
||||
end
|
||||
#
|
||||
#
|
||||
# @spec dispatch(any, State.t) :: {:reply, any, [], State.t }
|
||||
# defp dispatch(reply, new_state) do
|
||||
# GenStage.async_notify(new_state.context.monitor, new_state)
|
||||
|
|
|
@ -65,8 +65,11 @@ defmodule Farmbot.BotState.ProcessTracker do
|
|||
@doc """
|
||||
starts a process by its uuid or info struct
|
||||
"""
|
||||
@spec start_process(Context.t, State.uuid | Info.t) :: {:ok, pid} | {:error, term}
|
||||
def start_process(%Context{} = ctx, %Info{uuid: uuid}), do: start_process(ctx, uuid)
|
||||
@spec start_process(Context.t, State.uuid | Info.t)
|
||||
:: {:ok, pid} | {:error, term}
|
||||
def start_process(%Context{} = ctx, %Info{uuid: uuid}),
|
||||
do: start_process(ctx, uuid)
|
||||
|
||||
def start_process(%Context{} = ctx, uuid),
|
||||
do: GenServer.call(ctx.process_tracker, {:start_process, uuid})
|
||||
|
||||
|
|
|
@ -13,12 +13,26 @@ defmodule Farmbot.BotState.Supervisor do
|
|||
alias Farmbot.EasterEggs
|
||||
def init(ctx) do
|
||||
children = [
|
||||
worker(Farmbot.BotState.Monitor, [ctx, [name: Farmbot.BotState.Monitor] ], [restart: :permanent]),
|
||||
worker(Farmbot.BotState.Configuration, [ctx, [name: Farmbot.BotState.Configuration] ], [restart: :permanent]),
|
||||
worker(Farmbot.BotState.Hardware, [ctx, [name: Farmbot.BotState.Hardware] ], [restart: :permanent]),
|
||||
worker(Farmbot.BotState.ProcessSupervisor, [ctx, [name: Farmbot.BotState.ProcessSupervisor] ], [restart: :permanent]),
|
||||
worker(EasterEggs, [name: EasterEggs], [restart: :permanent])
|
||||
worker(Farmbot.BotState.Monitor,
|
||||
[ctx, [name: Farmbot.BotState.Monitor]],
|
||||
[restart: :permanent]),
|
||||
|
||||
worker(Farmbot.BotState.Configuration,
|
||||
[ctx, [name: Farmbot.BotState.Configuration]],
|
||||
[restart: :permanent]),
|
||||
|
||||
worker(Farmbot.BotState.Hardware,
|
||||
[ctx, [name: Farmbot.BotState.Hardware]],
|
||||
[restart: :permanent]),
|
||||
|
||||
worker(Farmbot.BotState.ProcessSupervisor,
|
||||
[ctx, [name: Farmbot.BotState.ProcessSupervisor]],
|
||||
[restart: :permanent]),
|
||||
|
||||
worker(EasterEggs,
|
||||
[name: EasterEggs], [restart: :permanent])
|
||||
]
|
||||
|
||||
opts = [strategy: :one_for_one]
|
||||
supervise(children, opts)
|
||||
end
|
||||
|
|
|
@ -56,7 +56,7 @@ defmodule Farmbot.BotState.Configuration do
|
|||
@commit Mix.Project.config()[:commit]
|
||||
|
||||
@spec load() :: {:ok, state} | no_return
|
||||
def load() do
|
||||
def load do
|
||||
initial = %State{
|
||||
informational_settings: %{
|
||||
controller_version: @version,
|
||||
|
|
|
@ -40,7 +40,8 @@ defmodule Farmbot.BotState.Hardware do
|
|||
@doc """
|
||||
Takes a Hardware State object, and makes it happen
|
||||
"""
|
||||
@spec set_initial_params(State.t, Ast.context) :: {:ok, :no_params} | :ok | {:error, term}
|
||||
@spec set_initial_params(State.t, Ast.context)
|
||||
:: {:ok, :no_params} | :ok | {:error, term}
|
||||
def set_initial_params(%State{} = state, %Farmbot.Context{} = context) do
|
||||
# BUG(Connor): The first param is rather unstable for some reason.
|
||||
# Try to send a fake packet just to make sure we have a good
|
||||
|
|
|
@ -7,7 +7,7 @@ defmodule Farmbot.StateTracker do
|
|||
|
||||
alias Farmbot.Context
|
||||
|
||||
@callback load() :: {:ok, map}
|
||||
@callback load :: {:ok, map}
|
||||
defmacro __using__(name: name, model: model) do
|
||||
quote do
|
||||
alias Farmbot.System.FS.ConfigStorage, as: FBConfigStorage
|
||||
|
@ -49,7 +49,7 @@ defmodule Farmbot.StateTracker do
|
|||
end
|
||||
|
||||
# this should be overriden.
|
||||
def load(), do: {:ok, %State{}}
|
||||
def load, do: {:ok, %State{}}
|
||||
|
||||
defp dispatch(reply, %unquote(name).State{} = state) do
|
||||
broadcast(state)
|
||||
|
|
|
@ -20,13 +20,16 @@ defmodule Farmbot.CeleryScript.Command.If do
|
|||
"""
|
||||
@spec run(%{}, [], Ast.context) :: Ast.context
|
||||
def run(%{_else: else_, _then: then_, lhs: lhs, op: op, rhs: rhs }, [], ctx) do
|
||||
lhs
|
||||
|> eval_lhs(ctx)
|
||||
|> eval_if(op, rhs, then_, else_, ctx)
|
||||
left = lhs |> eval_lhs(ctx)
|
||||
unless is_integer(left) do
|
||||
raise "could not evaluate left hand side of if statment! #{inspect lhs}"
|
||||
end
|
||||
|
||||
eval_if({left, op, rhs}, then_, else_, ctx)
|
||||
end
|
||||
|
||||
# figure out what the user wanted
|
||||
@spec eval_lhs(String.t, Ast.context) :: integer | {:error, String.t}
|
||||
@spec eval_lhs(binary, Ast.context) :: integer
|
||||
|
||||
defp eval_lhs("pin" <> num, %Farmbot.Context{} = context) do
|
||||
thing = String.to_integer(num)
|
||||
|
@ -39,41 +42,35 @@ defmodule Farmbot.CeleryScript.Command.If do
|
|||
"x" -> x
|
||||
"y" -> y
|
||||
"z" -> z
|
||||
_ -> {:error, axis} # its not an axis.
|
||||
end
|
||||
end
|
||||
|
||||
@spec eval_if({:error, String.t} | integer, String.t,
|
||||
integer, Ast.t, Ast.t, Ast.context)
|
||||
:: Ast.context
|
||||
@spec eval_if({integer, String.t, integer},
|
||||
Ast.t, Ast.t, Ast.context) :: Ast.context
|
||||
|
||||
defp eval_if({:error, lhs}, _op, _rhs, _then, _else, _context) do
|
||||
raise "could not evaluate left hand side of if statment! #{inspect lhs}"
|
||||
end
|
||||
|
||||
defp eval_if(lhs, ">", rhs, then_, else_, context) do
|
||||
defp eval_if({lhs, ">", rhs}, then_, else_, context) do
|
||||
if lhs > rhs,
|
||||
do: Command.do_command(then_, context),
|
||||
else: Command.do_command(else_, context)
|
||||
end
|
||||
|
||||
defp eval_if(lhs, "<", rhs, then_, else_, context) do
|
||||
defp eval_if({lhs, "<", rhs}, then_, else_, context) do
|
||||
if lhs < rhs,
|
||||
do: Command.do_command(then_, context),
|
||||
else: Command.do_command(else_, context)
|
||||
end
|
||||
|
||||
defp eval_if(lhs, "is", rhs, then_, else_, context) do
|
||||
defp eval_if({lhs, "is", rhs}, then_, else_, context) do
|
||||
if lhs == rhs,
|
||||
do: Command.do_command(then_, context),
|
||||
else: Command.do_command(else_, context)
|
||||
end
|
||||
|
||||
defp eval_if(lhs, "not", rhs, then_, else_, context) do
|
||||
defp eval_if({lhs, "not", rhs}, then_, else_, context) do
|
||||
if lhs != rhs,
|
||||
do: Command.do_command(then_, context),
|
||||
else: Command.do_command(else_, context)
|
||||
end
|
||||
|
||||
defp eval_if(_, _, _, _, _, _context), do: raise "Bad operator in if!"
|
||||
defp eval_if({_, _, _}, _, _, _context), do: raise "Bad operator in if!"
|
||||
end
|
||||
|
|
|
@ -75,7 +75,8 @@ defmodule Farmbot.CeleryScript.Command.ConfigUpdate do
|
|||
end
|
||||
|
||||
defp write_and_read(_context, int_and_str, val, tries \\ 0)
|
||||
defp write_and_read(_context, {_param_int, param_str}, _val, tries) when tries > 5 do
|
||||
defp write_and_read(_context, {_param_int, param_str}, _val, tries)
|
||||
when tries > 5 do
|
||||
Logger.info ">> failed to update update: #{param_str}!"
|
||||
{:error, :timeout}
|
||||
end
|
||||
|
@ -85,7 +86,8 @@ defmodule Farmbot.CeleryScript.Command.ConfigUpdate do
|
|||
gcode = "F22 P#{param_int} V#{val}"
|
||||
results = UartHan.write(context, gcode)
|
||||
case results do
|
||||
:timeout -> write_and_read(context, {param_int, param_str}, val, tries + 1)
|
||||
:timeout ->
|
||||
write_and_read(context, {param_int, param_str}, val, tries + 1)
|
||||
_ -> :ok
|
||||
end
|
||||
|
||||
|
|
|
@ -34,7 +34,8 @@ defmodule Farmbot.CeleryScript.Command.FactoryReset do
|
|||
defp do_fac_reset_fw(context, reboot \\ false) do
|
||||
Logger.info(">> Going to reset my arduino!", type: :warn)
|
||||
params =
|
||||
Farmbot.BotState.get_all_mcu_params(context)
|
||||
context
|
||||
|> Farmbot.BotState.get_all_mcu_params()
|
||||
|> Enum.map(fn({key, _value}) ->
|
||||
if key do
|
||||
param = key |> String.to_existing_atom()
|
||||
|
|
|
@ -18,6 +18,7 @@ defmodule Farmbot.CeleryScript.Command.MoveRelative do
|
|||
"""
|
||||
@spec run(%{speed: number, x: x, y: y, z: z}, [], Ast.context)
|
||||
:: Ast.context
|
||||
|
||||
def run(%{speed: speed, x: x, y: y, z: z}, [], context) do
|
||||
# make a coordinate of the relative movement we want to do
|
||||
loc = %{x: x, y: y, z: z}
|
||||
|
@ -28,9 +29,9 @@ defmodule Farmbot.CeleryScript.Command.MoveRelative do
|
|||
[cur_x,cur_y,cur_z] = Farmbot.BotState.get_current_pos(context)
|
||||
|
||||
# Make another coord for the offset
|
||||
args = %{x: cur_x, y: cur_y, z: cur_z}
|
||||
new_context3 = Command.coordinate(args, [], new_context2)
|
||||
{offset, new_context4} = Farmbot.Context.pop_data(new_context3)
|
||||
coord_args = %{x: cur_x, y: cur_y, z: cur_z}
|
||||
new_context3 = Command.coordinate(coord_args, [], new_context2)
|
||||
{offset, new_context4} = Farmbot.Context.pop_data(new_context3)
|
||||
|
||||
args = %{speed: speed, offset: offset, location: location}
|
||||
Command.move_absolute(args, [], new_context4)
|
||||
|
|
|
@ -23,7 +23,9 @@ defmodule Farmbot.CeleryScript.Command.RpcRequest do
|
|||
Command.do_command(ast, context)
|
||||
{[ast | win], fail}
|
||||
else
|
||||
new_context = Command.explanation(%{message: "unhandled: #{fun_name}"}, [], context)
|
||||
new_context = Command.explanation(
|
||||
%{message: "unhandled: #{fun_name}"}, [], context)
|
||||
|
||||
{exp, _} = Farmbot.Context.pop_data(new_context)
|
||||
Logger.error ">> got an unhandled rpc "
|
||||
<> "request: #{fun_name} #{inspect ast}"
|
||||
|
@ -39,17 +41,17 @@ defmodule Farmbot.CeleryScript.Command.RpcRequest do
|
|||
Ast.context) :: Ast.context
|
||||
defp handle_req({_, []}, id, context) do
|
||||
# there were no failed asts.
|
||||
context = Command.rpc_ok(%{label: id}, [], context)
|
||||
{item, context} = Farmbot.Context.pop_data(context)
|
||||
context1 = Command.rpc_ok(%{label: id}, [], context)
|
||||
{item, context2} = Farmbot.Context.pop_data(context1)
|
||||
Farmbot.Transport.emit(item)
|
||||
context
|
||||
context2
|
||||
end
|
||||
|
||||
defp handle_req({_, failed}, id, context) do
|
||||
# there were some failed asts.
|
||||
context = Command.rpc_error(%{label: id}, failed, context)
|
||||
{item, context} = Farmbot.Context.pop_data(context)
|
||||
context1 = Command.rpc_error(%{label: id}, failed, context)
|
||||
{item, context2} = Farmbot.Context.pop_data(context1)
|
||||
Farmbot.Transport.emit(item)
|
||||
context
|
||||
context2
|
||||
end
|
||||
end
|
||||
|
|
|
@ -20,15 +20,18 @@ defmodule Farmbot.CeleryScript.Command.SendMessage do
|
|||
# "error"
|
||||
@type message_channel :: Logger.Backends.FarmbotLogger.rpc_message_channel
|
||||
|
||||
@spec run(%{message: String.t, message_type: message_type}, [Ast.t], Ast.context)
|
||||
:: Ast.context
|
||||
@spec run(%{message: String.t, message_type: message_type},
|
||||
[Ast.t], Ast.context) :: Ast.context
|
||||
|
||||
def run(%{message: m, message_type: m_type}, channels, context) do
|
||||
rendered = Mustache.render(m, get_message_stuff(context))
|
||||
Logger.info ">> #{rendered}", type: m_type, channels: parse_channels(channels)
|
||||
Logger.info ">> #{rendered}",
|
||||
type: m_type, channels: parse_channels(channels)
|
||||
context
|
||||
end
|
||||
|
||||
@spec get_message_stuff(Ast.context) :: %{x: Command.x, y: Command.y, z: Command.z}
|
||||
@spec get_message_stuff(Ast.context)
|
||||
:: %{x: Command.x, y: Command.y, z: Command.z}
|
||||
defp get_message_stuff(context) do
|
||||
[x, y, z] = Farmbot.BotState.get_current_pos(context)
|
||||
%{x: x, y: y, z: z}
|
||||
|
|
|
@ -213,7 +213,7 @@ defmodule Farmbot.Configurator.Router do
|
|||
|
||||
## PRIVATE.
|
||||
|
||||
defp context(), do: Context.new()
|
||||
defp context, do: Context.new()
|
||||
|
||||
defp make_json(conn), do: conn |> put_resp_content_type("application/json")
|
||||
|
||||
|
|
|
@ -22,7 +22,11 @@ defmodule Farmbot.Context do
|
|||
|
||||
defimpl Inspect, for: __MODULE__ do
|
||||
def inspect(thing, _) do
|
||||
default_context = Farmbot.Context.new() |> Map.from_struct |> Map.delete(:data_stack)
|
||||
default_context =
|
||||
Farmbot.Context.new()
|
||||
|> Map.from_struct
|
||||
|> Map.delete(:data_stack)
|
||||
|
||||
thing = thing |> Map.from_struct() |> Map.delete(:data_stack)
|
||||
if thing == default_context do
|
||||
"#Context<default>"
|
||||
|
@ -76,7 +80,6 @@ defmodule Farmbot.Context do
|
|||
data_stack: [Ast.t]
|
||||
}
|
||||
|
||||
|
||||
@spec push_data(t, Ast.t) :: t
|
||||
def push_data(%__MODULE__{} = context, %Ast{} = data) do
|
||||
new_ds = [data | context.data_stack]
|
||||
|
|
|
@ -8,7 +8,6 @@ defmodule Farmbot.Database.Selectors do
|
|||
alias Farmbot.Context
|
||||
alias Syncable.Point
|
||||
|
||||
|
||||
@spec find_point(Context.t, binary, integer) :: Syncable.t
|
||||
# TODO(Rick): Add pattern match to DB param?
|
||||
def find_point(context, "Plant" = pt, id), do: _find_point(context, pt, id)
|
||||
|
|
|
@ -21,7 +21,6 @@ defmodule Farmbot.DebugLog do
|
|||
def color(:GRAY), do: "\e[0;30m"
|
||||
def color(:LIGHT_GRAY), do: "\e[0;37m"
|
||||
|
||||
|
||||
@doc """
|
||||
enables the `debug_log/1` function.
|
||||
"""
|
||||
|
@ -36,7 +35,8 @@ defmodule Farmbot.DebugLog do
|
|||
|
||||
if unquote(color) do
|
||||
defp debug_log(str) do
|
||||
GenEvent.notify Farmbot.DebugLog, {get_module(), {unquote(color), str}}
|
||||
GenEvent.notify(Farmbot.DebugLog,
|
||||
{get_module(), {unquote(color), str}})
|
||||
end
|
||||
else
|
||||
defp debug_log(str) do
|
||||
|
|
|
@ -7,7 +7,9 @@ defmodule Farmbot.EventSupervisor do
|
|||
alias Database.Syncable.{Sequence, Regimen}
|
||||
alias Farmbot.Context
|
||||
|
||||
@callback add_child(Context.t, Regimen.t | Sequence.t, DateTime.t) :: Spec.spec
|
||||
@callback add_child(Context.t, Regimen.t | Sequence.t, DateTime.t)
|
||||
:: Spec.spec
|
||||
|
||||
@callback remove_child(Context.t, Regimen.t | Sequence.t)
|
||||
:: :ok | {:error, error} when error: :not_found | :simple_one_for_one
|
||||
end
|
||||
|
|
|
@ -7,8 +7,10 @@ defmodule Farmbot.FarmEvent.Supervisor do
|
|||
|
||||
def init(context) do
|
||||
children = [
|
||||
worker(Farmbot.Regimen.Supervisor, [context, [name: Farmbot.Regimen.Supervisor ]], [restart: :permanent]),
|
||||
worker(Farmbot.FarmEventRunner, [context, [name: Farmbot.FarmEventRunner ]], [restart: :permanent])
|
||||
worker(Farmbot.Regimen.Supervisor,
|
||||
[context, [name: Farmbot.Regimen.Supervisor ]], [restart: :permanent]),
|
||||
worker(Farmbot.FarmEventRunner,
|
||||
[context, [name: Farmbot.FarmEventRunner ]], [restart: :permanent])
|
||||
]
|
||||
opts = [strategy: :one_for_one]
|
||||
supervise(children, opts)
|
||||
|
|
|
@ -29,8 +29,10 @@ defmodule Farmbot.FarmEventRunner do
|
|||
def handle_info(:checkup, {context, state}) do
|
||||
now = get_now()
|
||||
new_state = if now do
|
||||
all_events = Database.get_all(context, FarmEvent)
|
||||
|> Enum.map(fn(thing) -> thing.body end)
|
||||
all_events =
|
||||
context
|
||||
|> Database.get_all(FarmEvent)
|
||||
|> Enum.map(fn(thing) -> thing.body end)
|
||||
|
||||
{late_events, new} = do_checkup(context, all_events, now, state)
|
||||
unless Enum.empty?(late_events) do
|
||||
|
@ -46,7 +48,8 @@ defmodule Farmbot.FarmEventRunner do
|
|||
{:noreply, {context, new_state}}
|
||||
end
|
||||
|
||||
@spec start_events(Context.t, [Sequence.t | Regimen.t], DateTime.t) :: no_return
|
||||
@spec start_events(Context.t, [Sequence.t | Regimen.t], DateTime.t)
|
||||
:: no_return
|
||||
defp start_events(_context, [], _now), do: :ok
|
||||
defp start_events(context, [event | rest], now) do
|
||||
cond do
|
||||
|
@ -84,8 +87,12 @@ defmodule Farmbot.FarmEventRunner do
|
|||
|
||||
defp do_checkup(_, [], _now, late_events, state), do: {late_events, state}
|
||||
|
||||
defp do_checkup(%Context{} = ctx, [farm_event | rest], now, late_events, state) do
|
||||
{new_late, last_time} = check_event(ctx, farm_event, now, state[farm_event.id])
|
||||
defp do_checkup(%Context{} = ctx,
|
||||
[farm_event | rest], now, late_events, state)
|
||||
do
|
||||
{new_late, last_time} = check_event(ctx,
|
||||
farm_event, now, state[farm_event.id])
|
||||
|
||||
new_state = Map.put(state, farm_event.id, last_time)
|
||||
if new_late do
|
||||
do_checkup(ctx, rest, now, [new_late | late_events], new_state)
|
||||
|
|
|
@ -108,8 +108,8 @@ defmodule Farmware do
|
|||
path = FS.path() <> "/farmware/#{package_name}"
|
||||
if File.exists?(path) do
|
||||
Logger.info "uninstalling farmware: #{package_name}", type: :busy
|
||||
info = Farmbot.BotState.ProcessTracker.lookup(ctx, :farmware, package_name)
|
||||
deregister(ctx, info)
|
||||
i = Farmbot.BotState.ProcessTracker.lookup(ctx, :farmware, package_name)
|
||||
deregister(ctx, i)
|
||||
FS.transaction fn() ->
|
||||
File.rm_rf!(path)
|
||||
end, true
|
||||
|
|
|
@ -95,8 +95,6 @@ defmodule Farmware.FarmScript do
|
|||
spawn fn() -> handle_script_output(stuff, thing, ctx) end
|
||||
handle_port(port, thing, ctx)
|
||||
|
||||
_something -> handle_port(port, thing, ctx)
|
||||
|
||||
after
|
||||
@clean_up_timeout ->
|
||||
Logger.error ">> [#{thing.name}] Timed out"
|
||||
|
|
|
@ -33,7 +33,8 @@ defmodule Farmware.Supervisor do
|
|||
defp register_farmwares(%Context{} = ctx) do
|
||||
farmwares = File.ls!(FS.path() <> "/farmware")
|
||||
for farmware <- farmwares do
|
||||
Farmbot.BotState.ProcessTracker.register(ctx.process_tracker, :farmware, farmware, farmware)
|
||||
Farmbot.BotState.ProcessTracker.register(ctx.process_tracker,
|
||||
:farmware, farmware, farmware)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -20,8 +20,6 @@ defmodule Farmware.Tracker do
|
|||
defstruct [queue: [], worker: nil, context: nil]
|
||||
end
|
||||
|
||||
|
||||
|
||||
def init(context) do
|
||||
Logger.info "Starting Farmware Tracker"
|
||||
unless File.exists?("/tmp/images"), do: File.mkdir_p("/tmp/images")
|
||||
|
|
|
@ -19,7 +19,11 @@ defmodule Farmware.Worker do
|
|||
|
||||
def init(context) do
|
||||
Logger.info "Starting Farmware Worker"
|
||||
{:consumer, %{env: initial_env(context), context: context}, subscribe_to: [@tracker]}
|
||||
|
||||
{
|
||||
:consumer,
|
||||
%{env: initial_env(context), context: context}, subscribe_to: [@tracker]
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
|
|
@ -47,8 +47,8 @@ defmodule Farmbot.HTTP do
|
|||
def process_status_code(code), do: code
|
||||
|
||||
@spec build_auth(Context.t) :: {:ok, [any]} | {:error, :no_token}
|
||||
defp build_auth(%Context{} = context) do
|
||||
with {:ok, %Token{} = token} <- Auth.get_token(context.auth)
|
||||
defp build_auth(%Context{} = ctx) do
|
||||
with {:ok, %Token{} = token} <- Auth.get_token(ctx.auth)
|
||||
do
|
||||
{:ok,
|
||||
["Content-Type": "application/json",
|
||||
|
@ -103,8 +103,8 @@ defmodule Farmbot.HTTP do
|
|||
# We only want to upload if we get a 2XX response.
|
||||
defp finish_upload({:ok, %HTTPoison.Response{status_code: s}}, attachment_url)
|
||||
when s < 300 do
|
||||
context = Context.new()
|
||||
[x, y, z] = Farmbot.BotState.get_current_pos(context)
|
||||
ctx = Context.new()
|
||||
[x, y, z] = Farmbot.BotState.get_current_pos(ctx)
|
||||
meta = %{x: x, y: y, z: z}
|
||||
json = Poison.encode! %{"attachment_url" => attachment_url,
|
||||
"meta" => meta}
|
||||
|
|
|
@ -2,7 +2,7 @@ defmodule Farmbot.RegimenRunner do
|
|||
@moduledoc """
|
||||
Runs a regimen
|
||||
"""
|
||||
|
||||
|
||||
use GenServer
|
||||
alias Farmbot.Regimen.Supervisor, as: RegSup
|
||||
require Logger
|
||||
|
|
|
@ -59,7 +59,8 @@ defmodule Farmbot.Serial.Handler do
|
|||
@doc """
|
||||
Starts a UART GenServer
|
||||
"""
|
||||
def start_link(%Context{} = ctx, nerves, tty, opts) when is_pid(nerves) and is_binary(tty) do
|
||||
def start_link(%Context{} = ctx, nerves, tty, opts)
|
||||
when is_pid(nerves) and is_binary(tty) do
|
||||
GenServer.start_link(__MODULE__, {ctx, nerves, tty}, opts)
|
||||
end
|
||||
|
||||
|
@ -252,7 +253,9 @@ defmodule Farmbot.Serial.Handler do
|
|||
def handle_info({:nerves_uart, _, str}, state) when is_binary(str) do
|
||||
debug_log "Reading: #{str}"
|
||||
try do
|
||||
current = str |> Parser.parse_code |> do_handle(state.current, state.context)
|
||||
current = str
|
||||
|> Parser.parse_code
|
||||
|> do_handle(state.current, state.context)
|
||||
{:noreply, %{state | current: current, status: current[:status] || :idle}}
|
||||
rescue
|
||||
e ->
|
||||
|
|
|
@ -2,7 +2,6 @@ defmodule Module.concat([Farmbot,System,"host"]) do
|
|||
@moduledoc false
|
||||
@behaviour Farmbot.System
|
||||
|
||||
|
||||
def reboot, do: :ok
|
||||
def power_off, do: :ok
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ defmodule Farmbot.System.Updates do
|
|||
@spec mod(atom) :: atom
|
||||
defp mod(target), do: Module.concat([Farmbot, System, target, Updates])
|
||||
|
||||
defp releases_url() do
|
||||
defp releases_url do
|
||||
context = Farmbot.Context.new()
|
||||
{:ok, token} = Farmbot.Auth.get_token(context.auth)
|
||||
token.unencoded.os_update_server
|
||||
|
|
|
@ -24,11 +24,16 @@ defmodule Farmbot.Transport.Supervisor do
|
|||
@doc """
|
||||
Starts all the transports.
|
||||
"""
|
||||
def start_link(context, opts), do: Supervisor.start_link(__MODULE__, context, opts)
|
||||
def start_link(context, opts),
|
||||
do: Supervisor.start_link(__MODULE__, context, opts)
|
||||
|
||||
@spec build_children([atom], Context.t) :: [Supervisor.child]
|
||||
defp build_children(transports, %Context{} = context) do
|
||||
[worker(Farmbot.Transport, [context, [name: Farmbot.Transport]], restart: :permanent)] ++
|
||||
[
|
||||
worker(Farmbot.Transport,
|
||||
[context, [name: Farmbot.Transport]],
|
||||
restart: :permanent)
|
||||
] ++
|
||||
Enum.map(transports, fn(t) ->
|
||||
case t do
|
||||
module when is_atom(module) ->
|
||||
|
|
Loading…
Reference in New Issue