mirror of
https://github.com/shirou/mqttcli.git
synced 2025-04-28 13:48:50 +08:00
first commit
This commit is contained in:
commit
51150666a1
102
main.go
Normal file
102
main.go
Normal file
@ -0,0 +1,102 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/codegangsta/cli"
|
||||
)
|
||||
|
||||
var log = logrus.New()
|
||||
var usage = `
|
||||
Usage here
|
||||
`
|
||||
|
||||
func initFunc() {
|
||||
log.Formatter = new(logrus.TextFormatter)
|
||||
log.Level = logrus.Debug
|
||||
}
|
||||
|
||||
// connects MQTT broker
|
||||
func connect(c *cli.Context) (*MQTTClient, error){
|
||||
host := c.String("host")
|
||||
port := c.Int("p")
|
||||
clientId := c.String("i")
|
||||
|
||||
cafile := c.String("cafile")
|
||||
scheme := "tcp"
|
||||
if cafile != "" {
|
||||
scheme = "ssl"
|
||||
}
|
||||
|
||||
user := c.String("u")
|
||||
password := c.String("P")
|
||||
|
||||
brokerUri := fmt.Sprintf("%s://%s:%d", scheme, host, port)
|
||||
log.Info("Broker URI: %s", brokerUri)
|
||||
|
||||
client := NewMQTTClient()
|
||||
|
||||
log.Debug("Connecting...")
|
||||
_, err := client.Connect(brokerUri, clientId, user, password)
|
||||
if err != nil{
|
||||
return nil, err
|
||||
}
|
||||
log.Debug("Connected")
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
|
||||
func publish(c *cli.Context) {
|
||||
client, err := connect(c)
|
||||
if err != nil{
|
||||
log.Error(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
qos := c.Int("q")
|
||||
topic := c.String("t")
|
||||
if topic == "" {
|
||||
log.Errorf("Please specify topic")
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Infof("Topic: %s", topic)
|
||||
|
||||
payload := c.String("m")
|
||||
retain := c.Bool("r")
|
||||
log.Infof("Retain: %t", retain)
|
||||
|
||||
client.Publish(topic, []byte(payload), qos, retain)
|
||||
|
||||
log.Debug("Published")
|
||||
}
|
||||
|
||||
func main() {
|
||||
initFunc()
|
||||
|
||||
app := cli.NewApp()
|
||||
app.Name = "mqttcli"
|
||||
app.Usage = usage
|
||||
app.Commands = []cli.Command{
|
||||
{
|
||||
Name: "publish",
|
||||
Usage: "publish",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{"host", "test.mosquitto.org", "Broker IP address"},
|
||||
cli.IntFlag{"p", 1883, "Broker Port"},
|
||||
cli.StringFlag{"t", "", "Topic"},
|
||||
cli.IntFlag{"q", 0, "QoS"},
|
||||
cli.StringFlag{"cafile", "", "CA file"},
|
||||
cli.StringFlag{"u", "", "username"},
|
||||
cli.StringFlag{"P", "", "password"},
|
||||
cli.StringFlag{"i", "client0", "ClientiId"},
|
||||
cli.StringFlag{"m", "test message", "Message body"},
|
||||
cli.BoolFlag{"r", "Retain flag"},
|
||||
},
|
||||
Action: publish,
|
||||
},
|
||||
}
|
||||
app.Run(os.Args)
|
||||
}
|
58
mqtt.go
Normal file
58
mqtt.go
Normal file
@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
|
||||
"crypto/tls"
|
||||
)
|
||||
|
||||
type MQTTClient struct {
|
||||
Opts *MQTT.ClientOptions
|
||||
Client *MQTT.MqttClient
|
||||
}
|
||||
|
||||
func NewMQTTClient() *MQTTClient {
|
||||
return &MQTTClient{}
|
||||
}
|
||||
|
||||
// connect to MQTT broker
|
||||
func (m *MQTTClient) Connect(brokerUri string, clientId string,
|
||||
user string, password string) (*MQTT.MqttClient, error) {
|
||||
|
||||
m.Opts = MQTT.NewClientOptions()
|
||||
|
||||
m.Opts.SetBroker(brokerUri)
|
||||
m.Opts.SetClientId(clientId)
|
||||
m.Opts.SetTraceLevel(MQTT.Critical)
|
||||
// m.Opts.SetTraceLevel(MQTT.Verbose)
|
||||
if user != "" {
|
||||
m.Opts.SetUsername(user)
|
||||
}
|
||||
if password != "" {
|
||||
m.Opts.SetPassword(password)
|
||||
}
|
||||
|
||||
insecure := true
|
||||
if insecure {
|
||||
tlsConfig := &tls.Config{InsecureSkipVerify: true,}
|
||||
m.Opts.SetTlsConfig(tlsConfig)
|
||||
}
|
||||
|
||||
m.Client = MQTT.NewClient(m.Opts)
|
||||
_, err := m.Client.Start()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m.Client, nil
|
||||
}
|
||||
func (m *MQTTClient) Publish(topic string, payload []byte, qos int, retain bool) error {
|
||||
mqttmsg := MQTT.NewMessage(payload)
|
||||
// FIXME: validate qos number
|
||||
mqttmsg.SetQoS(MQTT.QoS(qos))
|
||||
mqttmsg.SetRetainedFlag(retain)
|
||||
|
||||
// receipt := m.Client.PublishMessage(msg.Destination, mqttmsg)
|
||||
receipt := m.Client.PublishMessage(topic, mqttmsg)
|
||||
<-receipt
|
||||
|
||||
return nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user