forked from iegomez/lds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forwarder.go
80 lines (64 loc) · 1.88 KB
/
forwarder.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
package main
import (
"strconv"
l "gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/iegomez/lds/lds"
matx "github.com/scartill/giox/material"
log "github.com/sirupsen/logrus"
)
// cNSClient is a direct NetworkServer connection handle
var cNSClient lds.NSClient
type forwarder struct {
Server string `toml:"nserver"`
Port string `toml:"nsport"`
}
var (
nserverEdit widget.Editor
nportEdit widget.Editor
nsConnectButton widget.Clickable
)
func forwarderResetGuiValues() {
nserverEdit.SetText(config.Forwarder.Server)
nportEdit.SetText(config.Forwarder.Port)
}
func forwarderForm(th *material.Theme) l.FlexChild {
config.Forwarder.Server = nserverEdit.Text()
config.Forwarder.Port = nportEdit.Text()
for nsConnectButton.Clicked() {
forwarderConnect()
}
wLabel := matx.RigidSection(th, "Forwarder")
wNS := matx.RigidEditor(th, "Network Server:", "192.168.1.1", &nserverEdit)
wNP := matx.RigidEditor(th, "UDP Port:", "1680", &nportEdit)
var wConnect l.FlexChild
if mqttClient == nil || !mqttClient.IsConnected() {
if !cNSClient.IsConnected() {
wConnect = matx.RigidButton(th, "Connect", &nsConnectButton)
} else {
wConnect = matx.RigidLabel(th, "UDP Listening")
}
} else {
wConnect = matx.RigidLabel(th, "MQTT Connected")
}
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, wLabel, wNS, wNP, wConnect)
})
})
}
func forwarderConnect() error {
port, err := strconv.Atoi(config.Forwarder.Port)
if err != nil {
log.Warn("network server UDP port must be a number")
return err
}
cNSClient.Server = config.Forwarder.Server
cNSClient.Port = port
cNSClient.Connect(config.GW.MAC, onIncomingDownlink)
log.Infoln("UDP Forwarder started (MQTT disabled)")
return nil
}