From a97fad2c59162f4a4b4d9f549c6bfc5bd7b5d06e Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Thu, 12 Dec 2019 17:22:48 -0800 Subject: WIP --- main.go | 2 ++ mqtt_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 mqtt_test.go diff --git a/main.go b/main.go index 8926c4d..fb8582e 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,8 @@ import ( "golang.org/x/crypto/ssh" ) +// TODO: Use SCP to init controller https://github.com/bramvdbogaerde/go-scp + const ( CMD_PATH = "/var/etc/persistent/power_control.sh" ) diff --git a/mqtt_test.go b/mqtt_test.go new file mode 100644 index 0000000..fc31002 --- /dev/null +++ b/mqtt_test.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "log" + "os" + "time" + + "github.com/eclipse/paho.mqtt.golang" +) + +var f mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { + fmt.Printf("TOPIC: %s\n", msg.Topic()) + fmt.Printf("MSG: %s\n", msg.Payload()) +} + +func main() { + // mqtt.DEBUG + mqtt.ERROR = log.New(os.Stdout, "", 0) + + opts := mqtt.NewClientOptions().AddBroker("tcp://172.16.0.191:1883").SetClientID("gotrivial") + opts.SetDefaultPublishHandler(f) + + c := mqtt.NewClient(opts) + if token := c.Connect(); token.Wait() && token.Error() != nil { + panic(token.Error()) + } + + if token := c.Subscribe("/mfi/reports", 0, nil); token.Wait() && token.Error() != nil { + fmt.Println(token.Error()) + os.Exit(1) + } + + token := c.Publish("/mfi/devices/NewOffice", 0, false, "{\"output\":3,\"state\":0}") + token.Wait() + time.Sleep(10 * time.Second) + + if token := c.Unsubscribe("/mfi/reports"); token.Wait() && token.Error() != nil { + fmt.Println(token.Error()) + os.Exit(1) + } + + c.Disconnect(250) +} -- cgit v1.2.3