More farm event stuff
This commit is contained in:
parent
324665856b
commit
7fd86393f3
|
@ -20,7 +20,8 @@ defmodule Farmbot.FarmEvent.Manager do
|
|||
alias Farmbot.FarmEvent.Execution
|
||||
alias Farmbot.Repo.FarmEvent
|
||||
|
||||
@checkup_time 20_000
|
||||
@checkup_time 5_000
|
||||
# @checkup_time 20_000
|
||||
|
||||
def wait_for_sync do
|
||||
GenServer.call(__MODULE__, :wait_for_sync, :infinity)
|
||||
|
|
|
@ -44,33 +44,26 @@ defmodule Farmbot.Repo.FarmEvent do
|
|||
|
||||
def build_calendar(%__MODULE__{calendar: nil} = fe), do: fe
|
||||
def build_calendar(%__MODULE__{calendar: calendar} = fe) when is_list(calendar) do
|
||||
origin_time = Timex.now()
|
||||
# Date Time objects.
|
||||
current_time_dt = Timex.now()
|
||||
{:ok, start_time_dt, _} = DateTime.from_iso8601(fe.start_time)
|
||||
{:ok, end_time_dt, _} = DateTime.from_iso8601(fe.end_time)
|
||||
|
||||
# Unix timestamps from those objects.
|
||||
current_time_unix = DateTime.to_unix(current_time_dt)
|
||||
start_time_unix = DateTime.to_unix(start_time_dt)
|
||||
end_time_unix = DateTime.to_unix(end_time_dt)
|
||||
|
||||
interval_seconds = time_unit_to_seconds(fe.repeat, fe.time_unit)
|
||||
{:ok, lower_bound, _} = DateTime.from_iso8601(fe.start_time)
|
||||
{:ok, upper_bound, _} = DateTime.from_iso8601(fe.end_time)
|
||||
# Math.ceil((lowerBound.unix() - originTime.unix()) / intervalSeconds);
|
||||
skip_intervals = :math.ceil((DateTime.to_unix(lower_bound) - DateTime.to_unix(origin_time)) / interval_seconds)
|
||||
first_item = Timex.add(origin_time, Timex.Duration.from_seconds(interval_seconds * skip_intervals))
|
||||
list = [first_item]
|
||||
all_events = Enum.reduce(0..60, list, fn(_, acc) ->
|
||||
x = List.last(acc)
|
||||
if x do
|
||||
# const item = x.clone().add(intervalSeconds, "seconds");
|
||||
# if (item.isBefore(upperBound)) {
|
||||
# list.push(item);
|
||||
# }
|
||||
item = Timex.add(x, Timex.Duration.from_seconds(interval_seconds))
|
||||
if Timex.before?(item, upper_bound) do
|
||||
acc ++ [item]
|
||||
else
|
||||
acc
|
||||
end
|
||||
else
|
||||
acc
|
||||
end
|
||||
end)
|
||||
grace_period_cutoff_dt = Timex.subtract(current_time_dt, Timex.Duration.from_minutes(1))
|
||||
|
||||
new_calendar = Range.new(start_time_unix, end_time_unix)
|
||||
|> Enum.take_every(fe.repeat * interval_seconds)
|
||||
|> Enum.map(&DateTime.from_unix!(&1))
|
||||
|> Enum.filter(&Timex.after?(&1, grace_period_cutoff_dt))
|
||||
|> Enum.map(&(Timex.shift(&1, seconds: -(&1.second), microseconds: -(&1.microsecond |> elem(0)))))
|
||||
|> Enum.map(&DateTime.to_iso8601(&1))
|
||||
%{fe | calendar: all_events}
|
||||
%{fe | calendar: new_calendar}
|
||||
end
|
||||
|
||||
defp time_unit_to_seconds(_, "never"), do: 0
|
||||
|
|
|
@ -1,10 +1,38 @@
|
|||
defmodule Farmbot.Repo.JSONType do
|
||||
@moduledoc false
|
||||
@moduledoc "Stores JSON as :text data type."
|
||||
@behaviour Ecto.Type
|
||||
|
||||
def type, do: :json
|
||||
def type, do: :text
|
||||
|
||||
def cast(any), do: {:ok, any}
|
||||
def load(value), do: Poison.decode(value, keys: :atoms)
|
||||
def dump(value), do: Poison.encode(value)
|
||||
# We don't need to translate anything for atoms or binaries.
|
||||
# We may have a problem with charlists here.
|
||||
def cast(basic) when is_binary(basic) or is_atom(basic) do
|
||||
{:ok, to_string(basic)}
|
||||
end
|
||||
|
||||
def cast(%{__meta__: _, __struct__: _} = struct) do
|
||||
Map.from_struct(struct)
|
||||
|> Map.delete(:__meta__)
|
||||
|> cast()
|
||||
end
|
||||
|
||||
# try to encode as json here.
|
||||
def cast(map_or_list) when is_list(map_or_list) or is_map(map_or_list) do
|
||||
case Poison.encode(map_or_list) do
|
||||
{:ok, bin} -> {:ok, bin}
|
||||
_reason -> :error
|
||||
end
|
||||
end
|
||||
|
||||
def cast(_), do: :error
|
||||
|
||||
def load(text) do
|
||||
case Poison.decode(text, keys: :atoms) do
|
||||
{:ok, data} -> {:ok, data}
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
# This doesn't feel correct.
|
||||
def dump(data), do: cast(data)
|
||||
end
|
||||
|
|
|
@ -160,7 +160,7 @@ defmodule Farmbot.Repo do
|
|||
def handle_call({:register_sync_cmd, remote_id, kind, body}, _from, state) do
|
||||
maybe_cancel_timer(state.timer)
|
||||
[_current_repo, other_repo] = state.repos
|
||||
|
||||
|
||||
case SyncCmd.changeset(struct(SyncCmd, %{remote_id: remote_id, kind: kind, body: body}))
|
||||
|> ConfigStorage.insert() do
|
||||
{:ok, sync_cmd} ->
|
||||
|
|
Loading…
Reference in a new issue