Add LED handler for green (sync status)

pull/974/head
connor rigby 2019-07-23 10:25:34 -07:00 committed by Connor Rigby
parent 2b058c9a14
commit e199cb8a90
No known key found for this signature in database
GPG Key ID: 29A88B24B70456E0
2 changed files with 16 additions and 2 deletions

View File

@ -8,7 +8,7 @@ defmodule FarmbotExt.AMQP.AutoSyncChannel do
use GenServer
use AMQP
alias FarmbotCore.{Asset, BotState, JSON}
alias FarmbotCore.{Asset, BotState, JSON, Leds}
alias FarmbotExt.AMQP.ConnectionWorker
alias FarmbotExt.API.{EagerLoader, Preloader}
@ -78,10 +78,14 @@ defmodule FarmbotExt.AMQP.AutoSyncChannel do
end
def handle_info(:preload, state) do
_ = Leds.green(:fast_blink)
with :ok <- Preloader.preload_all() do
if Asset.Query.auto_sync?() do
_ = Leds.green(:solid)
BotState.set_sync_status("synced")
else
_ = Leds.green(:slow_blink)
BotState.set_sync_status("sync_now")
end
@ -90,6 +94,7 @@ defmodule FarmbotExt.AMQP.AutoSyncChannel do
else
{:error, reason} ->
BotState.set_sync_status("sync_error")
_ = Leds.green(:slow_blink)
FarmbotCore.Logger.error(1, "Error preloading. #{inspect(reason)}")
Process.send_after(self(), :preload, 5000)
{:noreply, state}
@ -128,7 +133,7 @@ defmodule FarmbotExt.AMQP.AutoSyncChannel do
case String.split(key, ".") do
["bot", ^device, "sync", asset_kind, id_str] when asset_kind in @known_kinds ->
id = data["id"] || String.to_integer(id_str)
handle_asset(asset_kind, id, body)
_ = handle_asset(asset_kind, id, body)
["bot", ^device, "sync", asset_kind, _id_str] ->
Logger.warn("Unknown syncable asset: #{asset_kind}")
@ -155,9 +160,11 @@ defmodule FarmbotExt.AMQP.AutoSyncChannel do
def handle_asset(asset_kind, id, params) do
if Asset.Query.auto_sync?() do
:ok = BotState.set_sync_status("syncing")
_ = Leds.green(:fast_blink)
# Logger.info "Syncing #{asset_kind} #{id} #{inspect(params)}"
Asset.Command.update(asset_kind, id, params)
:ok = BotState.set_sync_status("synced")
_ = Leds.green(:solid)
else
cache_sync(asset_kind, id, params)
end
@ -165,12 +172,15 @@ defmodule FarmbotExt.AMQP.AutoSyncChannel do
def cache_sync(kind, id, params) when kind in @no_cache_kinds do
:ok = BotState.set_sync_status("syncing")
_ = Leds.green(:fast_blink)
:ok = Asset.Command.update(kind, id, params)
:ok = BotState.set_sync_status("synced")
_ = Leds.green(:solid)
end
def cache_sync(_, _, nil) do
:ok = BotState.set_sync_status("sync_now")
_ = Leds.green(:slow_blink)
end
def cache_sync(asset_kind, id, params) do
@ -178,6 +188,7 @@ defmodule FarmbotExt.AMQP.AutoSyncChannel do
changeset = Asset.Command.new_changeset(asset_kind, id, params)
:ok = EagerLoader.cache(changeset)
:ok = BotState.set_sync_status("sync_now")
_ = Leds.green(:slow_blink)
end
defp compute_reply_from_amqp_state(state, %{conn: conn, chan: chan}) do

View File

@ -526,6 +526,7 @@ defmodule FarmbotOS.SysCalls do
with {:ok, sync_changeset} <- API.get_changeset(Sync),
:ok <- BotState.set_sync_status("syncing"),
_ <- Leds.green(:fast_blink),
sync_changeset <- Reconciler.sync_group(sync_changeset, SyncGroup.group_0()),
sync_changeset <- Reconciler.sync_group(sync_changeset, SyncGroup.group_1()),
sync_changeset <- Reconciler.sync_group(sync_changeset, SyncGroup.group_2()),
@ -533,10 +534,12 @@ defmodule FarmbotOS.SysCalls do
_sync_changeset <- Reconciler.sync_group(sync_changeset, SyncGroup.group_4()) do
FarmbotCore.Logger.success(3, "Synced")
:ok = BotState.set_sync_status("synced")
_ = Leds.green(:solid)
:ok
else
error ->
:ok = BotState.set_sync_status("sync_error")
_ = Leds.green(:slow_blink)
{:error, inspect(error)}
end
end