This commit is contained in:
Wlad Meixner 2019-02-09 18:07:49 +01:00
parent 11eb5f4e93
commit b821e59869
5 changed files with 32 additions and 22 deletions

View File

@ -1,11 +1,9 @@
package main
import (
"bytes"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"gopkg.in/yaml.v2"
)
// Config hue api config
@ -19,19 +17,19 @@ type Config struct {
// TODO: Rename if this will be placed in a seperate package
// ReadConfig ...
func ReadConfig(path string) (conf *Config, err error) {
f, err := ioutil.ReadFile(path)
if err != nil {
return
f, err := ioutil.ReadFile(path)
if err != nil {
return
}
err = yaml.Unmarshal(f, conf)
if err != nil {
err = yaml.Unmarshal(f, conf)
if err != nil {
return
}
// TODO: check wether a user is already created and if not create one.
return
return
}
func (c *Config) WriteConfig(path string) (err error) {
@ -39,4 +37,4 @@ func (c *Config) WriteConfig(path string) (err error) {
err = ioutil.WriteFile(path, b, 0644)
return
}
}

View File

@ -1,5 +1,7 @@
package main
import "fmt"
// Light hue object
type Light struct {
State *LightState `json:"state"`
@ -21,3 +23,19 @@ type LightState struct {
ColorMode string `json:"colormode"`
Reachable bool `json:"reachable"`
}
type cmdResponse struct {
}
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)
}
func (b *Bridge) ToggleLight(id string, on bool) error {
cmd := &LightState{
On: on,
}
return b.putToBridge(LightsEndpoint+"/"+id+"/state", cmd, nil)
}

View File

@ -20,7 +20,7 @@ func main() {
fmt.Println("Created bridge ", bridge)
test := &BridgeState{}
errCom := bridge.getFromBridge("", test)
errCom := bridge.ToggleLight("2", true)
if errCom != nil {
fmt.Println("[ERROR]" + errCom.Error())
}

View File

@ -1,7 +1,5 @@
package main
import "fmt"
// -------------------------------------------------------------
// ~ Interfaces & Types
// -------------------------------------------------------------
@ -10,10 +8,6 @@ type BridgeState struct {
Lights map[string]*Light `json:"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)
}
func (bs *BridgeState) String() string {
str := ""
for k, l := range bs.Lights {

View File

@ -1,8 +1,8 @@
package main
import (
"encoding/json"
"bytes"
"encoding/json"
"fmt"
"net/http"
)
@ -15,7 +15,7 @@ type User struct {
// CreateNewUser will create a new user. This should be called only of there's none in the yaml config.
func CreateUser(addr string) (name, key string, succ bool) {
return "", "", false
}
// TODO: remove these comments
@ -41,4 +41,4 @@ func CreateUserExtended(addr, application, deviceName string) (u *User, err erro
}
return
}
}