Fix bug where multiple sensors wouldn't work
ci/woodpecker/push/build Pipeline was successful Details

This commit is contained in:
Jan Zípek 2025-03-07 21:39:35 +01:00
parent abc38b61f0
commit 9531bd95de
Signed by: kamen
GPG Key ID: A17882625B33AC31
1 changed files with 9 additions and 2 deletions

View File

@ -218,7 +218,7 @@ func (s *MQTTBrokersService) EnsureListeners() {
}
topics := map[string]byte{}
brokerSensors := []*models.SensorItem{}
brokerSensors := []models.SensorItem{}
for _, sensor := range sensors {
if sensor.MqttBrokerId == nil || *sensor.MqttBrokerId != broker.Id {
@ -230,7 +230,7 @@ func (s *MQTTBrokersService) EnsureListeners() {
}
topics[*sensor.MqttTopic] = byte(0)
brokerSensors = append(brokerSensors, &sensor)
brokerSensors = append(brokerSensors, sensor)
}
if len(brokerSensors) == 0 {
@ -249,6 +249,10 @@ func (s *MQTTBrokersService) EnsureListeners() {
log.Printf("MQTT broker %s: Listening for %d topics\n", broker.Name, len(topics))
for _, sensor := range brokerSensors {
log.Printf("MQTT broker %s: Sensor %s (%d) - %s\n", broker.Name, sensor.Name, sensor.Id, *sensor.MqttTopic)
}
go func() {
for {
data, ok := <-client.Channel
@ -275,6 +279,7 @@ func (s *MQTTBrokersService) EnsureListeners() {
}
if *sensor.MqttTopic != data.Topic {
log.Printf("WARN: Skipping sensor %s because it has different topic", sensor.Name)
continue
}
@ -298,6 +303,8 @@ func (s *MQTTBrokersService) EnsureListeners() {
continue
}
log.Printf("Value for sensor %d (%s) - %f", sensor.Id, sensor.Name, value)
s.ctx.Services.SensorValues.Push(sensor.Id, value)
}