mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-15 12:12:03 -06:00
figured out how to get conn/ctx into users method calls - by passing a configured service object to their callback functions
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
package light
|
||||
|
||||
type Light struct {
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package network
|
||||
@@ -5,20 +5,16 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
var ctx, ctxCancel = context.WithTimeout(context.Background(), time.Second*5)
|
||||
var conn, _, err = websocket.Dial(ctx, "ws://192.168.86.67:8123/api/websocket", nil)
|
||||
|
||||
type AuthMessage struct {
|
||||
MsgType string `json:"type"`
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
|
||||
func SendAuthMessage() error {
|
||||
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 {
|
||||
@@ -31,7 +27,7 @@ func SendAuthMessage() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteMessage[T any](msg T) error {
|
||||
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 {
|
||||
@@ -46,7 +42,7 @@ func WriteMessage[T any](msg T) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func ReadMessage() (string, error) {
|
||||
func ReadMessage(conn websocket.Conn, ctx context.Context) (string, error) {
|
||||
_, msg, err := conn.Read(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
50
internal/services/light.go
Normal file
50
internal/services/light.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/saml-dev/gome-assistant/internal/network"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
type Light struct {
|
||||
conn websocket.Conn
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
type LightRequest struct {
|
||||
Id int `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Domain string `json:"domain"`
|
||||
Service string `json:"service"`
|
||||
Target struct {
|
||||
EntityId string `json:"entity_id"`
|
||||
} `json:"target"`
|
||||
}
|
||||
|
||||
func LightOnRequest(entityId string) LightRequest {
|
||||
req := LightRequest{
|
||||
Id: 5,
|
||||
Type: "call_service",
|
||||
Domain: "light",
|
||||
Service: "turn_on",
|
||||
}
|
||||
req.Target.EntityId = entityId
|
||||
return req
|
||||
}
|
||||
|
||||
func LightOffRequest(entityId string) LightRequest {
|
||||
req := LightOnRequest(entityId)
|
||||
req.Service = "turn_off"
|
||||
return req
|
||||
}
|
||||
|
||||
func (l Light) TurnOn(entityId string) {
|
||||
req := LightOnRequest(entityId)
|
||||
network.WriteMessage(req, l.conn, l.ctx)
|
||||
}
|
||||
|
||||
func (l Light) TurnOff(entityId string) {
|
||||
req := LightOffRequest(entityId)
|
||||
network.WriteMessage(req, l.conn, l.ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user