Skip to content

Commit 42726f7

Browse files
authored
Merge pull request #199 from ConnorRigby/master
Update to 2.1.4
2 parents 7d0b042 + a5934a1 commit 42726f7

File tree

8 files changed

+169
-70
lines changed

8 files changed

+169
-70
lines changed

COMPAT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.3
1+
2.1.4

lib/bot_command/bot_commands.ex

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,58 @@ defmodule Command do
1616
on a timer.
1717
"""
1818
def e_stop do
19-
msg = "E STOPPING!"
20-
Logger.debug(msg)
21-
is_locked = BotState.get_status
22-
|> Map.get(:informational_settings)
23-
|> Map.get(:locked)
24-
if(is_locked == false) do
25-
GenServer.cast(BotState, {:update_info, :locked, true})
26-
RPC.MessageHandler.log(msg, [:error_toast, :error_ticker], [@log_tag])
27-
Serial.Handler.e_stop
28-
Farmbot.Scheduler.e_stop_lock
29-
end
19+
# The index of the lock "e_stop". should be an integer or nil
20+
e_stop(BotState.get_lock("e_stop"))
21+
end
22+
23+
def e_stop(integer) when is_integer(integer) do
24+
{:error, :already_locked}
25+
end
26+
27+
def e_stop(nil) do
28+
BotState.add_lock("e_stop")
29+
RPC.MessageHandler.log("E STOPPING!", [:error_toast, :error_ticker], [@log_tag])
30+
Serial.Handler.e_stop
31+
Farmbot.Scheduler.e_stop_lock
32+
:ok
3033
end
3134

3235
@doc """
3336
resume from an e stop
37+
This is way to complex
3438
"""
35-
@spec resume() :: :ok | :fail
39+
@spec resume() :: :ok | {:error, atom}
3640
def resume do
37-
is_locked = BotState.get_status
38-
|> Map.get(:informational_settings)
39-
|> Map.get(:locked)
40-
if(is_locked == true) do
41-
Serial.Handler.resume
42-
params = BotState.get_status.mcu_params
43-
# The firmware takes forever to become ready again.
44-
Process.sleep(2000)
45-
case Enum.partition(params, fn({param, value}) ->
46-
param_int = Gcode.Parser.parse_param(param)
47-
Command.update_param(param_int, value)
48-
end)
49-
do
50-
{_, []} ->
51-
RPC.MessageHandler.log("Bot Back Up and Running!", [:ticker], [@log_tag])
52-
GenServer.cast(BotState, {:update_info, :locked, false})
53-
Farmbot.Scheduler.e_stop_unlock
54-
:ok
55-
{_, failed} ->
56-
Logger.error("Param setting failed! #{inspect failed}")
57-
:fail
58-
end
41+
# The index of the lock "e_stop". should be an integer or nil
42+
resume(BotState.get_lock("e_stop"))
43+
end
44+
45+
@spec resume(integer | nil) :: :ok | {:error, atom}
46+
def resume(integer) when is_integer(integer) do
47+
Serial.Handler.resume
48+
params = BotState.get_status.mcu_params
49+
# The firmware takes forever to become ready again.
50+
Process.sleep(2000)
51+
case Enum.partition(params, fn({param, value}) ->
52+
param_int = Gcode.Parser.parse_param(param)
53+
Command.update_param(param_int, value)
54+
end)
55+
do
56+
{_, []} ->
57+
RPC.MessageHandler.log("Bot Back Up and Running!", [:ticker], [@log_tag])
58+
BotState.remove_lock("e_stop")
59+
Farmbot.Scheduler.e_stop_unlock
60+
:ok
61+
{_, failed} ->
62+
Logger.error("Param setting failed! #{inspect failed}")
63+
{:error, :prams}
5964
end
6065
end
6166

67+
def resume(nil) do
68+
{:error, :not_locked}
69+
end
70+
6271
@doc """
6372
Home All
6473
"""

lib/bot_state/bot_state.ex

Lines changed: 97 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,54 @@
11
defmodule BotState do
2+
@moduledoc """
3+
Farmbots Hardware State tracker
4+
"""
25
use GenServer
36
require Logger
47

58
@save_interval 15000
69
@twelve_hours 3600000
710

8-
def init(_) do
11+
defmodule State do
12+
@moduledoc """
13+
Farmbots Hardware State tracker State module
14+
"""
15+
defstruct [
16+
locks: [],
17+
mcu_params: %{},
18+
location: [0,0,0],
19+
pins: %{},
20+
configuration: %{},
21+
informational_settings: %{},
22+
authorization: %{
23+
token: nil,
24+
email: nil,
25+
pass: nil,
26+
server: nil,
27+
network: nil
28+
}
29+
]
30+
@type t :: %__MODULE__{
31+
locks: list(%{reason: String.t}),
32+
mcu_params: map,
33+
location: [number, ...], # i should change this to a tuple
34+
pins: %{},
35+
configuration: %{},
36+
informational_settings: %{},
37+
authorization: %{
38+
token: map | nil,
39+
email: String.t | nil,
40+
pass: String.t | nil,
41+
server: String.t | nil,
42+
network: String.t | nil
43+
}
44+
}
45+
end
46+
47+
def init(args) do
948
NetMan.put_pid(BotState)
1049
save_interval
1150
check_updates
12-
{:ok, load}
51+
{:ok, load(args)}
1352
end
1453

1554
def start_link(args) do
@@ -30,24 +69,24 @@ defmodule BotState do
3069
throttled
3170
end
3271

33-
def load do
72+
def load(%{target: target, compat_version: compat_version}) do
3473
token = case FarmbotAuth.get_token() do
3574
{:ok, token} -> token
3675
_ -> nil
3776
end
38-
default_state = %{
39-
mcu_params: %{},
40-
location: [0, 0, 0],
41-
pins: %{},
42-
configuration: %{ os_auto_update: false,
43-
fw_auto_update: false,
44-
timezone: nil,
45-
steps_per_mm: 500 },
77+
default_state = %State{
78+
configuration: %{
79+
os_auto_update: false,
80+
fw_auto_update: false,
81+
timezone: nil,
82+
steps_per_mm: 500
83+
},
4684
informational_settings: %{
4785
controller_version: Fw.version,
4886
private_ip: nil,
4987
throttled: get_throttled,
50-
locked: false
88+
target: target,
89+
compat_version: compat_version
5190
},
5291
authorization: %{
5392
token: token,
@@ -63,7 +102,7 @@ defmodule BotState do
63102
r = Map.keys(rcontents)
64103
Logger.debug("default: #{inspect l} saved: #{inspect r}")
65104
if(l != r) do # SORRY ABOUT THIS
66-
Logger.debug "UPDATING TO NEW STATE TREE FORMAT OR SOMETHING"
105+
Logger.warn("UPDATING TO NEW STATE TREE FORMAT OR SOMETHING")
67106
spawn fn -> apply_auth(default_state.authorization) end
68107
spawn fn -> apply_status(default_state) end
69108
default_state
@@ -137,6 +176,36 @@ defmodule BotState do
137176
{:reply, state.authorization.token, state}
138177
end
139178

179+
# Allow the frontend to do stuff again.
180+
def handle_call({:remove_lock, string}, _from, state) do
181+
maybe_index = Enum.find_index(state.locks, fn(%{reason: str}) -> str == string end)
182+
cond do
183+
is_integer(maybe_index) ->
184+
{:reply, :ok, Map.put(state, :locks,
185+
List.delete_at(state.locks, maybe_index))}
186+
true ->
187+
{:reply, {:error, :no_index}, state}
188+
end
189+
end
190+
191+
def handle_call({:get_lock, string}, _from, state) do
192+
maybe_index = Enum.find_index(state.locks, fn(%{reason: str}) -> str == string end)
193+
{:reply, maybe_index, state}
194+
end
195+
196+
# Lock the frontend from doing stuff
197+
def handle_cast({:add_lock, string}, state) do
198+
maybe_index = Enum.find_index(state.locks, fn(%{reason: str}) -> str == string end)
199+
cond do
200+
is_integer(maybe_index) ->
201+
{:noreply, Map.put(state, :locks,
202+
List.replace_at(state.locks, maybe_index, %{reason: string}) )}
203+
is_nil(maybe_index) ->
204+
{:noreply, Map.put(state, :locks,
205+
state.locks ++ [%{reason: string}] )}
206+
end
207+
end
208+
140209
def handle_cast({:update_info, key, value}, state) do
141210
new_info = Map.put(state.informational_settings, key, value)
142211
{:noreply, Map.put(state, :informational_settings, new_info)}
@@ -178,13 +247,6 @@ defmodule BotState do
178247
end
179248

180249
def handle_cast({:creds, {email, pass, server}}, state) do
181-
# authorization: %{
182-
# token: nil
183-
# email: nil,
184-
# pass: nil,
185-
# server: nil
186-
# network: nil
187-
# }
188250
auth = Map.merge(state.authorization,
189251
%{email: email, pass: pass, server: server, token: nil, network: nil})
190252
{:noreply, Map.put(state, :authorization, auth)}
@@ -260,6 +322,11 @@ defmodule BotState do
260322
GenServer.call(__MODULE__, :get_token)
261323
end
262324

325+
@spec get_lock(String.t) :: integer | nil
326+
def get_lock(string) when is_bitstring(string) do
327+
GenServer.call(__MODULE__, {:get_lock, string})
328+
end
329+
263330
def set_pos(x, y, z)
264331
when is_integer(x) and is_integer(y) and is_integer(z) do
265332
GenServer.cast(__MODULE__, {:set_pos, {x, y, z}})
@@ -282,6 +349,16 @@ defmodule BotState do
282349
GenServer.call(__MODULE__, {:get_pin, pin_number})
283350
end
284351

352+
@spec add_lock(String.t) :: :ok
353+
def add_lock(string) when is_bitstring(string) do
354+
GenServer.cast(__MODULE__, {:add_lock, string})
355+
end
356+
357+
@spec remove_lock(String.t) :: :ok | {:error, atom}
358+
def remove_lock(string) when is_bitstring(string) do
359+
GenServer.call(__MODULE__, {:remove_lock, string})
360+
end
361+
285362
def set_end_stop(_something) do
286363
#TODO
287364
nil

lib/bot_sync/bot_sync.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule BotSync do
2828
end
2929

3030
def load_old_resources do
31-
default = Sync.create(%{"checksum" => "loading...",
31+
default = Sync.create(%{"compat_num" => -1,
3232
"device" => %{
3333
"id" => -1,
3434
"planting_area_id" => -1,

lib/fw.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ defmodule Fw do
4242
:ok
4343
end
4444

45-
def init(_args) do
45+
def init([%{target: target, compat_version: compat_version}]) do
4646
children = [
4747
worker(SafeStorage, [@env], restart: :permanent),
48-
worker(BotState, [[]], restart: :permanent),
48+
worker(BotState, [%{target: target, compat_version: compat_version}], restart: :permanent),
4949
worker(Command.Tracker, [[]], restart: :permanent),
5050
worker(SSH, [@env], restart: :permanent),
5151
supervisor(Controller, [[]], restart: :permanent)

lib/syncables/sync.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule Sync do
33
The enitre Sync Object.
44
located at /api/sync
55
"""
6-
defstruct [:checksum,
6+
defstruct [:compat_num,
77
:device,
88
:peripherals,
99
:plants,
@@ -13,7 +13,7 @@ defmodule Sync do
1313
:users]
1414

1515
@type t :: %__MODULE__{
16-
checksum: String.t,
16+
compat_num: number,
1717
device: Device.t,
1818
peripherals: list(Peripheral.t),
1919
plants: list(Plant.t),
@@ -24,15 +24,15 @@ defmodule Sync do
2424
}
2525

2626
@spec create(map) :: t
27-
def create(%{"checksum" => checksum,
27+
def create(%{"compat_num" => compat_num,
2828
"device" => device,
2929
"peripherals" => peripherals,
3030
"plants" => plants,
3131
"regimen_items" => regimen_items,
3232
"regimens" => regimens,
3333
"sequences" => sequences,
3434
"users" => users})
35-
when is_bitstring(checksum)
35+
when is_integer(compat_num)
3636
and is_map(device)
3737
and is_list(peripherals)
3838
and is_list(plants)
@@ -41,7 +41,7 @@ defmodule Sync do
4141
and is_list(sequences)
4242
and is_list(users)
4343
do
44-
%Sync{checksum: checksum,
44+
%Sync{compat_num: compat_num,
4545
device: Device.create(device),
4646
plants: create_list(Plant,plants),
4747
regimen_items: create_list(RegimenItem,regimen_items),

0 commit comments

Comments
 (0)