Skip to content

Commit

Permalink
Format remaining charge time
Browse files Browse the repository at this point in the history
  • Loading branch information
adriankumpf committed Jan 14, 2020
1 parent cdd2c27 commit 38b86ae
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
19 changes: 19 additions & 0 deletions lib/teslamate/convert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,23 @@ defmodule TeslaMate.Convert do

def ft_to_m(nil), do: nil
def ft_to_m(ft), do: ft / 3.28084

@minute 60
@hour @minute * 60
@day @hour * 24
@week @day * 7
@divisor [@week, @day, @hour, @minute, 1]

def sec_to_str(sec) when sec < 5, do: nil

def sec_to_str(sec) when is_number(sec) do
{_, [s, m, h, d, w]} =
Enum.reduce(@divisor, {sec, []}, fn divisor, {n, acc} ->
{rem(n, divisor), [div(n, divisor) | acc]}
end)

["#{w} wk", "#{d} d", "#{h} h", "#{m} min", "#{s} s"]
|> Enum.reject(&String.starts_with?(&1, "0"))
|> Enum.take(2)
end
end
24 changes: 5 additions & 19 deletions lib/teslamate_web/live/car_live/summary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule TeslaMateWeb.CarLive.Summary do

alias TeslaMateWeb.CarView
alias TeslaMate.Vehicles.Vehicle.Summary
alias TeslaMate.Vehicles
alias TeslaMate.{Vehicles, Convert}

@impl true
def mount(%{summary: %Summary{car: car} = summary, settings: settings}, socket) do
Expand Down Expand Up @@ -110,24 +110,10 @@ defmodule TeslaMateWeb.CarLive.Summary do
defp cancel_timer(ref) when is_reference(ref), do: Process.cancel_timer(ref)

defp duration_str(nil), do: nil
defp duration_str(date), do: DateTime.utc_now() |> DateTime.diff(date, :second) |> sec_to_str()

@minute 60
@hour @minute * 60
@day @hour * 24
@week @day * 7
@divisor [@week, @day, @hour, @minute, 1]

def sec_to_str(sec) when sec < 5, do: nil

def sec_to_str(sec) do
{_, [s, m, h, d, w]} =
Enum.reduce(@divisor, {sec, []}, fn divisor, {n, acc} ->
{rem(n, divisor), [div(n, divisor) | acc]}
end)

["#{w} wk", "#{d} d", "#{h} h", "#{m} min", "#{s} s"]
|> Enum.reject(&String.starts_with?(&1, "0"))
|> Enum.take(2)
defp duration_str(date) do
DateTime.utc_now()
|> DateTime.diff(date, :second)
|> Convert.sec_to_str()
end
end
7 changes: 6 additions & 1 deletion lib/teslamate_web/templates/car/summary.html.leex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@
<%= if @summary.state == :charging and not is_nil(@summary.time_to_full_charge) do %>
<tr>
<td class="has-text-weight-medium"><%= gettext "Remaining Time" %></td>
<td><%= round(@summary.time_to_full_charge * 60) %> min</td>
<td><%=
round(@summary.time_to_full_charge * 60 * 60)
|> Convert.sec_to_str()
|> Enum.reject(&String.ends_with?(&1, "s"))
|> Enum.join(" ")
%></td>
</tr>
<% end %>
<%= unless is_nil(@summary.ideal_battery_range_km) do %>
Expand Down
2 changes: 1 addition & 1 deletion lib/teslamate_web/views/car_view.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule TeslaMateWeb.CarView do
use TeslaMateWeb, :view

alias TeslaMate.Locations
alias TeslaMate.{Locations, Convert}

def render("command_failed.json", %{reason: reason}) do
%{error: reason}
Expand Down
26 changes: 25 additions & 1 deletion test/teslamate_web/controllers/car_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ defmodule TeslaMateWeb.CarControllerTest do
assert html = response(conn, 200)
assert html =~ ~r/<p class="title is-5">FooCar<\/p>/
assert table_row(html, "Status", "charging")
assert table_row(html, "Remaining Time", "110 min")
assert table_row(html, "Remaining Time", "1 h 49 min")
assert icon(html, "Plugged in", "power-plug")
assert table_row(html, "Range (ideal)", "321.87 km")
assert table_row(html, "Range (est.)", "289.68 km")
Expand All @@ -222,6 +222,30 @@ defmodule TeslaMateWeb.CarControllerTest do
assert table_row(html, "Charge limit", "85%")
end

@tag :signed_in
test "does not render remaining seconds", %{conn: conn} do
events = [
{:ok,
online_event(
drive_state: %{timestamp: 0, latitude: 0.0, longitude: 0.0},
charge_state: %{
timestamp: 0,
charging_state: "Charging",
charge_energy_added: "4.32",
ideal_battery_range: 200,
time_to_full_charge: 0.33
}
)}
]

:ok = start_vehicles(events)

conn = get(conn, Routes.car_path(conn, :index))

assert html = response(conn, 200)
assert table_row(html, "Remaining Time", "19 min")
end

@tag :signed_in
test "renders current vehicle stats [:driving]", %{conn: conn} do
events = [
Expand Down

0 comments on commit 38b86ae

Please # to comment.