Skip to content
Permalink
Browse files

Render string values as handlebars strings in resource_update

This adds two new secial handlebars strings:

* `CURRENT_TIME`
* `NULL`
  • Loading branch information
ConnorRigby committed Nov 22, 2019
1 parent 2168e71 commit 111c30155871ead6766c305f1c9c65d7c3c09777
@@ -1,20 +1,26 @@
defmodule FarmbotOS.SysCalls.ResourceUpdate do
require Logger

alias FarmbotCore.{
Asset,
Asset.Private
}

alias FarmbotOS.SysCalls.SendMessage

@point_kinds ~w(Plant GenericPointer)

def resource_update("Device", 0, params) do
params
|> do_handlebars()
|> Asset.update_device!()
|> Private.mark_dirty!()

:ok
end

def resource_update(kind, id, params) when kind in @point_kinds do
params = do_handlebars(params)
point_resource_update(kind, id, params)
end

@@ -36,4 +42,22 @@ defmodule FarmbotOS.SysCalls.ResourceUpdate do
{:error, _changeset} -> {:error, "Failed to update #{type}.#{id}"}
end
end

@doc false
def do_handlebars(params) do
Map.new(params, fn
{key, value} when is_binary(value) ->
case SendMessage.render(value) do
{:ok, rendered} ->
{key, rendered}

_ ->
Logger.warn("failed to render #{key} => #{value} for resource_update")
{key, value}
end

{key, value} ->
{key, value}
end)
end
end
@@ -23,7 +23,9 @@ defmodule FarmbotOS.SysCalls.SendMessage do
def render(templ) do
with {:ok, pos} <- pos(),
{:ok, pins} <- pins(),
env <- Keyword.merge(pos, pins) do
{:ok, special} <- special(),
env <- Keyword.merge(pos, pins),
env <- Keyword.merge(env, special) do
env = Map.new(env, fn {k, v} -> {to_string(k), to_string(v)} end)

# Mini Mustache parser
@@ -62,4 +64,12 @@ defmodule FarmbotOS.SysCalls.SendMessage do
{:"pin#{p}", FarmbotCeleryScript.FormatUtil.format_float(v)}
end)}
end

def special() do
{:ok,
[
{:NULL, nil},
{:CURRENT_TIME, DateTime.utc_now()}
]}
end
end

0 comments on commit 111c301

Please sign in to comment.
You can’t perform that action at this time.