diff --git a/huego.go b/huego.go index f58b652..de92ba4 100644 --- a/huego.go +++ b/huego.go @@ -1,29 +1,39 @@ package main import ( + "bytes" "fmt" + "io" "log" "net" + "net/http" "time" "github.com/grandcat/zeroconf" "golang.org/x/net/context" ) -type Device struct { +type Bridge struct { IP net.IP Name string } -func discoverHueBridge() ([]Device, error) { +type RegisteredApplication struct { + bridge Bridge + applicationName string + deviceName string + token string +} + +func discoverHueBridge() ([]Bridge, error) { // Create a new resolver resolver, err := zeroconf.NewResolver(nil) if err != nil { log.Fatalf("Failed to initialize resolver: %v", err) } - // Instantiate a new devices slice - devices := make([]Device, 0) + // Instantiate a new bridges slice + bridges := make([]Bridge, 0) // Set up a context with a timeout ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) @@ -43,21 +53,46 @@ func discoverHueBridge() ([]Device, error) { // Process the results go func() { for entry := range entries { - devices = append(devices, Device{IP: entry.AddrIPv4[0], Name: entry.HostName}) + bridges = append(bridges, Bridge{IP: entry.AddrIPv4[0], Name: entry.HostName}) } }() // Wait for the context to expire <-ctx.Done() fmt.Println("mDNS query finished") - fmt.Printf("Number of devices found: %d\n", len(devices)) - for _, device := range devices { + fmt.Printf("Number of devices found: %d\n", len(bridges)) + for _, device := range bridges { fmt.Printf("Device: %s\n", device.Name) fmt.Printf("IP Address: %s\n", device.IP) } - return devices, nil + return bridges, nil +} + +func registerApplication(bridge Bridge) RegisteredApplication { + var applicationName string + var deviceName string + fmt.Printf("Enter name of your application: ") + fmt.Scan(&applicationName) + fmt.Printf("Enter name of your device: ") + fmt.Scan(&deviceName) + resp, err := http.Post("http://"+bridge.IP.String()+"/api", "application/json", bytes.NewBuffer([]byte(`{"devicetype:`+applicationName+` #`+deviceName+`"}`))) + if err != nil { + log.Fatal(err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatal(err) + } + registeredApplication := RegisteredApplication{ + bridge: bridge, + applicationName: applicationName, + deviceName: deviceName, + token: string(body)} + fmt.Println(registeredApplication) + return registeredApplication } func main() { - discoverHueBridge() + }