-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
282 lines (250 loc) · 6.97 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"time"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
type outlet struct {
id string
commands chan string
}
func (o outlet) CommandTopic() string {
return "/voltson/" + o.id
}
func (o outlet) AvailableTopic() string {
return o.CommandTopic() + "/available"
}
func (o outlet) StateTopic() string {
return o.CommandTopic() + "/state"
}
var subscribes = make(chan outlet)
var unsubscribes = make(chan outlet)
type message struct {
topic string
contents string
}
type RelayMessage struct {
Uri string `json:"uri"`
Action string `json:"action"`
}
type LoginReplyMessage struct {
Uri string `json:"uri"`
Error int `json:"error"`
Wd int `json:"wd"`
Year int `json:"year"`
Month int `json:"month"`
Day int `json:"day"`
Ms int `json:"ms"`
Hh int `json:"hh"`
Hl int `json:"hl"`
Lh int `json:"lh"`
Ll int `json:"ll"`
}
var messages = make(chan message)
type logWriter struct {
}
func (writer logWriter) Write(bytes []byte) (int, error) {
return fmt.Print(time.Now().UTC().Format("15:04:05.999Z") + " " + string(bytes))
}
func websocketRequest(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
log.Println("Client connected")
mqtt := make(chan string)
offline := make(chan bool)
device := make(chan map[string]interface{})
pendingCommand := false
go func() {
for {
var m map[string]interface{}
c.SetReadDeadline(time.Now().Add(20 * time.Second))
err := c.ReadJSON(&m)
if err != nil {
log.Println("read error:", err)
offline <- true
break
}
pendingCommand = false
device <- m
}
}()
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
o := outlet{"", mqtt}
outer:
for {
select {
case <-offline:
log.Printf("[%s] offline", o.id)
messages <- message{o.AvailableTopic(), "offline"}
unsubscribes <- o
break outer
case <-ticker.C:
log.Printf("[%s] ping", o.id)
if !pendingCommand {
err := c.WriteMessage(websocket.TextMessage, []byte("{\"uri\":\"/ka\"}"))
if err != nil {
log.Println("ping err:", err)
}
}
case m := <-device:
log.Printf("[%s] recv: %s", o.id, m)
if m["id"] != nil {
o.id = string(m["id"].(string))
subscribes <- o
messages <- message{o.AvailableTopic(), "online"}
now := time.Now()
msg, _ := json.Marshal(LoginReplyMessage{
Uri: "/loginReply",
Error: 0,
Wd: 3, // No idea what this is.
Year: now.Year(),
Month: int(now.Month()),
Day: now.Day(),
Ms: (now.Nanosecond() / 1000000),
Hh: 0, // No idea what these mean either
Hl: 0,
Lh: 0,
Ll: 0,
})
log.Printf("[%s] send: %s", o.id, msg)
err = c.WriteMessage(websocket.TextMessage, msg)
if m["relay"] == "open" {
messages <- message{o.StateTopic(), "true"}
} else {
messages <- message{o.StateTopic(), "false"}
}
}
if m["uri"] == "/ka" && m["rssi"] != nil {
now := time.Now()
msg, _ := json.Marshal(LoginReplyMessage{
Uri: "/kr",
Error: 0,
Wd: 3, // No idea what this is.
Year: now.Year(),
Month: int(now.Month()),
Day: now.Day(),
Ms: (now.Nanosecond() / 1000000),
})
log.Printf("[%s] send: %s", o.id, msg)
err = c.WriteMessage(websocket.TextMessage, msg)
}
if m["uri"] == "/runtimeInfo" {
if m["relay"] == "open" {
messages <- message{o.StateTopic(), "true"}
} else {
messages <- message{o.StateTopic(), "false"}
}
}
if m["uri"] == "/state" {
if m["relay"] == "open" {
messages <- message{o.StateTopic(), "true"}
} else {
messages <- message{o.StateTopic(), "false"}
}
}
case command := <-mqtt:
log.Printf("[%s] command: %s", o.id, command)
var err error
var msg []byte
if command == "true" {
msg, _ = json.Marshal(RelayMessage{Uri: "/relay", Action: "open"})
} else {
msg, _ = json.Marshal(RelayMessage{Uri: "/relay", Action: "break"})
}
pendingCommand = true
log.Printf("[%s] send: %s", o.id, msg)
err = c.WriteMessage(websocket.TextMessage, msg)
if err != nil {
log.Println("write error:", err)
break outer
}
default:
time.Sleep(100 * time.Millisecond)
}
}
}
func connectMqtt(mqttPtr *string, mqttUserPtr *string, mqttPasswordPtr *string, unsubscribes chan outlet, subscribes chan outlet, messages chan message) {
subscribedOutlets := map[string]outlet{}
opts := MQTT.NewClientOptions()
opts.SetClientID("voltlet")
opts.AddBroker("tcp://" + *mqttPtr)
opts.SetUsername(*mqttUserPtr)
opts.SetPassword(*mqttPasswordPtr)
opts.SetKeepAlive(2 * time.Second)
opts.SetPingTimeout(1 * time.Second)
opts.SetOnConnectHandler(func(client MQTT.Client) {
log.Print("Connected to mqtt broker")
for _, o := range subscribedOutlets {
mqttSubscribe(client, o)
}
})
opts.SetConnectionLostHandler(func(client MQTT.Client, reason error) {
log.Printf("Connection lost to mqtt broker: %s", reason)
})
c := MQTT.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
for {
select {
case o := <-subscribes:
mqttSubscribe(c, o)
subscribedOutlets[o.CommandTopic()] = o
case o := <-unsubscribes:
mqttUnsubscribe(c, o)
delete(subscribedOutlets, o.CommandTopic())
case m := <-messages:
token := c.Publish(m.topic, 0, true, m.contents)
if token.Wait() && token.Error() != nil {
log.Printf("Error publishing %s on %s", m.contents, m.topic)
}
default:
time.Sleep(100 * time.Millisecond)
}
}
}
func mqttSubscribe(c MQTT.Client, o outlet) {
log.Printf("Subscribing to mqtt topic: %s", o.CommandTopic())
if token := c.Subscribe(o.CommandTopic(), 0, func(client MQTT.Client, msg MQTT.Message) {
o.commands <- string(msg.Payload())
}); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
}
log.Printf("Subscribed to mqtt topic: %s", o.CommandTopic())
}
func mqttUnsubscribe(c MQTT.Client, o outlet) {
log.Printf("Unsubscribing to mqtt topic: %s", o.CommandTopic())
if token := c.Unsubscribe(o.CommandTopic()); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
}
log.Printf("Unsubscribed to mqtt topic: %s", o.CommandTopic())
}
func startWebsocket() {
log.Print("Starting websocket")
http.HandleFunc("/gnws", websocketRequest)
log.Fatal(http.ListenAndServe("0.0.0.0:17273", nil))
}
func main() {
log.SetFlags(0)
log.SetOutput(new(logWriter))
mqttPtr := flag.String("mqtt-broker", "localhost:1883", "The host and port of the MQTT broker")
mqttUserPtr := flag.String("mqtt-user", "", "The MQTT broker user")
mqttPasswordPtr := flag.String("mqtt-password", "", "The MQTT broker password")
flag.Parse()
go connectMqtt(mqttPtr, mqttUserPtr, mqttPasswordPtr, unsubscribes, subscribes, messages)
startWebsocket()
}