aboutsummaryrefslogtreecommitdiff
path: root/bridge.go
blob: 2214e58fcd1f831c7e1a0b5d28e8dadb5364731e (plain)
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
283
284
285
286
287
288
289
/*
* bridge.go
* GoHue library for Philips Hue
* Copyright (C) 2016 Collin Guarino (Collinux) collin.guarino@gmail.com
* License: GPL version 2 or higher http://www.gnu.org/licenses/gpl.html
*/
// All things start with the bridge. You will find many Bridge.Func() items
// to use once a bridge has been created and identified.
// See the getting started guide on the Philips hue website:
// http://www.developers.meethue.com/documentation/getting-started

package hue

import (
    "log"
    "encoding/xml"
    "encoding/json"
    "net/http"
    "io/ioutil"
    "runtime"
    "fmt"
    "strings"
    "bytes"
    "io"
    "errors"
)

// Bridge struct defines hardware that is used to communicate with the lights.
type Bridge struct {
    IPAddress   string
    Username    string
    Info        BridgeInfo
}

// BridgeInfo struct is the format for parsing xml from a bridge.
type BridgeInfo struct {
    XMLName	    xml.Name	`xml:"root"`
    Device      struct {
        XMLName	            xml.Name    `xml:"device"`
        DeviceType          string      `xml:"deviceType"`
        FriendlyName        string      `xml:"friendlyName"`
        Manufacturer        string      `xml:"manufacturer"`
        ManufacturerURL     string      `xml:"manufacturerURL"`
        ModelDescription    string      `xml:"modelDescription"`
        ModelName           string      `xml:"modelName"`
        ModelNumber         string      `xml:"modelNumber"`
        ModelURL            string      `xml:"modelURL"`
        SerialNumber        string      `xml:"serialNumber"`
        UDN                 string      `xml:"UDN"`
    } `xml:"device"`
}

// bridge.Get sends an http GET to the bridge
func (bridge *Bridge) Get(path string) ([]byte, io.Reader, error) {
    resp, err := http.Get("http://" + bridge.IPAddress + path)
    if bridge.Error(resp, err) {
        return []byte{}, nil, err
    }
    return HandleResponse(resp)
}

// Bridge.Put sends an http PUT to the bridge with
// a body formatted with parameters (in a generic interface)
func (bridge *Bridge) Put(path string, params interface{}) ([]byte, io.Reader, error) {
    uri := fmt.Sprintf("http://" + bridge.IPAddress + path)
    client := &http.Client{}

    data, err := json.Marshal(params)
    if err != nil {
        trace("", err)
        return []byte{}, nil, err
    }
    //fmt.Println("\n\nPARAMS: ", params)
    //log.Println("\nSending PUT body: ", string(data))

	request, err := http.NewRequest("PUT", uri, bytes.NewReader(data))
    resp, err := client.Do(request)
	if err != nil {
        trace("", err)
		return []byte{}, nil, err
    }
    return HandleResponse(resp)
}

// bridge.Post sends an http POST to the bridge with
// a body formatted with parameters (in a generic interface)
func (bridge *Bridge) Post(path string, params interface{}) ([]byte, io.Reader, error) {
    // Add the params to the request
    request, err := json.Marshal(params)
    if err != nil {
        panic(err)
        trace("", err)
        return []byte{}, nil, nil
    }
    //log.Println("\nSending POST body: ", string(request))

    // Send the request and handle the response
    uri := fmt.Sprintf("http://" + bridge.IPAddress + path)
    resp, err := http.Post(uri, "text/json", bytes.NewReader(request))
    if bridge.Error(resp, err) {
        panic(err)
        return []byte{}, nil, err
    }
    return HandleResponse(resp)
}

// Bridge.Delete sends an http DELETE to the bridge
func (bridge *Bridge) Delete(path string) error {
    uri := fmt.Sprintf("http://" + bridge.IPAddress + path)
    client := &http.Client{}
    req, err := http.NewRequest("DELETE", uri, nil)
    resp, err := client.Do(req)
    if err != nil {
        return err
    }
    _, _, err = HandleResponse(resp)
    if err != nil {
        trace("", err)
        return err
    }
    return nil
}

// HandleResponse manages the http.Response content from a
// bridge Get/Put/Post/Delete by checking it for errors
// and invalid return types.
func HandleResponse(resp *http.Response) ([]byte, io.Reader, error) {
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        trace("Error parsing bridge description xml.", nil)
        return []byte{}, nil, err
    }
    reader := bytes.NewReader(body)
    if strings.Contains(string(body), "error") {
        errString := string(body)
        errNum := errString[strings.Index(errString, "type\":")+6 :
            strings.Index(errString, ",\"address")]
        errDesc := errString[strings.Index(errString, "description\":\"")+14 :
            strings.Index(errString, "\"}}")]
        errOut := fmt.Sprintf("Error type %s: %s.", errNum, errDesc)
        err = errors.New(errOut)
        log.Println(err)
        return []byte{}, nil, err
    }
    return body, reader, nil
}

// Bridge.Error handles all bridge response status errors and
// will return a boolean to notify functions to proceed or not.
func (bridge *Bridge) Error(resp *http.Response, err error) bool {
    if err != nil {
        trace("", err)
        return true
    } else if resp.StatusCode != 200 {
        // TODO: handle other status codes
        //log.Println(fmt.Sprintf("Bridge status error: %d", resp.StatusCode))
        return true
    }
    return false
}

// NewBridge defines hardware that is compatible with Hue.
func NewBridge(ip string) (*Bridge, error) {
    bridge := Bridge {
        IPAddress: ip,
    }
    err := bridge.GetInfo()
    if err != nil {
        return &Bridge{}, err
    }
    return &bridge, nil
}

// Bridge.Login assigns a username to access the bridge with and
// will create the username key if it does not exist.
func (bridge *Bridge) Login(username string) error {
    bridge.Username = username
    err := bridge.CreateUser(username)
    if err != nil {
        return err
    }
    return nil
}

// GetBridgeInfo retreives the description.xml file from the bridge.
func (bridge *Bridge) GetInfo() error {
    _, reader, err := bridge.Get("/description.xml")
    if err != nil {
        return err
    }
    data := BridgeInfo{}
    err = xml.NewDecoder(reader).Decode(&data)
    if err != nil {
        return err
    }
    bridge.Info = data
    return nil
}

// Bridge.CreateUser posts to ./api on the bridge to create a new whitelisted user.
func (bridge *Bridge) CreateUser(deviceType string) error {
    params := map[string]string{"devicetype": deviceType}
    body, _, err := bridge.Post("/api", params)
    if err != nil {
        return err
    }
    content := string(body)
    username := content[strings.LastIndex(content, ":\"")+2 :
        strings.LastIndex(content, "\"")]
    bridge.Username = username
    return nil
}

// Bridge.DeleteUser deletes a user given its USER KEY, not the string name.
// See http://www.developers.meethue.com/documentation/configuration-api
// for description on `username` deprecation in place of the devicetype key.
func (bridge *Bridge) DeleteUser(username string) error {
    uri := fmt.Sprintf("/api/%s/config/whitelist/%s", bridge.Username, username)
    err := bridge.Delete(uri)
    if err != nil {
        return err
    }
    return nil
}

// GetAllLights retreives the state of all lights that the bridge is aware of.
func (bridge *Bridge) GetAllLights() ([]Light, error) {
    // Loop through all light indicies to see if they exist
    // and parse their values. Supports 100 lights.
    var lights []Light
    for index := 1; index < 101; index++ {
        light, err := bridge.GetLightByIndex(index)
        if err != nil {
            break
        }
        lights = append(lights, light)
    }
    return lights, nil
}

// GetLightByIndex returns a light struct containing data on
// a light given its index stored on the bridge. This is used for
// quickly updating an individual light.
func (bridge *Bridge) GetLightByIndex(index int) (Light, error) {
    // Send an http GET and inspect the response
    uri := fmt.Sprintf("/api/%s/lights/%d", bridge.Username, index)
    body, _, err := bridge.Get(uri)
    if err != nil {
        return Light{}, err
    }
    if strings.Contains(string(body), "not available") {
        return Light{}, errors.New("Index Error")
    }

    // Parse and load the response into the light array
    light := Light{}
    err = json.Unmarshal(body, &light)
    if err != nil {
        trace("", err)
    }
    light.Index = index
    light.Bridge = bridge
    return light, nil
}

// GetLight returns a light struct containing data on a given name.
func (bridge *Bridge) GetLightByName(name string) (Light, error) {
    lights, _ := bridge.GetAllLights()
    for _, light := range lights {
        if light.Name == name {
            return light, nil
        }
    }
    return Light{}, errors.New("Light not found.")
}

// Log the date, time, file location, line number, and function.
// Message can be "" or Err can be nil (not both)
func trace(message string, err error) {
    pc := make([]uintptr, 10)
    runtime.Callers(2, pc)
    f := runtime.FuncForPC(pc[0])
    file, line := f.FileLine(pc[0])
    if err != nil {
        log.Printf("%s:%d %s: %s\n", file, line, f.Name(), err)
    } else {
        log.Printf("%s:%d %s: %s\n", file, line, f.Name(), message)
    }
}