-
Notifications
You must be signed in to change notification settings - Fork 2
/
mqttrules.go
87 lines (73 loc) · 2.11 KB
/
mqttrules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"flag"
log "github.com/Sirupsen/logrus"
"github.com/crenz/mqttrules/agent"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/mattn/go-colorable"
)
func init() {
log.SetLevel(log.InfoLevel)
log.SetOutput(colorable.NewColorableStdout())
}
const defaultBroker string = "tcp://localhost:1883"
func getLogLevel(llString string) log.Level {
level, error := log.ParseLevel(llString)
if error != nil {
log.Errorf("Unknown log level %s, using info", llString)
return log.InfoLevel
}
return level
}
func main() {
pBroker := flag.String("broker", "", "(optional) MQTT broker URI (e.g. tcp://localhost:1883)")
pConfigFile := flag.String("config", "", "(optional) configuration file")
pUsername := flag.String("username", "", "(optional) user name for MQTT broker access")
pPassword := flag.String("password", "", "(optional) password for MQTT broker access")
pLogLevel := flag.String("loglevel", "", "(optional) logging level (panic, fatal, error, warn, info, debug)")
flag.Parse()
var c *agent.ConfigFile
var err error
if len(*pConfigFile) > 0 {
c, err = agent.ConfigFromFile(*pConfigFile)
if err != nil {
log.Errorf("Error reading config file %s: %v", *pConfigFile, err)
return
}
} else {
c = &agent.ConfigFile{}
}
// Arguments given via command line have precedence
if len(*pBroker) > 0 {
c.Config.Broker = *pBroker
}
if len(c.Config.Broker) == 0 {
c.Config.Broker = defaultBroker
}
if len(*pUsername) > 0 {
c.Config.Username = *pUsername
}
if len(*pPassword) > 0 {
c.Config.Username = *pPassword
}
if len(*pLogLevel) > 0 {
c.Config.Loglevel = *pLogLevel
}
log.SetLevel(getLogLevel(c.Config.Loglevel))
log.Infoln("mqtt-rules connecting to broker", c.Config.Broker)
opts := mqtt.NewClientOptions()
opts.SetClientID(c.Config.ClientID)
opts.AddBroker(c.Config.Broker)
opts.SetUsername(c.Config.Username)
opts.SetPassword(c.Config.Password)
mqttClient := agent.NewPahoClient(opts)
a := agent.New(mqttClient, c.Config.Prefix)
if a.Connect() {
a.InjectConfigFile(*c)
a.Subscribe()
for {
a.Listen()
}
// will never be reached: c.Disconnect()
}
}