mirror of
https://github.com/gosticks/go-hue-interface.git
synced 2025-10-16 11:45:35 +00:00
added user.go, read and write conf
This commit is contained in:
parent
41a3c0ba41
commit
63df86e8d9
40
config.go
40
config.go
@ -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
44
user.go
Normal 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
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user