summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2019-12-12 17:22:48 -0800
committerMike Crute <mike@crute.us>2019-12-12 17:22:48 -0800
commita97fad2c59162f4a4b4d9f549c6bfc5bd7b5d06e (patch)
treeb85f16c96668dc62e9ffabb531eb79078461cae1
parent3009b54d04c8b5bfcfa05c6373d67b92db7c8d27 (diff)
downloadmfi_homekit-mqtt-test.tar.bz2
mfi_homekit-mqtt-test.tar.xz
mfi_homekit-mqtt-test.zip
-rw-r--r--main.go2
-rw-r--r--mqtt_test.go44
2 files changed, 46 insertions, 0 deletions
diff --git a/main.go b/main.go
index 8926c4d..fb8582e 100644
--- a/main.go
+++ b/main.go
@@ -14,6 +14,8 @@ import (
14 "golang.org/x/crypto/ssh" 14 "golang.org/x/crypto/ssh"
15) 15)
16 16
17// TODO: Use SCP to init controller https://github.com/bramvdbogaerde/go-scp
18
17const ( 19const (
18 CMD_PATH = "/var/etc/persistent/power_control.sh" 20 CMD_PATH = "/var/etc/persistent/power_control.sh"
19) 21)
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 @@
1package main
2
3import (
4 "fmt"
5 "log"
6 "os"
7 "time"
8
9 "github.com/eclipse/paho.mqtt.golang"
10)
11
12var f mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
13 fmt.Printf("TOPIC: %s\n", msg.Topic())
14 fmt.Printf("MSG: %s\n", msg.Payload())
15}
16
17func main() {
18 // mqtt.DEBUG
19 mqtt.ERROR = log.New(os.Stdout, "", 0)
20
21 opts := mqtt.NewClientOptions().AddBroker("tcp://172.16.0.191:1883").SetClientID("gotrivial")
22 opts.SetDefaultPublishHandler(f)
23
24 c := mqtt.NewClient(opts)
25 if token := c.Connect(); token.Wait() && token.Error() != nil {
26 panic(token.Error())
27 }
28
29 if token := c.Subscribe("/mfi/reports", 0, nil); token.Wait() && token.Error() != nil {
30 fmt.Println(token.Error())
31 os.Exit(1)
32 }
33
34 token := c.Publish("/mfi/devices/NewOffice", 0, false, "{\"output\":3,\"state\":0}")
35 token.Wait()
36 time.Sleep(10 * time.Second)
37
38 if token := c.Unsubscribe("/mfi/reports"); token.Wait() && token.Error() != nil {
39 fmt.Println(token.Error())
40 os.Exit(1)
41 }
42
43 c.Disconnect(250)
44}