forked from heatxsink/hued
/
hued-web.go
228 lines (211 loc) · 7.53 KB
/
hued-web.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
package main
import (
"encoding/json"
"net/http"
"net/http/fcgi"
"net"
"fmt"
"flag"
"os"
"github.com/golang/glog"
"github.com/heatxsink/go-hue/src/lights"
"github.com/heatxsink/go-hue/src/portal"
"github.com/heatxsink/go-hue/src/groups"
"github.com/heatxsink/go-hue/src/key"
)
var (
config_filename string = ""
username_api_key string = ""
hostname string = ""
hue_hostname string = ""
local_web_server_flag bool = true
web_bind_address string = "127.0.0.1"
web_bind_port int = 9000
)
type ApiResponse struct {
Result bool `json:"result"`
Message string `json:"message"`
StatusCode int `json:"status_code"`
}
func group_name_presets(name string) int {
return_value := -1
if name == "all" {
return_value = 0
} else if name == "office" {
return_value = 1
} else if name == "bedroom" {
return_value = 2
} else if name == "living-room" {
return_value = 3
} else if name == "upstairs" {
return_value = 4
}
return return_value
}
func light_state_presets(name string) lights.State {
on := lights.State { On: true }
off := lights.State { On: false }
red := lights.State { On: true, Hue: 65527, Effect: "none", Bri: 13, Sat: 253, Ct: 500, Xy: []float32{0.6736, 0.3221}, Alert: "none", TransitionTime: 4 }
blue := lights.State { On: true, Hue: 46573, Effect: "none", Bri: 254, Sat: 251, Ct: 500, Xy: []float32{0.1754, 0.0556}, Alert: "none", TransitionTime: 4 }
energize := lights.State { On: true, Hue: 34495, Effect: "none", Bri: 203, Sat: 232, Ct: 155, Xy: []float32{0.3151, 0.3252}, Alert: "none", TransitionTime: 4 }
relax := lights.State { On: true, Hue: 13088, Effect: "none", Bri: 144, Sat: 212, Ct: 467, Xy: []float32{0.5128, 0.4147}, Alert: "none", TransitionTime: 4 }
reading := lights.State { On: true, Hue: 15331, Effect: "none", Bri: 222, Sat: 121, Ct: 343, Xy: []float32{0.4448, 0.4066}, Alert: "none", TransitionTime: 4 }
concentrate := lights.State { On: true, Hue: 33849, Effect: "none", Bri: 219, Sat: 44, Ct: 234, Xy: []float32{0.3693, 0.3695}, Alert: "none", TransitionTime: 4 }
candle_light := lights.State { On: true, Hue: 15339, Effect: "none", Bri: 19, Sat: 120, Ct: 343, Xy: []float32{0.4443, 0.4064}, Alert: "none", TransitionTime: 4 }
virgin_america := lights.State { On: true, Hue: 54179, Effect: "none", Bri: 230, Sat: 253, Ct: 223, Xy: []float32{0.3621, 0.1491}, Alert: "none", TransitionTime: 4 }
white := lights.State { On: true, Hue: 34495, Effect: "none", Bri: 203, Sat: 232, Ct: 155, Xy: []float32{0.3151, 0.3252}, Alert: "none", TransitionTime: 4 }
orange := lights.State { On: true, Hue: 4868, Effect: "none", Bri: 254, Sat: 252, Ct: 500, Xy: []float32{0.6225, 0.3594}, Alert: "none", TransitionTime: 4 }
deep_sea := lights.State { On: true, Hue: 65527, Effect: "none", Bri: 253, Sat: 253, Ct: 500, Xy: []float32{0.6736, 0.3221}, Alert: "none", TransitionTime: 4 }
green := lights.State { On: true, Hue: 25654, Effect: "none", Bri: 254, Sat: 253, Ct: 290, Xy: []float32{0.4083, 0.5162}, Alert: "none", TransitionTime: 4 }
snow := lights.State { On: true, Hue: 34258, Effect: "none", Bri: 254, Sat: 176, Ct: 181, Xy: []float32{0.3327, 0.3413}, Alert: "none", TransitionTime: 4 }
movie_mode := lights.State { On: true, Hue: 65527, Effect: "none", Bri: 51, Sat: 253, Ct: 500, Xy: []float32{0.6736, 0.3221}, Alert: "none", TransitionTime: 4 }
// default ...
return_value := on
// on with the show!
if name == "on" {
return on
} else if name == "off" {
return off
} else if name == "red" {
return red
} else if name == "blue" {
return blue
} else if name == "energize" {
return energize
} else if name == "relax" {
return relax
} else if name == "reading" {
return reading
} else if name == "concentrate" {
return concentrate
} else if name == "candle-light" {
return candle_light
} else if name == "virgin-america" {
return virgin_america
} else if name == "white" {
return white
} else if name == "orange" {
return orange
} else if name == "deep-sea" {
return deep_sea
} else if name == "green" {
return green
} else if name == "snow" {
return snow
} else if name == "movie-mode" {
return movie_mode
}
return return_value
}
func groupV1(w http.ResponseWriter, req *http.Request) {
api_response := ApiResponse{ Result: true, Message: "", StatusCode: http.StatusOK }
req.ParseForm()
query_params := req.Form
if req.Method == "GET" {
name, name_exists := query_params["name"]
state, state_exists := query_params["state"]
if name_exists {
group_id := group_name_presets(name[0])
if state_exists {
gg := groups.NewGroup(hue_hostname, username_api_key)
group_state := light_state_presets(state[0])
gg.SetGroupState(group_id, group_state)
} else {
api_response.Result = false
api_response.Message = "Invalid state."
api_response.StatusCode = http.StatusUnauthorized
}
} else {
api_response.Result = false
api_response.Message = "Invalid id or name."
api_response.StatusCode = http.StatusUnauthorized
}
} else {
api_response.Result = false
api_response.Message = "Not an HTTP GET."
api_response.StatusCode = http.StatusForbidden
}
w.Header().Set("content-type", "application/json")
w.WriteHeader(api_response.StatusCode)
json_data, _ := json.Marshal(&api_response)
w.Write([]byte(json_data))
}
func statusV1(w http.ResponseWriter, req *http.Request) {
w.Header().Set("content-type", "text/plain")
w.WriteHeader(http.StatusTeapot)
w.Write([]byte(http.StatusText(http.StatusTeapot)))
}
func user_interface_phoneV1(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, "./www/phone.html")
}
func user_interface_tabletV1(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, "./www/tablet.html")
}
func static_root(w http.ResponseWriter, req *http.Request) {
if len(req.URL.Path) == 1 {
root(w, req)
} else if string(req.URL.Path[1:7]) == "static" {
path := fmt.Sprintf("./www/%s", req.URL.Path[1:])
http.ServeFile(w, req, path)
} else {
root(w, req)
}
}
func root(w http.ResponseWriter, req *http.Request) {
w.Header().Set("content-type", "text/plain")
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(http.StatusText(http.StatusNotFound)))
}
func check_error(err error) {
if err != nil {
glog.Fatalf("Error: %s\n", err.Error())
glog.Flush()
}
}
func web_start() {
mux := http.NewServeMux()
mux.HandleFunc("/api/1/group", groupV1)
mux.HandleFunc("/api/1/status", statusV1)
mux.HandleFunc("/tablet", user_interface_tabletV1)
mux.HandleFunc("/phone", user_interface_phoneV1)
mux.HandleFunc("/", static_root)
if local_web_server_flag {
hostname := fmt.Sprintf(":%d", web_bind_port)
start_message := fmt.Sprintf("Starting local hued-web on %s\n", hostname)
fmt.Println(start_message)
glog.Infof(start_message)
http.ListenAndServe(hostname, mux)
} else {
hostname := fmt.Sprintf("%s:%d", web_bind_address, web_bind_port)
l, err := net.Listen("tcp", hostname)
if err == nil {
start_message := fmt.Sprintf("Starting hued-web on %s\n", hostname)
fmt.Println(start_message)
glog.Infof(start_message)
fcgi.Serve(l, mux)
} else {
glog.Error("Error starting server, is the port already in use?")
}
}
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: hued-web -config=[string]\n")
flag.PrintDefaults()
os.Exit(2)
}
func init() {
flag.StringVar(&config_filename, "config", "", "The configuration file that has your philips hue username api key in it.")
flag.Parse()
flag.Usage = usage
}
func main() {
if config_filename != "" {
k := key.New(config_filename)
username_api_key = k.Username
portal := portal.GetPortal()
hue_hostname = portal[0].InternalIpAddress
web_start()
} else {
usage()
}
}