2019-03-05 12:35:09 -07:00
|
|
|
defmodule FarmbotOS.SysCalls do
|
|
|
|
require FarmbotCore.Logger
|
2019-04-23 14:34:56 -06:00
|
|
|
require Logger
|
2019-03-05 12:35:09 -07:00
|
|
|
|
|
|
|
alias FarmbotCeleryScript.AST
|
|
|
|
alias FarmbotFirmware
|
|
|
|
|
2019-04-15 17:04:50 -06:00
|
|
|
alias FarmbotOS.SysCalls.{
|
|
|
|
SendMessage,
|
|
|
|
ExecuteScript,
|
|
|
|
FlashFirmware,
|
|
|
|
ChangeOwnership,
|
|
|
|
DumpInfo
|
|
|
|
}
|
2019-03-05 12:35:09 -07:00
|
|
|
|
2019-04-15 17:04:50 -06:00
|
|
|
alias FarmbotCore.{Asset, Asset.Repo, Asset.Private, Asset.Sync, BotState}
|
2019-03-05 12:35:09 -07:00
|
|
|
alias FarmbotExt.{API, API.Reconciler, API.SyncGroup}
|
|
|
|
alias Ecto.{Changeset, Multi}
|
|
|
|
|
|
|
|
@behaviour FarmbotCeleryScript.SysCalls
|
2019-02-20 12:57:45 -07:00
|
|
|
|
|
|
|
defdelegate send_message(level, message, channels), to: SendMessage
|
2019-03-14 11:29:23 -06:00
|
|
|
defdelegate execute_script(name, env), to: ExecuteScript
|
2019-03-19 14:59:22 -06:00
|
|
|
defdelegate flash_firmware(package), to: FlashFirmware
|
2019-04-18 13:55:49 -06:00
|
|
|
defdelegate change_ownership(email, secret, server), to: ChangeOwnership
|
2019-04-15 17:04:50 -06:00
|
|
|
defdelegate dump_info(), to: DumpInfo
|
|
|
|
|
2019-04-17 12:19:28 -06:00
|
|
|
def check_update do
|
|
|
|
_ = FarmbotOS.Platform.Target.NervesHubClient.check_update()
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2019-04-15 17:04:50 -06:00
|
|
|
def reboot do
|
|
|
|
FarmbotOS.System.reboot("Reboot requested by sequence or frontend")
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def power_off do
|
|
|
|
FarmbotOS.System.reboot("Shut down requested by sequence or frontend")
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def factory_reset do
|
2019-04-18 14:41:14 -06:00
|
|
|
FarmbotOS.System.factory_reset("Factory reset requested by sequence or frontent", true)
|
2019-04-15 17:04:50 -06:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def firmware_reboot do
|
|
|
|
GenServer.stop(FarmbotFirmware, :reboot)
|
|
|
|
end
|
|
|
|
|
|
|
|
def resource_update(kind, id, params) do
|
|
|
|
module = Module.concat(Asset, kind)
|
|
|
|
|
|
|
|
with true <- Code.ensure_loaded?(module),
|
|
|
|
%{} = orig <- Repo.get_by(module, [id: id], preload: [:local_meta]),
|
|
|
|
%{valid?: true} = change <- module.changeset(orig, params),
|
|
|
|
{:ok, new} <- Repo.update(change),
|
|
|
|
new <- Repo.preload(new, [:local_meta]) do
|
|
|
|
Private.mark_dirty!(new, %{})
|
|
|
|
:ok
|
|
|
|
else
|
|
|
|
false ->
|
|
|
|
{:error, "unknown asset kind: #{kind}"}
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, "Could not find asset by kind: #{kind} and id: #{id}"}
|
|
|
|
|
|
|
|
%{valid?: false} = changeset ->
|
|
|
|
{:error, "failed to update #{kind}: #{inspect(changeset.errors)}"}
|
|
|
|
end
|
|
|
|
end
|
2019-02-20 12:57:45 -07:00
|
|
|
|
|
|
|
def read_status do
|
2019-03-27 08:33:32 -06:00
|
|
|
:ok = FarmbotExt.AMQP.BotStateNGChannel.force()
|
2019-02-20 12:57:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_user_env(key, value) do
|
2019-03-05 12:35:09 -07:00
|
|
|
FarmbotCore.BotState.set_user_env(key, value)
|
2019-02-20 12:57:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_current_x do
|
|
|
|
get_position(:x)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_current_y do
|
|
|
|
get_position(:y)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_current_z do
|
|
|
|
get_position(:z)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_pin(pin_number, mode) do
|
2019-04-17 15:16:49 -06:00
|
|
|
case FarmbotFirmware.request({:pin_read, [p: pin_number, m: mode]}) do
|
2019-03-04 14:32:56 -07:00
|
|
|
{:ok, {_, {:report_pin_value, [p: _, v: val]}}} ->
|
|
|
|
val
|
|
|
|
|
|
|
|
{:error, reason} ->
|
2019-04-08 15:48:17 -06:00
|
|
|
{:error, "Firmware error: #{inspect(reason)}"}
|
2019-03-04 14:32:56 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_pin(pin_number, mode, value) do
|
2019-03-05 12:35:09 -07:00
|
|
|
case FarmbotFirmware.command({:pin_write, [p: pin_number, v: value, m: mode]}) do
|
2019-04-08 15:48:17 -06:00
|
|
|
:ok ->
|
|
|
|
:ok
|
|
|
|
|
|
|
|
{:error, reason} ->
|
|
|
|
{:error, "Firmware error: #{inspect(reason)}"}
|
2019-03-04 14:32:56 -07:00
|
|
|
end
|
2019-02-20 12:57:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def point(kind, id) do
|
2019-03-05 12:35:09 -07:00
|
|
|
case Asset.get_point(id: id) do
|
2019-02-20 12:57:45 -07:00
|
|
|
nil -> {:error, "#{kind} not found"}
|
|
|
|
%{x: x, y: y, z: z} -> %{x: x, y: y, z: z}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_position(axis) do
|
2019-04-09 15:43:41 -06:00
|
|
|
axis = assert_axis!(axis)
|
|
|
|
|
2019-03-05 12:35:09 -07:00
|
|
|
case FarmbotFirmware.request({nil, {:position_read, []}}) do
|
2019-03-04 14:32:56 -07:00
|
|
|
{:ok, {_, {:report_position, params}}} ->
|
2019-02-20 12:57:45 -07:00
|
|
|
Keyword.fetch!(params, axis)
|
|
|
|
|
2019-03-04 14:32:56 -07:00
|
|
|
{:error, reason} ->
|
2019-04-08 15:48:17 -06:00
|
|
|
{:error, "Firmware error: #{inspect(reason)}"}
|
2019-02-20 12:57:45 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_absolute(x, y, z, speed) do
|
|
|
|
params = [x: x / 1.0, y: y / 1.0, z: z / 1.0, s: speed / 1.0]
|
2019-04-23 14:34:56 -06:00
|
|
|
# Logger.debug "moving to location: #{inspect(params)}"
|
2019-02-20 12:57:45 -07:00
|
|
|
|
2019-03-05 12:35:09 -07:00
|
|
|
case FarmbotFirmware.command({nil, {:command_movement, params}}) do
|
2019-03-04 14:32:56 -07:00
|
|
|
:ok ->
|
|
|
|
:ok
|
|
|
|
|
|
|
|
{:error, reason} ->
|
2019-04-08 15:48:17 -06:00
|
|
|
{:error, "Firmware error: #{inspect(reason)}"}
|
2019-03-04 14:32:56 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def calibrate(axis) do
|
2019-04-09 15:43:41 -06:00
|
|
|
axis = assert_axis!(axis)
|
|
|
|
|
2019-04-17 15:16:49 -06:00
|
|
|
case FarmbotFirmware.command({:command_movement_calibrate, [axis]}) do
|
2019-03-04 14:32:56 -07:00
|
|
|
:ok ->
|
|
|
|
:ok
|
|
|
|
|
|
|
|
{:error, reason} ->
|
2019-04-09 15:43:41 -06:00
|
|
|
{:error, "Firmware error: #{inspect(reason)}"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_home(axis) do
|
|
|
|
axis = assert_axis!(axis)
|
|
|
|
|
2019-04-17 15:16:49 -06:00
|
|
|
case FarmbotFirmware.command({:command_movement_find_home, [axis]}) do
|
2019-04-09 15:43:41 -06:00
|
|
|
:ok ->
|
|
|
|
:ok
|
|
|
|
|
|
|
|
{:error, reason} ->
|
|
|
|
{:error, "Firmware error: #{inspect(reason)}"}
|
2019-02-20 12:57:45 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-17 17:04:12 -06:00
|
|
|
def home(axis, _speed) do
|
|
|
|
# TODO(Connor) fix speed
|
|
|
|
axis = assert_axis!(axis)
|
|
|
|
|
|
|
|
case FarmbotFirmware.command({:command_movement_home, [axis]}) do
|
|
|
|
:ok ->
|
|
|
|
:ok
|
|
|
|
|
|
|
|
{:error, reason} ->
|
|
|
|
{:error, "Firmware error: #{inspect(reason)}"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-17 11:52:44 -06:00
|
|
|
def emergency_lock do
|
|
|
|
_ = FarmbotFirmware.command({:command_emergency_lock, []})
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def emergency_unlock do
|
|
|
|
_ = FarmbotFirmware.command({:command_emergency_unlock, []})
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2019-04-09 15:43:41 -06:00
|
|
|
defp assert_axis!(axis) when is_atom(axis),
|
|
|
|
do: axis
|
|
|
|
|
|
|
|
defp assert_axis!(axis) when axis in ~w(x y z),
|
|
|
|
do: String.to_existing_atom(axis)
|
|
|
|
|
|
|
|
defp assert_axis!(axis) do
|
|
|
|
# {:error, "unknown axis #{axis}"}
|
|
|
|
raise("unknown axis #{axis}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait(ms) do
|
|
|
|
Process.sleep(ms)
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2019-04-23 15:03:54 -06:00
|
|
|
def named_pin("Peripheral", id) do
|
|
|
|
case Asset.get_peripheral(id: id) do
|
|
|
|
%{pin: pin} -> pin
|
|
|
|
nil -> {:error, "Could not find peripheral by id: #{id}"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def named_pin("Sensor", id) do
|
|
|
|
case Asset.get_sensor(id: id) do
|
|
|
|
%{pin: pin} -> pin
|
|
|
|
nil -> {:error, "Could not find peripheral by id: #{id}"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-09 15:43:41 -06:00
|
|
|
def named_pin(kind, id) do
|
|
|
|
{:error, "unknown pin kind: #{kind} of id: #{id}"}
|
|
|
|
end
|
|
|
|
|
2019-02-20 12:57:45 -07:00
|
|
|
def get_sequence(id) do
|
2019-03-05 12:35:09 -07:00
|
|
|
case Asset.get_sequence(id: id) do
|
2019-02-20 12:57:45 -07:00
|
|
|
nil -> {:error, "sequence not found"}
|
|
|
|
%{} = sequence -> AST.decode(sequence)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-17 17:04:12 -06:00
|
|
|
def get_toolslot_for_tool(id) do
|
|
|
|
with %{id: ^id} <- Asset.get_tool(id: id),
|
|
|
|
%{x: x, y: y, z: z} <- Asset.get_point(tool_id: id) do
|
|
|
|
%{x: x, y: y, z: z}
|
|
|
|
else
|
|
|
|
nil -> {:error, "Could not find point for tool by id: #{id}"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-20 12:57:45 -07:00
|
|
|
def sync() do
|
2019-03-05 12:35:09 -07:00
|
|
|
FarmbotCore.Logger.busy(3, "Syncing")
|
2019-02-20 12:57:45 -07:00
|
|
|
sync_changeset = API.get_changeset(Sync)
|
|
|
|
sync = Changeset.apply_changes(sync_changeset)
|
|
|
|
multi = Multi.new()
|
|
|
|
|
2019-03-05 12:35:09 -07:00
|
|
|
:ok = BotState.set_sync_status("syncing")
|
2019-02-20 12:57:45 -07:00
|
|
|
|
2019-03-14 15:37:03 -06:00
|
|
|
with {:ok, multi} <- Reconciler.sync_group(multi, sync, SyncGroup.group_0()),
|
|
|
|
{:ok, multi} <- Reconciler.sync_group(multi, sync, SyncGroup.group_1()),
|
2019-02-20 12:57:45 -07:00
|
|
|
{:ok, multi} <- Reconciler.sync_group(multi, sync, SyncGroup.group_2()),
|
|
|
|
{:ok, multi} <- Reconciler.sync_group(multi, sync, SyncGroup.group_3()),
|
|
|
|
{:ok, multi} <- Reconciler.sync_group(multi, sync, SyncGroup.group_4()) do
|
|
|
|
Multi.insert(multi, :syncs, sync_changeset)
|
|
|
|
|> Repo.transaction()
|
|
|
|
|
2019-03-05 12:35:09 -07:00
|
|
|
FarmbotCore.Logger.success(3, "Synced")
|
|
|
|
:ok = BotState.set_sync_status("synced")
|
2019-02-20 12:57:45 -07:00
|
|
|
:ok
|
|
|
|
else
|
|
|
|
error ->
|
2019-03-05 12:35:09 -07:00
|
|
|
:ok = BotState.set_sync_status("sync_error")
|
2019-02-20 12:57:45 -07:00
|
|
|
{:error, inspect(error)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|