Fix firmware timeouts even more

pull/974/head
Connor Rigby 2019-09-12 15:48:22 -07:00
parent c8e2bc4f4c
commit f948f6d503
No known key found for this signature in database
GPG Key ID: 29A88B24B70456E0
3 changed files with 22 additions and 9 deletions

View File

@ -209,7 +209,7 @@ defmodule FarmbotFirmware do
end
@doc """
Sets the Firmware server to stop recording input and output
Sets the Firmware server to stop recording input and output
GCODES.
"""
def exit_vcr_mode(server \\ __MODULE__) do
@ -330,11 +330,15 @@ defmodule FarmbotFirmware do
end
def handle_info(:timeout, %{current: c} = state) when is_tuple(c) do
if state.caller_pid, do: send(state.caller_pid, {state.tag, {:report_busy, []}})
for {pid, _code} <- state.command_queue, do: send(pid, {state.tag, {:report_busy, []}})
# Logger.debug "Got checkup message when current command still executing"
{:noreply, state}
end
def handle_info(:timeout, %{command_queue: [{pid, {tag, code}} | rest]} = state) do
for {pid, _code} <- state.command_queue, do: send(pid, {state.tag, {:report_busy, []}})
case GenServer.call(state.transport_pid, {tag, code}) do
:ok ->
new_state = %{state | tag: tag, current: code, command_queue: rest, caller_pid: pid}
@ -552,6 +556,8 @@ defmodule FarmbotFirmware do
# report_idle => goto(_, :idle)
def handle_report({:report_idle, []}, %{status: _} = state) do
for {pid, _code} <- state.command_queue, do: send(pid, {state.tag, {:report_busy, []}})
side_effects(state, :handle_busy, [false])
side_effects(state, :handle_idle, [true])
send(self(), :timeout)

View File

@ -1,10 +1,13 @@
defmodule FarmbotFirmware.Command do
@moduledoc false
alias Farmbot.{Firmware, Firmware.GCODE}
alias FarmbotFirmware
alias FarmbotFirmware.GCODE
@spec command(GenServer.server(), GCODE.t() | {GCODE.kind(), GCODE.args()}) ::
:ok | {:error, :invalid_command | :firmware_error | :emergency_lock | Firmware.status()}
def command(firmware_server \\ Firmware, code)
:ok
| {:error,
:invalid_command | :firmware_error | :emergency_lock | FarmbotFirmware.status()}
def command(firmware_server \\ FarmbotFirmware, code)
def command(firmware_server, {_tag, {_, _}} = code) do
case GenServer.call(firmware_server, code, :infinity) do
@ -17,7 +20,7 @@ defmodule FarmbotFirmware.Command do
command(firmware_server, {to_string(:rand.uniform(100)), code})
end
defp wait_for_command_result(_tag, code, retries \\ 0, err \\ nil) do
defp wait_for_command_result(tag, code, retries \\ 0, err \\ nil) do
receive do
{tag, {:report_begin, []}} ->
wait_for_command_result(tag, code, retries, err)
@ -50,7 +53,9 @@ defmodule FarmbotFirmware.Command do
wait_for_command_result(tag, code, retries, err)
after
30_000 ->
raise("Firmware server failed to respond within 30 seconds")
raise(
"Firmware command: #{GCODE.encode({tag, code})} failed to respond within 30 seconds"
)
end
end
end

View File

@ -1,10 +1,12 @@
defmodule FarmbotFirmware.Request do
@moduledoc false
alias Farmbot.{Firmware, Firmware.GCODE}
alias FarmbotFirmware
alias FarmbotFirmware.GCODE
@spec request(GenServer.server(), GCODE.t()) ::
{:ok, GCODE.t()} | {:error, :invalid_command | :firmware_error | Firmware.status()}
def request(firmware_server \\ Firmware, code)
{:ok, GCODE.t()}
| {:error, :invalid_command | :firmware_error | FarmbotFirmware.status()}
def request(firmware_server \\ FarmbotFirmware, code)
def request(firmware_server, {_tag, {kind, _}} = code) do
if kind not in [