This commit is contained in:
Wlad Meixner 2019-02-09 18:00:41 +01:00
parent fdcd78eb85
commit b7c06e801c
3 changed files with 56 additions and 22 deletions

View File

@ -60,6 +60,37 @@ func (b *Bridge) postToBridge(endpoint string, payload interface{}) (interface{}
return res, nil
}
func (b *Bridge) putToBridge(endpoint string, payload interface{}, respData interface{}) error {
data, errMarhshal := json.Marshal(payload)
if errMarhshal != nil {
return errMarhshal
}
uri := b.getBridgeAPIURI() + endpoint
req, err := http.NewRequest(http.MethodPut, uri, bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return errors.New("Hue responded with error" + res.Status + fmt.Sprint(res.StatusCode))
}
// Unmarshal data
errDecode := json.NewDecoder(res.Body).Decode(respData)
if errDecode != nil {
return errDecode
}
return nil
}
func (b *Bridge) getFromBridge(endpoint string, target interface{}) error {
uri := b.getBridgeAPIURI() + endpoint
@ -82,7 +113,7 @@ func (b *Bridge) getFromBridge(endpoint string, target interface{}) error {
// Unmarshal data
errDecode := json.NewDecoder(res.Body).Decode(target)
if errDecode != nil {
return err
return errDecode
}
return nil

23
lights.go Normal file
View File

@ -0,0 +1,23 @@
package main
// Light hue object
type Light struct {
State *LightState `json:"state"`
Type string `json:"type"`
Name string `json:"name"`
ModelID string `json:"modelid"`
SwVersion string `json:"swversion"`
}
// LightState is the hue light>state object
type LightState struct {
On bool `json:"on"`
BridgeID int `json:"bridgeid"`
Hue int `json:"hue"`
XY []int `json:"xy"`
Ct int `json:"ct"`
Alert string `json:"alert"`
Effect string `json:"effect"`
ColorMode string `json:"colormode"`
Reachable bool `json:"reachable"`
}

View File

@ -10,28 +10,8 @@ type BridgeState struct {
Lights map[string]*Light `json:"lights"`
}
type Light struct {
State *LightState `json:"state"`
Type string `json:"type"`
Name string `json:"name"`
ModelID string `json:"modelid"`
SwVersion string `json:"swversion"`
}
type LightState struct {
On bool `json:"on"`
BridgeID int `json:"bridgeid"`
Hue int `json:"hue"`
XY []int `json:"xy"`
Ct int `json:"ct"`
Alert string `json:"alert"`
Effect string `json:"effect"`
ColorMode string `json:"colormode"`
Reachable bool `json:"reachable"`
}
func (l *Light) String() string {
return fmt.Sprintf("Name=\"%s\" Model=\"%s\" On=\"%x\" \n", l.Name, l.ModelID, l.State.On)
return fmt.Sprintf("Name=\"%s\" Model=\"%s\" On=\"%x\" XY=\"%x\" \n", l.Name, l.ModelID, l.State.On, l.State.XY)
}
func (bs *BridgeState) String() string {