From 4a6869ee9c78f6c1496fefa35cd5b71afd1bd44d Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Wed, 3 Feb 2016 23:08:25 -0500 Subject: Cleaned up bridge.go file, added more documentation, and fixed line spacing. --- bridge.go | 68 +++++++-------------------------------------------------------- 1 file changed, 7 insertions(+), 61 deletions(-) diff --git a/bridge.go b/bridge.go index 581e180..7172289 100644 --- a/bridge.go +++ b/bridge.go @@ -1,6 +1,5 @@ package hue -// username: 319b36233bd2328f3e40731b23479207 import ( "log" "os" @@ -16,17 +15,22 @@ import ( "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 outermost (root) structure +// for parsing xml from a bridge. type BridgeInfo struct { XMLName xml.Name `xml:"root"` Device Device `xml:"device"` } +// Device struct is the innermost (base) structure +// for parsing device info xml from a bridge. type Device struct { XMLName xml.Name `xml:"device"` DeviceType string `xml:"deviceType"` @@ -41,6 +45,7 @@ type Device struct { UDN string `xml:"UDN"` } +// bridge.Get will send an http GET to the bridge func (self *Bridge) Get(path string) ([]byte, io.Reader, error) { resp, err := http.Get("http://" + self.IPAddress + path) if self.Error(resp, err) { @@ -78,7 +83,6 @@ func handleResponse(resp *http.Response) ([]byte, io.Reader, error) { return []byte{}, nil, err } reader := bytes.NewReader(body) - return body, reader, nil } @@ -89,7 +93,7 @@ func (self *Bridge) Error(resp *http.Response, err error) (bool) { return true } else if resp.StatusCode != 200 { // TODO: handle other status codes - trace(fmt.Sprintf("Bridge status error: %d", resp.StatusCode), nil) + log.Println(fmt.Sprintf("Bridge status error: %d", resp.StatusCode)) return true } return false @@ -103,70 +107,12 @@ type Error struct { Details string } -// Error Return Values -// http://www.developers.meethue.com/documentation/error-messages -var ( - // Not from Hue documentation - NoErr = Error{} - ErrResponse = Error{0, "Could not read or parse response from bridge", - "Data structure for return type may be invalid."} - - - // Generic Errors from Hue SDK - ErrAuth = Error{1, "Unauthorized User", - `This will be returned if an invalid username is used in the request, - or if the username does not have the rights to modify the resource.`} - ErrJson = Error{2, "Body contains invalid JSON.", - "This will be returned if the body of the message contains invalid JSON."} - ErrResource = Error{3, "Resource, , not available.", - `This will be returned if the addressed resource does not exist. - E.g. the user specifies a light ID that does not exist.`} - ErrMethod = Error{4, "Method, , not available for resource, ", - `This will be returned if the method (GET/POST/PUT/DELETE) - used is not supported by the URL e.g. DELETE is not - supported on the /config resource`} - ErrParamMissing = Error{5, "Missing parameters in body.", `Will be returned if - required parameters are not present in the message body. The presence - of invalid parameters should not trigger this error as long as all - required parameters are present.`} - ErrParamNA = Error{6, "Parameter, , not available.", - `This will be returned if a parameter sent in the message body does - not exist. This error is specific to PUT commands; invalid parameters - in other commands are simply ignored.`} - ErrParamInvalid = Error{7, "Invalid value, , for parameter, ", - `This will be returned if the value set for a parameter is of the - incorrect format or is out of range.`} - ErrParamStatic = Error{8, "Parameter, , is not modifiable", - `This will be returned if an attempt to modify - a read only parameter is made.`} - ErrItemOverflow = Error{11, "Too many items to list.", - "List in request contains too many items"} - ErrPortalConn = Error{12, "Portal connection required.", - `Command requires portal connection. - Returned if portalservices is “false“ or the portal connection is down`} - ErrorInternal = Error{901, "Internal error, ", - `This will be returned if there is an internal error in the - processing of the command. This indicates an error in the - bridge, not in the message being sent.`} - - // Command Specific Errors from Hue SDK - ErrLink = Error{101, "Link button not pressed.", - `/config/linkbutton is false. Link button has - not been pressed in last 30 seconds.`} - ErrDHCP = Error{110, "DHCP cannot be disabled.", - "DHCP can only be disabled if there is a valid static IP configuration"} - ErrUpdate = Error{111, "Invalid updatestate.", - "Checkforupdate can only be set in updatestate 0 and 1."} - // TODO: Need to add 201, 301, 305, 306, 402, 403, 501, 502, 601... -) - // NewBridge defines hardware that is compatible with Hue. func NewBridge(ip string, username string) *Bridge { bridge := Bridge { IPAddress: ip, Username: username, } - GetBridgeInfo(&bridge) return &bridge } -- cgit v1.2.3