-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
executable file
·149 lines (129 loc) · 2.84 KB
/
app.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
package main
import (
"encoding/json"
"fmt"
"log"
"masterjulz/hhz-photon/config"
"masterjulz/hhz-photon/wiot"
"net/http"
"os"
"time"
"github.com/gorilla/websocket"
//for extracting service credentials from VCAP_SERVICES
//"github.com/cloudfoundry-community/go-cfenv"
)
const (
DEFAULT_PORT = "8080"
)
var likeCount = 0
var dislikeCount = 0
var smokeCount = 0
var mealCount = 0
var wcCount = 0
var coffeeCount = 0
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
type Message struct {
Type string `json:"type"`
Data Data `json:"data"`
}
type Data struct {
Button string `json:"button"`
Count int `json:"count"`
Time time.Time `json:"time"`
}
func MakeHandler(fn func(http.ResponseWriter, *http.Request, chan string), ch chan string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fn(w, r, ch)
}
}
func WSHandler(w http.ResponseWriter, r *http.Request, ch chan string) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
initCounts(conn)
for {
select {
case res := <-ch:
t := time.Now().UTC()
var d Data
b := []byte(res)
err := json.Unmarshal(b, &d)
switch d.Button {
case "like":
likeCount += 1
d.Count = likeCount
d.Time = t
fmt.Println("likes", likeCount)
case "dislike":
dislikeCount += 1
d.Count = dislikeCount
d.Time = t
fmt.Println("dislikes", dislikeCount)
case "smoke":
likeCount += 1
d.Count = smokeCount
d.Time = t
fmt.Println("smoke count:", likeCount)
case "coffee":
dislikeCount += 1
d.Count = coffeeCount
d.Time = t
fmt.Println("coffee count", dislikeCount)
case "meal":
likeCount += 1
d.Count = mealCount
d.Time = t
fmt.Println("meal count", likeCount)
case "wc":
dislikeCount += 1
d.Count = wcCount
d.Time = t
fmt.Println("wc count", dislikeCount)
}
err = conn.WriteJSON(Message{Type: "Photon", Data: d})
if err != nil {
log.Println(err)
return
}
}
}
}
func initCounts(conn *websocket.Conn) {
err := conn.WriteJSON(Message{Type: "Photon", Data: Data{Button: "like", Count: likeCount}})
if err != nil {
log.Println(err)
return
}
err = conn.WriteJSON(Message{Type: "Photon", Data: Data{Button: "dislike", Count: dislikeCount}})
if err != nil {
log.Println(err)
return
}
}
func main() {
var port string
if port = os.Getenv("PORT"); len(port) == 0 {
port = DEFAULT_PORT
}
conf := config.Get()
wiot := wiot.New(
conf.Url,
conf.Topic,
conf.ClientID,
conf.ApiKey,
conf.AuthToken,
)
ch := make(chan string)
wiot.Connect()
go wiot.Subscribe(ch)
fs := http.FileServer(http.Dir("public"))
http.Handle("/", fs)
http.HandleFunc("/ws", MakeHandler(WSHandler, ch))
log.Printf("Starting app on port %+v\n", port)
http.ListenAndServe(":"+port, nil)
}