diff --git a/.gitignore b/.gitignore index 93708e63..1ac000d4 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,5 @@ config/*.env /deploy/ conf/wizzy.json + +.env diff --git a/lib/teslamate/log.ex b/lib/teslamate/log.ex index 39594e80..b6a4c1bb 100644 --- a/lib/teslamate/log.ex +++ b/lib/teslamate/log.ex @@ -255,6 +255,12 @@ defmodule TeslaMate.Log do end end + def cancel_update(update_id) do + Update + |> Repo.get!(update_id) + |> Repo.delete() + end + def finish_update(update_id, version) do Update |> Repo.get!(update_id) diff --git a/lib/teslamate/vehicles/vehicle/vehicle.ex b/lib/teslamate/vehicles/vehicle/vehicle.ex index 23ef8162..7fcfb39b 100644 --- a/lib/teslamate/vehicles/vehicle/vehicle.ex +++ b/lib/teslamate/vehicles/vehicle/vehicle.ex @@ -341,6 +341,14 @@ defmodule TeslaMate.Vehicles.Vehicle do %VehicleState.SoftwareUpdate{status: "installing"} -> {:keep_state, %Data{data | last_used: DateTime.utc_now()}, schedule_fetch(15)} + %VehicleState.SoftwareUpdate{status: "available"} = software_update -> + {:ok, %Log.Update{}} = call(data.deps.log, :cancel_update, [update_id]) + + Logger.warn("Update canceled | #{inspect(software_update)}") + + {:next_state, :start, %Data{data | last_used: DateTime.utc_now()}, + {:next_event, :internal, {:update, {:online, vehicle}}}} + %VehicleState.SoftwareUpdate{status: status} = software_update -> if status != "" do Logger.error("Update failed: #{status} | #{inspect(software_update)}") diff --git a/mix.lock b/mix.lock index 843ba5f1..e9db6a38 100644 --- a/mix.lock +++ b/mix.lock @@ -6,7 +6,7 @@ "cowlib": {:hex, :cowlib, "2.7.3", "a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9", [:rebar3], [], "hexpm"}, "db_connection": {:hex, :db_connection, "2.1.0", "122e2f62c4906bf2e49554f1e64db5030c19229aa40935f33088e7d543aa79d0", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"}, "decimal": {:hex, :decimal, "1.7.0", "30d6b52c88541f9a66637359ddf85016df9eb266170d53105f02e4a67e00c5aa", [:mix], [], "hexpm"}, - "ecto": {:hex, :ecto, "3.1.5", "07dfe89630857ea1a90fd5575c41b974696dae27071ac91bcbb7b769a15b66db", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"}, + "ecto": {:hex, :ecto, "3.1.6", "e890bf66c1d4d8e2b8e010f7cba092a08139b55437bc3382371f72a6ee40757e", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"}, "ecto_enum": {:hex, :ecto_enum, "1.2.0", "9ead3ee04efc4cb68a50560a9d9ebb665dd697f957f1c3df8e81bf863cf7a4e9", [:mix], [{:ecto, ">= 2.0.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, ">= 0.0.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm"}, "ecto_sql": {:hex, :ecto_sql, "3.1.5", "b5201fe99fa6bf6a93f64adb2d4976ded3d201f932b7c5bd4c44468642f4fb1f", [:mix], [{:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.1.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.9.1", [hex: :mariaex, repo: "hexpm", optional: true]}, {:myxql, "~> 0.2.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.14.0 or ~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"}, "file_system": {:hex, :file_system, "0.2.7", "e6f7f155970975789f26e77b8b8d8ab084c59844d8ecfaf58cbda31c494d14aa", [:mix], [], "hexpm"}, @@ -25,7 +25,7 @@ "phoenix_ecto": {:hex, :phoenix_ecto, "4.0.0", "c43117a136e7399ea04ecaac73f8f23ee0ffe3e07acfcb8062fe5f4c9f0f6531", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"}, "phoenix_html": {:hex, :phoenix_html, "2.13.3", "850e292ff6e204257f5f9c4c54a8cb1f6fbc16ed53d360c2b780a3d0ba333867", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"}, "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.2.1", "274a4b07c4adbdd7785d45a8b0bb57634d0b4f45b18d2c508b26c0344bd59b8f", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm"}, - "phoenix_live_view": {:git, "https://github.com/phoenixframework/phoenix_live_view.git", "c4486ec75cd4ef4aa876cdbe2e849b1e8aa7e8cb", []}, + "phoenix_live_view": {:git, "https://github.com/phoenixframework/phoenix_live_view.git", "bcacd1d0545e923f069738fe244c27904a58973d", []}, "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.1.2", "496c303bdf1b2e98a9d26e89af5bba3ab487ba3a3735f74bf1f4064d2a845a3e", [:mix], [], "hexpm"}, "plug": {:hex, :plug, "1.8.2", "0bcce1daa420f189a6491f3940cc77ea7fb1919761175c9c3b59800d897440fc", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm"}, "plug_cowboy": {:hex, :plug_cowboy, "2.0.2", "6055f16868cc4882b24b6e1d63d2bada94fb4978413377a3b32ac16c18dffba2", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"}, diff --git a/test/support/mocks/log.ex b/test/support/mocks/log.ex index b73290d7..0fa31650 100644 --- a/test/support/mocks/log.ex +++ b/test/support/mocks/log.ex @@ -21,6 +21,7 @@ defmodule LogMock do def close_trip(name, trip_id), do: GenServer.call(name, {:close_trip, trip_id}) def start_update(name, car_id), do: GenServer.call(name, {:start_update, car_id}) + def cancel_update(name, update_id), do: GenServer.call(name, {:cancel_update, update_id}) def finish_update(name, update_id, version), do: GenServer.call(name, {:finish_update, update_id, version}) @@ -82,7 +83,12 @@ defmodule LogMock do {:reply, {:ok, 111}, state} end - def handle_call({:finish_update, _trip_id, _version} = action, _from, %State{pid: pid} = state) do + def handle_call({:cancel_update, _update_id} = action, _from, %State{pid: pid} = state) do + send(pid, action) + {:reply, {:ok, %Update{}}, state} + end + + def handle_call({:finish_update, _upd_id, _version} = action, _from, %State{pid: pid} = state) do send(pid, action) {:reply, {:ok, %Update{}}, state} end diff --git a/test/teslamate/log/log_update_test.exs b/test/teslamate/log/log_update_test.exs index 8e09289d..db6586e7 100644 --- a/test/teslamate/log/log_update_test.exs +++ b/test/teslamate/log/log_update_test.exs @@ -28,8 +28,19 @@ defmodule TeslaMate.LogUpdateTest do end end + describe "cancel_update/1" do + test "deletes an update" do + assert %Car{id: car_id} = car_fixture() + assert {:ok, update_id} = Log.start_update(car_id) + + assert {:ok, %Update{} = update} = Log.cancel_update(update_id) + + assert nil == Repo.get(Update, update_id) + end + end + describe "finish_update/1" do - test "closes charging process with zero charges " do + test "logs an update including its version" do assert %Car{id: car_id} = car_fixture() assert {:ok, update_id} = Log.start_update(car_id) diff --git a/test/teslamate/vehicles/vehicle/updating_test.exs b/test/teslamate/vehicles/vehicle/updating_test.exs index f269d991..322b8fc7 100644 --- a/test/teslamate/vehicles/vehicle/updating_test.exs +++ b/test/teslamate/vehicles/vehicle/updating_test.exs @@ -41,4 +41,29 @@ defmodule TeslaMate.Vehicles.Vehicle.UpdatingTest do refute_receive _ end + + @tag :capture_log + test "cancels an update", %{test: name} do + events = [ + {:ok, online_event()}, + {:ok, update_event("installing", "2019.8.4 530d1d3")}, + {:ok, update_event("available", "2019.8.4 530d1d3")} + ] + + :ok = start_vehicle(name, events) + + assert_receive {:start_state, car_id, :online} + assert_receive {:insert_position, ^car_id, %{}} + assert_receive {:pubsub, {:broadcast, _server, _topic, %Summary{state: :online}}} + + assert_receive {:start_update, ^car_id} + assert_receive {:pubsub, {:broadcast, _server, _topic, %Summary{state: :updating}}} + assert_receive {:cancel_update, _upate_id}, 200 + + assert_receive {:start_state, ^car_id, :online} + assert_receive {:insert_position, ^car_id, %{}} + assert_receive {:pubsub, {:broadcast, _server, _topic, %Summary{state: :online}}} + + refute_receive _ + end end