/
homegw.go
155 lines (131 loc) · 3.02 KB
/
homegw.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/yosssi/gmq/mqtt"
"github.com/yosssi/gmq/mqtt/client"
)
var wmux sync.Mutex
var datamux sync.Mutex
var fgdata string
var lastseen string
var light1 string
var light1ls string
type hwdata struct {
Message string `json:"message"`
Lastseen string `json:"lastseen"`
Lux string `json:"lux"`
Temp string `json:"temp"`
Lstate string `json:"lstate"`
Light1 string `json:"light1"`
Light1ls string `json:"light1ls"`
}
var curdata hwdata
func mqttWorker(c *client.Client) {
// Subscribe to topics.
err := c.Subscribe(&client.SubscribeOptions{
SubReqs: []*client.SubReq{
&client.SubReq{
TopicFilter: []byte("fgdata"),
QoS: mqtt.QoS0,
// Define the processing of the message handler.
Handler: func(topicName, message []byte) {
wmux.Lock()
fgdata = string(message)
lastseen = time.Now().String()
wmux.Unlock()
},
},
&client.SubReq{
TopicFilter: []byte("light1"),
QoS: mqtt.QoS0,
// Define the processing of the message handler.
Handler: func(topicName, message []byte) {
wmux.Lock()
light1 = string(message)
light1ls = time.Now().String()
wmux.Unlock()
},
},
},
})
if err != nil {
fmt.Println("Error", err)
}
}
func main() {
r := gin.New()
r.Use(gin.Recovery())
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
"lastseen": time.Now().String(),
})
})
r.GET("/fgdata", func(c *gin.Context) {
datamux.Lock()
if fgdata != "" && strings.Contains(fgdata, "#") {
s := strings.Split(fgdata, "#")
curdata.Message = "OK"
curdata.Lastseen = lastseen
curdata.Lux = s[1]
curdata.Temp = s[2]
curdata.Lstate = s[0]
}
if light1 != "" {
curdata.Light1 = light1
curdata.Light1ls = light1ls
} else {
curdata.Light1 = "N/A"
curdata.Light1ls = light1ls
}
datamux.Unlock()
if fgdata != "" && strings.Contains(fgdata, "#") {
//s := strings.Split(fgdata, "#")
c.JSON(200, curdata)
// c.JSON(200, gin.H{
// "message": "OK",
// "lastseen": lastseen,
// "lux": s[1],
// "temp": s[2],
// "lstate": s[0],
//})
} else {
c.JSON(200, gin.H{
"message": "No data retreived. Check Network Connection",
})
}
})
r.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/static")
})
r.StaticFS("/static", http.Dir("./web"))
//MQTT
// Create an MQTT Client.
cli := client.New(&client.Options{
// Define the processing of the error handler.
ErrorHandler: func(err error) {
fmt.Println(err)
},
})
// Terminate the Client.
defer cli.Terminate()
defer cli.Disconnect()
// Connect to the MQTT Server.
err := cli.Connect(&client.ConnectOptions{
Network: "tcp",
Address: "192.168.2.37:1883",
ClientID: []byte("example-client"),
})
if err != nil {
fmt.Println("Error on mqtt connect:", err)
os.Exit(1)
}
go mqttWorker(cli)
r.Run() // listen and server on 0.0.0.0:8080
}