made package exportable

This commit is contained in:
Wlad Meixner 2019-02-10 20:43:22 +01:00
parent 0feaa3822b
commit d43b434884
11 changed files with 86 additions and 23 deletions

View File

@ -0,0 +1 @@
{"Name":"191280A3-4A87-4669-96C6-064794876004","PublicKey":"oSLgMjcuMkZynqswmP9PSaNOkQvWgWvNKgOPWjLlVsw=","PrivateKey":null}

View File

@ -0,0 +1 @@
{"Name":"94:a8:80:9b:72:3d","PublicKey":"57Z/hKa1lyMTMPPwxWbYf9nGH/tVmXGBnPfMcswvh5U=","PrivateKey":"OWViYzNiZTk1YjI4ZDkzYTA5YzlkNjZiZjQ0ZmMyOWHntn+EprWXIxMw8/DFZth/2cYf+1WZcYGc98xyzC+HlQ=="}

View File

@ -0,0 +1 @@
AŽèŠ<²ù¥‚ çôè„

1
Personal Light Bulb/uuid Normal file
View File

@ -0,0 +1 @@
94:a8:80:9b:72:3d

View File

@ -0,0 +1 @@
1

View File

@ -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))

View File

@ -1,4 +1,4 @@
package main
package hue
import (
"io/ioutil"

View File

@ -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
}

49
main.go
View File

@ -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
}
}
}()
}

View File

@ -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"`
}

View File

@ -1,4 +1,4 @@
package main
package hue
import (
"bytes"