Add location topic (lat and long in one json blob) (#3729)

* Add location topic

Fixes #3660.

* doc: update mqtt topics with new location topic

---------

Co-authored-by: Jakob Lichterfeld <jakob-lichterfeld@gmx.de>
This commit is contained in:
Brian May
2024-03-18 08:36:21 +01:00
committed by GitHub
co-authored by Jakob Lichterfeld
parent e78e6ddb42
commit b5285d2ace
4 changed files with 48 additions and 1 deletions
@@ -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
@@ -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
@@ -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", _}}
@@ -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
+1
View File
@@ -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 |