2020-04-17 09:33:51 -06:00
|
|
|
defmodule FarmbotCeleryScript.Compiler.UpdateResource do
|
2020-05-12 10:48:33 -06:00
|
|
|
alias FarmbotCeleryScript.{AST, DotProps}
|
2020-04-28 14:24:02 -06:00
|
|
|
|
2020-05-09 15:35:16 -06:00
|
|
|
def update_resource(%AST{args: args, body: body}, _env) do
|
2020-05-08 16:47:20 -06:00
|
|
|
quote location: :keep do
|
2020-05-09 13:37:24 -06:00
|
|
|
me = unquote(__MODULE__)
|
|
|
|
variable = unquote(Map.fetch!(args, :resource))
|
2020-05-08 16:47:20 -06:00
|
|
|
update = unquote(unpair(body, %{}))
|
2020-05-16 18:02:10 -06:00
|
|
|
|
2020-05-14 16:22:25 -06:00
|
|
|
# Go easy on the API...
|
2020-05-09 13:37:24 -06:00
|
|
|
case variable do
|
|
|
|
%AST{kind: :identifier} ->
|
2020-05-09 15:35:16 -06:00
|
|
|
args = Map.fetch!(variable, :args)
|
|
|
|
label = Map.fetch!(args, :label)
|
|
|
|
resource = Map.fetch!(better_params, label)
|
2020-05-11 10:20:38 -06:00
|
|
|
me.do_update(resource, update)
|
2020-05-12 10:48:33 -06:00
|
|
|
|
2020-05-11 10:34:40 -06:00
|
|
|
%AST{kind: :point} ->
|
|
|
|
me.do_update(variable.args, update)
|
2020-05-12 10:48:33 -06:00
|
|
|
|
2020-05-11 10:34:40 -06:00
|
|
|
%AST{kind: :resource} ->
|
|
|
|
me.do_update(variable.args, update)
|
2020-05-12 10:48:33 -06:00
|
|
|
|
|
|
|
res ->
|
|
|
|
raise "Resource error. Please notfiy support: #{inspect(res)}"
|
2020-05-09 13:37:24 -06:00
|
|
|
end
|
|
|
|
end
|
2020-04-29 18:04:46 -06:00
|
|
|
end
|
|
|
|
|
2020-05-11 09:59:36 -06:00
|
|
|
def do_update(%{pointer_id: id, pointer_type: kind}, update_params) do
|
|
|
|
FarmbotCeleryScript.SysCalls.update_resource(kind, id, update_params)
|
|
|
|
end
|
|
|
|
|
2020-05-09 13:37:24 -06:00
|
|
|
def do_update(%{resource_id: id, resource_type: kind}, update_params) do
|
2020-05-06 16:18:51 -06:00
|
|
|
FarmbotCeleryScript.SysCalls.update_resource(kind, id, update_params)
|
|
|
|
end
|
|
|
|
|
2020-05-11 10:34:40 -06:00
|
|
|
def do_update(%{args: %{pointer_id: id, pointer_type: k}}, update_params) do
|
|
|
|
FarmbotCeleryScript.SysCalls.update_resource(k, id, update_params)
|
|
|
|
end
|
|
|
|
|
2020-05-09 13:37:24 -06:00
|
|
|
def do_update(other, update) do
|
2020-05-06 16:18:51 -06:00
|
|
|
raise String.trim("""
|
2020-05-12 10:48:33 -06:00
|
|
|
MARK AS can only be used to mark resources like plants and devices.
|
|
|
|
It cannot be used on things like coordinates.
|
|
|
|
Ensure that your sequences and farm events us MARK AS on plants and not
|
|
|
|
coordinates (#{inspect(other)} / #{inspect(update)})
|
|
|
|
""")
|
2020-05-06 16:18:51 -06:00
|
|
|
end
|
|
|
|
|
2020-04-29 18:04:46 -06:00
|
|
|
defp unpair([pair | rest], acc) do
|
2020-04-22 16:49:23 -06:00
|
|
|
key = Map.fetch!(pair.args, :label)
|
|
|
|
val = Map.fetch!(pair.args, :value)
|
2020-04-30 14:10:34 -06:00
|
|
|
next_acc = Map.merge(acc, DotProps.create(key, val))
|
2020-04-29 18:04:46 -06:00
|
|
|
unpair(rest, next_acc)
|
2020-04-22 16:49:23 -06:00
|
|
|
end
|
|
|
|
|
2020-04-29 18:04:46 -06:00
|
|
|
defp unpair([], acc) do
|
2020-04-22 16:49:23 -06:00
|
|
|
acc
|
|
|
|
end
|
2020-04-17 09:33:51 -06:00
|
|
|
end
|