Skip to content

Commit

Permalink
Enable streaming for new users as well
Browse files Browse the repository at this point in the history
  • Loading branch information
adriankumpf committed Apr 4, 2020
1 parent 774c693 commit a457d17
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 125 deletions.
5 changes: 1 addition & 4 deletions lib/teslamate/settings/car_settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ defmodule TeslaMate.Settings.CarSettings do
schema "car_settings" do
field :suspend_min, :integer, default: 21
field :suspend_after_idle_min, :integer, default: 15

field :req_not_unlocked, :boolean, default: true

field :free_supercharging, :boolean, default: false

field :use_streaming_api, :boolean, default: false
field :use_streaming_api, :boolean, default: true

has_one :car, Car, foreign_key: :settings_id
end
Expand Down
2 changes: 1 addition & 1 deletion priv/repo/migrations/20200318164021_use_streaming_api.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule TeslaMate.Repo.Migrations.UseStreamingApi do

def change do
alter table(:car_settings) do
add :use_streaming_api, :boolean, default: false, null: false
add :use_streaming_api, :boolean, default: true, null: false
end
end
end
6 changes: 3 additions & 3 deletions test/teslamate/settings_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ defmodule TeslaMate.SettingsTest do
suspend_after_idle_min: 60,
req_not_unlocked: true,
free_supercharging: true,
use_streaming_api: true
use_streaming_api: false
}
@invalid_attrs %{
suspend_min: nil,
Expand All @@ -141,7 +141,7 @@ defmodule TeslaMate.SettingsTest do
assert settings.suspend_after_idle_min == 15
assert settings.req_not_unlocked == true
assert settings.free_supercharging == false
assert settings.use_streaming_api == false
assert settings.use_streaming_api == true
end

test "update_car_settings/2 with valid data updates the settings" do
Expand All @@ -158,7 +158,7 @@ defmodule TeslaMate.SettingsTest do
assert settings.suspend_after_idle_min == 60
assert settings.req_not_unlocked == true
assert settings.free_supercharging == true
assert settings.use_streaming_api == true
assert settings.use_streaming_api == false
end

test "update_car_settings/2 publishes the settings" do
Expand Down
3 changes: 3 additions & 0 deletions test/teslamate/updater_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ defmodule TeslaMate.UpdaterTest do
with_mocks MojitoMock.vsn("v5.1.2") do
## current_version > new_version
{:ok, pid} = start_updater(name, "5.1.3-dev", id: 0)
Process.sleep(100)
assert nil == Updater.get_update(pid)

## current_version == new_version
{:ok, pid} = start_updater(name, "5.1.2", id: 1)
Process.sleep(100)
assert nil == Updater.get_update(pid)

## current_version < new_version
{:ok, pid} = start_updater(name, "4.99.0", id: 2)
Process.sleep(100)
assert "5.1.2" == Updater.get_update(pid)
end
end
Expand Down
4 changes: 3 additions & 1 deletion test/teslamate/vehicles/vehicle/charging_sync_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingSyncTest do

@tag :capture_log
test "handles invalid charge data", %{test: name} do
car = car_fixture()
%{eid: eid} = car = car_fixture()
now_ts = DateTime.utc_now() |> DateTime.to_unix(:millisecond)

events = [
Expand All @@ -30,9 +30,11 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingSyncTest do
|> Process.whereis()
|> Process.link()

assert_receive {ApiMock, {:stream, ^eid, _}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :charging}}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :charging}}}
assert_receive {:"$websockex_cast", :disconnect}

refute_receive _
end) =~
Expand Down
33 changes: 23 additions & 10 deletions test/teslamate/vehicles/vehicle/charging_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingTest do

start_date = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^start_date}, 400
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online, since: s0}}}

assert_receive {:start_charging_process, ^car, %{latitude: 0.0, longitude: 0.0},
[lookup_address: true]}

assert_receive {:"$websockex_cast", :disconnect}

assert_receive {:insert_charge, %ChargingProcess{id: process_id} = cproc,
%{
date: _,
Expand Down Expand Up @@ -80,6 +83,7 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingTest do

start_date = DateTime.from_unix!(now_ts + 4, :millisecond)
assert_receive {:start_state, ^car, :online, date: ^start_date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online, since: s2}}}
assert DateTime.diff(s1, s2, :nanosecond) < 0
Expand Down Expand Up @@ -114,12 +118,15 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingTest do

start_date = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^start_date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}

assert_receive {:start_charging_process, ^car, %{latitude: 0.0, longitude: 0.0},
[lookup_address: true]}

assert_receive {:"$websockex_cast", :disconnect}

assert_receive {:insert_charge, %ChargingProcess{id: cproc_id} = cproc,
%{date: _, charge_energy_added: 0.1}}

Expand All @@ -137,6 +144,7 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingTest do

start_date = DateTime.from_unix!(now_ts + 4, :millisecond)
assert_receive {:start_state, ^car, :online, date: ^start_date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}

Expand All @@ -157,19 +165,23 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingTest do
{:ok, %TeslaApi.Vehicle{state: "online", charge_state: nil}},
{:ok, charging_event(now_ts + 3, "Charging", 0.3)},
{:ok, charging_event(now_ts + 5, "Complete", 0.3)},
{:ok, online_event(drive_state: %{timestamp: now_ts + 6, latitude: 0.2, longitude: 0.2})}
{:ok, online_event(drive_state: %{timestamp: now_ts + 6, latitude: 0.2, longitude: 0.2})},
fn -> Process.sleep(10_000) end
]

:ok = start_vehicle(name, events)

start_date = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^start_date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}

assert_receive {:start_charging_process, ^car, %{latitude: 0.0, longitude: 0.0},
[lookup_address: true]}

assert_receive {:"$websockex_cast", :disconnect}

assert_receive {:insert_charge, cproc, %{date: _, charge_energy_added: 0.1}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :charging}}}

Expand All @@ -184,6 +196,7 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingTest do

start_date = DateTime.from_unix!(now_ts + 5, :millisecond)
assert_receive {:start_state, ^car, :online, date: ^start_date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}

Expand All @@ -197,25 +210,24 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingTest do

events = [
{:ok, online_event()},
{:ok, charging_event(now_ts, "Charging", 22)}
{:ok, charging_event(now_ts, "Charging", 22)},
fn -> Process.sleep(10_000) end
]

:ok = start_vehicle(name, events)

start_date = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^start_date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}

assert_receive {:start_charging_process, ^car, %{latitude: 0.0, longitude: 0.0},
[lookup_address: true]}

assert_receive {:"$websockex_cast", :disconnect}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :charging}}}

assert_receive {:insert_charge, charging_event, %{date: _, charge_energy_added: 22}}
assert_receive {:insert_charge, ^charging_event, %{date: _, charge_energy_added: 22}}

# ...

refute_received _
end
Expand All @@ -230,29 +242,30 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingTest do
{:ok, charging_event(now_ts + 1, "Charging", 0.1)},
{:ok, charging_event(now_ts + 2, "Charging", 0.2)},
{:error, :vehicle_unavailable},
{:ok, %TeslaApi.Vehicle{state: "asleep"}}
{:ok, %TeslaApi.Vehicle{state: "asleep"}},
fn -> Process.sleep(10_000) end
]

:ok = start_vehicle(name, events)

start_date = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^start_date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}

assert_receive {:start_charging_process, ^car, %{latitude: 0.0, longitude: 0.0},
[lookup_address: true]}

assert_receive {:"$websockex_cast", :disconnect}

assert_receive {:insert_charge, %ChargingProcess{id: cproc_id} = cproc,
%{date: _, charge_energy_added: 0.1}}

assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :charging}}}

assert_receive {:insert_charge, ^cproc, %{date: _, charge_energy_added: 0.2}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :charging}}}

assert_receive {:complete_charging_process, ^cproc}

assert_receive {:start_state, ^car, :asleep, []}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :asleep}}}

Expand Down
12 changes: 12 additions & 0 deletions test/teslamate/vehicles/vehicle/driving_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule TeslaMate.Vehicles.Vehicle.DrivingTest do

start_date = DateTime.from_unix!(now_ts + 1, :millisecond)
assert_receive {:start_state, car, :online, date: ^start_date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online, since: s0}}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :driving, since: s1}}}
Expand Down Expand Up @@ -66,6 +67,7 @@ defmodule TeslaMate.Vehicles.Vehicle.DrivingTest do

start_date = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^start_date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :driving}}}
Expand Down Expand Up @@ -100,6 +102,7 @@ defmodule TeslaMate.Vehicles.Vehicle.DrivingTest do

start_date = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^start_date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :driving}}}
Expand Down Expand Up @@ -127,6 +130,7 @@ defmodule TeslaMate.Vehicles.Vehicle.DrivingTest do

date = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}

Expand All @@ -148,6 +152,7 @@ defmodule TeslaMate.Vehicles.Vehicle.DrivingTest do

start_date = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^start_date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :driving}}}
Expand Down Expand Up @@ -216,6 +221,7 @@ defmodule TeslaMate.Vehicles.Vehicle.DrivingTest do

date = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :driving}}}
Expand Down Expand Up @@ -286,6 +292,7 @@ defmodule TeslaMate.Vehicles.Vehicle.DrivingTest do

date = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :driving}}}
Expand Down Expand Up @@ -314,6 +321,7 @@ defmodule TeslaMate.Vehicles.Vehicle.DrivingTest do

date = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :driving}}}
Expand All @@ -325,6 +333,7 @@ defmodule TeslaMate.Vehicles.Vehicle.DrivingTest do
# Timeout
assert_receive {:close_drive, ^drive, lookup_address: true}, 1200
assert_receive {:start_state, car, :asleep, []}
assert_receive {:"$websockex_cast", :disconnect}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :asleep}}}

refute_receive _
Expand Down Expand Up @@ -359,6 +368,7 @@ defmodule TeslaMate.Vehicles.Vehicle.DrivingTest do

d0 = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^d0}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :driving}}}
Expand Down Expand Up @@ -420,6 +430,7 @@ defmodule TeslaMate.Vehicles.Vehicle.DrivingTest do

d0 = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^d0}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :driving}}}
Expand Down Expand Up @@ -466,6 +477,7 @@ defmodule TeslaMate.Vehicles.Vehicle.DrivingTest do
:ok = start_vehicle(name, events)

assert_receive {:start_state, car, :online, date: _}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{longitude: 45}}

assert_receive {:pubsub,
Expand Down
6 changes: 6 additions & 0 deletions test/teslamate/vehicles/vehicle/suspend_logging_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule TeslaMate.Vehicles.Vehicle.SuspendLoggingTest do

:ok = start_vehicle(name, events, settings: %{suspend_min: 1000})
assert_receive {:start_state, car, :online, date: _}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}

Expand Down Expand Up @@ -271,12 +272,14 @@ defmodule TeslaMate.Vehicles.Vehicle.SuspendLoggingTest do

d0 = DateTime.from_unix!(now_ts, :millisecond)
assert_receive {:start_state, car, :online, date: ^d0}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online, since: s0}}}

assert_receive {:start_charging_process, ^car, %{latitude: 0.0, longitude: 0.0},
[lookup_address: true]}

assert_receive {:"$websockex_cast", :disconnect}
assert_receive {:insert_charge, cproc, %{date: _, charge_energy_added: 1.5}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :charging, since: s1}}}
assert DateTime.diff(s0, s1, :nanosecond) < 0
Expand All @@ -287,6 +290,7 @@ defmodule TeslaMate.Vehicles.Vehicle.SuspendLoggingTest do

d1 = DateTime.from_unix!(now_ts + 1, :millisecond)
assert_receive {:start_state, ^car, :online, date: ^d1}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online, since: s2}}}
assert DateTime.diff(s1, s2, :nanosecond) < 0
Expand All @@ -313,6 +317,7 @@ defmodule TeslaMate.Vehicles.Vehicle.SuspendLoggingTest do
)

assert_receive {:start_state, car, :online, date: _}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online, since: s0}}}

Expand Down Expand Up @@ -356,6 +361,7 @@ defmodule TeslaMate.Vehicles.Vehicle.SuspendLoggingTest do

date = DateTime.from_unix!(0, :millisecond)
assert_receive {:start_state, car, :online, date: ^date}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}

assert {:error, :unlocked} = Vehicle.suspend_logging(name)
Expand Down
Loading

0 comments on commit a457d17

Please sign in to comment.