diff options
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | mqtt_test.go | 44 |
2 files changed, 46 insertions, 0 deletions
@@ -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 | |||
17 | const ( | 19 | const ( |
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 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "log" | ||
6 | "os" | ||
7 | "time" | ||
8 | |||
9 | "github.com/eclipse/paho.mqtt.golang" | ||
10 | ) | ||
11 | |||
12 | var 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 | |||
17 | func 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 | } | ||