Skip to content

Commit

Permalink
Implement loginReply messages and improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mcolyer committed Feb 12, 2018
1 parent b18ded8 commit ccb9311
Showing 1 changed file with 59 additions and 8 deletions.
67 changes: 59 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ type RelayMessage struct {
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 {
Expand All @@ -68,6 +82,7 @@ func websocketRequest(w http.ResponseWriter, r *http.Request) {
mqtt := make(chan string)
offline := make(chan bool)
device := make(chan map[string]interface{})
pendingCommand := false

go func() {
for {
Expand All @@ -79,6 +94,7 @@ func websocketRequest(w http.ResponseWriter, r *http.Request) {
offline <- true
break
}
pendingCommand = false
device <- m
}
}()
Expand All @@ -98,22 +114,56 @@ outer:
break outer
case <-ticker.C:
log.Printf("[%s] ping", o.id)
err := c.WriteMessage(websocket.TextMessage, []byte("{\"uri\":\"/ka\"}"))
if err != nil {
log.Println("ping err:", err)
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"}
Expand All @@ -131,13 +181,15 @@ outer:
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"})
err = c.WriteMessage(websocket.TextMessage, []byte(msg))
msg, _ = json.Marshal(RelayMessage{Uri: "/relay", Action: "open"})
} else {
msg, _ := json.Marshal(RelayMessage{Uri: "/relay", Action: "break"})
err = c.WriteMessage(websocket.TextMessage, []byte(msg))
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)
Expand Down Expand Up @@ -169,7 +221,6 @@ func connectMqtt(mqttPtr *string, mqttUserPtr *string, mqttPasswordPtr *string,
case o := <-subscribes:
log.Printf("Subscribing to mqtt topic: %s", o.CommandTopic())
if token := c.Subscribe(o.CommandTopic(), 0, func(client MQTT.Client, msg MQTT.Message) {
log.Printf("Received mqtt msg: %s", msg.Payload())
o.commands <- string(msg.Payload())
}); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
Expand Down

0 comments on commit ccb9311

Please sign in to comment.