1
0
mirror of https://github.com/shirou/mqttcli.git synced 2025-04-26 13:49:17 +08:00
mqttcli/main.go

203 lines
4.4 KiB
Go
Raw Normal View History

2014-07-15 12:20:59 +09:00
package main
import (
2014-09-01 22:34:38 +09:00
"bufio"
2014-07-15 12:20:59 +09:00
"os"
2015-03-24 23:28:32 +09:00
"sync"
"time"
2014-07-15 12:20:59 +09:00
2014-09-01 22:34:38 +09:00
MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
log "github.com/Sirupsen/logrus"
2014-07-15 12:20:59 +09:00
"github.com/codegangsta/cli"
2014-09-01 23:35:54 +09:00
colorable "github.com/mattn/go-colorable"
2014-07-15 12:20:59 +09:00
)
var usage = `
Usage here
`
2015-04-04 13:53:12 +09:00
var version string
2014-12-15 23:30:11 +09:00
2015-10-09 13:49:11 +09:00
func init() {
2014-09-01 22:34:38 +09:00
log.SetLevel(log.WarnLevel)
2014-09-01 23:35:54 +09:00
log.SetOutput(colorable.NewColorableStdout())
2014-07-15 12:20:59 +09:00
}
// connects MQTT broker
2015-03-24 23:28:32 +09:00
func connect(c *cli.Context, opts *MQTT.ClientOptions, subscribed map[string]byte) (*MQTTClient, error) {
2014-09-05 18:36:35 +09:00
willPayload := c.String("will-payload")
willQoS := c.Int("will-qos")
willRetain := c.Bool("will-retain")
willTopic := c.String("will-topic")
if willPayload != "" && willTopic != "" {
opts.SetWill(willTopic, willPayload, byte(willQoS), willRetain)
2014-09-05 18:36:35 +09:00
}
2014-09-01 22:34:38 +09:00
client := &MQTTClient{Opts: opts}
2015-03-24 23:28:32 +09:00
client.lock = new(sync.Mutex)
client.Subscribed = subscribed
opts.SetOnConnectHandler(client.SubscribeOnConnect)
opts.SetConnectionLostHandler(client.ConnectionLost)
2014-09-01 22:34:38 +09:00
_, err := client.Connect()
if err != nil {
2014-07-15 12:20:59 +09:00
return nil, err
}
return client, nil
}
2014-09-01 23:31:27 +09:00
func pubsub(c *cli.Context) {
if c.Bool("d") {
log.SetLevel(log.DebugLevel)
}
opts, err := NewOption(c)
if err != nil {
log.Error(err)
os.Exit(1)
}
2014-09-01 23:31:27 +09:00
qos := c.Int("q")
subtopic := c.String("sub")
if subtopic == "" {
log.Errorf("Please specify sub topic")
os.Exit(1)
}
log.Infof("Sub Topic: %s", subtopic)
pubtopic := c.String("pub")
if pubtopic == "" {
log.Errorf("Please specify pub topic")
os.Exit(1)
}
log.Infof("Pub Topic: %s", pubtopic)
retain := c.Bool("r")
2015-03-24 23:28:32 +09:00
subscribed := map[string]byte{
subtopic: byte(0),
}
client, err := connect(c, opts, subscribed)
if err != nil {
log.Error(err)
os.Exit(1)
}
2014-09-01 23:31:27 +09:00
go func() {
// Read from Stdin and publish
2014-09-01 23:31:27 +09:00
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
2015-03-30 19:09:52 +09:00
err = client.Publish(pubtopic, []byte(scanner.Text()), qos, retain, false)
2014-09-01 23:31:27 +09:00
if err != nil {
log.Error(err)
}
}
}()
2015-03-24 23:28:32 +09:00
// while loop
for {
time.Sleep(1 * time.Second)
2014-09-01 23:31:27 +09:00
}
}
2014-07-15 12:20:59 +09:00
func main() {
app := cli.NewApp()
app.Name = "mqttcli"
app.Usage = usage
2014-12-15 23:30:11 +09:00
app.Version = version
2014-09-01 22:34:38 +09:00
commonFlags := []cli.Flag{
cli.StringFlag{
Name: "host",
Value: "",
2014-09-01 22:34:38 +09:00
Usage: "mqtt host to connect to. Defaults to localhost",
EnvVar: "MQTT_HOST"},
cli.IntFlag{
Name: "p, port",
Value: 1883,
Usage: "network port to connect to. Defaults to 1883",
EnvVar: "MQTT_PORT"},
cli.StringFlag{
Name: "u,user",
Value: "",
Usage: "provide a username",
EnvVar: "MQTT_USERNAME"},
cli.StringFlag{
Name: "P,password",
Value: "",
Usage: "provide a password",
EnvVar: "MQTT_PASSWORD"},
cli.StringFlag{"t", "", "mqtt topic to publish to.", ""},
cli.IntFlag{"q", 0, "QoS", ""},
2015-07-08 21:15:48 +09:00
cli.StringFlag{"cafile", "", "CA certificates", ""},
cli.StringFlag{"cert", "", "Client certificates", ""},
2015-10-09 13:44:25 +09:00
cli.StringFlag{"key", "", "Client private key", ""},
2014-09-01 22:34:38 +09:00
cli.StringFlag{"i", "", "ClientiId. Defaults random.", ""},
cli.StringFlag{"m", "test message", "Message body", ""},
cli.BoolFlag{"r", "message should be retained.", ""},
cli.BoolFlag{"d", "enable debug messages", ""},
cli.BoolFlag{"insecure", "do not check that the server certificate", ""},
2014-11-07 12:58:41 +09:00
cli.StringFlag{"conf", "~/.mqttcli.cfg", "config file path", ""},
2014-09-01 22:34:38 +09:00
cli.StringFlag{
Name: "will-payload",
Value: "",
Usage: "payload for the client Will",
},
cli.IntFlag{
Name: "will-qos",
Value: 0,
Usage: "QoS level for the client Will",
},
cli.BoolFlag{
Name: "will-retain",
Usage: "if given, make the client Will retained",
},
cli.StringFlag{
Name: "will-topic",
Value: "",
Usage: "the topic on which to publish the client Will",
},
2014-09-05 18:36:35 +09:00
}
pubFlags := append(commonFlags,
cli.BoolFlag{"s", "read message from stdin, sending line by line as a message", ""},
2014-09-01 22:34:38 +09:00
)
subFlags := append(commonFlags,
cli.BoolFlag{
Name: "c",
Usage: "disable 'clean session'",
},
)
2014-09-01 23:31:27 +09:00
pubsubFlags := append(commonFlags,
cli.StringFlag{
Name: "pub",
Usage: "publish topic",
},
cli.StringFlag{
Name: "sub",
Usage: "subscribe topic",
},
)
2014-09-01 22:34:38 +09:00
2014-07-15 12:20:59 +09:00
app.Commands = []cli.Command{
{
2014-09-01 22:34:38 +09:00
Name: "pub",
Usage: "publish",
Flags: pubFlags,
2014-07-15 12:20:59 +09:00
Action: publish,
},
2014-09-01 22:34:38 +09:00
{
Name: "sub",
Usage: "subscribe",
Flags: subFlags,
Action: subscribe,
},
2014-09-01 23:31:27 +09:00
{
Name: "pubsub",
Usage: "subscribe and publish",
Flags: pubsubFlags,
Action: pubsub,
},
2014-07-15 12:20:59 +09:00
}
app.Run(os.Args)
}