farmbot_os/farmbot_celery_script/lib/farmbot_celery_script/compilers/update_resource_compiler.ex

62 lines
1.9 KiB
Elixir
Raw Normal View History

2020-04-17 09:33:51 -06:00
defmodule FarmbotCeleryScript.Compiler.UpdateResource do
alias FarmbotCeleryScript.{AST, DotProps}
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
# Go easy on the API...
2020-05-09 13:37:24 -06:00
case variable do
%AST{kind: :identifier} ->
args = Map.fetch!(variable, :args)
label = Map.fetch!(args, :label)
resource = Map.fetch!(better_params, label)
me.do_update(resource, update)
%AST{kind: :point} ->
me.do_update(variable.args, update)
%AST{kind: :resource} ->
me.do_update(variable.args, update)
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
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
FarmbotCeleryScript.SysCalls.update_resource(kind, id, update_params)
end
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
raise String.trim("""
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)})
""")
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)
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