From c240a5cf67d6c4f2c12635a7c01e345cb50849ca Mon Sep 17 00:00:00 2001 From: Mikko Ahlroth Date: Sun, 11 Sep 2022 01:01:37 +0300 Subject: [PATCH] Add CubDB for persisting current view and settings --- assets/css/app.css | 2 +- config/runtime.exs | 5 ++- lib/geo_therminator/application.ex | 3 ++ .../live/main/device_list.ex | 17 +++++++- .../live/main/device_list.html.heex | 6 ++- lib/geo_therminator_web/live/main/index.ex | 43 +++++++++++++++---- .../live/main/index.html.heex | 24 ++++++----- lib/geo_therminator_web/live/main/pump.ex | 2 + mix.exs | 3 +- mix.lock | 1 + 10 files changed, 82 insertions(+), 24 deletions(-) diff --git a/assets/css/app.css b/assets/css/app.css index d1ea3d7..9de23e2 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -105,6 +105,6 @@ main.container, cursor: progress; } -.login-container { +.page-container { padding: 10px; } diff --git a/config/runtime.exs b/config/runtime.exs index 05d6c7c..4e200df 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -76,7 +76,10 @@ config :geo_therminator, ), api_device_temp_set_reg_index: 3, 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 config :geo_therminator, GeoTherminatorWeb.Endpoint, diff --git a/lib/geo_therminator/application.ex b/lib/geo_therminator/application.ex index 9ae010a..22553fc 100644 --- a/lib/geo_therminator/application.ex +++ b/lib/geo_therminator/application.ex @@ -7,7 +7,10 @@ defmodule GeoTherminator.Application do @impl true def start(_type, _args) do + db_dir = System.get_env("HOME") |> Path.join(Application.get_env(:geo_therminator, :db_dir)) + children = [ + {CubDB, name: GeoTherminator.DB, data_dir: db_dir}, {Finch, name: GeoTherminator.PumpAPI.HTTP}, {Phoenix.PubSub, name: GeoTherminator.PumpAPI.Device.PubSub}, {Registry, keys: :unique, name: GeoTherminator.PumpAPI.Device.Registry}, diff --git a/lib/geo_therminator_web/live/main/device_list.ex b/lib/geo_therminator_web/live/main/device_list.ex index 25e37b0..ee20248 100644 --- a/lib/geo_therminator_web/live/main/device_list.ex +++ b/lib/geo_therminator_web/live/main/device_list.ex @@ -1,8 +1,10 @@ defmodule GeoTherminatorWeb.MainLive.DeviceList do use GeoTherminatorWeb, :live_view - @impl true + @impl Phoenix.LiveView def mount(_params, _session, socket) do + CubDB.delete(GeoTherminator.DB, :viewing_pump) + user = GeoTherminator.PumpAPI.Auth.Server.get_auth(GeoTherminator.PumpAPI.Auth.Server) installations = @@ -10,4 +12,17 @@ defmodule GeoTherminatorWeb.MainLive.DeviceList do {:ok, assign(socket, user: user, installations: installations)} 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 diff --git a/lib/geo_therminator_web/live/main/device_list.html.heex b/lib/geo_therminator_web/live/main/device_list.html.heex index 52e57b0..51cba2a 100644 --- a/lib/geo_therminator_web/live/main/device_list.html.heex +++ b/lib/geo_therminator_web/live/main/device_list.html.heex @@ -1,4 +1,4 @@ -
+

Welcome, <%= @user.first_name %>!

@@ -16,5 +16,7 @@
-
+
+ +
diff --git a/lib/geo_therminator_web/live/main/index.ex b/lib/geo_therminator_web/live/main/index.ex index 1b9dd24..f8159c7 100644 --- a/lib/geo_therminator_web/live/main/index.ex +++ b/lib/geo_therminator_web/live/main/index.ex @@ -3,33 +3,60 @@ defmodule GeoTherminatorWeb.MainLive.Index do @impl Phoenix.LiveView 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 @impl Phoenix.LiveView def handle_event(evt, val, socket) 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 = DynamicSupervisor.start_child( GeoTherminator.PumpAPI.Auth.Server.Supervisor, {GeoTherminator.PumpAPI.Auth.Server, %GeoTherminator.PumpAPI.Auth.Server.Options{ server_name: GeoTherminator.PumpAPI.Auth.Server, - username: values["username"], - password: values["password"] + username: username, + password: password }} ) case on_start do {:ok, _} -> - {:noreply, - push_redirect(socket, - to: Routes.live_path(socket, GeoTherminatorWeb.MainLive.DeviceList) - )} + CubDB.put(GeoTherminator.DB, :auth_credentials, %{ + "username" => username, + "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 diff --git a/lib/geo_therminator_web/live/main/index.html.heex b/lib/geo_therminator_web/live/main/index.html.heex index eff13ae..347d5b8 100644 --- a/lib/geo_therminator_web/live/main/index.html.heex +++ b/lib/geo_therminator_web/live/main/index.html.heex @@ -1,13 +1,17 @@ -
-
-

Log in

+
+ <%= if connected?(@socket) do %> + +

Log in

- - - + + + - <%= if @error do %> -
Error occurred, please try again.
- <% end %> - + <%= if @error do %> +
Error occurred, please try again.
+ <% end %> + + <% else %> +

Loading…

+ <% end %>
diff --git a/lib/geo_therminator_web/live/main/pump.ex b/lib/geo_therminator_web/live/main/pump.ex index 54ac600..23f4ef9 100644 --- a/lib/geo_therminator_web/live/main/pump.ex +++ b/lib/geo_therminator_web/live/main/pump.ex @@ -15,6 +15,8 @@ defmodule GeoTherminatorWeb.MainLive.Pump do %Device.Status{} = status <- Device.Server.get_status(pid), %Device.RegisterCollection{} = registers <- Device.Server.get_registers(pid), %Device.OpStat{} = opstat <- Device.Server.get_opstat(pid) do + CubDB.put(GeoTherminator.DB, :viewing_pump, str_id) + assign(socket, pid: pid, device: device, diff --git a/mix.exs b/mix.exs index 0a53755..793109b 100644 --- a/mix.exs +++ b/mix.exs @@ -53,7 +53,8 @@ defmodule GeoTherminator.MixProject do {:plug_cowboy, "~> 2.5"}, {:dotenv_parser, "~> 1.2"}, {:finch, "~> 0.9.0"}, - {:desktop, "~> 1.4"} + {:desktop, "~> 1.4"}, + {:cubdb, "~> 2.0"} ] if Mix.target() in [:android, :ios] do diff --git a/mix.lock b/mix.lock index 2955dd9..14ee227 100644 --- a/mix.lock +++ b/mix.lock @@ -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_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"}, + "cubdb": {:hex, :cubdb, "2.0.1", "24cab8fb4128df704c52ed641f5ed70af352f7a3a80cebbb44c3bbadc3fd5f45", [:mix], [], "hexpm", "57cf25aebfc34f4580d9075da06882b4fe3e0739f5353d4dcc213e9cc1b10cdf"}, "dbus": {:hex, :dbus, "0.8.0", "7c800681f35d909c199265e55a8ee4aea9ebe4acccce77a0740f89f29cc57648", [:make], [], "hexpm", "a9784f2d9717ffa1f74169144a226c39633ac0d9c7fe8cb3594aeb89c827cca5"}, "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"},