Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix agressive switch to offline when stream gets offline message #3672

Merged
merged 2 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/teslamate/vehicles/vehicle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,12 @@ defmodule TeslaMate.Vehicles.Vehicle do

def handle_event(:info, {:stream, msg}, _state, data)
when msg in [:vehicle_offline] do
Logger.info("Stream reports vehicle as offline … ", car_id: data.car.id)
Logger.warning("Stream reports vehicle as offline, fetching vehicle state ...",
car_id: data.car.id
)

{:next_state, :start, data,
[broadcast_fetch(false), {:next_event, :internal, {:update, {:offline, data.last_response}}}]}
# fetch data right away and let the result decide the real state
{:keep_state_and_data, schedule_fetch(0, data)}
end

def handle_event(:info, {:stream, stream_data}, _state, data) do
Expand Down
28 changes: 5 additions & 23 deletions test/teslamate/vehicles/vehicle/streaming_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -572,22 +572,10 @@ defmodule TeslaMate.Vehicles.Vehicle.StreamingTest do
end

describe "offline" do
test "go offline when stream get :vehicle_offline", %{test: name} do
me = self()

test "fetch state when stream get :vehicle_offline", %{test: name} do
events = [
{:ok, online_event()},
{:ok, online_event()},
{:ok, online_event()},
fn ->
send(me, :continue?)

receive do
:continue -> {:ok, %TeslaApi.Vehicle{state: "offline"}}
after
5_000 -> raise "No :continue after 5s"
end
end,
fn -> Process.sleep(10_000) end
]

Expand All @@ -601,16 +589,10 @@ defmodule TeslaMate.Vehicles.Vehicle.StreamingTest do
assert_receive {ApiMock, {:stream, _eid, func}} when is_function(func)
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}

send(name, {:stream, :vehicle_offline})

assert_receive :continue?
send(:"api_#{name}", :continue)

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

refute_receive _
assert capture_log(@log_opts, fn ->
send(name, {:stream, :vehicle_offline})
refute_receive _
end) =~ "Stream reports vehicle as offline, fetching vehicle state ..."
end
end
end
Loading