From c92c79347f70e9e8daa97f300388c41db6a3af94 Mon Sep 17 00:00:00 2001 From: Connor Rigby Date: Wed, 3 Jul 2019 13:04:53 -0700 Subject: [PATCH] Add back step logs --- .../lib/farmbot_celery_script/compiler.ex | 54 +++++++++++-------- .../lib/farmbot_celery_script/sys_calls.ex | 6 +++ .../farmbot_celery_script/sys_calls/stubs.ex | 39 ++++++++++++++ .../farmbot_celery_script/compiler_test.exs | 27 ++++++++-- farmbot_os/lib/farmbot_os/sys_calls.ex | 10 ++++ test/support/celery_script/test_sys_calls.ex | 5 ++ 6 files changed, 115 insertions(+), 26 deletions(-) diff --git a/farmbot_celery_script/lib/farmbot_celery_script/compiler.ex b/farmbot_celery_script/lib/farmbot_celery_script/compiler.ex index 79a08fc3..2faaadf4 100644 --- a/farmbot_celery_script/lib/farmbot_celery_script/compiler.ex +++ b/farmbot_celery_script/lib/farmbot_celery_script/compiler.ex @@ -245,9 +245,13 @@ defmodule FarmbotCeleryScript.Compiler do # nothing() # end quote location: :keep do + FarmbotCeleryScript.SysCalls.log("IF Statement evaultaion") + if unquote(if_eval) do + FarmbotCeleryScript.SysCalls.log("IF Statement will branch") unquote(compile_block(then_ast)) else + FarmbotCeleryScript.SysCalls.log("IF Statement will not branch") unquote(compile_block(else_ast)) end end @@ -259,6 +263,8 @@ defmodule FarmbotCeleryScript.Compiler do # We have to lookup the sequence by it's id. case FarmbotCeleryScript.SysCalls.get_sequence(unquote(id)) do %FarmbotCeleryScript.AST{} = ast -> + # TODO(Connor) - figure out a way of inserting the sequence name here + FarmbotCeleryScript.SysCalls.log("Executing Sequence") # compile the ast env = unquote(compile_params_to_function_args(parameter_applications)) FarmbotCeleryScript.Compiler.compile(ast, env) @@ -278,10 +284,10 @@ defmodule FarmbotCeleryScript.Compiler do end) quote location: :keep do - FarmbotCeleryScript.SysCalls.execute_script( - unquote(compile_ast(package)), - unquote(Macro.escape(Map.new(env))) - ) + package = unquote(compile_ast(package)) + env = unquote(Macro.escape(Map.new(env))) + FarmbotCeleryScript.SysCalls.log("Executing Farmware: #{package}") + FarmbotCeleryScript.SysCalls.execute_script(package, env) end end @@ -308,6 +314,7 @@ defmodule FarmbotCeleryScript.Compiler do compile :install_first_party_farmware, _ do quote location: :keep do + FarmbotCeleryScript.SysCalls.log("Installing first party Farmware") FarmbotCeleryScript.SysCalls.install_first_party_farmware() end end @@ -335,6 +342,7 @@ defmodule FarmbotCeleryScript.Compiler do locz + offz ] + FarmbotCeleryScript.SysCalls.log("Moving to position: #{x}, #{y}, #{z}") FarmbotCeleryScript.SysCalls.move_absolute(x, y, z, unquote(compile_ast(speed))) end end @@ -359,18 +367,15 @@ defmodule FarmbotCeleryScript.Compiler do end # compiles write_pin - compile :write_pin, %{pin_number: num, pin_mode: mode, pin_value: val} do + compile :write_pin, %{pin_number: num, pin_mode: mode, pin_value: value} do quote location: :keep do - with :ok <- - FarmbotCeleryScript.SysCalls.write_pin( - unquote(compile_ast(num)), - unquote(compile_ast(mode)), - unquote(compile_ast(val)) - ) do - FarmbotCeleryScript.SysCalls.read_pin( - unquote(compile_ast(num)), - unquote(compile_ast(mode)) - ) + pin = unquote(compile_ast(num)) + mode = unquote(compile_ast(mode)) + value = unquote(compile_ast(value)) + FarmbotCeleryScript.SysCalls.log("Writing pin: #{pin} in mode: #{mode}: #{value}") + + with :ok <- FarmbotCeleryScript.SysCalls.write_pin(pin, mode, value) do + FarmbotCeleryScript.SysCalls.read_pin(pin, mode) end end end @@ -378,17 +383,20 @@ defmodule FarmbotCeleryScript.Compiler do # compiles read_pin compile :read_pin, %{pin_number: num, pin_mode: mode} do quote location: :keep do - FarmbotCeleryScript.SysCalls.read_pin(unquote(compile_ast(num)), unquote(compile_ast(mode))) + pin = unquote(compile_ast(num)) + mode = unquote(compile_ast(mode)) + FarmbotCeleryScript.SysCalls.log("Reading pin: #{pin} in mode: #{mode}") + FarmbotCeleryScript.SysCalls.read_pin(pin, mode) end end # compiles set_servo_angle compile :set_servo_angle, %{pin_number: pin_number, pin_value: pin_value} do quote location: :keep do - FarmbotCeleryScript.SysCalls.set_servo_angle( - unquote(compile_ast(pin_number)), - unquote(compile_ast(pin_value)) - ) + pin = unquote(compile_ast(pin_number)) + angle = unquote(compile_ast(pin_value)) + FarmbotCeleryScript.SysCalls.log("Writing servo: #{pin}: #{angle}") + FarmbotCeleryScript.SysCalls.set_servo_angle(pin, angle) end end @@ -406,6 +414,7 @@ defmodule FarmbotCeleryScript.Compiler do compile :find_home, %{axis: axis} do quote location: :keep do with axis when axis in ["x", "y", "z"] <- unquote(compile_ast(axis)) do + FarmbotCeleryScript.SysCalls.log("Finding home on axis: #{axis}") FarmbotCeleryScript.SysCalls.find_home(axis) else {:error, reason} -> @@ -430,6 +439,7 @@ defmodule FarmbotCeleryScript.Compiler do quote location: :keep do with axis when axis in ["x", "y", "z"] <- unquote(compile_ast(axis)), speed when is_number(speed) <- unquote(compile_ast(speed)) do + FarmbotCeleryScript.SysCalls.log("Homing axis: #{axis}") FarmbotCeleryScript.SysCalls.home(axis, speed) else {:error, reason} -> @@ -452,6 +462,7 @@ defmodule FarmbotCeleryScript.Compiler do compile :zero, %{axis: axis} do quote location: :keep do with axis when axis in ["x", "y", "z"] <- unquote(compile_ast(axis)) do + FarmbotCeleryScript.SysCalls.log("Zeroing axis: #{axis}") FarmbotCeleryScript.SysCalls.zero(axis) else {:error, reason} -> @@ -477,6 +488,7 @@ defmodule FarmbotCeleryScript.Compiler do compile :calibrate, %{axis: axis} do quote location: :keep do with axis when axis in ["x", "y", "z"] <- unquote(compile_ast(axis)) do + FarmbotCeleryScript.SysCalls.log("Calibrating axis: #{axis}") FarmbotCeleryScript.SysCalls.calibrate(axis) else {:error, reason} -> @@ -488,6 +500,7 @@ defmodule FarmbotCeleryScript.Compiler do compile :wait, %{milliseconds: millis} do quote location: :keep do with millis when is_integer(millis) <- unquote(compile_ast(millis)) do + FarmbotCeleryScript.SysCalls.log("Waiting for #{millis} milliseconds") FarmbotCeleryScript.SysCalls.wait(millis) else {:error, reason} -> @@ -508,7 +521,6 @@ defmodule FarmbotCeleryScript.Compiler do end) quote location: :keep do - # send_message("success", "Hello world!", [:email, :toast]) FarmbotCeleryScript.SysCalls.send_message( unquote(compile_ast(type)), unquote(compile_ast(msg)), diff --git a/farmbot_celery_script/lib/farmbot_celery_script/sys_calls.ex b/farmbot_celery_script/lib/farmbot_celery_script/sys_calls.ex index ff1bc7c4..cbeed355 100644 --- a/farmbot_celery_script/lib/farmbot_celery_script/sys_calls.ex +++ b/farmbot_celery_script/lib/farmbot_celery_script/sys_calls.ex @@ -63,6 +63,12 @@ defmodule FarmbotCeleryScript.SysCalls do ok_or_error @callback zero(axis) :: ok_or_error + @callback log(message :: String.t()) :: any() + + def log(sys_calls \\ @sys_calls, message) when is_binary(message) do + apply(sys_calls, :log, [message]) + end + def calibrate(sys_calls \\ @sys_calls, axis) when axis in ["x", "y", "z"] do ok_or_error(sys_calls, :calibrate, [axis]) end diff --git a/farmbot_celery_script/lib/farmbot_celery_script/sys_calls/stubs.ex b/farmbot_celery_script/lib/farmbot_celery_script/sys_calls/stubs.ex index fa064ef6..a17cced9 100644 --- a/farmbot_celery_script/lib/farmbot_celery_script/sys_calls/stubs.ex +++ b/farmbot_celery_script/lib/farmbot_celery_script/sys_calls/stubs.ex @@ -5,77 +5,116 @@ defmodule FarmbotCeleryScript.SysCalls.Stubs do @behaviour FarmbotCeleryScript.SysCalls require Logger + + @impl true + def log(message), do: error(:log, [message]) + + @impl true def calibrate(axis), do: error(:calibrate, [axis]) + @impl true def change_ownership(email, secret, server), do: error(:change_ownership, [email, secret, server]) + @impl true def check_update(), do: error(:check_update, []) + @impl true def coordinate(x, y, z), do: error(:coordinate, [x, y, z]) + @impl true def dump_info(), do: error(:dump_info, []) + @impl true def emergency_lock(), do: error(:emergency_lock, []) + @impl true def emergency_unlock(), do: error(:emergency_unlock, []) + @impl true def execute_script(package, args), do: error(:execute_script, [package, args]) + @impl true def factory_reset(), do: error(:factory_reset, []) + @impl true def find_home(axis), do: error(:find_home, [axis]) + @impl true def firmware_reboot(), do: error(:firmware_reboot, []) + @impl true def flash_firmware(package), do: error(:flash_firmware, [package]) + @impl true def get_current_x(), do: error(:get_current_x, []) + @impl true def get_current_y(), do: error(:get_current_y, []) + @impl true def get_current_z(), do: error(:get_current_z, []) + @impl true def get_sequence(resource_id), do: error(:get_sequence, [resource_id]) + @impl true def get_toolslot_for_tool(resource_id), do: error(:get_toolslot_for_tool, [resource_id]) + @impl true def home(axis, speed), do: error(:home, [axis, speed]) + @impl true def install_first_party_farmware(), do: error(:install_first_party_farmware, []) + @impl true def move_absolute(x, y, z, speed), do: error(:move_absolute, [x, y, z, speed]) + @impl true def named_pin(named_pin_type, resource_id), do: error(:named_pin, [named_pin_type, resource_id]) + @impl true def nothing(), do: error(:nothing, []) + @impl true def point(point_type, resource_id), do: error(:point, [point_type, resource_id]) + @impl true def power_off(), do: error(:power_off, []) + @impl true def read_pin(pin_num, pin_mode), do: error(:read_pin, [pin_num, pin_mode]) + @impl true def read_status(), do: error(:read_status, []) + @impl true def reboot(), do: error(:reboot, []) + @impl true def resource_update(kind, resource_id, data), do: error(:resource_update, [kind, resource_id, data]) + @impl true def send_message(type, message, channels), do: error(:send_message, [type, message, channels]) + @impl true def set_servo_angle(pin, value), do: error(:set_servo_angle, [pin, value]) + @impl true def set_user_env(env_name, env_value), do: error(:set_user_env, [env_name, env_value]) + @impl true def sync(), do: error(:sync, []) + @impl true def wait(millis), do: error(:wait, [millis]) + @impl true def write_pin(pin_num, pin_mode, pin_value), do: error(:write_pin, [pin_num, pin_mode, pin_value]) + @impl true def zero(axis), do: error(:zero, [axis]) defp error(fun, _args) do diff --git a/farmbot_celery_script/test/farmbot_celery_script/compiler_test.exs b/farmbot_celery_script/test/farmbot_celery_script/compiler_test.exs index 10e49200..3a25b9ff 100644 --- a/farmbot_celery_script/test/farmbot_celery_script/compiler_test.exs +++ b/farmbot_celery_script/test/farmbot_celery_script/compiler_test.exs @@ -133,6 +133,7 @@ defmodule FarmbotCeleryScript.CompilerTest do strip_nl(""" case(FarmbotCeleryScript.SysCalls.get_sequence(100)) do %FarmbotCeleryScript.AST{} = ast -> + FarmbotCeleryScript.SysCalls.log("Executing Sequence") env = [] FarmbotCeleryScript.Compiler.compile(ast, env) @@ -154,7 +155,10 @@ defmodule FarmbotCeleryScript.CompilerTest do assert compiled == strip_nl(""" - FarmbotCeleryScript.SysCalls.execute_script("take-photo", %{"a" => "123"}) + package = "take-photo" + env = %{"a" => "123"} + FarmbotCeleryScript.SysCalls.log("Executing Farmware: \#{package}") + FarmbotCeleryScript.SysCalls.execute_script(package, env) """) end @@ -186,6 +190,7 @@ defmodule FarmbotCeleryScript.CompilerTest do assert compiled == strip_nl(""" + FarmbotCeleryScript.SysCalls.log("Installing first party Farmware") FarmbotCeleryScript.SysCalls.install_first_party_farmware() """) end @@ -229,6 +234,7 @@ defmodule FarmbotCeleryScript.CompilerTest do %{x: offx, y: offy, z: offz} = FarmbotCeleryScript.SysCalls.coordinate(-20, -20, -20) ) do [x, y, z] = [locx + offx, locy + offy, locz + offz] + FarmbotCeleryScript.SysCalls.log(\"Moving to position: \#{x}, \#{y}, \#{z}\") FarmbotCeleryScript.SysCalls.move_absolute(x, y, z, 100) end """) @@ -273,8 +279,13 @@ defmodule FarmbotCeleryScript.CompilerTest do assert compiled == strip_nl(""" - with(:ok <- FarmbotCeleryScript.SysCalls.write_pin(17, 0, 1)) do - FarmbotCeleryScript.SysCalls.read_pin(17, 0) + pin = 17 + mode = 0 + value = 1 + FarmbotCeleryScript.SysCalls.log("Writing pin: \#{pin} in mode: \#{mode}: \#{value}") + + with(:ok <- FarmbotCeleryScript.SysCalls.write_pin(pin, mode, value)) do + FarmbotCeleryScript.SysCalls.read_pin(pin, mode) end """) end @@ -288,7 +299,10 @@ defmodule FarmbotCeleryScript.CompilerTest do assert compiled == strip_nl(""" - FarmbotCeleryScript.SysCalls.read_pin(23, 0) + pin = 23 + mode = 0 + FarmbotCeleryScript.SysCalls.log("Reading pin: \#{pin} in mode: \#{mode}") + FarmbotCeleryScript.SysCalls.read_pin(pin, mode) """) end @@ -301,7 +315,10 @@ defmodule FarmbotCeleryScript.CompilerTest do assert compiled == strip_nl(""" - FarmbotCeleryScript.SysCalls.set_servo_angle(23, 90) + pin = 23 + angle = 90 + FarmbotCeleryScript.SysCalls.log("Writing servo: \#{pin}: \#{angle}") + FarmbotCeleryScript.SysCalls.set_servo_angle(pin, angle) """) end diff --git a/farmbot_os/lib/farmbot_os/sys_calls.ex b/farmbot_os/lib/farmbot_os/sys_calls.ex index b7282960..2a447ed2 100644 --- a/farmbot_os/lib/farmbot_os/sys_calls.ex +++ b/farmbot_os/lib/farmbot_os/sys_calls.ex @@ -40,6 +40,16 @@ defmodule FarmbotOS.SysCalls do @impl true defdelegate read_status(), to: FarmbotExt.AMQP.BotStateChannel + @impl true + def log(message) do + if FarmbotCore.Asset.fbos_config(:sequence_body_log) do + FarmbotCore.Logger.info(2, message) + :ok + else + :ok + end + end + @impl true def reboot do FarmbotOS.System.reboot("Reboot requested by Sequence or frontend") diff --git a/test/support/celery_script/test_sys_calls.ex b/test/support/celery_script/test_sys_calls.ex index d8c86b25..0bd98668 100644 --- a/test/support/celery_script/test_sys_calls.ex +++ b/test/support/celery_script/test_sys_calls.ex @@ -41,6 +41,11 @@ defmodule Farmbot.TestSupport.CeleryScript.TestSysCalls do {:reply, {handler, kind, args}, state} end + @impl true + def log(_message) do + :ok + end + @impl true def coordinate(x, y, z) do %{x: x, y: y, z: z}