good progress yay:

- impl http client
- create http client in App()
- generic builder for Service.*
- set Service on app to pass to callbacks later
- impl State
- set State on app to pass to callbacks later
- change panic to log.Fatalln
This commit is contained in:
Sam Lewis
2022-10-11 01:22:23 -04:00
parent 689a6ce4d3
commit 7bcca889f9
13 changed files with 296 additions and 93 deletions

View File

@@ -1,6 +1,83 @@
// http is used to interact with the home assistant
// REST API, currently only for retrieving state for
// REST API. Currently only used to retrieve state for
// a single entity_id
package http
// TODO: impl http struct, should be initialized as part of App()
import (
"errors"
"fmt"
"io"
"net/http"
)
type HttpClient struct {
url string
token string
}
func NewHttpClient(url, token string) *HttpClient {
url = fmt.Sprintf("http://%s/api", url)
return &HttpClient{url, token}
}
func (c *HttpClient) GetState(entityId string) ([]byte, error) {
resp, err := get(c.url+"/states/"+entityId, c.token)
if err != nil {
return nil, err
}
return resp, nil
}
func get(url, token string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, errors.New("Error creating HTTP request: " + err.Error())
}
req.Header.Add("Authorization", "Bearer "+token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, errors.New("Error on response.\n[ERROR] -" + err.Error())
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("Error while reading the response bytes:" + err.Error())
}
return body, nil
}
// func post(url string, token string, data any) ([]byte, error) {
// postBody, err := json.Marshal(data)
// if err != nil {
// return nil, err
// }
// req, err := http.NewRequest("GET", url, bytes.NewBuffer(postBody))
// if err != nil {
// return nil, errors.New("Error building post request: " + err.Error())
// }
// req.Header.Add("Authorization", "Bearer "+token)
// client := &http.Client{}
// resp, err := client.Do(req)
// if err != nil {
// return nil, errors.New("Error in post response: " + err.Error())
// }
// defer resp.Body.Close()
// if resp.StatusCode == 401 {
// log.Fatalln("ERROR: Auth token is invalid. Please double check it or create a new token in your Home Assistant profile")
// }
// body, err := io.ReadAll(resp.Body)
// if err != nil {
// log.Fatalln(err)
// }
// return body, nil
// }

View File

@@ -0,0 +1,11 @@
package services
import (
"context"
"nhooyr.io/websocket"
)
func BuildService[T Light | HomeAssistant](conn *websocket.Conn, ctx context.Context) *T {
return &T{conn: conn, ctx: ctx}
}

View File

@@ -0,0 +1,18 @@
package services
import (
"context"
"github.com/saml-dev/gome-assistant/internal/http"
"nhooyr.io/websocket"
)
type HomeAssistant struct {
conn *websocket.Conn
ctx context.Context
httpClient *http.HttpClient
}
// TODO: design how much reuse I can get between request types. E.g.
// only difference between light.turnon and homeassistant.turnon is
// domain and extra data

View File

@@ -3,13 +3,15 @@ package services
import (
"context"
"github.com/saml-dev/gome-assistant/internal/http"
"github.com/saml-dev/gome-assistant/internal/setup"
"nhooyr.io/websocket"
)
type Light struct {
conn *websocket.Conn
ctx context.Context
conn *websocket.Conn
ctx context.Context
httpClient *http.HttpClient
}
type LightRequest struct {
@@ -22,7 +24,21 @@ type LightRequest struct {
} `json:"target"`
}
func LightOnRequest(entityId string) LightRequest {
/* Public API */
func (l Light) TurnOn(entityId string) {
req := newLightOnRequest(entityId)
setup.WriteMessage(req, l.conn, l.ctx)
}
func (l Light) TurnOff(entityId string) {
req := newLightOffRequest(entityId)
setup.WriteMessage(req, l.conn, l.ctx)
}
/* Internal */
func newLightOnRequest(entityId string) LightRequest {
req := LightRequest{
Id: 5,
Type: "call_service",
@@ -33,18 +49,8 @@ func LightOnRequest(entityId string) LightRequest {
return req
}
func LightOffRequest(entityId string) LightRequest {
req := LightOnRequest(entityId)
func newLightOffRequest(entityId string) LightRequest {
req := newLightOnRequest(entityId)
req.Service = "turn_off"
return req
}
func (l Light) TurnOn(entityId string) {
req := LightOnRequest(entityId)
setup.WriteMessage(req, l.conn, l.ctx)
}
func (l Light) TurnOff(entityId string) {
req := LightOffRequest(entityId)
setup.WriteMessage(req, l.conn, l.ctx)
}

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"time"
@@ -39,40 +40,38 @@ func ReadMessage(conn *websocket.Conn, ctx context.Context) (string, error) {
return string(msg), nil
}
func SetupConnection(connString string) (*websocket.Conn, context.Context, context.CancelFunc, error) {
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*5)
func SetupConnection(connString string) (*websocket.Conn, context.Context, context.CancelFunc) {
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*3)
// Init websocket connection
conn, _, err := websocket.Dial(ctx, fmt.Sprintf("ws://%s/api/websocket", connString), nil)
if err != nil {
fmt.Printf("ERROR: Failed to connect to websocket at ws://%s/api/websocket. Check IP address and port\n", connString)
ctxCancel()
return nil, nil, nil, err
log.Fatalf("ERROR: Failed to connect to websocket at ws://%s/api/websocket. Check IP address and port\n", connString)
}
// Read auth_required message
_, err = ReadMessage(conn, ctx)
if err != nil {
ctxCancel()
return nil, nil, nil, err
log.Fatalln("Unknown error creating websocket client")
}
// Send auth message
err = SendAuthMessage(conn, ctx)
if err != nil {
ctxCancel()
return nil, nil, nil, err
log.Fatalln("Unknown error creating websocket client")
}
// Verify auth message
err = VerifyAuthResponse(conn, ctx)
if err != nil {
fmt.Println("ERROR: Auth token is invalid. Please double check it or create a new token in your Home Assistant profile")
ctxCancel()
return nil, nil, nil, err
log.Fatalln("ERROR: Auth token is invalid. Please double check it or create a new token in your Home Assistant profile")
}
return conn, ctx, ctxCancel, err
return conn, ctx, ctxCancel
}
func SendAuthMessage(conn *websocket.Conn, ctx context.Context) error {