Fix auth errors and display errors

This commit is contained in:
Mikko Ahlroth 2023-01-15 00:35:51 +02:00
parent d7a05f3b1f
commit 82bb4ff253
6 changed files with 25 additions and 12 deletions

View file

@ -75,7 +75,7 @@ defmodule GeoTherminator.PumpAPI.Auth.Server do
schedule_token_check()
if diff < @token_check_diff do
Logger.debug("Renewing auth token since #{diff} < #{@token_check_diff}")
Logger.info("Renewing auth token since #{diff} < #{@token_check_diff}")
case init_state(state.username, state.password) do
{:ok, new_state} -> {:noreply, new_state}

View file

@ -11,6 +11,9 @@ defmodule GeoTherminatorWeb.Components.MainView do
@impl true
def update(assigns, socket) do
priority_set =
Device.OpStat.record(assigns.opstat, :priority) |> elem(1) |> Map.keys() |> MapSet.new()
{:ok,
assign(socket,
set_temp: Device.Status.record(assigns.status, :heating_effect),
@ -45,7 +48,7 @@ defmodule GeoTherminatorWeb.Components.MainView do
Device.RegisterCollection.record(assigns.registers, :outdoor_temp),
:value
),
priority: Device.OpStat.record(assigns.opstat, :priority)
priority_set: priority_set
)}
end
end

View file

@ -86,7 +86,7 @@
pointer-events="none"
/>
<%= if @priority == :heating do %>
<%= if MapSet.member?(@priority_set, :heating) do %>
<ellipse
cx="172"
cy="289"
@ -98,7 +98,7 @@
/>
<% end %>
<%= if @priority == :hot_water do %>
<%= if MapSet.member?(@priority_set, :hot_water) do %>
<ellipse
cx="100"
cy="169"

View file

@ -37,3 +37,9 @@ pub external fn from_iso8601(String) -> DateTimeFromISO8601Result =
pub external fn from_unix(Int) -> Result(DateTime, #(Atom, Atom)) =
"Elixir.DateTime" "from_unix"
pub external fn utc_now() -> DateTime =
"Elixir.DateTime" "utc_now"
pub external fn to_unix(DateTime) -> Int =
"Elixir.DateTime" "to_unix"

View file

@ -17,15 +17,19 @@ pub fn auth(username: String, password: String) -> Result(User, ApiError) {
try tokens =
b2c.authenticate(username, password)
|> result.map_error(fn(err) { AuthError(inner: err) })
try access_token_expires_in =
date_time.from_unix(tokens.access_token_expires_in)
try access_token_expiry =
date_time.from_unix(
date_time.to_unix(date_time.utc_now()) + tokens.access_token_expires_in,
)
|> result.replace_error(InvalidData(
msg: "Access token expiry could not be converted into DateTime: " <> string.inspect(
tokens.access_token_expires_in,
),
))
try refresh_token_expires_in =
date_time.from_unix(tokens.refresh_token_expires_in)
try refresh_token_expiry =
date_time.from_unix(
date_time.to_unix(date_time.utc_now()) + tokens.refresh_token_expires_in,
)
|> result.replace_error(InvalidData(
msg: "Refresh token expiry could not be converted into DateTime: " <> string.inspect(
tokens.refresh_token_expires_in,
@ -34,9 +38,9 @@ pub fn auth(username: String, password: String) -> Result(User, ApiError) {
Ok(User(tokens: Tokens(
access_token: tokens.access_token,
access_token_expiry: access_token_expires_in,
access_token_expiry: access_token_expiry,
refresh_token: tokens.refresh_token,
refresh_token_expiry: refresh_token_expires_in,
refresh_token_expiry: refresh_token_expiry,
)))
}

View file

@ -275,8 +275,8 @@ fn opstat_bitmask_map(register: Register) {
let #(int, priority) = mapping
case band(register.value, int) {
0 -> set.insert(output, priority)
_ -> priority_set
0 -> output
_ -> set.insert(output, priority)
}
},
)