From d43b434884903050985dac922d68e6e6655a7022 Mon Sep 17 00:00:00 2001 From: Wlad Meixner Date: Sun, 10 Feb 2019 20:43:22 +0100 Subject: [PATCH] made package exportable --- ...d393643362d303634373934383736303034.entity | 1 + .../39343a61383a38303a39623a37323a3364.entity | 1 + Personal Light Bulb/configHash | 1 + Personal Light Bulb/uuid | 1 + Personal Light Bulb/version | 1 + bridge.go | 27 ++++++++-- config.go | 2 +- lights.go | 21 +++++--- main.go | 49 ++++++++++++++++--- state.go | 3 +- user.go | 2 +- 11 files changed, 86 insertions(+), 23 deletions(-) create mode 100644 Personal Light Bulb/31393132383041332d344138372d343636392d393643362d303634373934383736303034.entity create mode 100644 Personal Light Bulb/39343a61383a38303a39623a37323a3364.entity create mode 100644 Personal Light Bulb/configHash create mode 100644 Personal Light Bulb/uuid create mode 100644 Personal Light Bulb/version diff --git a/Personal Light Bulb/31393132383041332d344138372d343636392d393643362d303634373934383736303034.entity b/Personal Light Bulb/31393132383041332d344138372d343636392d393643362d303634373934383736303034.entity new file mode 100644 index 0000000..4bb377e --- /dev/null +++ b/Personal Light Bulb/31393132383041332d344138372d343636392d393643362d303634373934383736303034.entity @@ -0,0 +1 @@ +{"Name":"191280A3-4A87-4669-96C6-064794876004","PublicKey":"oSLgMjcuMkZynqswmP9PSaNOkQvWgWvNKgOPWjLlVsw=","PrivateKey":null} \ No newline at end of file diff --git a/Personal Light Bulb/39343a61383a38303a39623a37323a3364.entity b/Personal Light Bulb/39343a61383a38303a39623a37323a3364.entity new file mode 100644 index 0000000..ee2a6ac --- /dev/null +++ b/Personal Light Bulb/39343a61383a38303a39623a37323a3364.entity @@ -0,0 +1 @@ +{"Name":"94:a8:80:9b:72:3d","PublicKey":"57Z/hKa1lyMTMPPwxWbYf9nGH/tVmXGBnPfMcswvh5U=","PrivateKey":"OWViYzNiZTk1YjI4ZDkzYTA5YzlkNjZiZjQ0ZmMyOWHntn+EprWXIxMw8/DFZth/2cYf+1WZcYGc98xyzC+HlQ=="} \ No newline at end of file diff --git a/Personal Light Bulb/configHash b/Personal Light Bulb/configHash new file mode 100644 index 0000000..d185cdf --- /dev/null +++ b/Personal Light Bulb/configHash @@ -0,0 +1 @@ +AŽèŠ<²ù¥‚ çôè„ \ No newline at end of file diff --git a/Personal Light Bulb/uuid b/Personal Light Bulb/uuid new file mode 100644 index 0000000..4bd9f1f --- /dev/null +++ b/Personal Light Bulb/uuid @@ -0,0 +1 @@ +94:a8:80:9b:72:3d \ No newline at end of file diff --git a/Personal Light Bulb/version b/Personal Light Bulb/version new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/Personal Light Bulb/version @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/bridge.go b/bridge.go index 6af507c..ace81bb 100644 --- a/bridge.go +++ b/bridge.go @@ -1,4 +1,4 @@ -package main +package hue import ( "bytes" @@ -31,6 +31,23 @@ type BridgeUserConfig struct { ReplacesBridgeID string `json:"replacesbridgeid"` } +// BridgeResponse is the response object returned to a bridge command +type BridgeResponse struct { + Success map[string]interface{} `json:"success"` + Error *BridgeResponseError `json:"error"` +} + +// BridgeResponseError provides info about a bridge api error +type BridgeResponseError struct { + Type uint `json:"type"` + Address string `json:"address"` + Description string `json:"description"` +} + +func (err *BridgeResponseError) String() string { + return fmt.Sprintf("Type=\"%d\" Addr=\"%s\" Desc=\"%s\" \n", err.Type, err.Address, err.Description) +} + // ------------------------------------------------------------- // Methods // ------------------------------------------------------------- @@ -65,7 +82,7 @@ func (b *Bridge) postToBridge(endpoint string, payload interface{}) (interface{} func (b *Bridge) putToBridge(endpoint string, payload interface{}, respData interface{}) error { // TODO: remove fmt.Println("load:", payload) - + data, errMarhshal := json.Marshal(payload) if errMarhshal != nil { return errMarhshal @@ -85,9 +102,9 @@ func (b *Bridge) putToBridge(endpoint string, payload interface{}, respData inte // TODO: remove fmt.Println("response Status:", res.Status) - fmt.Println("response Headers:", res.Header) - body, _ := ioutil.ReadAll(res.Body) - fmt.Println("response Body:", string(body)) + fmt.Println("response Headers:", res.Header) + body, _ := ioutil.ReadAll(res.Body) + fmt.Println("response Body:", string(body)) if res.StatusCode != http.StatusOK { return errors.New("Hue responded with error" + res.Status + fmt.Sprint(res.StatusCode)) diff --git a/config.go b/config.go index 9166b57..97d3079 100644 --- a/config.go +++ b/config.go @@ -1,4 +1,4 @@ -package main +package hue import ( "io/ioutil" diff --git a/lights.go b/lights.go index d110f5b..2b8acff 100644 --- a/lights.go +++ b/lights.go @@ -1,4 +1,4 @@ -package main +package hue import "fmt" @@ -24,18 +24,23 @@ type LightState struct { Reachable bool `json:"reachable,omitempty"` } -type cmdResponse struct { -} - +// LightsEndpoint for the lights const LightsEndpoint = "/lights" func (l *Light) String() string { - return fmt.Sprintf("Name=\"%s\" Model=\"%s\" On=\"%x\" XY=\"%x\" \n", l.Name, l.ModelID, l.State.On, l.State.XY) + return fmt.Sprintf("Name=\"%s\" Model=\"%s\" On=\"%v\" XY=\"%x\" \n", l.Name, l.ModelID, l.State.On, l.State.XY) } -func (b *Bridge) ToggleLight(id string, on bool) error { - cmd := &LightState{ +// ToggleLight switches light on or off +func (b *Bridge) ToggleLight(id string, on bool) (resp *BridgeResponse, err error) { + state := &LightState{ On: on, } - return b.putToBridge(LightsEndpoint+"/"+id+"/state", cmd, nil) + return b.SetLightState(id, state) +} + +// SetLightState updates the light state +func (b *Bridge) SetLightState(id string, state *LightState) (resp *BridgeResponse, err error) { + err = b.putToBridge(LightsEndpoint+"/"+id+"/state", state, resp) + return resp, err } diff --git a/main.go b/main.go index 818b4bd..7e6e987 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,10 @@ -package main +package hue import ( "fmt" + "log" + "time" + ) const VERSION = "0.1.2" @@ -19,10 +22,42 @@ func main() { fmt.Println("Created bridge ", bridge) - test := &BridgeState{} - errCom := bridge.ToggleLight("2", false) - if errCom != nil { - fmt.Println("[ERROR]" + errCom.Error()) - } - fmt.Println(test) + // errCom := bridge.ToggleLight("2", false) + // if errCom != nil { + // fmt.Println("[ERROR]" + errCom.Error()) + // } + //tickSwitch(bridge) + testLightBulb(bridge) + select {} + //fmt.Println(test) +} + + +func strobeLight(b *Bridge, id string) { + ticker := time.NewTicker(200 * time.Millisecond) + quit := make(chan struct{}) + go func() { + state := false + for { + select { + case <-ticker.C: + resp, errCom := b.ToggleLight(id, state) + if errCom != nil { + fmt.Println("[ERROR]" + errCom.Error()) + ticker.Stop() + return + } + if resp.Error != nil { + fmt.Println("[ERROR]" + resp.Error.String()) + ticker.Stop() + return + } + state = !state + // do stuff + case <-quit: + ticker.Stop() + return + } + } + }() } diff --git a/state.go b/state.go index 96fdb51..681cb84 100644 --- a/state.go +++ b/state.go @@ -1,9 +1,10 @@ -package main +package hue // ------------------------------------------------------------- // ~ Interfaces & Types // ------------------------------------------------------------- +// BridgeState provides all data for a bridge type BridgeState struct { Lights map[string]*Light `json:"lights"` } diff --git a/user.go b/user.go index 686713d..3c7c0d5 100644 --- a/user.go +++ b/user.go @@ -1,4 +1,4 @@ -package main +package hue import ( "bytes"