dt1t and counter and refactoring using embedded interfaces
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
2023-12-21 13:05:00 +01:00
parent 99d678b4b1
commit 8e6bea3f19
15 changed files with 276 additions and 174 deletions

View File

@ -7,6 +7,7 @@ import MQTT "github.com/eclipse/paho.mqtt.golang"
import "github.com/google/uuid"
import "crypto/tls"
import "udi/config"
import "udi/counter"
type Message struct {
Topic string
@ -26,10 +27,12 @@ func onMessageReceived(client MQTT.Client, message MQTT.Message) {
}
select {
case InputChannel <- m:
counter.S("Received")
{}
//log.Println("Message sent to channel")
default:
log.Println("Channel full, message lost")
counter.F("Received")
}
}
@ -54,7 +57,7 @@ func onConnect(client MQTT.Client) {
if token := client.Subscribe(topic, 0, onMessageReceived); token.Wait() && token.Error() != nil {
log.Fatalf("Unable to subscribe to topic %s, error %s", topic, token.Error())
}
log.Printf("Successfully subscribed to topic %s", topic)
log.Printf("Topic %s subscribed", topic)
}
}
@ -101,19 +104,19 @@ func StartMqttClient() {
enableTls := config.Config.Mqtt.TlsEnable
if enableTls == "true" {
log.Println("Enabling TLS connection")
//log.Println("Enabling TLS connection")
tlsConfig := &tls.Config {
InsecureSkipVerify: true,
}
opts.SetTLSConfig(tlsConfig)
}
log.Println("Trying to connect to broker")
log.Println("Broker connecting")
mqttClient = MQTT.NewClient(opts)
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
log.Fatalf("Unable to connect to broker %s, error %s", broker, token.Error())
}
log.Printf("Successfully connected to broker %s", broker)
//log.Printf("Successfully connected to broker %s", broker)
go outputDispatcher(mqttClient)