Update discovery, go mod, and make a simple commandline control
This commit is contained in:
parent
c62aa93ae0
commit
c50304bbcd
5 changed files with 162 additions and 12 deletions
|
|
@ -6,13 +6,16 @@ import (
|
|||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/amimof/huego"
|
||||
)
|
||||
|
||||
// UserInfo is the saved data struture of a Hue app connection
|
||||
type UserInfo struct {
|
||||
User string
|
||||
User string `json:",omitempty"`
|
||||
LastSync time.Time `json:",omitempty"`
|
||||
LastIP *huego.Bridge `json:",omitempty"`
|
||||
}
|
||||
|
||||
func DiscoverAndConnectFromConfig(appName string) (*huego.Bridge, error) {
|
||||
|
|
@ -40,14 +43,27 @@ func DiscoverAndConnectFromConfig(appName string) (*huego.Bridge, error) {
|
|||
if !ok {
|
||||
ui = UserInfo{}
|
||||
}
|
||||
|
||||
if ui.LastIP != nil && ui.LastSync.After(time.Now().Add(-(time.Hour * 24 * 30))) {
|
||||
newb := ui.LastIP.Login(ui.User)
|
||||
return newb, nil
|
||||
}
|
||||
|
||||
b, err := huego.Discover()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ui.User != "" {
|
||||
data[appName] = UserInfo{
|
||||
User: ui.User,
|
||||
LastSync: time.Now(),
|
||||
LastIP: b,
|
||||
}
|
||||
err := saveData(data, filename)
|
||||
newb := b.Login(ui.User)
|
||||
return newb, nil
|
||||
return newb, err
|
||||
}
|
||||
|
||||
fmt.Println("Please press the Hue Bridge button, then press Enter")
|
||||
fmt.Scanln()
|
||||
user, err := b.CreateUser(appName)
|
||||
|
|
@ -57,18 +73,20 @@ func DiscoverAndConnectFromConfig(appName string) (*huego.Bridge, error) {
|
|||
data[appName] = UserInfo{User: user}
|
||||
|
||||
b = b.Login(user)
|
||||
|
||||
f, err := os.Create(filename)
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = json.NewEncoder(f).Encode(data)
|
||||
err = saveData(data, filename)
|
||||
return b, err
|
||||
}
|
||||
|
||||
func saveData(data map[string]UserInfo, filename string) error {
|
||||
f, err := os.Create(filename)
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.NewEncoder(f).Encode(data)
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("vim-go")
|
||||
b, err := DiscoverAndConnectFromConfig("barak-hue-test")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue