Merge branch 'master' of github.com:gosticks/go-hue-interface

This commit is contained in:
Wlad Meixner 2019-02-09 18:00:45 +01:00
commit 11eb5f4e93
2 changed files with 65 additions and 19 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"bytes"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
)
@ -15,26 +16,27 @@ type Config struct {
BridgeAddrScheme string `yaml:"bridgeAddressScheme"`
}
// createNewUser will create a new user. This should be called only of there's none in the yaml config.
func (c *Config) createNewUser() {
// TODO: read/create the url
url := "http://192.168.178.46/api"
var reqBody = []byte(`{"devicetype": "go-hue-interface#Philips hue"}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
// 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
}
err = yaml.Unmarshal(f, conf)
if err != nil {
return
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
// TODO: check wether a user is already created and if not create one.
// json.Unmarshal([]byte(str), &resp)
fmt.Println(resp)
return
}
func (c *Config) WriteConfig(path string) (err error) {
b, err := yaml.Marshal(c)
err = ioutil.WriteFile(path, b, 0644)
return
}

44
user.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"encoding/json"
"bytes"
"fmt"
"net/http"
)
type User struct {
username string
// TODO:
//key string
}
// 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) {
}
// TODO: remove these comments
// example application: "go.hue.interface"
// example deviceName: "Philips hue"
func CreateUserExtended(addr, application, deviceName string) (u *User, err error) {
uri := "http://" + addr + "/api"
var reqBody = []byte(fmt.Sprintf(`{"devicetype": "%s#%s"}`, application, deviceName))
req, err := http.NewRequest("POST", uri, bytes.NewBuffer(reqBody))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return
}
defer res.Body.Close()
// Unmarshal data
err = json.NewDecoder(res.Body).Decode(u)
if err != nil {
return
}
return
}