forked from iegomez/lds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt.go
115 lines (98 loc) · 3.4 KB
/
mqtt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"time"
l "gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
paho "github.com/eclipse/paho.mqtt.golang"
matx "github.com/scartill/giox/material"
log "github.com/sirupsen/logrus"
)
var mqttClient paho.Client
type mqtt struct {
Server string `toml:"server"`
User string `toml:"user"`
Password string `toml:"password"`
DownlinkTopic string `toml:"downlink_topic"`
UplinkTopic string `toml:"uplink_topic"`
}
type gateway struct {
MAC string `toml:"mac"`
BridgeVersion string `toml:"bridge_version"`
}
var (
mqttServerEdit widget.Editor
mqttUserEdit widget.Editor
mqttPasswordEdit widget.Editor
mqttMACEdit widget.Editor
mqttDownlinkEdit widget.Editor
mqttUplinkEdit widget.Editor
mqttConnectButton widget.Clickable
mqttDisconnectButton widget.Clickable
)
func mqttResetGuiValue() {
mqttServerEdit.SetText(config.MQTT.Server)
mqttUserEdit.SetText(config.MQTT.User)
mqttPasswordEdit.SetText(config.MQTT.Password)
mqttMACEdit.SetText(config.GW.MAC)
mqttDownlinkEdit.SetText(config.MQTT.DownlinkTopic)
mqttUplinkEdit.SetText(config.MQTT.UplinkTopic)
}
func mqttForm(th *material.Theme) l.FlexChild {
config.MQTT.Server = mqttServerEdit.Text()
config.MQTT.User = mqttUserEdit.Text()
config.MQTT.Password = mqttPasswordEdit.Text()
config.GW.MAC = mqttMACEdit.Text()
config.MQTT.DownlinkTopic = mqttDownlinkEdit.Text()
config.MQTT.UplinkTopic = mqttUplinkEdit.Text()
for mqttConnectButton.Clicked() {
connectClient()
}
for mqttDisconnectButton.Clicked() {
mqttClient.Disconnect(200)
}
widgets := []l.FlexChild{
matx.RigidSection(th, "MQTT & Gateway"),
matx.RigidEditor(th, "MQTT Server:", "192.168.1.1", &mqttServerEdit),
matx.RigidEditor(th, "MQTT User:", "<username>", &mqttUserEdit),
matx.RigidEditor(th, "MQTT Password:", "<password>", &mqttPasswordEdit),
matx.RigidEditor(th, "Gateway MAC:", "DEADBEEFDEADBEEF", &mqttMACEdit),
matx.RigidEditor(th, "Downlink Topic:", "gateway/%s/command/down", &mqttDownlinkEdit),
matx.RigidEditor(th, "Uplink Topic:", "gateway/%s/event/up", &mqttUplinkEdit)}
if !cNSClient.IsConnected() {
widgets = append(widgets, matx.RigidButton(th, "Connect", &mqttConnectButton))
} else {
widgets = append(widgets, matx.RigidLabel(th, "Forwarder connected"))
}
if mqttClient != nil && mqttClient.IsConnected() {
widgets = append(widgets, matx.RigidButton(th, "Disconnect", &mqttDisconnectButton))
}
inset := l.Inset{Left: unit.Dp(30)}
return l.Rigid(func(gtx l.Context) l.Dimensions {
return inset.Layout(gtx, func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Vertical}.Layout(gtx, widgets...)
})
})
}
func connectClient() error {
//Connect to the broker
opts := paho.NewClientOptions()
opts.AddBroker(config.MQTT.Server)
opts.SetUsername(config.MQTT.User)
opts.SetPassword(config.MQTT.Password)
opts.SetAutoReconnect(true)
opts.SetClientID(fmt.Sprintf("lds-%d", time.Now().UnixNano()))
mqttClient = paho.NewClient(opts)
log.Infoln("MQTT connecting...")
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
log.Errorf("connection error: %s", token.Error())
return token.Error()
}
log.Infoln("connection established")
mqttClient.Subscribe(fmt.Sprintf(config.MQTT.DownlinkTopic, config.GW.MAC), 1, func(c paho.Client, msg paho.Message) {
onIncomingDownlink(msg.Payload())
})
return nil
}