Skip to content

Commit 3fe1151

Browse files
committed
Migrate compound state reads to struct field guards
This PR migrates all pattern-match reads of compound state tuples ({:driving, status, drive}, {:charging, cproc}, {:updating, update}) to use the corresponding Data struct fields instead. Changes: - vehicle_in_service driving case: guard on data.current_drive != nil, read drive from data.current_drive, clear both current_drive and driving_status on exit from driving state - remaining_distance interval: use {current_drive, current_charging_process} tuple instead of compound state pattern match - asleep handler: guard on driving_status != nil && driving_status != :available to detect offline sub-states, clear driving_status on :start exit - fetch can_fall_asleep?: add guards on current_drive/update/charging_process - fetch reachable: same guards added - All 6 transitions to :start from driving state now clear driving_status: nil The compound state tuples remain in next_state positions (handled in prior PR), so this is purely a read migration. No behaviour changes expected. No behaviour changes - all guards are guaranteed true at runtime given correct dual-write population from prior PR.
1 parent 3ef21e1 commit 3fe1151

1 file changed

Lines changed: 32 additions & 19 deletions

File tree

lib/teslamate/vehicles/vehicle.ex

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,9 @@ defmodule TeslaMate.Vehicles.Vehicle do
468468
Logger.info("Vehicle is currently in service", car_id: data.car.id)
469469

470470
case state do
471-
{:driving, _, %Log.Drive{} = drive} ->
471+
{:driving, _, _} when data.current_drive != nil ->
472+
drive = data.current_drive
473+
472474
{:ok, %Log.Drive{distance: km, duration_min: min}} =
473475
call(data.deps.log, :close_drive, [drive])
474476

@@ -478,7 +480,8 @@ defmodule TeslaMate.Vehicles.Vehicle do
478480
car_id: data.car.id
479481
)
480482

481-
{:next_state, :start, %Data{data | last_used: DateTime.utc_now()},
483+
{:next_state, :start,
484+
%Data{data | last_used: DateTime.utc_now(), driving_status: nil, current_drive: nil},
482485
[
483486
broadcast_fetch(false),
484487
broadcast_summary(),
@@ -525,11 +528,18 @@ defmodule TeslaMate.Vehicles.Vehicle do
525528
end
526529

527530
interval =
528-
case state do
529-
{:driving, _, _} -> 10
530-
{:charging, _} -> 15
531-
:online -> 20
532-
_ -> 30
531+
case {data.current_drive != nil, data.current_charging_process != nil} do
532+
{true, _} ->
533+
10
534+
535+
{_, true} ->
536+
15
537+
538+
_ ->
539+
case state do
540+
:online -> 20
541+
_ -> 30
542+
end
533543
end
534544

535545
{:keep_state, data,
@@ -1206,7 +1216,8 @@ defmodule TeslaMate.Vehicles.Vehicle do
12061216
{:driving, {:offline, _last}, nil},
12071217
%Data{} = data
12081218
) do
1209-
{:next_state, :start, %Data{data | last_used: DateTime.utc_now()}, schedule_fetch(data)}
1219+
{:next_state, :start, %Data{data | last_used: DateTime.utc_now(), driving_status: nil},
1220+
schedule_fetch(data)}
12101221
end
12111222

12121223
def handle_event(
@@ -1269,13 +1280,13 @@ defmodule TeslaMate.Vehicles.Vehicle do
12691280

12701281
Logger.info("Vehicle was charged while being offline: #{added} kWh", car_id: data.car.id)
12711282

1272-
{:next_state, :start, %{data | last_used: DateTime.utc_now()},
1283+
{:next_state, :start, %{data | last_used: DateTime.utc_now(), driving_status: nil},
12731284
{:next_event, :internal, {:update, {:online, now}}}}
12741285

12751286
not has_gained_range? and offline_min >= @drive_timeout_min ->
12761287
unless is_nil(drv), do: timeout_drive(drv, data)
12771288

1278-
{:next_state, :start, %{data | last_used: DateTime.utc_now()},
1289+
{:next_state, :start, %{data | last_used: DateTime.utc_now(), driving_status: nil},
12791290
{:next_event, :internal, {:update, {:online, now}}}}
12801291

12811292
not is_nil(drv) ->
@@ -1287,9 +1298,10 @@ defmodule TeslaMate.Vehicles.Vehicle do
12871298

12881299
#### msg: asleep
12891300

1290-
def handle_event(:internal, {:update, {:asleep, _vehicle}}, {:driving, _, drv}, data) do
1301+
def handle_event(:internal, {:update, {:asleep, _vehicle}}, {:driving, _, drv}, data)
1302+
when data.driving_status != nil and data.driving_status != :available do
12911303
unless is_nil(drv), do: timeout_drive(drv, data)
1292-
{:next_state, :start, data, schedule_fetch(data)}
1304+
{:next_state, :start, %{data | driving_status: nil}, schedule_fetch(data)}
12931305
end
12941306

12951307
#### msg: :online
@@ -1345,7 +1357,8 @@ defmodule TeslaMate.Vehicles.Vehicle do
13451357
Logger.info("End of drive initiated by: #{inspect(vehicle.drive_state)}")
13461358
Logger.info("Driving / Ended / #{km && round(km)} km – #{min} min", car_id: data.car.id)
13471359

1348-
{:next_state, :start, %{data | last_used: DateTime.utc_now(), geofence: geofence},
1360+
{:next_state, :start,
1361+
%{data | last_used: DateTime.utc_now(), driving_status: nil, geofence: geofence},
13491362
{:next_event, :internal, {:update, {:online, vehicle}}}}
13501363

13511364
%Vehicle{drive_state: nil} ->
@@ -1513,13 +1526,13 @@ defmodule TeslaMate.Vehicles.Vehicle do
15131526
:online ->
15141527
true
15151528

1516-
{:driving, _, _} ->
1529+
{:driving, _, _} when data.current_drive != nil ->
15171530
true
15181531

1519-
{:updating, _} ->
1532+
{:updating, _} when data.current_update != nil ->
15201533
true
15211534

1522-
{:charging, _} ->
1535+
{:charging, _} when data.current_charging_process != nil ->
15231536
true
15241537

15251538
:start ->
@@ -1545,9 +1558,9 @@ defmodule TeslaMate.Vehicles.Vehicle do
15451558
reachable? =
15461559
case expected_state do
15471560
:online -> true
1548-
{:driving, _, _} -> true
1549-
{:updating, _} -> true
1550-
{:charging, _} -> true
1561+
{:driving, _, _} when data.current_drive != nil -> true
1562+
{:updating, _} when data.current_update != nil -> true
1563+
{:charging, _} when data.current_charging_process != nil -> true
15511564
:start -> false
15521565
{:offline, _} -> false
15531566
{:asleep, _} -> false

0 commit comments

Comments
 (0)