mirror of
https://github.com/Luzifer/culmqtt.git
synced 2024-11-09 22:40:07 +00:00
Properly check for errors
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
bee38cbff8
commit
e617b92069
5 changed files with 41 additions and 17 deletions
17
fs20.go
17
fs20.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -34,13 +35,13 @@ func publishFS20ToCUL(client mqtt.Client, msg mqtt.Message) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func publishFS20ToMQTT(housecode, device, command string) error {
|
func publishFS20ToMQTT(housecode, device, command string) error {
|
||||||
tok := brokerClient.Publish(
|
return errors.Wrap(
|
||||||
strings.Join([]string{"culmqtt", fmt.Sprintf("%s%s", housecode, device), "state"}, "/"),
|
mqttTokToErr(brokerClient.Publish(
|
||||||
0x01, // QOS Level 1: At least once
|
strings.Join([]string{"culmqtt", fmt.Sprintf("%s%s", housecode, device), "state"}, "/"),
|
||||||
true,
|
0x01, // QOS Level 1: At least once
|
||||||
command,
|
true,
|
||||||
|
command,
|
||||||
|
)),
|
||||||
|
"publishing message",
|
||||||
)
|
)
|
||||||
|
|
||||||
tok.Wait()
|
|
||||||
return tok.Error()
|
|
||||||
}
|
}
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -6,6 +6,7 @@ require (
|
||||||
github.com/Luzifer/rconfig/v2 v2.2.1
|
github.com/Luzifer/rconfig/v2 v2.2.1
|
||||||
github.com/eclipse/paho.mqtt.golang v1.3.2
|
github.com/eclipse/paho.mqtt.golang v1.3.2
|
||||||
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4
|
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4
|
||||||
|
github.com/pkg/errors v0.9.1
|
||||||
github.com/sirupsen/logrus v1.7.0
|
github.com/sirupsen/logrus v1.7.0
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
|
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -8,6 +8,8 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U
|
||||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4 h1:G2ztCwXov8mRvP0ZfjE6nAlaCX2XbykaeHdbT6KwDz0=
|
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4 h1:G2ztCwXov8mRvP0ZfjE6nAlaCX2XbykaeHdbT6KwDz0=
|
||||||
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4/go.mod h1:2RvX5ZjVtsznNZPEt4xwJXNJrM3VTZoQf7V6gk0ysvs=
|
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4/go.mod h1:2RvX5ZjVtsznNZPEt4xwJXNJrM3VTZoQf7V6gk0ysvs=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
|
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
|
||||||
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||||
|
|
14
main.go
14
main.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/Luzifer/rconfig/v2"
|
"github.com/Luzifer/rconfig/v2"
|
||||||
"github.com/jacobsa/go-serial/serial"
|
"github.com/jacobsa/go-serial/serial"
|
||||||
|
@ -13,12 +14,13 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cfg = struct {
|
cfg = struct {
|
||||||
CULDevice string `flag:"cul-device" default:"/dev/ttyACM0" description:"TTY of the CUL to connect to"`
|
CULDevice string `flag:"cul-device" default:"/dev/ttyACM0" description:"TTY of the CUL to connect to"`
|
||||||
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
||||||
MQTTHost string `flag:"mqtt-host" default:"tcp://127.0.0.1:1883" description:"Connection URI for the broker"`
|
MQTTHost string `flag:"mqtt-host" default:"tcp://127.0.0.1:1883" description:"Connection URI for the broker"`
|
||||||
MQTTUser string `flag:"mqtt-user" default:"" description:"Username for broker connection"`
|
MQTTUser string `flag:"mqtt-user" default:"" description:"Username for broker connection"`
|
||||||
MQTTPass string `flag:"mqtt-pass" default:"" description:"Password for broker connection"`
|
MQTTPass string `flag:"mqtt-pass" default:"" description:"Password for broker connection"`
|
||||||
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
MQTTTimeout time.Duration `flag:"mqtt-timeout" default:"2s" description:"Timeout for MQTT actions"`
|
||||||
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
port io.ReadWriteCloser
|
port io.ReadWriteCloser
|
||||||
|
|
24
mqtt.go
24
mqtt.go
|
@ -1,6 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import mqtt "github.com/eclipse/paho.mqtt.golang"
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
var brokerClient mqtt.Client
|
var brokerClient mqtt.Client
|
||||||
|
|
||||||
|
@ -12,6 +17,19 @@ func init() {
|
||||||
|
|
||||||
brokerClient = mqtt.NewClient(opts)
|
brokerClient = mqtt.NewClient(opts)
|
||||||
|
|
||||||
brokerClient.Connect().Wait()
|
if err := mqttTokToErr(brokerClient.Connect()); err != nil {
|
||||||
brokerClient.Subscribe("culmqtt/+/send", 0x01, publishFS20ToCUL)
|
log.WithError(err).Fatal("Connect to MQTT broker")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := mqttTokToErr(brokerClient.Subscribe("culmqtt/+/send", 0x01, publishFS20ToCUL)); err != nil {
|
||||||
|
log.WithError(err).Fatal("Subscribe to topic")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mqttTokToErr(tok mqtt.Token) error {
|
||||||
|
if !tok.WaitTimeout(cfg.MQTTTimeout) {
|
||||||
|
return errors.New("command timed out")
|
||||||
|
}
|
||||||
|
|
||||||
|
return tok.Error()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue