Skip to content

Commit

Permalink
Add location topic
Browse files Browse the repository at this point in the history
Fixes #3660.
  • Loading branch information
brianmay committed Mar 11, 2024
1 parent aa84ab5 commit 6834fd5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 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,21 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriber do
nil
end)

if summary.latitude != nil and summary.longitude != nil and summary.elevation != nil do
location =
%{
latitude: summary.latitude,
longitude: summary.longitude,
elevation: summary.elevation
}
|> Jason.encode!()

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

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

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

assert_receive {MqttPublisherMock,
{:publish, "teslamate/cars/0/location",
"{\"latitude\":37.889602,\"longitude\":41.129182,\"elevation\":100}",
[retain: true, qos: 1]}}

refute_receive _
end

Expand Down

0 comments on commit 6834fd5

Please sign in to comment.