Skip to content

Commit

Permalink
fix empty topic strings
Browse files Browse the repository at this point in the history
  • Loading branch information
brchri committed Dec 13, 2023
1 parent bed35fd commit ccc1262
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
9 changes: 4 additions & 5 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,23 +236,22 @@ func onMqttConnect(client mqtt.Client) {
logger.Infof("Subscribing to MQTT topics for car %d", car.ID)

// define which topics are relevant for each car based on config
topics := car.GarageDoor.Geofence.GetMqttTopics()
topics := car.GarageDoor.Geofence.GetMqttTopics(car.ID)

// subscribe to topics
for _, topic := range topics {
topicSubscribed := false
// retry topic subscription attempts with 1 sec delay between attempts
for retryAttempts := 5; retryAttempts > 0; retryAttempts-- {
fullTopic := fmt.Sprintf("%s", topic)
logger.Debugf("Subscribing to topic: %s", fullTopic)
logger.Debugf("Subscribing to topic: %s", topic)
if token := client.Subscribe(
fullTopic,
topic,
0,
func(client mqtt.Client, message mqtt.Message) {
messageChan <- message
}); token.Wait() && token.Error() == nil {
topicSubscribed = true
logger.Debugf("Topic subscribed successfully: %s", fullTopic)
logger.Debugf("Topic subscribed successfully: %s", topic)
break
} else {
logger.Infof("Failed to subscribe to topic %s for car %d, will make %d more attempts. Error: %v", topic, car.ID, retryAttempts, token.Error())
Expand Down
13 changes: 6 additions & 7 deletions internal/geo/circular.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ type (
}
)

var circularMqttTopics = []string{
util.Config.Global.MqttSettings.LatTopic,
util.Config.Global.MqttSettings.LngTopic,
}

func (c *CircularGeofence) GetMqttTopics() []string {
return circularMqttTopics
func (c *CircularGeofence) GetMqttTopics(carId int) []string {
// doesn't care about carId, just needs to match interface signature
return []string{
util.Config.Global.MqttSettings.LatTopic,
util.Config.Global.MqttSettings.LngTopic,
}
}

func distance(point1 Point, point2 Point) float64 {
Expand Down
2 changes: 1 addition & 1 deletion internal/geo/geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type (
// and what action should be taken if those are different (indicating a crossing of geofences)
getEventChangeAction(*Car) string
// get teslamate mqtt topics relevant to the implemented geofence type
GetMqttTopics() []string
GetMqttTopics(carId int) []string
// parse the settings: of a geofence into the specific geofence type struct
parseSettings(map[string]interface{}) error
}
Expand Down
13 changes: 6 additions & 7 deletions internal/geo/polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ type (
}
)

var polygonMqttTopics = []string{
util.Config.Global.MqttSettings.LatTopic,
util.Config.Global.MqttSettings.LngTopic,
}

func init() {
logger.SetFormatter(&util.CustomFormatter{})
logger.SetOutput(os.Stdout)
Expand All @@ -50,8 +45,12 @@ func init() {
}
}

func (p *PolygonGeofence) GetMqttTopics() []string {
return polygonMqttTopics
func (p *PolygonGeofence) GetMqttTopics(carId int) []string {
// doesn't care about carId, just needs to match interface signature
return []string{
util.Config.Global.MqttSettings.LatTopic,
util.Config.Global.MqttSettings.LngTopic,
}
}

// get action based on whether we had a polygon geofence change event
Expand Down
6 changes: 2 additions & 4 deletions internal/geo/teslamate_geofence.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ type (
}
)

var teslamateMqttTopics = []string{"geofence"}

func (t *TeslamateGeofence) GetMqttTopics() []string {
return teslamateMqttTopics
func (t *TeslamateGeofence) GetMqttTopics(carId int) []string {
return []string{fmt.Sprintf("teslamate/cars/%d/geofence", carId)}
}

// gets action based on if there was a relevant geofence event change
Expand Down

0 comments on commit ccc1262

Please sign in to comment.