-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (109 loc) · 3.36 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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"github.com/gorilla/websocket"
"github.com/wichopy/sense-go/app"
)
const SenseBaseURL = "https://api.sense.com/apiservice/api/v1"
const Port = ":8080"
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func getDeviceList(monitorId int64, accessToken string) (app.DeviceResponse, error) {
client := &http.Client{}
path := fmt.Sprintf("/app/monitors/%s/devices?include_merged=true", strconv.FormatInt(monitorId, 10))
req, _ := http.NewRequest("GET", SenseBaseURL+path, nil)
req.Header.Set("content-type", "application/x-www-form-urlencoded; charset=UTF-8;")
req.Header.Set("authorization", fmt.Sprintf("bearer %s", accessToken))
res, err := client.Do(req)
if err != nil {
log.Printf("Error getting device list")
return nil, err
}
defer res.Body.Close()
rawBody, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalf("Error reading response body, %v", err)
return nil, err
}
devices, err := app.UnmarshalDevices(rawBody)
if err != nil {
log.Fatalf("Error Unmarshalling response body, %v", err)
return nil, err
}
log.Printf("%v", devices)
return devices, err
}
func authHandler(w http.ResponseWriter, r *http.Request) {
path := "/authenticate"
resp, err := http.Post(SenseBaseURL+path, "application/x-www-form-urlencoded; charset=UTF-8;", r.Body)
if err != nil {
log.Fatalf("Error posting auth body, %v", err)
return
}
// log.Printf("Response: %v, %s, %s", resp.StatusCode, resp.Status, resp.Header)
defer resp.Body.Close()
rawBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Error reading response body, %v", err)
return
}
body, err := app.UnmarshalSenseAuthenticationResponse(rawBody)
if err != nil {
log.Fatalf("Error Unmarshalling response body, %v", err)
return
}
auth := app.Auth{
AccessToken: body.AccessToken,
AccountID: body.AccountID,
UserID: body.UserID,
// FIXME: Hard coded getting the first monitor.
MonitorID: body.Monitors[0].ID,
}
// log.Printf("Response:, %v", body)
// log.Printf("Auth:, %v", auth)
getDeviceList(auth.MonitorID, auth.AccessToken)
// realtimeClient(auth.MonitorID, auth.AccessToken)
}
func realtimeClient(monitorId int64, accessToken string) {
addr := "clientrt.sense.com"
path := fmt.Sprintf("monitors/%s/realtimefeed", strconv.FormatInt(monitorId, 10))
query := fmt.Sprintf("access_token=%s", accessToken)
u := url.URL{Scheme: "wss", Host: addr, Path: path, RawQuery: query}
log.Printf("connecting to %s", u.String())
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
log.Printf("Connected to web socket, waiting for specified devices to toggle...")
// defer c.Close()
// done := make(chan struct{})
// go func() {
// defer close(done)
for {
_, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
m, err := app.UnmarshalSenseRealTimeMessage(message)
for _, device := range m.Payload.Devices {
// FIXME: Use a configurable list of devices to watch.
if device.Name == "Fridge" {
log.Printf("Fridge is on")
}
}
}
// }()
}
func main() {
http.HandleFunc("/auth", authHandler)
http.HandleFunc("/", handler)
log.Printf("Start server at http://localhost%s", Port)
log.Fatal(http.ListenAndServe(Port, nil))
}