authing the app works now, but the string needs to be trimmed
This commit is contained in:
parent
ae11219aaf
commit
fe6e3d231b
52
huego.go
52
huego.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@ -74,10 +75,14 @@ func discoverHueBridge() ([]Bridge, error) {
|
|||||||
func registerApplication(bridge Bridge) RegisteredApplication {
|
func registerApplication(bridge Bridge) RegisteredApplication {
|
||||||
var applicationName string
|
var applicationName string
|
||||||
var deviceName string
|
var deviceName string
|
||||||
|
var token string
|
||||||
|
|
||||||
fmt.Printf("Enter name of your application: ")
|
fmt.Printf("Enter name of your application: ")
|
||||||
fmt.Scan(&applicationName)
|
fmt.Scan(&applicationName)
|
||||||
fmt.Printf("Enter name of your device: ")
|
fmt.Printf("Enter name of your device: ")
|
||||||
fmt.Scan(&deviceName)
|
fmt.Scan(&deviceName)
|
||||||
|
|
||||||
|
for {
|
||||||
resp, err := http.Post(
|
resp, err := http.Post(
|
||||||
"http://"+bridge.IP.String()+"/api",
|
"http://"+bridge.IP.String()+"/api",
|
||||||
"application/json",
|
"application/json",
|
||||||
@ -85,31 +90,48 @@ func registerApplication(bridge Bridge) RegisteredApplication {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
defer resp.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
// if body has link button not pressed, tell user to press it then send the request again to register the application
|
|
||||||
if strings.Contains(string(body), "link button not pressed") {
|
|
||||||
fmt.Println("Link button not pressed. Press link button on bridge and press anter to continue.")
|
|
||||||
bufio.NewReader(os.Stdin).ReadBytes('\n')
|
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
http.Post(
|
|
||||||
"http://"+bridge.IP.String()+"/api",
|
|
||||||
"application/json",
|
|
||||||
bytes.NewBuffer([]byte(`{"devicetype":"`+applicationName+`#`+deviceName+`"}`)))
|
|
||||||
body, err = io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Success struct {
|
||||||
|
Username string `json:"username"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(string(body), "link button not pressed") {
|
||||||
|
// Define a struct matching the expected JSON response structure
|
||||||
|
type SuccessResponse struct {
|
||||||
|
Success struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
} `json:"success"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal the response body into the struct
|
||||||
|
var successResponse []SuccessResponse
|
||||||
|
if err := json.Unmarshal(body, &successResponse); err != nil {
|
||||||
|
log.Fatalf("Error parsing response: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assign the username to the token
|
||||||
|
if len(successResponse) > 0 {
|
||||||
|
token = successResponse[0].Success.Username
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Link button not pressed. Press link button on bridge and press Enter to continue.")
|
||||||
|
bufio.NewReader(os.Stdin).ReadBytes('\n')
|
||||||
|
}
|
||||||
|
|
||||||
registeredApplication := RegisteredApplication{
|
registeredApplication := RegisteredApplication{
|
||||||
bridge: bridge,
|
bridge: bridge,
|
||||||
applicationName: applicationName,
|
applicationName: applicationName,
|
||||||
deviceName: deviceName,
|
deviceName: deviceName,
|
||||||
token: string(body)}
|
token: token,
|
||||||
resp.Body.Close()
|
}
|
||||||
return registeredApplication
|
return registeredApplication
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user