From dabe5db843da94868a0a4559da527403a4c88ed9 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Fri, 29 Mar 2024 23:37:04 +0100 Subject: [PATCH 1/2] add mqtt location logic --- src/v1_TeslaMateAPICarsStatus.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/v1_TeslaMateAPICarsStatus.go b/src/v1_TeslaMateAPICarsStatus.go index b517736..b34eafd 100644 --- a/src/v1_TeslaMateAPICarsStatus.go +++ b/src/v1_TeslaMateAPICarsStatus.go @@ -2,6 +2,7 @@ package main import ( "crypto/tls" + "encoding/json" "errors" "fmt" "log" @@ -78,6 +79,10 @@ type statusInfo struct { MQTTDataActiveRouteDestination string MQTTDataActiveRouteLatitude float64 MQTTDataActiveRouteLongitude float64 + MQTTDataLocation struct { + MQTTDataLatitude float64 + MQTTDataLongitude float64 + } } type statusCache struct { @@ -351,6 +356,11 @@ func (s *statusCache) newMessage(c mqtt.Client, msg mqtt.Message) { stat.MQTTDataActiveRouteLatitude = convertStringToFloat(string(msg.Payload())) case "active_route_longitude": stat.MQTTDataActiveRouteLongitude = convertStringToFloat(string(msg.Payload())) + case "location": + err := json.Unmarshal(msg.Payload(), &stat.MQTTDataLocation) + if err != nil { + log.Printf("[warning] TeslaMateAPICarsStatusV1 mqtt.MessageHandler issue.. extraction of data for location faulty: %v", err) + } default: log.Printf("[warning] TeslaMateAPICarsStatusV1 mqtt.MessageHandler issue.. extraction of data for %s not implemented!", MqttTopic) } @@ -404,11 +414,17 @@ func (s *statusCache) TeslaMateAPICarsStatusV1(c *gin.Context) { SpoilerType string `json:"spoiler_type"` // None - The spoiler type WheelType string `json:"wheel_type"` // Pinwheel18 - The wheel type } + // Location struct - child of CarGeodata + type Location struct { + Latitude float64 `json:"latitude"` // 37.889544 - Last reported car latitude + Longitude float64 `json:"longitude"` // 41.128817 - Last reported car longitude + } // CarGeodata struct - child of MQTTInformation type CarGeodata struct { - Geofence string `json:"geofence"` // Home - The name of the Geo-fence, if one exists at the current position - Latitude float64 `json:"latitude"` // 35.278131 - Last reported car latitude - Longitude float64 `json:"longitude"` // 29.744801 - Last reported car longitude + Geofence string `json:"geofence"` // Home - The name of the Geo-fence, if one exists at the current position + Latitude float64 `json:"latitude"` // 35.278131 - Last reported car latitude + Longitude float64 `json:"longitude"` // 29.744801 - Last reported car longitude + Location Location `json:"location"` // struct } // CarStatus struct - child of MQTTInformation type CarStatus struct { @@ -598,6 +614,8 @@ func (s *statusCache) TeslaMateAPICarsStatusV1(c *gin.Context) { MQTTInformationData.TpmsDetails.TpmsSoftWarningFR = stat.MQTTDataTpmsSoftWarningFR MQTTInformationData.TpmsDetails.TpmsSoftWarningRL = stat.MQTTDataTpmsSoftWarningRL MQTTInformationData.TpmsDetails.TpmsSoftWarningRR = stat.MQTTDataTpmsSoftWarningRR + MQTTInformationData.CarGeodata.Location.Latitude = stat.MQTTDataLocation.MQTTDataLatitude + MQTTInformationData.CarGeodata.Location.Longitude = stat.MQTTDataLocation.MQTTDataLongitude // converting values based of settings UnitsLength if UnitsLength == "mi" { From d78b01b0b926c173408c5f21e5a522b716289e53 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Fri, 29 Mar 2024 23:59:28 +0100 Subject: [PATCH 2/2] reverting parts.. --- src/v1_TeslaMateAPICarsStatus.go | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/src/v1_TeslaMateAPICarsStatus.go b/src/v1_TeslaMateAPICarsStatus.go index b34eafd..4c5dbcf 100644 --- a/src/v1_TeslaMateAPICarsStatus.go +++ b/src/v1_TeslaMateAPICarsStatus.go @@ -2,7 +2,6 @@ package main import ( "crypto/tls" - "encoding/json" "errors" "fmt" "log" @@ -79,10 +78,6 @@ type statusInfo struct { MQTTDataActiveRouteDestination string MQTTDataActiveRouteLatitude float64 MQTTDataActiveRouteLongitude float64 - MQTTDataLocation struct { - MQTTDataLatitude float64 - MQTTDataLongitude float64 - } } type statusCache struct { @@ -357,10 +352,7 @@ func (s *statusCache) newMessage(c mqtt.Client, msg mqtt.Message) { case "active_route_longitude": stat.MQTTDataActiveRouteLongitude = convertStringToFloat(string(msg.Payload())) case "location": - err := json.Unmarshal(msg.Payload(), &stat.MQTTDataLocation) - if err != nil { - log.Printf("[warning] TeslaMateAPICarsStatusV1 mqtt.MessageHandler issue.. extraction of data for location faulty: %v", err) - } + // doing nothing, since this is identical data to latitude and longitude topics default: log.Printf("[warning] TeslaMateAPICarsStatusV1 mqtt.MessageHandler issue.. extraction of data for %s not implemented!", MqttTopic) } @@ -414,17 +406,11 @@ func (s *statusCache) TeslaMateAPICarsStatusV1(c *gin.Context) { SpoilerType string `json:"spoiler_type"` // None - The spoiler type WheelType string `json:"wheel_type"` // Pinwheel18 - The wheel type } - // Location struct - child of CarGeodata - type Location struct { - Latitude float64 `json:"latitude"` // 37.889544 - Last reported car latitude - Longitude float64 `json:"longitude"` // 41.128817 - Last reported car longitude - } // CarGeodata struct - child of MQTTInformation type CarGeodata struct { - Geofence string `json:"geofence"` // Home - The name of the Geo-fence, if one exists at the current position - Latitude float64 `json:"latitude"` // 35.278131 - Last reported car latitude - Longitude float64 `json:"longitude"` // 29.744801 - Last reported car longitude - Location Location `json:"location"` // struct + Geofence string `json:"geofence"` // Home - The name of the Geo-fence, if one exists at the current position + Latitude float64 `json:"latitude"` // 35.278131 - Last reported car latitude + Longitude float64 `json:"longitude"` // 29.744801 - Last reported car longitude } // CarStatus struct - child of MQTTInformation type CarStatus struct { @@ -614,8 +600,6 @@ func (s *statusCache) TeslaMateAPICarsStatusV1(c *gin.Context) { MQTTInformationData.TpmsDetails.TpmsSoftWarningFR = stat.MQTTDataTpmsSoftWarningFR MQTTInformationData.TpmsDetails.TpmsSoftWarningRL = stat.MQTTDataTpmsSoftWarningRL MQTTInformationData.TpmsDetails.TpmsSoftWarningRR = stat.MQTTDataTpmsSoftWarningRR - MQTTInformationData.CarGeodata.Location.Latitude = stat.MQTTDataLocation.MQTTDataLatitude - MQTTInformationData.CarGeodata.Location.Longitude = stat.MQTTDataLocation.MQTTDataLongitude // converting values based of settings UnitsLength if UnitsLength == "mi" {