Add CubDB for persisting current view and settings

This commit is contained in:
Mikko Ahlroth 2022-09-11 01:01:37 +03:00
parent 7274a81dc3
commit c240a5cf67
10 changed files with 82 additions and 24 deletions

View file

@ -105,6 +105,6 @@ main.container,
cursor: progress; cursor: progress;
} }
.login-container { .page-container {
padding: 10px; padding: 10px;
} }

View file

@ -76,7 +76,10 @@ config :geo_therminator,
), ),
api_device_temp_set_reg_index: 3, api_device_temp_set_reg_index: 3,
api_device_reg_set_client_id: get_env("API_DEVICE_REG_SET_CLIENT_ID"), api_device_reg_set_client_id: get_env("API_DEVICE_REG_SET_CLIENT_ID"),
api_refresh: 10_000 api_refresh: 10_000,
# Database directory for settings, relative to app HOME path
db_dir: get_env("DB_DIR", "db")
if config_env() == :dev do if config_env() == :dev do
config :geo_therminator, GeoTherminatorWeb.Endpoint, config :geo_therminator, GeoTherminatorWeb.Endpoint,

View file

@ -7,7 +7,10 @@ defmodule GeoTherminator.Application do
@impl true @impl true
def start(_type, _args) do def start(_type, _args) do
db_dir = System.get_env("HOME") |> Path.join(Application.get_env(:geo_therminator, :db_dir))
children = [ children = [
{CubDB, name: GeoTherminator.DB, data_dir: db_dir},
{Finch, name: GeoTherminator.PumpAPI.HTTP}, {Finch, name: GeoTherminator.PumpAPI.HTTP},
{Phoenix.PubSub, name: GeoTherminator.PumpAPI.Device.PubSub}, {Phoenix.PubSub, name: GeoTherminator.PumpAPI.Device.PubSub},
{Registry, keys: :unique, name: GeoTherminator.PumpAPI.Device.Registry}, {Registry, keys: :unique, name: GeoTherminator.PumpAPI.Device.Registry},

View file

@ -1,8 +1,10 @@
defmodule GeoTherminatorWeb.MainLive.DeviceList do defmodule GeoTherminatorWeb.MainLive.DeviceList do
use GeoTherminatorWeb, :live_view use GeoTherminatorWeb, :live_view
@impl true @impl Phoenix.LiveView
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
CubDB.delete(GeoTherminator.DB, :viewing_pump)
user = GeoTherminator.PumpAPI.Auth.Server.get_auth(GeoTherminator.PumpAPI.Auth.Server) user = GeoTherminator.PumpAPI.Auth.Server.get_auth(GeoTherminator.PumpAPI.Auth.Server)
installations = installations =
@ -10,4 +12,17 @@ defmodule GeoTherminatorWeb.MainLive.DeviceList do
{:ok, assign(socket, user: user, installations: installations)} {:ok, assign(socket, user: user, installations: installations)}
end end
@impl Phoenix.LiveView
def handle_event(evt, value, socket)
def handle_event("logout", _value, socket) do
DynamicSupervisor.stop(GeoTherminator.PumpAPI.Auth.Server.Supervisor)
CubDB.delete(GeoTherminator.DB, :auth_credentials)
{:noreply,
Phoenix.LiveView.push_redirect(socket,
to: Routes.live_path(socket, GeoTherminatorWeb.MainLive.Index)
)}
end
end end

View file

@ -1,4 +1,4 @@
<section id="main-section"> <section id="main-section" class="page-container">
<header> <header>
<h1>Welcome, <%= @user.first_name %>!</h1> <h1>Welcome, <%= @user.first_name %>!</h1>
</header> </header>
@ -16,5 +16,7 @@
</ul> </ul>
</section> </section>
<footer></footer> <footer>
<button type="button" phx-click="logout">Log out</button>
</footer>
</section> </section>

View file

@ -3,33 +3,60 @@ defmodule GeoTherminatorWeb.MainLive.Index do
@impl Phoenix.LiveView @impl Phoenix.LiveView
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
{:ok, assign(socket, error: false)} socket = assign(socket, error: false)
credentials = CubDB.get(GeoTherminator.DB, :auth_credentials)
socket =
if connected?(socket) and is_map(credentials) do
try_init(socket, credentials["username"], credentials["password"])
else
socket
end
{:ok, socket}
end end
@impl Phoenix.LiveView @impl Phoenix.LiveView
def handle_event(evt, val, socket) def handle_event(evt, val, socket)
def handle_event("login", values, socket) do def handle_event("login", values, socket) do
{:noreply, try_init(socket, values["username"], values["password"])}
end
defp try_init(socket, username, password) do
on_start = on_start =
DynamicSupervisor.start_child( DynamicSupervisor.start_child(
GeoTherminator.PumpAPI.Auth.Server.Supervisor, GeoTherminator.PumpAPI.Auth.Server.Supervisor,
{GeoTherminator.PumpAPI.Auth.Server, {GeoTherminator.PumpAPI.Auth.Server,
%GeoTherminator.PumpAPI.Auth.Server.Options{ %GeoTherminator.PumpAPI.Auth.Server.Options{
server_name: GeoTherminator.PumpAPI.Auth.Server, server_name: GeoTherminator.PumpAPI.Auth.Server,
username: values["username"], username: username,
password: values["password"] password: password
}} }}
) )
case on_start do case on_start do
{:ok, _} -> {:ok, _} ->
{:noreply, CubDB.put(GeoTherminator.DB, :auth_credentials, %{
push_redirect(socket, "username" => username,
to: Routes.live_path(socket, GeoTherminatorWeb.MainLive.DeviceList) "password" => password
)} })
viewing_pump = CubDB.get(GeoTherminator.DB, :viewing_pump)
if viewing_pump != nil do
push_redirect(socket,
to: Routes.live_path(socket, GeoTherminatorWeb.MainLive.Pump, viewing_pump)
)
else
push_redirect(socket,
to: Routes.live_path(socket, GeoTherminatorWeb.MainLive.DeviceList)
)
end
_ -> _ ->
{:noreply, assign(socket, error: true)} assign(socket, error: true)
end end
end end
end end

View file

@ -1,13 +1,17 @@
<div class="container login-container"> <div class="container page-container">
<form phx-submit="login"> <%= if connected?(@socket) do %>
<h1>Log in</h1> <form phx-submit="login">
<h1>Log in</h1>
<label>Email <input type="email" name="username" /></label> <label>Email <input type="email" name="username" /></label>
<label>Password <input type="password" name="password" /></label> <label>Password <input type="password" name="password" /></label>
<button>Log in</button> <button>Log in</button>
<%= if @error do %> <%= if @error do %>
<div class="error">Error occurred, please try again.</div> <div class="error">Error occurred, please try again.</div>
<% end %> <% end %>
</form> </form>
<% else %>
<h1>Loading…</h1>
<% end %>
</div> </div>

View file

@ -15,6 +15,8 @@ defmodule GeoTherminatorWeb.MainLive.Pump do
%Device.Status{} = status <- Device.Server.get_status(pid), %Device.Status{} = status <- Device.Server.get_status(pid),
%Device.RegisterCollection{} = registers <- Device.Server.get_registers(pid), %Device.RegisterCollection{} = registers <- Device.Server.get_registers(pid),
%Device.OpStat{} = opstat <- Device.Server.get_opstat(pid) do %Device.OpStat{} = opstat <- Device.Server.get_opstat(pid) do
CubDB.put(GeoTherminator.DB, :viewing_pump, str_id)
assign(socket, assign(socket,
pid: pid, pid: pid,
device: device, device: device,

View file

@ -53,7 +53,8 @@ defmodule GeoTherminator.MixProject do
{:plug_cowboy, "~> 2.5"}, {:plug_cowboy, "~> 2.5"},
{:dotenv_parser, "~> 1.2"}, {:dotenv_parser, "~> 1.2"},
{:finch, "~> 0.9.0"}, {:finch, "~> 0.9.0"},
{:desktop, "~> 1.4"} {:desktop, "~> 1.4"},
{:cubdb, "~> 2.0"}
] ]
if Mix.target() in [:android, :ios] do if Mix.target() in [:android, :ios] do

View file

@ -3,6 +3,7 @@
"cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"}, "cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"},
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"}, "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"},
"cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"}, "cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"},
"cubdb": {:hex, :cubdb, "2.0.1", "24cab8fb4128df704c52ed641f5ed70af352f7a3a80cebbb44c3bbadc3fd5f45", [:mix], [], "hexpm", "57cf25aebfc34f4580d9075da06882b4fe3e0739f5353d4dcc213e9cc1b10cdf"},
"dbus": {:hex, :dbus, "0.8.0", "7c800681f35d909c199265e55a8ee4aea9ebe4acccce77a0740f89f29cc57648", [:make], [], "hexpm", "a9784f2d9717ffa1f74169144a226c39633ac0d9c7fe8cb3594aeb89c827cca5"}, "dbus": {:hex, :dbus, "0.8.0", "7c800681f35d909c199265e55a8ee4aea9ebe4acccce77a0740f89f29cc57648", [:make], [], "hexpm", "a9784f2d9717ffa1f74169144a226c39633ac0d9c7fe8cb3594aeb89c827cca5"},
"debouncer": {:hex, :debouncer, "0.1.7", "a7f59fb55cdb54072aff8ece461f4d041d2a709da84e07ed0ab302d348724640", [:mix], [], "hexpm", "b7fd0623df8ab16933bb164d19769884b18c98cab8677cd53eed59587f290603"}, "debouncer": {:hex, :debouncer, "0.1.7", "a7f59fb55cdb54072aff8ece461f4d041d2a709da84e07ed0ab302d348724640", [:mix], [], "hexpm", "b7fd0623df8ab16933bb164d19769884b18c98cab8677cd53eed59587f290603"},
"desktop": {:hex, :desktop, "1.4.1", "69e4741fbf72e2e71c8d6078cd18da0e6fddc6e87f67837ac501a2412a0ddc64", [:mix], [{:debouncer, "~> 0.1", [hex: :debouncer, repo: "hexpm", optional: false]}, {:ex_sni, "~> 0.2", [hex: :ex_sni, repo: "hexpm", optional: false]}, {:gettext, "> 0.10.0", [hex: :gettext, repo: "hexpm", optional: false]}, {:oncrash, "~> 0.1", [hex: :oncrash, repo: "hexpm", optional: false]}, {:phoenix, "> 1.0.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_live_view, "> 0.15.0", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:plug, "> 1.0.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "fd3716fd912d2379448d94e445ce9de6e65e89cd2bd9e35f8d76efd497f1ec66"}, "desktop": {:hex, :desktop, "1.4.1", "69e4741fbf72e2e71c8d6078cd18da0e6fddc6e87f67837ac501a2412a0ddc64", [:mix], [{:debouncer, "~> 0.1", [hex: :debouncer, repo: "hexpm", optional: false]}, {:ex_sni, "~> 0.2", [hex: :ex_sni, repo: "hexpm", optional: false]}, {:gettext, "> 0.10.0", [hex: :gettext, repo: "hexpm", optional: false]}, {:oncrash, "~> 0.1", [hex: :oncrash, repo: "hexpm", optional: false]}, {:phoenix, "> 1.0.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_live_view, "> 0.15.0", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:plug, "> 1.0.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "fd3716fd912d2379448d94e445ce9de6e65e89cd2bd9e35f8d76efd497f1ec66"},