diff --git a/config/test.exs b/config/test.exs index eda7f7c1..977a52f3 100644 --- a/config/test.exs +++ b/config/test.exs @@ -1,9 +1,8 @@ use Mix.Config import_config "dev.exs" -# # We hopefully don't need logger ¯\_(ツ)_/¯ -# config :logger, :console, -# format: "" +# We hopefully don't need logger ¯\_(ツ)_/¯ +config :logger, :console, format: "" config :farmbot, path: "/tmp/farmbot_test", diff --git a/lib/farmbot.ex b/lib/farmbot.ex index afa2db58..ff696d9a 100644 --- a/lib/farmbot.ex +++ b/lib/farmbot.ex @@ -33,7 +33,7 @@ defmodule Farmbot do # The worker for diffing db entries. worker(Farmbot.Database.Supervisor, [], restart: :permanent), # Handles tracking of various parts of the bots state. - supervisor(Farmbot.BotState.Supervisor, [name: Farmbot.BotState.Supervisor], restart: :permanent), + supervisor(Farmbot.BotState.Supervisor, [context, [name: Farmbot.BotState.Supervisor]], restart: :permanent), # Handles FarmEvents. supervisor(Farmbot.FarmEvent.Supervisor, [], restart: :permanent), # Handles the passing of messages from one part of the system to another. diff --git a/lib/farmbot/bot_state/process_manager/process_supervisor.ex b/lib/farmbot/bot_state/process_manager/process_supervisor.ex index bbf6413f..ccb83f79 100644 --- a/lib/farmbot/bot_state/process_manager/process_supervisor.ex +++ b/lib/farmbot/bot_state/process_manager/process_supervisor.ex @@ -10,12 +10,15 @@ defmodule Farmbot.BotState.ProcessSupervisor do @doc """ Starts the Farm Procss Supervisor """ - def start_link(%Context{} = ctx, opts), do: Supervisor.start_link(__MODULE__, ctx, opts) + def start_link(%Context{} = ctx, opts), + do: Supervisor.start_link(__MODULE__, ctx, opts) def init(ctx) do Logger.info ">> Starting FarmProcess Supervisor" children = [ - worker(Farmbot.BotState.ProcessTracker, [ctx, name: Farmbot.BotState.ProcessTracker], [restart: :permanent]) + worker(Farmbot.BotState.ProcessTracker, + [ctx, [name: Farmbot.BotState.ProcessTracker]], + [restart: :permanent]) ] opts = [strategy: :one_for_one] supervise(children, opts) diff --git a/lib/farmbot/bot_state/process_manager/process_tracker.ex b/lib/farmbot/bot_state/process_manager/process_tracker.ex index 72c542dc..0a02fed8 100644 --- a/lib/farmbot/bot_state/process_manager/process_tracker.ex +++ b/lib/farmbot/bot_state/process_manager/process_tracker.ex @@ -11,6 +11,7 @@ defmodule Farmbot.BotState.ProcessTracker do require Logger alias Nerves.Lib.UUID alias Farmbot.RegimenRunner + alias Farmbot.CeleryScript.Ast.Context defmodule Info do @moduledoc false @@ -42,8 +43,8 @@ defmodule Farmbot.BotState.ProcessTracker do @doc """ Starts the Process Tracker """ - @spec start_link :: {:ok, pid} - def start_link, do: GenServer.start_link(__MODULE__, [], name: __MODULE__) + def start_link(%Context{} = context, opts), + do: GenServer.start_link(__MODULE__, [], opts) @doc """ Registers a kind, name with a database entry to be tracked diff --git a/lib/farmbot/bot_state/supervisor.ex b/lib/farmbot/bot_state/supervisor.ex index 08dbe20b..697f17a5 100644 --- a/lib/farmbot/bot_state/supervisor.ex +++ b/lib/farmbot/bot_state/supervisor.ex @@ -13,10 +13,10 @@ 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(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] diff --git a/lib/farmbot/celery_script/ast/context.ex b/lib/farmbot/celery_script/ast/context.ex index 53b015d1..fcd56e99 100644 --- a/lib/farmbot/celery_script/ast/context.ex +++ b/lib/farmbot/celery_script/ast/context.ex @@ -5,8 +5,10 @@ defmodule Farmbot.CeleryScript.Ast.Context do alias Farmbot.CeleryScript.Ast - @enforce_keys [:auth, :database, :network, :serial, :hardware, :configuration] - defstruct [:auth, :database, :network, :serial, :hardware, :configuration, data_stack: []] + @enforce_keys [:auth, :database, :network, :serial, :hardware, + :monitor, :configuration] + defstruct [:auth, :database, :network, :serial, :hardware, + :monitor,:configuration, data_stack: []] @typedoc false @type database :: Farmbot.Database.database @@ -23,6 +25,9 @@ defmodule Farmbot.CeleryScript.Ast.Context do @typedoc false @type hardware :: Farmbot.BotState.Hardware.hardware + @typedoc false + @type monitor :: Farmbot.BotState.Monitor.monitor + @typedoc false @type configuration :: Farmbot.BotState.Configuration.configuration @@ -35,8 +40,9 @@ defmodule Farmbot.CeleryScript.Ast.Context do network: network, serial: serial, configuration: configuration, + monitor: monitor, hardware: hardware, - data_stack: [Ast.t] + data_stack: [Ast.t] } @@ -61,6 +67,7 @@ defmodule Farmbot.CeleryScript.Ast.Context do %__MODULE__{ data_stack: [], configuration: Farmbot.BotState.Configuration, hardware: Farmbot.BotState.Hardware, + monitor: Farmbot.BotState.Monitor, database: Farmbot.Database, network: Farmbot.System.Network, serial: Farmbot.Serial.Handler, diff --git a/lib/farmbot/farmware/worker.ex b/lib/farmbot/farmware/worker.ex index a04fc849..4abfc894 100644 --- a/lib/farmbot/farmware/worker.ex +++ b/lib/farmbot/farmware/worker.ex @@ -7,7 +7,7 @@ defmodule Farmware.Worker do use GenStage @tracker Farmware.Tracker alias Farmware.FarmScript - + alias Farmbot.CeleryScript.Ast.Context @type env :: map @doc """ @@ -30,10 +30,11 @@ defmodule Farmware.Worker do @spec initial_env :: env defp initial_env do + context = Context.new() %{"WRITE_PATH" => "/tmp", # common write path (non persistant) "BEGIN_CS" => "<<< ", # some in band signaling for executing celeryscript. "IMAGES" => "/tmp/images"} # Dump images here to upload them to the api - |> Map.merge(Farmbot.BotState.get_config(:user_env)) + |> Map.merge(Farmbot.BotState.get_config(context, :user_env)) end # when a queue of scripts comes in execute them in order diff --git a/lib/farmbot/http.ex b/lib/farmbot/http.ex index 9ffb9a35..87f58b3f 100644 --- a/lib/farmbot/http.ex +++ b/lib/farmbot/http.ex @@ -103,9 +103,11 @@ 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 - [x, y, z] = Farmbot.BotState.get_current_pos - meta = %{x: x, y: y, z: z} - json = Poison.encode! %{"attachment_url" => attachment_url, "meta" => meta} + context = Context.new() + [x, y, z] = Farmbot.BotState.get_current_pos(context) + meta = %{x: x, y: y, z: z} + json = Poison.encode! %{"attachment_url" => attachment_url, + "meta" => meta} Farmbot.HTTP.post "/api/images", json end end diff --git a/lib/farmbot/system/updates.ex b/lib/farmbot/system/updates.ex index 231eaf6e..24331f55 100644 --- a/lib/farmbot/system/updates.ex +++ b/lib/farmbot/system/updates.ex @@ -9,6 +9,7 @@ defmodule Farmbot.System.Updates do @path "/tmp/update.fw" require Logger alias Farmbot.System.FS + alias Farmbot.CeleryScript.Ast.Context # TODO(connor): THIS IS A MINOR IMPROVEMENT FROM THE LAST VERSION OF THIS FILE # BUT IT DEFINATELY NEEDS FURTHER REFACTORING. @@ -26,7 +27,8 @@ defmodule Farmbot.System.Updates do Checks for updates if the bot says so """ def do_update_check do - if Farmbot.BotState.get_config(:os_auto_update) do + context = Farmbot.CeleryScript.Ast.Context.new() + if Farmbot.BotState.get_config(context, :os_auto_update) do check_and_download_updates() else Logger.info ">> Will not do update check!" @@ -58,7 +60,8 @@ defmodule Farmbot.System.Updates do """ @spec check_updates :: {:update, binary} | :no_updates | {:error, term} def check_updates do - current = Farmbot.BotState.get_os_version + context = Context.new() + current = Farmbot.BotState.get_os_version(context) if String.contains?(current, "rc") do msg = "Release Candidate Releases don't currently support updates!" Logger.info msg, type: :warn diff --git a/mix.lock b/mix.lock index 22035b04..bc310a60 100644 --- a/mix.lock +++ b/mix.lock @@ -23,7 +23,7 @@ "exquisite": {:hex, :exquisite, "0.1.7", "4106503e976f409246731b168cd76eb54262bd04f4facc5cba82c2f53982aaf0", [:mix], []}, "exvcr": {:hex, :exvcr, "0.8.7", "e76f33b10dfefbcf32afa6d6867140566d0d54797e352b47485eed0241dd7edf", [:mix], [{:exactor, "~> 2.2", [hex: :exactor, optional: false]}, {:exjsx, "~> 3.2", [hex: :exjsx, optional: false]}, {:httpoison, "~> 0.8", [hex: :httpoison, optional: true]}, {:httpotion, "~> 3.0", [hex: :httpotion, optional: true]}, {:ibrowse, "~> 4.2.2", [hex: :ibrowse, optional: true]}, {:meck, "~> 0.8.3", [hex: :meck, optional: false]}]}, "faker": {:hex, :faker, "0.7.0", "2c42deeac7be717173c78c77fb3edc749fb5d5e460e33d01fe592ae99acc2f0d", [:mix], []}, - "farmbot_simulator": {:hex, :farmbot_simulator, "0.1.3", "6b1d1d6028f7fe12ea465434becee536e98a2c9eb4da8fac8deda6bf8b22cf87", [:make, :mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, optional: false]}, {:nerves_uart, "~> 0.1.2", [hex: :nerves_uart, optional: false]}]}, + "farmbot_simulator": {:git, "https://github.com/farmbot-labs/farmbot_simulator.git", "f5b878dc0d4fc845db0b678e7df9e361b3e80834", []}, "fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []}, "gen_mqtt": {:hex, :gen_mqtt, "0.3.1", "6ce6af7c2bcb125d5b4125c67c5ab1f29bcec2638236509bcc6abf510a6661ed", [:mix], [{:vmq_commons, "1.0.0", [hex: :vmq_commons, optional: false]}]}, "gen_stage": {:hex, :gen_stage, "0.11.0", "943bdfa85c75fa624e0a36a9d135baad20a523be040178f5a215444b45c66ea4", [:mix], []}, diff --git a/test/farmbot/bot_state/trackers/configuration_state_test.exs b/test/farmbot/bot_state/trackers/configuration_state_test.exs index d8811ed6..c57958b5 100644 --- a/test/farmbot/bot_state/trackers/configuration_state_test.exs +++ b/test/farmbot/bot_state/trackers/configuration_state_test.exs @@ -1,29 +1,35 @@ defmodule Farmbot.BotState.ConfigurationTest do use ExUnit.Case, async: false + alias Farmbot.CeleryScript.Ast.Context - test "makes sure we dont mess state up with bad calls or casts" do - before_call = get_state() - resp = GenServer.call(Farmbot.BotState.Configuration, :do_a_barrel_roll) - after_call = get_state() + setup_all do + cs_context = Context.new() + [cs_context: cs_context] + end + + test "makes sure we dont mess state up with bad calls or casts", context do + before_call = get_state(context.cs_context) + resp = GenServer.call(context.cs_context.configuration, :do_a_barrel_roll) + after_call = get_state(context.cs_context) assert(resp == :unhandled) assert(after_call == before_call) - GenServer.cast(Farmbot.BotState.Configuration, :bot_net_start) - after_cast = get_state() + GenServer.cast(context.cs_context.configuration, :bot_net_start) + after_cast = get_state(context.cs_context) assert(before_call == after_cast) end - test "updates a setting inside informational settings" do - old = get_state() - GenServer.cast(Farmbot.BotState.Configuration, - {:update_info, :i_know_this, :its_unix}) + test "updates a setting inside informational settings", context do + conf = context.cs_context.configuration + old = get_state(context.cs_context) + GenServer.cast(conf, {:update_info, :i_know_this, :its_unix}) # maybe bug? change this cast to a call? - new = get_state() + new = get_state(context.cs_context) assert(old != new) end - defp get_state() do + defp get_state(ctx) do Process.sleep(10) - Farmbot.BotState.Monitor.get_state() |> Map.get(:configuration) + :sys.get_state(ctx.monitor).state |> Map.fetch!(:configuration) end end