diff --git a/farmbot_core/lib/farmbot_core/asset/query.ex b/farmbot_core/lib/farmbot_core/asset/query.ex index 642b45d3..689621a4 100644 --- a/farmbot_core/lib/farmbot_core/asset/query.ex +++ b/farmbot_core/lib/farmbot_core/asset/query.ex @@ -7,8 +7,15 @@ defmodule FarmbotCore.Asset.Query do @callback auto_sync?() :: boolean() + @callback first_sync?() :: boolean() + @doc "Returns the configuration value for auto_sync" def auto_sync?() do Asset.fbos_config().auto_sync end + + @doc "Checks if initial syncing is still required" + def first_sync?() do + is_nil(Asset.fbos_config().id) + end end diff --git a/farmbot_ext/lib/farmbot_ext/amqp/auto_sync_channel.ex b/farmbot_ext/lib/farmbot_ext/amqp/auto_sync_channel.ex index 23b617cc..bd538b80 100644 --- a/farmbot_ext/lib/farmbot_ext/amqp/auto_sync_channel.ex +++ b/farmbot_ext/lib/farmbot_ext/amqp/auto_sync_channel.ex @@ -79,9 +79,13 @@ defmodule FarmbotExt.AMQP.AutoSyncChannel do def handle_info(:preload, state) do _ = Leds.green(:really_fast_blink) + # this must be called __before__ preloading. + # if it's not, it will have been reset by the time the + # preload completes + first_sync? = Asset.Query.first_sync?() with :ok <- Preloader.preload_all() do - if Asset.Query.auto_sync?() do + if Asset.Query.auto_sync?() || first_sync? do _ = Leds.green(:solid) BotState.set_sync_status("synced") else diff --git a/farmbot_ext/lib/farmbot_ext/api/preloader.ex b/farmbot_ext/lib/farmbot_ext/api/preloader.ex index d79b681f..70789d94 100644 --- a/farmbot_ext/lib/farmbot_ext/api/preloader.ex +++ b/farmbot_ext/lib/farmbot_ext/api/preloader.ex @@ -21,10 +21,19 @@ defmodule FarmbotExt.API.Preloader do actually sync all resources. If it is not, preload all resources. """ def preload_all() do + # this must be called __before__ preloading. + # if it's not, it will have been reset by the time the + # preload completes + first_sync? = Query.first_sync?() + + if first_sync? do + FarmbotCore.Logger.info(2, "Farmbot doing first sync") + end + with {:ok, sync_changeset} <- API.get_changeset(Sync), sync_changeset <- Reconciler.sync_group(sync_changeset, SyncGroup.group_0()) do FarmbotCore.Logger.success(3, "Successfully preloaded resources.") - maybe_auto_sync(sync_changeset, Query.auto_sync?()) + maybe_auto_sync(sync_changeset, Query.auto_sync?() || first_sync?) end end diff --git a/farmbot_ext/test/farmbot_ext/amqp/auto_sync_channel_test.exs b/farmbot_ext/test/farmbot_ext/amqp/auto_sync_channel_test.exs index c90b9c0b..74ce703e 100644 --- a/farmbot_ext/test/farmbot_ext/amqp/auto_sync_channel_test.exs +++ b/farmbot_ext/test/farmbot_ext/amqp/auto_sync_channel_test.exs @@ -36,6 +36,7 @@ defmodule AutoSyncChannelTest do test_pid = self() expect(Query, :auto_sync?, 2, fn -> false end) + expect(Query, :first_sync?, 2, fn -> false end) expect(API, :get_changeset, fn _module -> send(test_pid, :preload_all_called) @@ -148,6 +149,8 @@ defmodule AutoSyncChannelTest do false end) + stub(Query, :first_sync?, fn -> false end) + send(pid, {:basic_deliver, payload, %{routing_key: key}}) assert_receive :called_auto_sync? end @@ -158,6 +161,7 @@ defmodule AutoSyncChannelTest do payload = '{"args":{"label":"foo"},"body":{}}' key = "bot.device_15.sync.Device.999" stub(Query, :auto_sync?, fn -> true end) + stub(Query, :first_sync?, fn -> false end) stub(Command, :update, fn x, y, z -> send(test_pid, {:update_called, x, y, z}) @@ -175,6 +179,7 @@ defmodule AutoSyncChannelTest do key = "bot.device_15.sync.#{module_name}.999" stub(Query, :auto_sync?, fn -> true end) + stub(Query, :first_sync?, fn -> false end) stub(Command, :update, fn x, y, z -> send(test_pid, {:update_called, x, y, z}) @@ -203,6 +208,8 @@ defmodule AutoSyncChannelTest do false end) + stub(Query, :first_sync?, fn -> false end) + stub(Command, :update, fn kind, id, params -> send(test_pid, {:update_called, kind, id, params}) :ok @@ -225,6 +232,8 @@ defmodule AutoSyncChannelTest do false end) + stub(Query, :first_sync?, fn -> false end) + stub(Command, :new_changeset, fn kind, id, params -> send(test_pid, {:new_changeset_called, kind, id, params}) :ok @@ -246,6 +255,7 @@ defmodule AutoSyncChannelTest do key = "bot.device_15.sync.#{module_name}.999" stub(Query, :auto_sync?, fn -> true end) + stub(Query, :first_sync?, fn -> false end) stub(Command, :update, fn x, y, z -> send(test_pid, {:update_called, x, y, z}) diff --git a/farmbot_ext/test/farmbot_ext/api/preloader_test.exs b/farmbot_ext/test/farmbot_ext/api/preloader_test.exs index 8c98f97b..f05fc02f 100644 --- a/farmbot_ext/test/farmbot_ext/api/preloader_test.exs +++ b/farmbot_ext/test/farmbot_ext/api/preloader_test.exs @@ -4,7 +4,7 @@ defmodule FarmbotExt.API.PreloaderTest do alias FarmbotCore.{ # Asset, - # Asset.Query, + Asset.Query, Asset.Sync } @@ -17,6 +17,8 @@ defmodule FarmbotExt.API.PreloaderTest do {:error, "some descriptive API error"} end) + expect(Query, :first_sync?, 1, fn -> false end) + assert {:error, "some descriptive API error"} = Preloader.preload_all() end end