added function for registrering applicaiton with hue bridge (untested)
This commit is contained in:
parent
669a5c7d57
commit
8d63a2415c
53
huego.go
53
huego.go
@ -1,29 +1,39 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grandcat/zeroconf"
|
"github.com/grandcat/zeroconf"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Device struct {
|
type Bridge struct {
|
||||||
IP net.IP
|
IP net.IP
|
||||||
Name string
|
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
|
// Create a new resolver
|
||||||
resolver, err := zeroconf.NewResolver(nil)
|
resolver, err := zeroconf.NewResolver(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to initialize resolver: %v", err)
|
log.Fatalf("Failed to initialize resolver: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Instantiate a new devices slice
|
// Instantiate a new bridges slice
|
||||||
devices := make([]Device, 0)
|
bridges := make([]Bridge, 0)
|
||||||
|
|
||||||
// Set up a context with a timeout
|
// Set up a context with a timeout
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
|
||||||
@ -43,21 +53,46 @@ func discoverHueBridge() ([]Device, error) {
|
|||||||
// Process the results
|
// Process the results
|
||||||
go func() {
|
go func() {
|
||||||
for entry := range entries {
|
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
|
// Wait for the context to expire
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
fmt.Println("mDNS query finished")
|
fmt.Println("mDNS query finished")
|
||||||
fmt.Printf("Number of devices found: %d\n", len(devices))
|
fmt.Printf("Number of devices found: %d\n", len(bridges))
|
||||||
for _, device := range devices {
|
for _, device := range bridges {
|
||||||
fmt.Printf("Device: %s\n", device.Name)
|
fmt.Printf("Device: %s\n", device.Name)
|
||||||
fmt.Printf("IP Address: %s\n", device.IP)
|
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() {
|
func main() {
|
||||||
discoverHueBridge()
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user