Bump version
parent
228b73b26c
commit
a0f94da083
|
@ -16,49 +16,58 @@ defmodule Command do
|
|||
on a timer.
|
||||
"""
|
||||
def e_stop do
|
||||
msg = "E STOPPING!"
|
||||
Logger.debug(msg)
|
||||
is_locked = BotState.get_status
|
||||
|> Map.get(:informational_settings)
|
||||
|> Map.get(:locked)
|
||||
if(is_locked == false) do
|
||||
GenServer.cast(BotState, {:update_info, :locked, true})
|
||||
RPC.MessageHandler.log(msg, [:error_toast, :error_ticker], [@log_tag])
|
||||
Serial.Handler.e_stop
|
||||
Farmbot.Scheduler.e_stop_lock
|
||||
end
|
||||
# The index of the lock "e_stop". should be an integer or nil
|
||||
e_stop(BotState.get_lock("e_stop"))
|
||||
end
|
||||
|
||||
def e_stop(integer) when is_integer(integer) do
|
||||
{:error, :already_locked}
|
||||
end
|
||||
|
||||
def e_stop(nil) do
|
||||
BotState.add_lock("e_stop")
|
||||
RPC.MessageHandler.log("E STOPPING!", [:error_toast, :error_ticker], [@log_tag])
|
||||
Serial.Handler.e_stop
|
||||
Farmbot.Scheduler.e_stop_lock
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
resume from an e stop
|
||||
This is way to complex
|
||||
"""
|
||||
@spec resume() :: :ok | :fail
|
||||
@spec resume() :: :ok | {:error, atom}
|
||||
def resume do
|
||||
is_locked = BotState.get_status
|
||||
|> Map.get(:informational_settings)
|
||||
|> Map.get(:locked)
|
||||
if(is_locked == true) do
|
||||
Serial.Handler.resume
|
||||
params = BotState.get_status.mcu_params
|
||||
# The firmware takes forever to become ready again.
|
||||
Process.sleep(2000)
|
||||
case Enum.partition(params, fn({param, value}) ->
|
||||
param_int = Gcode.Parser.parse_param(param)
|
||||
Command.update_param(param_int, value)
|
||||
end)
|
||||
do
|
||||
{_, []} ->
|
||||
RPC.MessageHandler.log("Bot Back Up and Running!", [:ticker], [@log_tag])
|
||||
GenServer.cast(BotState, {:update_info, :locked, false})
|
||||
Farmbot.Scheduler.e_stop_unlock
|
||||
:ok
|
||||
{_, failed} ->
|
||||
Logger.error("Param setting failed! #{inspect failed}")
|
||||
:fail
|
||||
end
|
||||
# The index of the lock "e_stop". should be an integer or nil
|
||||
resume(BotState.get_lock("e_stop"))
|
||||
end
|
||||
|
||||
@spec resume(integer | nil) :: :ok | {:error, atom}
|
||||
def resume(integer) when is_integer(integer) do
|
||||
Serial.Handler.resume
|
||||
params = BotState.get_status.mcu_params
|
||||
# The firmware takes forever to become ready again.
|
||||
Process.sleep(2000)
|
||||
case Enum.partition(params, fn({param, value}) ->
|
||||
param_int = Gcode.Parser.parse_param(param)
|
||||
Command.update_param(param_int, value)
|
||||
end)
|
||||
do
|
||||
{_, []} ->
|
||||
RPC.MessageHandler.log("Bot Back Up and Running!", [:ticker], [@log_tag])
|
||||
BotState.remove_lock("e_stop")
|
||||
Farmbot.Scheduler.e_stop_unlock
|
||||
:ok
|
||||
{_, failed} ->
|
||||
Logger.error("Param setting failed! #{inspect failed}")
|
||||
{:error, :prams}
|
||||
end
|
||||
end
|
||||
|
||||
def resume(nil) do
|
||||
{:error, :not_locked}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Home All
|
||||
"""
|
||||
|
|
|
@ -1,15 +1,54 @@
|
|||
defmodule BotState do
|
||||
@moduledoc """
|
||||
Farmbots Hardware State tracker
|
||||
"""
|
||||
use GenServer
|
||||
require Logger
|
||||
|
||||
@save_interval 15000
|
||||
@twelve_hours 3600000
|
||||
|
||||
def init(_) do
|
||||
defmodule State do
|
||||
@moduledoc """
|
||||
Farmbots Hardware State tracker State module
|
||||
"""
|
||||
defstruct [
|
||||
locks: [],
|
||||
mcu_params: %{},
|
||||
location: [0,0,0],
|
||||
pins: %{},
|
||||
configuration: %{},
|
||||
informational_settings: %{},
|
||||
authorization: %{
|
||||
token: nil,
|
||||
email: nil,
|
||||
pass: nil,
|
||||
server: nil,
|
||||
network: nil
|
||||
}
|
||||
]
|
||||
@type t :: %__MODULE__{
|
||||
locks: list(%{reason: String.t}),
|
||||
mcu_params: map,
|
||||
location: [number, ...], # i should change this to a tuple
|
||||
pins: %{},
|
||||
configuration: %{},
|
||||
informational_settings: %{},
|
||||
authorization: %{
|
||||
token: map | nil,
|
||||
email: String.t | nil,
|
||||
pass: String.t | nil,
|
||||
server: String.t | nil,
|
||||
network: String.t | nil
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def init(args) do
|
||||
NetMan.put_pid(BotState)
|
||||
save_interval
|
||||
check_updates
|
||||
{:ok, load}
|
||||
{:ok, load(args)}
|
||||
end
|
||||
|
||||
def start_link(args) do
|
||||
|
@ -30,24 +69,24 @@ defmodule BotState do
|
|||
throttled
|
||||
end
|
||||
|
||||
def load do
|
||||
def load(%{target: target, compat_version: compat_version}) do
|
||||
token = case FarmbotAuth.get_token() do
|
||||
{:ok, token} -> token
|
||||
_ -> nil
|
||||
end
|
||||
default_state = %{
|
||||
mcu_params: %{},
|
||||
location: [0, 0, 0],
|
||||
pins: %{},
|
||||
configuration: %{ os_auto_update: false,
|
||||
fw_auto_update: false,
|
||||
timezone: nil,
|
||||
steps_per_mm: 500 },
|
||||
default_state = %State{
|
||||
configuration: %{
|
||||
os_auto_update: false,
|
||||
fw_auto_update: false,
|
||||
timezone: nil,
|
||||
steps_per_mm: 500
|
||||
},
|
||||
informational_settings: %{
|
||||
controller_version: Fw.version,
|
||||
private_ip: nil,
|
||||
throttled: get_throttled,
|
||||
locked: false
|
||||
target: target,
|
||||
compat_version: compat_version
|
||||
},
|
||||
authorization: %{
|
||||
token: token,
|
||||
|
@ -63,7 +102,7 @@ defmodule BotState do
|
|||
r = Map.keys(rcontents)
|
||||
Logger.debug("default: #{inspect l} saved: #{inspect r}")
|
||||
if(l != r) do # SORRY ABOUT THIS
|
||||
Logger.debug "UPDATING TO NEW STATE TREE FORMAT OR SOMETHING"
|
||||
Logger.warn("UPDATING TO NEW STATE TREE FORMAT OR SOMETHING")
|
||||
spawn fn -> apply_auth(default_state.authorization) end
|
||||
spawn fn -> apply_status(default_state) end
|
||||
default_state
|
||||
|
@ -137,6 +176,36 @@ defmodule BotState do
|
|||
{:reply, state.authorization.token, state}
|
||||
end
|
||||
|
||||
# Allow the frontend to do stuff again.
|
||||
def handle_call({:remove_lock, string}, _from, state) do
|
||||
maybe_index = Enum.find_index(state.locks, fn(%{reason: str}) -> str == string end)
|
||||
cond do
|
||||
is_integer(maybe_index) ->
|
||||
{:reply, :ok, Map.put(state, :locks,
|
||||
List.delete_at(state.locks, maybe_index))}
|
||||
true ->
|
||||
{:reply, {:error, :no_index}, state}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_call({:get_lock, string}, _from, state) do
|
||||
maybe_index = Enum.find_index(state.locks, fn(%{reason: str}) -> str == string end)
|
||||
{:reply, maybe_index, state}
|
||||
end
|
||||
|
||||
# Lock the frontend from doing stuff
|
||||
def handle_cast({:add_lock, string}, state) do
|
||||
maybe_index = Enum.find_index(state.locks, fn(%{reason: str}) -> str == string end)
|
||||
cond do
|
||||
is_integer(maybe_index) ->
|
||||
{:noreply, Map.put(state, :locks,
|
||||
List.replace_at(state.locks, maybe_index, %{reason: string}) )}
|
||||
is_nil(maybe_index) ->
|
||||
{:noreply, Map.put(state, :locks,
|
||||
state.locks ++ [%{reason: string}] )}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_cast({:update_info, key, value}, state) do
|
||||
new_info = Map.put(state.informational_settings, key, value)
|
||||
{:noreply, Map.put(state, :informational_settings, new_info)}
|
||||
|
@ -178,13 +247,6 @@ defmodule BotState do
|
|||
end
|
||||
|
||||
def handle_cast({:creds, {email, pass, server}}, state) do
|
||||
# authorization: %{
|
||||
# token: nil
|
||||
# email: nil,
|
||||
# pass: nil,
|
||||
# server: nil
|
||||
# network: nil
|
||||
# }
|
||||
auth = Map.merge(state.authorization,
|
||||
%{email: email, pass: pass, server: server, token: nil, network: nil})
|
||||
{:noreply, Map.put(state, :authorization, auth)}
|
||||
|
@ -260,6 +322,11 @@ defmodule BotState do
|
|||
GenServer.call(__MODULE__, :get_token)
|
||||
end
|
||||
|
||||
@spec get_lock(String.t) :: integer | nil
|
||||
def get_lock(string) when is_bitstring(string) do
|
||||
GenServer.call(__MODULE__, {:get_lock, string})
|
||||
end
|
||||
|
||||
def set_pos(x, y, z)
|
||||
when is_integer(x) and is_integer(y) and is_integer(z) do
|
||||
GenServer.cast(__MODULE__, {:set_pos, {x, y, z}})
|
||||
|
@ -282,6 +349,16 @@ defmodule BotState do
|
|||
GenServer.call(__MODULE__, {:get_pin, pin_number})
|
||||
end
|
||||
|
||||
@spec add_lock(String.t) :: :ok
|
||||
def add_lock(string) when is_bitstring(string) do
|
||||
GenServer.cast(__MODULE__, {:add_lock, string})
|
||||
end
|
||||
|
||||
@spec remove_lock(String.t) :: :ok | {:error, atom}
|
||||
def remove_lock(string) when is_bitstring(string) do
|
||||
GenServer.call(__MODULE__, {:remove_lock, string})
|
||||
end
|
||||
|
||||
def set_end_stop(_something) do
|
||||
#TODO
|
||||
nil
|
||||
|
|
|
@ -28,7 +28,7 @@ defmodule BotSync do
|
|||
end
|
||||
|
||||
def load_old_resources do
|
||||
default = Sync.create(%{"checksum" => "loading...",
|
||||
default = Sync.create(%{"compat_num" => -1,
|
||||
"device" => %{
|
||||
"id" => -1,
|
||||
"planting_area_id" => -1,
|
||||
|
|
|
@ -42,10 +42,10 @@ defmodule Fw do
|
|||
:ok
|
||||
end
|
||||
|
||||
def init(_args) do
|
||||
def init([%{target: target, compat_version: compat_version}]) do
|
||||
children = [
|
||||
worker(SafeStorage, [@env], restart: :permanent),
|
||||
worker(BotState, [[]], restart: :permanent),
|
||||
worker(BotState, [%{target: target, compat_version: compat_version}], restart: :permanent),
|
||||
worker(Command.Tracker, [[]], restart: :permanent),
|
||||
worker(SSH, [@env], restart: :permanent),
|
||||
supervisor(Controller, [[]], restart: :permanent)
|
||||
|
|
|
@ -3,7 +3,7 @@ defmodule Sync do
|
|||
The enitre Sync Object.
|
||||
located at /api/sync
|
||||
"""
|
||||
defstruct [:checksum,
|
||||
defstruct [:compat_num,
|
||||
:device,
|
||||
:peripherals,
|
||||
:plants,
|
||||
|
@ -13,7 +13,7 @@ defmodule Sync do
|
|||
:users]
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
checksum: String.t,
|
||||
compat_num: number,
|
||||
device: Device.t,
|
||||
peripherals: list(Peripheral.t),
|
||||
plants: list(Plant.t),
|
||||
|
@ -24,7 +24,7 @@ defmodule Sync do
|
|||
}
|
||||
|
||||
@spec create(map) :: t
|
||||
def create(%{"checksum" => checksum,
|
||||
def create(%{"compat_num" => compat_num,
|
||||
"device" => device,
|
||||
"peripherals" => peripherals,
|
||||
"plants" => plants,
|
||||
|
@ -32,7 +32,7 @@ defmodule Sync do
|
|||
"regimens" => regimens,
|
||||
"sequences" => sequences,
|
||||
"users" => users})
|
||||
when is_bitstring(checksum)
|
||||
when is_integer(compat_num)
|
||||
and is_map(device)
|
||||
and is_list(peripherals)
|
||||
and is_list(plants)
|
||||
|
@ -41,7 +41,7 @@ defmodule Sync do
|
|||
and is_list(sequences)
|
||||
and is_list(users)
|
||||
do
|
||||
%Sync{checksum: checksum,
|
||||
%Sync{compat_num: compat_num,
|
||||
device: Device.create(device),
|
||||
plants: create_list(Plant,plants),
|
||||
regimen_items: create_list(RegimenItem,regimen_items),
|
||||
|
|
26
mix.exs
26
mix.exs
|
@ -3,18 +3,30 @@ defmodule Fw.Mixfile do
|
|||
|
||||
use Mix.Project
|
||||
|
||||
@target System.get_env("NERVES_TARGET") || "rpi3"
|
||||
def target(:prod) do
|
||||
System.get_env("NERVES_TARGET") || "rpi3"
|
||||
end
|
||||
|
||||
def target(_) do
|
||||
System.get_env("NERVES_TARGET") || "development"
|
||||
end
|
||||
|
||||
@version Path.join(__DIR__, "VERSION")
|
||||
|> File.read!
|
||||
|> String.strip
|
||||
|
||||
@compat_version Path.join(__DIR__, "COMPAT")
|
||||
|> File.read!
|
||||
|> String.strip
|
||||
|> String.to_integer
|
||||
|
||||
def project do
|
||||
[app: :fw,
|
||||
version: @version,
|
||||
target: @target,
|
||||
target: target(Mix.env),
|
||||
archives: [nerves_bootstrap: "~> 0.1.4"],
|
||||
deps_path: "deps/#{@target}",
|
||||
build_path: "_build/#{@target}",
|
||||
deps_path: "deps/#{target(Mix.env)}",
|
||||
build_path: "_build/#{target(Mix.env)}",
|
||||
build_embedded: Mix.env == :prod,
|
||||
start_permanent: Mix.env == :prod,
|
||||
config_path: "config/config.exs",
|
||||
|
@ -23,7 +35,7 @@ defmodule Fw.Mixfile do
|
|||
end
|
||||
|
||||
def application do
|
||||
[mod: {Fw, []},
|
||||
[mod: {Fw, [%{target: target(Mix.env), compat_version: @compat_version}]},
|
||||
applications: apps(Mix.env)]
|
||||
end
|
||||
|
||||
|
@ -45,7 +57,7 @@ defmodule Fw.Mixfile do
|
|||
|
||||
# on device
|
||||
def apps(:prod) do
|
||||
apps ++ platform_apps(@target) ++
|
||||
apps ++ platform_apps(target(:prod)) ++
|
||||
[
|
||||
:nerves,
|
||||
:nerves_firmware_http,
|
||||
|
@ -83,7 +95,7 @@ defmodule Fw.Mixfile do
|
|||
end
|
||||
|
||||
def deps(:prod) do
|
||||
deps ++ platform_deps(@target) ++ system(@target) ++
|
||||
deps ++ platform_deps(target(Mix.env)) ++ system(target(Mix.env)) ++
|
||||
[
|
||||
{:nerves, "~> 0.3.0"},
|
||||
{:nerves_firmware_http, github: "nerves-project/nerves_firmware_http"},
|
||||
|
|
Loading…
Reference in New Issue