mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-09 20:07:18 -06:00
websocket initialization done in App
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
type AuthMessage struct {
|
||||
MsgType string `json:"type"`
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
|
||||
func SendAuthMessage(conn websocket.Conn, ctx context.Context) error {
|
||||
token := os.Getenv("AUTH_TOKEN")
|
||||
msgJson, err := json.Marshal(AuthMessage{MsgType: "auth", AccessToken: token})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = conn.Write(ctx, websocket.MessageText, msgJson)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteMessage[T any](msg T, conn websocket.Conn, ctx context.Context) error {
|
||||
msgJson, err := json.Marshal(msg)
|
||||
fmt.Println(string(msgJson))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = conn.Write(ctx, websocket.MessageText, msgJson)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ReadMessage(conn websocket.Conn, ctx context.Context) (string, error) {
|
||||
_, msg, err := conn.Read(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(msg), nil
|
||||
}
|
||||
@@ -3,12 +3,12 @@ package services
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/saml-dev/gome-assistant/internal/network"
|
||||
"github.com/saml-dev/gome-assistant/internal/setup"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
type Light struct {
|
||||
conn websocket.Conn
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
@@ -41,10 +41,10 @@ func LightOffRequest(entityId string) LightRequest {
|
||||
|
||||
func (l Light) TurnOn(entityId string) {
|
||||
req := LightOnRequest(entityId)
|
||||
network.WriteMessage(req, l.conn, l.ctx)
|
||||
setup.WriteMessage(req, l.conn, l.ctx)
|
||||
}
|
||||
|
||||
func (l Light) TurnOff(entityId string) {
|
||||
req := LightOffRequest(entityId)
|
||||
network.WriteMessage(req, l.conn, l.ctx)
|
||||
setup.WriteMessage(req, l.conn, l.ctx)
|
||||
}
|
||||
|
||||
106
internal/setup/setup.go
Normal file
106
internal/setup/setup.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
type AuthMessage struct {
|
||||
MsgType string `json:"type"`
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
|
||||
func WriteMessage[T any](msg T, conn *websocket.Conn, ctx context.Context) error {
|
||||
msgJson, err := json.Marshal(msg)
|
||||
fmt.Println(string(msgJson))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = conn.Write(ctx, websocket.MessageText, msgJson)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ReadMessage(conn *websocket.Conn, ctx context.Context) (string, error) {
|
||||
_, msg, err := conn.Read(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(msg), nil
|
||||
}
|
||||
|
||||
func SetupConnection(connString string) (*websocket.Conn, context.Context, context.CancelFunc, error) {
|
||||
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// Read auth_required message
|
||||
_, err = ReadMessage(conn, ctx)
|
||||
if err != nil {
|
||||
ctxCancel()
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
// Send auth message
|
||||
err = SendAuthMessage(conn, ctx)
|
||||
if err != nil {
|
||||
ctxCancel()
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
return conn, ctx, ctxCancel, err
|
||||
}
|
||||
|
||||
func SendAuthMessage(conn *websocket.Conn, ctx context.Context) error {
|
||||
token := os.Getenv("AUTH_TOKEN")
|
||||
err := WriteMessage(AuthMessage{MsgType: "auth", AccessToken: token}, conn, ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type authResponse struct {
|
||||
MsgType string `json:"type"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func VerifyAuthResponse(conn *websocket.Conn, ctx context.Context) error {
|
||||
_, msg, err := conn.Read(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var authResp authResponse
|
||||
json.Unmarshal(msg, &authResp)
|
||||
fmt.Println(authResp)
|
||||
if authResp.MsgType != "auth_ok" {
|
||||
return errors.New("invalid auth token")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user