Skip to content

Commit

Permalink
Made MQTT Interval to be time.Duration.
Browse files Browse the repository at this point in the history
  • Loading branch information
icemarkom committed Nov 25, 2021
1 parent c803c22 commit cdbd1b0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
type MQTTConfig struct {
Enabled, SSL bool
Broker, Name, Password, Topic, TopicConfig, TopicState, Username string
Interval, Port int
Port int
Interval time.Duration
}

// MQTTDevice describes sensor for Home Assistant.
Expand Down Expand Up @@ -67,7 +68,7 @@ func mqttClient() (mqtt.Client, error) {
o.ConnectRetry = true
o.AutoReconnect = true
o.CleanSession = true
o.ConnectRetryInterval = time.Duration(cfg.MQTT.Interval) * time.Second
o.ConnectRetryInterval = cfg.MQTT.Interval
o.OnConnectAttempt = handlerOnConnectAttempt
o.OnConnectionLost = handlerOnConnectionLost
o.OnReconnecting = handlerReconnecting
Expand All @@ -82,7 +83,7 @@ func mqttClient() (mqtt.Client, error) {
}

func loopWait() {
time.Sleep(time.Duration(cfg.MQTT.Interval) * time.Second)
time.Sleep(cfg.MQTT.Interval)
}

func doMQTT(wg *sync.WaitGroup) {
Expand All @@ -109,6 +110,9 @@ func doMQTT(wg *sync.WaitGroup) {
}

c, err := mqttClient()
if err != nil {
log.Fatalf("Could not create MQTT client: %v", err)
}
for {
if !c.IsConnected() || !c.IsConnectionOpen() {
loopWait()
Expand Down
2 changes: 1 addition & 1 deletion pitemp.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func init() {
flag.BoolVar(&cfg.MQTT.SSL, "mqtt_ssl", false, "MQTT SSL")
flag.StringVar(&cfg.MQTT.Name, "mqtt_client", hostname, "MQTT Client Name")
flag.StringVar(&cfg.MQTT.Topic, "mqtt_topic", fmt.Sprintf(defaultMQTTTopic, hostname), "MQTT Topic prefix")
flag.IntVar(&cfg.MQTT.Interval, "mqtt_interval", defaultMQTTInterval, "MQTT notification interval in seconds")
flag.DurationVar(&cfg.MQTT.Interval, "mqtt_interval", defaultMQTTInterval*time.Second, "MQTT notification interval in seconds")
flag.StringVar(&cfg.MQTT.Username, "mqtt_username", "", "MQTT username")
flag.StringVar(&cfg.MQTT.Password, "mqtt_password", "", "MQTT password")
flag.Parse()
Expand Down

0 comments on commit cdbd1b0

Please sign in to comment.