Fix more namespaces

pull/974/head
connor rigby 2019-03-11 08:26:56 -07:00 committed by Connor Rigby
parent 95bd827518
commit 218b33785d
No known key found for this signature in database
GPG Key ID: 29A88B24B70456E0
43 changed files with 128 additions and 72 deletions

View File

@ -1,5 +1,9 @@
use Mix.Config use Mix.Config
config :logger, config :logger,
handle_otp_reports: true, handle_otp_reports: false,
handle_sasl_reports: true handle_sasl_reports: false
config :lagger,
handlers: [],
extra_sinks: []

View File

@ -1,7 +1,12 @@
defmodule FarmbotCore do defmodule FarmbotCore do
@moduledoc """ @moduledoc """
Core Farmbot Services. Core Farmbot Services.
This includes Logging, Configuration, Asset management and Firmware. This includes
* Core global state management
* Data storage management
* Firmware management
* RPC and IPC management
""" """
use Application use Application
@ -12,15 +17,10 @@ defmodule FarmbotCore do
children = [ children = [
FarmbotCore.EctoMigrator, FarmbotCore.EctoMigrator,
# TODO(Connor) - Put these in their own supervisor FarmbotCore.BotState.Supervisor,
FarmbotCore.BotState, FarmbotCore.StorageSupervisor,
FarmbotCore.BotState.FileSystem, FarmbotCeleryScript.Scheduler
FarmbotCore.Logger.Supervisor,
FarmbotCore.Config.Supervisor,
FarmbotCore.Asset.Supervisor,
FarmbotCore.FirmwareSupervisor,
FarmbotCeleryScript.Scheduler,
] ]
Supervisor.init(children, [strategy: :one_for_all]) Supervisor.init(children, [strategy: :one_for_one])
end end
end end

View File

@ -21,6 +21,7 @@ defimpl FarmbotCore.AssetWorker, for: FarmbotCore.Asset.FbosConfig do
end end
def handle_info(:timeout, %FbosConfig{} = fbos_config) do def handle_info(:timeout, %FbosConfig{} = fbos_config) do
Logger.warn "Setting config to state"
:ok = BotState.set_config_value(:arduino_debug_messages, fbos_config.arduino_debug_messages) :ok = BotState.set_config_value(:arduino_debug_messages, fbos_config.arduino_debug_messages)
:ok = BotState.set_config_value(:auto_sync, fbos_config.auto_sync) :ok = BotState.set_config_value(:auto_sync, fbos_config.auto_sync)
:ok = BotState.set_config_value(:beta_opt_in, fbos_config.beta_opt_in) :ok = BotState.set_config_value(:beta_opt_in, fbos_config.beta_opt_in)
@ -40,6 +41,7 @@ defimpl FarmbotCore.AssetWorker, for: FarmbotCore.Asset.FbosConfig do
current_hardware = get_config_value(:string, "settings", "firmware_hardware") current_hardware = get_config_value(:string, "settings", "firmware_hardware")
if current_hardware != target_hardware do if current_hardware != target_hardware do
raise("firmware hardware change")
Logger.debug("Updating firmware_hardware from #{current_hardware} to #{target_hardware}") Logger.debug("Updating firmware_hardware from #{current_hardware} to #{target_hardware}")
update_config_value(:string, "settings", "firmware_hardware", target_hardware) update_config_value(:string, "settings", "firmware_hardware", target_hardware)
end end

View File

@ -0,0 +1,15 @@
defmodule FarmbotCore.BotState.Supervisor do
use Supervisor
def start_link(args) do
Supervisor.start_link(__MODULE__, args, [name: __MODULE__])
end
def init([]) do
children = [
FarmbotCore.BotState,
FarmbotCore.BotState.FileSystem
]
Supervisor.init(children, [strategy: :one_for_one])
end
end

View File

@ -0,0 +1,16 @@
defmodule FarmbotCore.StorageSupervisor do
use Supervisor
def start_link(args) do
Supervisor.start_link(__MODULE__, args, [name: __MODULE__])
end
def init([]) do
children = [
FarmbotCore.Logger.Supervisor,
FarmbotCore.Config.Supervisor,
FarmbotCore.Asset.Supervisor
]
Supervisor.init(children, [strategy: :one_for_one])
end
end

View File

@ -65,7 +65,6 @@ defmodule FarmbotCore.MixProject do
defp deps do defp deps do
[ [
{:farmbot_celery_script, path: "../farmbot_celery_script", env: Mix.env()}, {:farmbot_celery_script, path: "../farmbot_celery_script", env: Mix.env()},
{:farmbot_firmware, path: "../farmbot_firmware", env: Mix.env()},
{:elixir_make, "~> 0.4", runtime: false}, {:elixir_make, "~> 0.4", runtime: false},
{:sqlite_ecto2, "~> 2.3"}, {:sqlite_ecto2, "~> 2.3"},
{:timex, "~> 3.4"}, {:timex, "~> 3.4"},

View File

@ -30,5 +30,8 @@ config :farmbot_core, FarmbotCore.Core.CeleryScript.RunTimeWrapper,
config :farmbot_core, FarmbotCore.EctoMigrator, config :farmbot_core, FarmbotCore.EctoMigrator,
default_firmware_io_logs: false, default_firmware_io_logs: false,
default_server: "https://my.farm.bot", default_server: "https://my.farm.bot",
default_dns_name: "my.farm.bot",
default_ntp_server_1: "0.pool.ntp.org",
default_ntp_server_2: "1.pool.ntp.org",
default_currently_on_beta: default_currently_on_beta:
String.contains?(to_string(:os.cmd('git rev-parse --abbrev-ref HEAD')), "beta") String.contains?(to_string(:os.cmd('git rev-parse --abbrev-ref HEAD')), "beta")

View File

@ -1,5 +1,13 @@
use Mix.Config use Mix.Config
config :logger,
handle_otp_reports: false,
handle_sasl_reports: false
config :lager, :error_logger_redirect, false config :lager, :error_logger_redirect, false
config :lager, :error_logger_whitelist, [] config :lager, :error_logger_whitelist, []
config :lager, :crash_log, false config :lager, :crash_log, false
config :lager, :handlers, []
config :lager,
handlers: [],
extra_sinks: []

View File

@ -8,7 +8,7 @@ defmodule FarmbotExt.AMQP.AutoSyncTransport do
} }
alias FarmbotExt.AMQP.ConnectionWorker alias FarmbotExt.AMQP.ConnectionWorker
alias FarmbotExt.API.EagerLoader alias FarmbotExt.API.{Preloader, EagerLoader}
require Logger require Logger
require FarmbotCore.Logger require FarmbotCore.Logger
@ -23,8 +23,23 @@ defmodule FarmbotExt.AMQP.AutoSyncTransport do
} }
@exchange "amq.topic" @exchange "amq.topic"
@known_kinds ~w(
Device
FarmEvent
FarmwareEnv
FarmwareInstallation
FbosConfig
FirmwareConfig
Peripheral
PinBinding
Point
Regimen
Sensor
Sequence
Tool
)
defstruct [:conn, :chan, :jwt] defstruct [:conn, :chan, :jwt, :preloaded]
alias __MODULE__, as: State alias __MODULE__, as: State
@doc false @doc false
@ -35,7 +50,7 @@ defmodule FarmbotExt.AMQP.AutoSyncTransport do
def init(args) do def init(args) do
Process.flag(:sensitive, true) Process.flag(:sensitive, true)
jwt = Keyword.fetch!(args, :jwt) jwt = Keyword.fetch!(args, :jwt)
{:ok, %State{conn: nil, chan: nil, jwt: jwt}, 1000} {:ok, %State{conn: nil, chan: nil, jwt: jwt, preloaded: false}, 1000}
end end
def terminate(reason, state) do def terminate(reason, state) do
@ -44,7 +59,12 @@ defmodule FarmbotExt.AMQP.AutoSyncTransport do
if state.chan, do: AMQP.Channel.close(state.chan) if state.chan, do: AMQP.Channel.close(state.chan)
end end
def handle_info(:timeout, state) do def handle_info(:timeout, %{preloaded: false} = state) do
:ok = Preloader.preload_all()
{:noreply, %{state | preloaded: true}, 0}
end
def handle_info(:timeout, %{preloaded: true} = state) do
jwt = state.jwt jwt = state.jwt
bot = jwt.bot bot = jwt.bot
auto_sync = bot <> "_auto_sync" auto_sync = bot <> "_auto_sync"
@ -85,15 +105,21 @@ defmodule FarmbotExt.AMQP.AutoSyncTransport do
def handle_info({:basic_deliver, payload, %{routing_key: key}}, state) do def handle_info({:basic_deliver, payload, %{routing_key: key}}, state) do
device = state.jwt.bot device = state.jwt.bot
data = JSON.decode!(payload)
label = data["args"]["label"]
case String.split(key, ".") do case String.split(key, ".") do
["bot", ^device, "sync", asset_kind, id_str] -> ["bot", ^device, "sync", asset_kind, id_str] when asset_kind in @known_kinds ->
asset_kind = Module.concat([Farmbot, Asset, asset_kind]) asset_kind = Module.concat([Farmbot, Asset, asset_kind])
data = JSON.decode!(payload)
id = data["id"] || String.to_integer(id_str) id = data["id"] || String.to_integer(id_str)
params = data["body"] params = data["body"]
label = data["args"]["label"] :ok = handle_asset(asset_kind, label, id, params, state)
handle_asset(asset_kind, label, id, params, state)
_ ->
Logger.info("ignoring router: #{key}")
json = JSON.encode!(%{args: %{label: label}, kind: "rpc_ok"})
:ok = Basic.publish(state.chan, @exchange, "bot.#{device}.from_device", json)
{:noreply, state}
end end
end end
@ -128,27 +154,23 @@ defmodule FarmbotExt.AMQP.AutoSyncTransport do
:ok :ok
auto_sync? -> auto_sync? ->
if Code.ensure_loaded?(asset_kind) do case Repo.get_by(asset_kind, id: id) do
case Repo.get_by(asset_kind, id: id) do nil ->
nil -> struct(asset_kind)
struct(asset_kind) |> asset_kind.changeset(params)
|> asset_kind.changeset(params) |> Repo.insert!()
|> Repo.insert!()
asset -> asset ->
asset_kind.changeset(asset, params) asset_kind.changeset(asset, params)
|> Repo.update!() |> Repo.update!()
end
end end
:ok
true -> true ->
if Code.ensure_loaded?(asset_kind) do asset = Repo.get_by(asset_kind, id: id) || struct(asset_kind)
asset = Repo.get_by(asset_kind, id: id) || struct(asset_kind) changeset = asset_kind.changeset(asset, params)
changeset = asset_kind.changeset(asset, params) :ok = EagerLoader.cache(changeset)
:ok = EagerLoader.cache(changeset)
else
:ok
end
end end
device = state.jwt.bot device = state.jwt.bot

View File

@ -1,4 +1,4 @@
defmodule FarmbotExt.Bootstrap.APITask do defmodule FarmbotExt.API.Preloader do
@moduledoc """ @moduledoc """
Task to ensure Farmbot has synced: Task to ensure Farmbot has synced:
* FarmbotCore.Asset.Device * FarmbotCore.Asset.Device
@ -17,18 +17,12 @@ defmodule FarmbotExt.Bootstrap.APITask do
Asset.Sync Asset.Sync
} }
def child_spec(_) do @doc """
%{ Syncronous call to sync or preload assets.
id: __MODULE__, Starts with `group_0` to check if `auto_sync` is enabled. If it is,
start: {__MODULE__, :sync_all, []}, actually sync all resources. If it is not, preload all resources.
type: :worker, """
restart: :transient, def preload_all() do
shutdown: 500
}
end
@doc false
def sync_all() do
sync_changeset = API.get_changeset(Sync) sync_changeset = API.get_changeset(Sync)
sync = Changeset.apply_changes(sync_changeset) sync = Changeset.apply_changes(sync_changeset)
@ -45,8 +39,6 @@ defmodule FarmbotExt.Bootstrap.APITask do
:ok = maybe_auto_sync(sync_changeset, auto_sync_change || Asset.fbos_config().auto_sync) :ok = maybe_auto_sync(sync_changeset, auto_sync_change || Asset.fbos_config().auto_sync)
end end
:ignore
end end
# When auto_sync is enabled, do the full sync. # When auto_sync is enabled, do the full sync.

View File

@ -10,7 +10,6 @@ defmodule FarmbotExt.Bootstrap.Supervisor do
children = [ children = [
FarmbotExt.API.EagerLoader.Supervisor, FarmbotExt.API.EagerLoader.Supervisor,
FarmbotExt.API.DirtyWorker.Supervisor, FarmbotExt.API.DirtyWorker.Supervisor,
FarmbotExt.Bootstrap.APITask,
FarmbotExt.AMQP.Supervisor, FarmbotExt.AMQP.Supervisor,
FarmbotExt.API.ImageUploader FarmbotExt.API.ImageUploader
] ]

View File

@ -23,14 +23,14 @@ config :farmbot_core, FarmbotCore.Asset.Repo,
config :farmbot, FarmbotOS.Init.Supervisor, config :farmbot, FarmbotOS.Init.Supervisor,
init_children: [ init_children: [
FarmbotOS.TTYDetector, FarmbotOS.FirmwareTTYDetector,
FarmbotOS.Platform.Host.Configurator FarmbotOS.Platform.Host.Configurator
] ]
config :farmbot, config :farmbot,
ecto_repos: [FarmbotCore.Config.Repo, FarmbotCore.Logger.Repo, FarmbotCore.Asset.Repo] ecto_repos: [FarmbotCore.Config.Repo, FarmbotCore.Logger.Repo, FarmbotCore.Asset.Repo]
config :farmbot, FarmbotOS.TTYDetector, config :farmbot, FarmbotOS.FirmwareTTYDetector,
expected_names: [ expected_names: [
System.get_env("FARMBOT_TTY") System.get_env("FARMBOT_TTY")
] ]

View File

@ -1,4 +1,4 @@
defmodule FarmbotCore.FirmwareEstopTimer do defmodule FarmbotOS.FirmwareEstopTimer do
@moduledoc """ @moduledoc """
Process that wraps a `Process.send_after/3` call. Process that wraps a `Process.send_after/3` call.
When `:timeout` is received, a `fatal_email` log message will be When `:timeout` is received, a `fatal_email` log message will be

View File

@ -1,9 +1,10 @@
defmodule FarmbotCore.FirmwareSideEffects do defmodule FarmbotOS.FirmwareSideEffects do
@moduledoc "Handles firmware data and syncing it with BotState." @moduledoc "Handles firmware data and syncing it with BotState."
@behaviour FarmbotFirmware.SideEffects @behaviour FarmbotFirmware.SideEffects
require Logger require Logger
require FarmbotCore.Logger require FarmbotCore.Logger
alias FarmbotCore.{Asset, BotState, FirmwareEstopTimer} alias FarmbotCore.{Asset, BotState}
alias FarmbotOS.FirmwareEstopTimer
def handle_position(x: x, y: y, z: z) do def handle_position(x: x, y: y, z: z) do
:ok = BotState.set_position(x, y, z) :ok = BotState.set_position(x, y, z)

View File

@ -1,4 +1,4 @@
defmodule FarmbotOS.TTYDetector do defmodule FarmbotOS.FirmwareTTYDetector do
use GenServer use GenServer
require Logger require Logger
require FarmbotCore.Logger require FarmbotCore.Logger

View File

@ -47,7 +47,7 @@ defmodule FarmbotOS.Init.FSCheckup do
File.rm_rf(fw) File.rm_rf(fw)
end end
init_logger_backend_ecto() init_logger_backend_sqlite()
:ok :ok
err -> err ->
@ -57,7 +57,7 @@ defmodule FarmbotOS.Init.FSCheckup do
end end
end end
defp init_logger_backend_ecto do defp init_logger_backend_sqlite do
Logger.flush() Logger.flush()
try do try do

View File

@ -11,7 +11,7 @@ defmodule FarmbotOS.Init.Supervisor do
children = children =
(config[:init_children] || []) ++ (config[:init_children] || []) ++
[ [
{FarmbotOS.Init.FSCheckup, []} FarmbotOS.Init.FSCheckup
] ]
Supervisor.init(children, strategy: :one_for_all) Supervisor.init(children, strategy: :one_for_all)

View File

@ -48,6 +48,7 @@ defmodule FarmbotOS.MixProject do
# Farmbot stuff # Farmbot stuff
{:farmbot_core, path: "../farmbot_core", env: Mix.env()}, {:farmbot_core, path: "../farmbot_core", env: Mix.env()},
{:farmbot_ext, path: "../farmbot_ext", env: Mix.env()}, {:farmbot_ext, path: "../farmbot_ext", env: Mix.env()},
{:farmbot_firmware, path: "../farmbot_firmware", env: Mix.env()},
# Nerves stuff. # Nerves stuff.
{:nerves, "~> 1.3", runtime: false}, {:nerves, "~> 1.3", runtime: false},

View File

@ -9,7 +9,7 @@
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"}, "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
"cors_plug": {:hex, :cors_plug, "2.0.0", "238ddb479f92b38f6dc1ae44b8d81f0387f9519101a6da442d543ab70ee0e482", [:mix], [{:plug, "~> 1.3 or ~> 1.4 or ~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"}, "cors_plug": {:hex, :cors_plug, "2.0.0", "238ddb479f92b38f6dc1ae44b8d81f0387f9519101a6da442d543ab70ee0e482", [:mix], [{:plug, "~> 1.3 or ~> 1.4 or ~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"db_connection": {:hex, :db_connection, "1.1.3", "89b30ca1ef0a3b469b1c779579590688561d586694a3ce8792985d4d7e575a61", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"}, "db_connection": {:hex, :db_connection, "1.1.3", "89b30ca1ef0a3b469b1c779579590688561d586694a3ce8792985d4d7e575a61", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
"decimal": {:hex, :decimal, "1.6.0", "bfd84d90ff966e1f5d4370bdd3943432d8f65f07d3bab48001aebd7030590dcc", [:mix], [], "hexpm"}, "decimal": {:hex, :decimal, "1.7.0", "30d6b52c88541f9a66637359ddf85016df9eb266170d53105f02e4a67e00c5aa", [:mix], [], "hexpm"},
"dhcp_server": {:hex, :dhcp_server, "0.6.0", "6cc0cf110b8d112455f033ae49eda570e9aeeb42a2fd1c79cc437835ecaa0716", [:make, :mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm"}, "dhcp_server": {:hex, :dhcp_server, "0.6.0", "6cc0cf110b8d112455f033ae49eda570e9aeeb42a2fd1c79cc437835ecaa0716", [:make, :mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm"},
"dialyxir": {:hex, :dialyxir, "1.0.0-rc.4", "71b42f5ee1b7628f3e3a6565f4617dfb02d127a0499ab3e72750455e986df001", [:mix], [{:erlex, "~> 0.1", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm"}, "dialyxir": {:hex, :dialyxir, "1.0.0-rc.4", "71b42f5ee1b7628f3e3a6565f4617dfb02d127a0499ab3e72750455e986df001", [:mix], [{:erlex, "~> 0.1", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm"},
"distillery": {:hex, :distillery, "2.0.12", "6e78fe042df82610ac3fa50bd7d2d8190ad287d120d3cd1682d83a44e8b34dfb", [:mix], [{:artificery, "~> 0.2", [hex: :artificery, repo: "hexpm", optional: false]}], "hexpm"}, "distillery": {:hex, :distillery, "2.0.12", "6e78fe042df82610ac3fa50bd7d2d8190ad287d120d3cd1682d83a44e8b34dfb", [:mix], [{:artificery, "~> 0.2", [hex: :artificery, repo: "hexpm", optional: false]}], "hexpm"},

View File

@ -285,13 +285,7 @@ defmodule FarmbotOS.Platform.Target.Configurator.Router do
case conn.body_params do case conn.body_params do
%{"firmware_hardware" => hw} when hw in ["arduino", "farmduino", "farmduino_k14"] -> %{"firmware_hardware" => hw} when hw in ["arduino", "farmduino", "farmduino_k14"] ->
update_config_value(:string, "settings", "firmware_hardware", hw) update_config_value(:string, "settings", "firmware_hardware", hw)
raise("configure firmware?")
if Application.get_env(:farmbot, :behaviour)[:firmware_handler] ==
FarmbotFirmware.UartHandler do
FarmbotCore.Logger.warn(1, "Updating #{hw} firmware is broke!!!!!!")
# /shrug?
# Farmbot.Firmware.UartHandler.Update.force_update_firmware(hw)
end
redir(conn, "/credentials") redir(conn, "/credentials")