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

Add location topic #3729

Merged
merged 2 commits into from
Mar 18, 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
30 changes: 30 additions & 0 deletions lib/teslamate/mqtt/pubsub/vehicle_subscriber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,36 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriber do
nil
end)

if state.last_summary == nil or
state.last_summary.latitude != summary.latitude or
state.last_summary.longitude != summary.longitude do
lat_lng =
case {summary.latitude, summary.longitude} do
{nil, _} -> nil
{_, nil} -> nil
{%Decimal{} = lat, %Decimal{} = lon} -> {Decimal.to_float(lat), Decimal.to_float(lon)}
{lat, lon} -> {lat, lon}
end

case lat_lng do
nil ->
nil

{lat, lon} ->
location =
%{
latitude: lat,
longitude: lon
}
|> Jason.encode!()

case publish({"location", location}, state) do
:ok -> nil
{:error, reason} -> Logger.warning("Failed to publish location: #{inspect(reason)}")
end
end
end

{:noreply, %State{state | last_summary: summary}}
end

Expand Down
10 changes: 9 additions & 1 deletion test/teslamate/mqtt/pubsub/vehicle_subscriber_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriberTest do
assert_receive {MqttPublisherMock, {:publish, ^topic, "", [retain: true, qos: 1]}}
end

assert_receive {MqttPublisherMock,
{:publish, "teslamate/cars/0/location", data, [retain: true, qos: 1]}}

assert Jason.decode!(data) == %{
"latitude" => 37.889602,
"longitude" => 41.129182
}

refute_receive _
end

Expand Down Expand Up @@ -158,7 +166,7 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriberTest do
geofence = %GeoFence{id: 0, name: "Home", latitude: 0.0, longitude: 0.0, radius: 20}
other_geofence = %GeoFence{id: 0, name: "Work", latitude: 0.0, longitude: 0.0, radius: 20}

# Send geofence
# Send geofence
send(pid, %Summary{geofence: geofence, version: "1"})
assert_receive {MqttPublisherMock, {:publish, "teslamate/cars/0/geofence", "Home", _}}
assert_receive {MqttPublisherMock, {:publish, "teslamate/cars/0/version", "1", _}}
Expand Down
8 changes: 8 additions & 0 deletions test/teslamate/vehicles/vehicle_sync_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ defmodule TeslaMate.Vehicles.VehicleSyncTest do
assert_receive {MqttPublisherMock, {:publish, ^topic, ^data, [retain: true, qos: 1]}}
end

topic = "teslamate/cars/#{car.id}/location"
assert_receive {MqttPublisherMock, {:publish, ^topic, data, [retain: true, qos: 1]}}

assert Jason.decode!(data) == %{
"latitude" => 37.889544,
"longitude" => 41.128817
}

refute_receive _
end
end
Expand Down
1 change: 1 addition & 0 deletions website/docs/integrations/mqtt.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Vehicle data will be published to the following topics:
| | | |
| `teslamate/cars/$car_id/latitude` | 35.278131 | Last reported car latitude |
| `teslamate/cars/$car_id/longitude` | 29.744801 | Last reported car longitude |
| `teslamate/cars/$car_id/location` | "latitude": 37.889544, "longitude: 41.128817 | Last reported car location |
| `teslamate/cars/$car_id/shift_state` | D | Current/Last Shift State (D/N/R/P) |
| `teslamate/cars/$car_id/power` | -9 | Current battery power in watts. Positive value on discharge, negative value on charge |
| `teslamate/cars/$car_id/speed` | 12 | Current Speed in km/h |
Expand Down
Loading